Show warning when selected dates are in the past

This commit is contained in:
Ewan Breakey 2023-06-12 02:16:30 +10:00
parent 909715bdf9
commit 0aaf9aa35e
4 changed files with 30 additions and 2 deletions

View file

@ -91,3 +91,12 @@
color: rgba(255,255,255,.5); color: rgba(255,255,255,.5);
} }
} }
.warningLabel {
display: flex;
padding-block: 0.9em;
gap: 0.5em;
align-items: center;
opacity: .75;
font-size: .9em;
}

View file

@ -1,7 +1,7 @@
import { useCallback, useMemo, useRef, useState } from 'react' import { useCallback, useMemo, useRef, useState } from 'react'
import { rotateArray } from '@giraugh/tools' import { rotateArray } from '@giraugh/tools'
import { Temporal } from '@js-temporal/polyfill' import { Temporal } from '@js-temporal/polyfill'
import { ChevronLeft, ChevronRight } from 'lucide-react' import { AlertTriangle, AlertTriangleIcon, ChevronLeft, ChevronRight, FileWarningIcon } from 'lucide-react'
import Button from '/src/components/Button/Button' import Button from '/src/components/Button/Button'
import { useTranslation } from '/src/i18n/client' import { useTranslation } from '/src/i18n/client'
@ -22,7 +22,8 @@ const Month = ({ value, onChange }: MonthProps) => {
const weekStart = useStore(useSettingsStore, state => state.weekStart) ?? 0 const weekStart = useStore(useSettingsStore, state => state.weekStart) ?? 0
const [page, setPage] = useState<Temporal.PlainYearMonth>(Temporal.Now.plainDateISO().toPlainYearMonth()) const now = useMemo(() => Temporal.Now.plainDateISO(), [])
const [page, setPage] = useState<Temporal.PlainYearMonth>(now.toPlainYearMonth())
const dates = useMemo(() => calculateMonth(page, weekStart, i18n.language), [page, weekStart, i18n.language]) const dates = useMemo(() => calculateMonth(page, weekStart, i18n.language), [page, weekStart, i18n.language])
// Ref and state required to rerender but also access static version in callbacks // Ref and state required to rerender but also access static version in callbacks
@ -36,6 +37,12 @@ const Month = ({ value, onChange }: MonthProps) => {
const startPos = useRef({ x: 0, y: 0 }) const startPos = useRef({ x: 0, y: 0 })
const mode = useRef<'add' | 'remove'>() const mode = useRef<'add' | 'remove'>()
// Is 1 or more of the selected dates in the past?
const hasPastDates = useMemo(() => value
.some(plainDate => Temporal.PlainDate.compare(
now, Temporal.PlainDate.from(plainDate)
) > 0), [value])
const handleFinishSelection = useCallback(() => { const handleFinishSelection = useCallback(() => {
if (mode.current === 'add') { if (mode.current === 'add') {
onChange([...value, ...selectingRef.current]) onChange([...value, ...selectingRef.current])
@ -112,6 +119,11 @@ const Month = ({ value, onChange }: MonthProps) => {
>{date.label}</button>) >{date.label}</button>)
)} )}
</div> </div>
{hasPastDates && <div className={styles.warningLabel}>
<AlertTriangle size='1.2em' />
<span>{t('form.dates.warnings.date_in_past')}</span>
</div>}
</> </>
} }

View file

@ -17,7 +17,11 @@
"previous": "Previous month", "previous": "Previous month",
"next": "Next month", "next": "Next month",
"today": "today" "today": "today"
},
"warnings": {
"date_in_past": "Some of the dates you've selected are in the past"
} }
}, },
"times": { "times": {
"label": "What times might work?", "label": "What times might work?",

View file

@ -17,6 +17,9 @@
"previous": "Previous month", "previous": "Previous month",
"next": "Next month", "next": "Next month",
"today": "today" "today": "today"
},
"warnings": {
"date_in_past": "Some of the dates you've selected are in the past"
} }
}, },
"times": { "times": {