| 164 | * otherwise everything goes in one PATCH, which converts a legacy row. |
| 165 | */ |
| 166 | export function buildCommitmentEdit(state: CommitmentEditState, form: CommitmentEditForm, ctx: { zone: string; now: Date }): CommitmentEditPlan | { field: 'date' | 'time' | 'deadline'; message: string } { |
| 167 | const init = state.initial; |
| 168 | const timingChanged = form.date !== init.date || form.time !== init.time || form.rule !== init.rule; |
| 169 | const d = form.deadline, i = init.deadline; |
| 170 | const deadlineChanged = (d === null) !== (i === null) || (!!d && !!i && (d.days !== i.days || d.secs !== i.secs || d.behavior !== i.behavior)); |
| 171 | if (state.timingLock && timingChanged) return { field: 'date', message: state.timingLock }; |
| 172 | if (state.deadlineLock && deadlineChanged) return { field: 'deadline', message: state.deadlineLock }; |
| 173 | if (!isDateOnly(form.date)) return { field: 'date', message: 'Choose a target date.' }; |
| 174 | if (form.time && !/^\d{2}:\d{2}$/.test(form.time)) return { field: 'time', message: 'Choose a valid time.' }; |
| 175 | if (state.timeLocked && !form.time !== !init.time) return { field: 'time', message: 'The time can be added or removed only while the date has no other roles.' }; |
| 176 | if (d && (d.days < 0 || (d.days === 0 && d.secs < 0))) return { field: 'deadline', message: 'The deadline cannot be before the target.' }; |
| 177 | if (!form.time && d?.secs) return { field: 'deadline', message: 'An all-day date takes an all-day deadline.' }; |
| 178 | if (!timingChanged && !deadlineChanged) return { kind: 'none' }; |
| 179 | if (timingChanged && form.rule === 'once') { |
| 180 | const passed = form.time ? Date.parse(wallToInstant(`${form.date}T${form.time}`, ctx.zone)) <= ctx.now.getTime() : form.date < todayIn(ctx.zone, ctx.now); |
| 181 | if (passed) return { field: 'date', message: 'Choose a date that has not passed.' }; |
| 182 | } |
| 183 | if (state.nativeDeadline) { |
| 184 | if (!d) return { kind: 'renegotiate', body: { scope: 'series', remove_deadline: true } }; |
| 185 | const moved = d.days !== i!.days || d.secs !== i!.secs; |
| 186 | return { kind: 'renegotiate', body: { scope: 'series', ...(moved ? { deadline_offset: { deadline_days: d.days, deadline_secs: d.secs } } : {}), ...(d.behavior !== i!.behavior ? { deadline_behavior: d.behavior } : {}) } }; |
| 187 | } |
| 188 | const body: CommitmentUpdateRequest = {}; |
| 189 | if (form.date !== init.date || form.time !== init.time) body.dtstart = form.time ? `${form.date}T${form.time}${form.time === init.time ? state.seconds : ''}` : form.date; |
| 190 | if (form.rule !== init.rule) body.rrule = COMMITMENT_RECURRENCES.find(([value]) => value === form.rule)?.[1] ?? null; |
| 191 | if (deadlineChanged) body.roles = d ? { deadline_days: d.days, deadline_secs: d.secs, deadline_behavior: d.behavior } : { deadline_days: null, deadline_secs: null, deadline_behavior: null }; |
| 192 | return { kind: 'patch', body }; |
| 193 | } |
| 194 | |
| 195 | export function describeRecurrence(view: Pick<ItemCommitmentView, 'rrule' | 'rdates' | 'dtstart' | 'anchor'>): string { |
| 196 | if (!view.rrule) return view.rdates.length ? 'specific dates' : 'once'; |