Skip to content

Commit

Permalink
retain time and weekday filter if swapping radio options (#3741)
Browse files Browse the repository at this point in the history
  • Loading branch information
mastercactapus authored Mar 12, 2024
1 parent cde3e78 commit daf721a
Showing 1 changed file with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from '@mui/material'
import makeStyles from '@mui/styles/makeStyles'
import { DateTime } from 'luxon'
import React from 'react'
import React, { useEffect } from 'react'

import { FormContainer, FormField } from '../../forms'
import { renderMenuItem } from '../../selection/DisableableMenuItem'
Expand Down Expand Up @@ -66,17 +66,35 @@ export default function ScheduleOnCallNotificationsFormDest(
(d) => d.type === props.value.dest.type,
)

const [ruleType, setRuleType] = React.useState<'on-change' | 'time-of-day'>(
props.value.time ? 'time-of-day' : 'on-change',
)
const [lastTime, setLastTime] = React.useState<string | null>(
props.value.time,
)
const [lastFilter, setLastFilter] = React.useState<WeekdayFilter>(
props.value.weekdayFilter,
)
useEffect(() => {
if (!props.value.time) return
setLastTime(props.value.time)
setLastFilter(props.value.weekdayFilter)
}, [props.value.time, props.value.weekdayFilter])

if (!currentType) throw new Error('invalid destination type')

const handleRuleChange = (e: React.ChangeEvent<HTMLInputElement>): void => {
if (e.target.value === 'on-change') {
setRuleType('on-change')
props.onChange({ ...formProps.value, time: null, weekdayFilter: NO_DAY })
return
}

setRuleType('time-of-day')
props.onChange({
...props.value,
weekdayFilter: EVERY_DAY,
time: DateTime.fromObject({ hour: 9 }, { zone }).toISO(),
weekdayFilter: lastTime ? lastFilter : EVERY_DAY,
time: lastTime || DateTime.fromObject({ hour: 9 }, { zone }).toISO(),
})
}

Expand All @@ -98,7 +116,7 @@ export default function ScheduleOnCallNotificationsFormDest(
<Grid item>
<RadioGroup
name='ruleType'
value={formProps.value.time ? 'time-of-day' : 'on-change'}
value={ruleType}
onChange={handleRuleChange}
>
<FormControlLabel
Expand Down

0 comments on commit daf721a

Please sign in to comment.