fix(ui): clarify scheduler logic

- use full conditional syntax with `{}`
- do not mutate `action.payload` in a reducer
This commit is contained in:
psychedelicious 2023-06-18 18:47:59 +10:00
parent be8c0bb952
commit f1a8b9daee
2 changed files with 15 additions and 5 deletions

View File

@ -24,14 +24,17 @@ const ParamScheduler = () => {
const { t } = useTranslation();
useEffect(() => {
if (selectedSchedulers.length === 0)
if (selectedSchedulers.length === 0) {
dispatch(setSelectedSchedulers([...SCHEDULER_ITEMS]));
}
const schedulerFound = activeSchedulers.find(
(activeSchedulers) => activeSchedulers.value === scheduler
);
if (!schedulerFound)
if (!schedulerFound) {
dispatch(setScheduler(activeSchedulers[0].value as Scheduler));
}
}, [dispatch, selectedSchedulers, scheduler, activeSchedulers]);
const handleChange = useCallback(

View File

@ -99,13 +99,20 @@ export const uiSlice = createSlice({
setSelectedSchedulers: (state, action: PayloadAction<string[]>) => {
const selectedSchedulerData: SelectItem[] = [];
if (action.payload.length === 0) action.payload = [SCHEDULERS[0].value];
let selectedSchedulers = [...action.payload];
action.payload.forEach((item) => {
if (selectedSchedulers.length === 0) {
selectedSchedulers = [SCHEDULERS[0].value];
}
selectedSchedulers.forEach((item) => {
const schedulerData = SCHEDULERS.find(
(scheduler) => scheduler.value === item
);
if (schedulerData) selectedSchedulerData.push(schedulerData);
if (schedulerData) {
selectedSchedulerData.push(schedulerData);
}
});
state.activeSchedulers = selectedSchedulerData;