Skip to content

Commit ecda90d

Browse files
committed
♻️ Refactor: Simplify event form field updates
Modify event form field update logic to use a more concise and consistent approach: - Simplify onSetEventField to accept a partial event object - Update type definitions to match new implementation - Adjust event field updates across multiple components to use new method
1 parent 02033da commit ecda90d

File tree

6 files changed

+22
-31
lines changed

6 files changed

+22
-31
lines changed

packages/web/src/views/Forms/EventForm/DateTimeSection/DatePickers/DatePickers.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ export const DatePickers: FC<Props> = ({
145145
});
146146
} else {
147147
const newStartDate = dayjs(start).format(MONTH_DAY_YEAR);
148-
onSetEventField("startDate", newStartDate);
148+
onSetEventField({ startDate: newStartDate });
149149
}
150150
};
151151

@@ -167,7 +167,7 @@ export const DatePickers: FC<Props> = ({
167167
});
168168
} else {
169169
const newEndDate = dayjs(end).add(1, "day").format(MONTH_DAY_YEAR);
170-
onSetEventField("endDate", newEndDate);
170+
onSetEventField({ endDate: newEndDate });
171171
}
172172
};
173173

packages/web/src/views/Forms/EventForm/EventForm.tsx

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export const EventForm: React.FC<FormProps> = ({
116116
const onChangeEventTextField =
117117
(fieldName: "title" | "description") =>
118118
(e: React.ChangeEvent<HTMLTextAreaElement>) => {
119-
onSetEventField(fieldName, e.target.value);
119+
onSetEventField({ [fieldName]: e.target.value });
120120
};
121121

122122
const onClose = () => {
@@ -165,16 +165,8 @@ export const EventForm: React.FC<FormProps> = ({
165165
onClose();
166166
};
167167

168-
const onSetEventField: SetEventFormField = (field, value) => {
169-
const oldEvent = { ...event };
170-
// Handle multiple fields
171-
if (typeof field === "object") {
172-
setEvent({ ...oldEvent, ...field });
173-
return;
174-
}
175-
176-
// Handle single field
177-
setEvent({ ...oldEvent, [field]: value });
168+
const onSetEventField: SetEventFormField = (field) => {
169+
setEvent({ ...event, ...field });
178170
};
179171

180172
const onFormKeyDown: KeyboardEventHandler<HTMLFormElement> = (e) => {

packages/web/src/views/Forms/EventForm/PrioritySection/PrioritySection.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ export const PrioritySection: React.FC<Props> = ({
2121
bordered={priority === Priorities.WORK}
2222
color={colorByPriority.work}
2323
onClick={() => {
24-
onSetEventField("priority", Priorities.WORK);
24+
onSetEventField({ priority: Priorities.WORK });
2525
}}
26-
onFocus={() => onSetEventField("priority", Priorities.WORK)}
26+
onFocus={() => onSetEventField({ priority: Priorities.WORK })}
2727
role="tab"
2828
tabIndex={0}
2929
title="Doing your best work"
@@ -34,8 +34,8 @@ export const PrioritySection: React.FC<Props> = ({
3434
<PriorityButton
3535
bordered={priority === Priorities.SELF}
3636
color={colorByPriority.self}
37-
onClick={() => onSetEventField("priority", Priorities.SELF)}
38-
onFocus={() => onSetEventField("priority", Priorities.SELF)}
37+
onClick={() => onSetEventField({ priority: Priorities.SELF })}
38+
onFocus={() => onSetEventField({ priority: Priorities.SELF })}
3939
role="tab"
4040
tabIndex={0}
4141
title="Nurturing your authentic self"
@@ -47,9 +47,9 @@ export const PrioritySection: React.FC<Props> = ({
4747
bordered={priority === Priorities.RELATIONS}
4848
color={colorByPriority.relationships}
4949
onClick={() => {
50-
onSetEventField("priority", Priorities.RELATIONS);
50+
onSetEventField({ priority: Priorities.RELATIONS });
5151
}}
52-
onFocus={() => onSetEventField("priority", Priorities.RELATIONS)}
52+
onFocus={() => onSetEventField({ priority: Priorities.RELATIONS })}
5353
role="tab"
5454
tabIndex={0}
5555
title="Connecting with others"

packages/web/src/views/Forms/EventForm/RepeatSection/RepeatSection.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ export const RepeatSection: FC<Props> = ({
1616
onSetEventField,
1717
recurrence,
1818
}) => {
19-
const [isRepeat, setIsRepeat] = useState(recurrence?.rule?.length > 0);
19+
const [isRepeat, setIsRepeat] = useState((recurrence?.rule?.length ?? 0) > 0);
2020

2121
const toggleRecurrence = () => {
2222
if (isRepeat) {
23-
onSetEventField("recurrence", null);
23+
onSetEventField({ recurrence: { ...recurrence, rule: [] } });
2424
setIsRepeat(false);
2525
} else {
26-
onSetEventField("recurrence", { ...recurrence, rule: [RRULE.WEEK] });
26+
onSetEventField({ recurrence: { ...recurrence, rule: [RRULE.WEEK] } });
2727
setIsRepeat(true);
2828
}
2929
};
@@ -35,9 +35,9 @@ export const RepeatSection: FC<Props> = ({
3535
bgColor={bgColor}
3636
onChangeRecurrence={(rule) => {
3737
if (rule === null) {
38-
onSetEventField("recurrence", { ...recurrence, rule: null });
38+
onSetEventField({ recurrence: { ...recurrence, rule: [] } });
3939
} else {
40-
onSetEventField("recurrence", { ...recurrence, rule });
40+
onSetEventField({ recurrence: { ...recurrence, rule } });
4141
}
4242
}}
4343
recurrence={recurrence}

packages/web/src/views/Forms/EventForm/types.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,14 @@ export interface FormProps {
1515
setEvent: (event: Schema_Event) => SetStateAction<Schema_Event> | void;
1616
}
1717

18-
//++ TODO delete export type SetEventFormField = <FieldName extends keyof Schema_Event>(
1918
type EventField =
2019
| "title"
2120
| "description"
2221
| "startDate"
2322
| "endDate"
2423
| "priority";
2524
export type SetEventFormField = (
26-
field: EventField | Partial<Schema_Event>,
25+
field: Partial<Schema_Event>,
2726
value?: Schema_Event[EventField],
2827
) => void;
2928

packages/web/src/views/Forms/SomedayEventForm/SomedayEventForm.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export const SomedayEventForm: React.FC<FormProps> = ({
4545
hasInstances && event.recurrence?.rule?.length === 0;
4646

4747
if (removedRecurrence) {
48-
onSetEventField("recurrence", { ...event.recurrence, rule: null });
48+
onSetEventField({ recurrence: { ...event.recurrence, rule: [] } });
4949
}
5050

5151
onSubmit(event);
@@ -54,7 +54,7 @@ export const SomedayEventForm: React.FC<FormProps> = ({
5454
const onChangeEventTextField =
5555
(fieldName: "title" | "description") =>
5656
(e: React.ChangeEvent<HTMLTextAreaElement>) => {
57-
onSetEventField(fieldName, e.target.value);
57+
onSetEventField({ [fieldName]: e.target.value });
5858
};
5959

6060
const onDelete = () => {
@@ -93,10 +93,10 @@ export const SomedayEventForm: React.FC<FormProps> = ({
9393
}
9494
};
9595

96-
const onSetEventField: SetEventFormField = (field, value) => {
97-
const newEvent = { ...event, [field]: value };
96+
const onSetEventField: SetEventFormField = (field) => {
97+
const newEvent = { ...event, ...field };
9898

99-
if (value === null) {
99+
if (field === null) {
100100
delete newEvent[field];
101101
}
102102

0 commit comments

Comments
 (0)