@jsb188/calendar 1.1.10 → 1.1.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,333 @@
1
+ import i18n from '@jsb188/app/i18n/index.ts';
2
+ import type { ScheduleResourceGQL } from '@jsb188/graphql/schedule.d';
3
+ import type { ShiftFormValues } from '../types/shift-form.d.ts';
4
+ import { memo } from 'react';
5
+
6
+ /* Render a local-time shift editor with structured slots and a bounded recurrence form. */
7
+ export const ShiftFormUI = memo(
8
+ (p: {
9
+ values: ShiftFormValues;
10
+ rooms: ScheduleResourceGQL[];
11
+ existing: boolean;
12
+ series: boolean;
13
+ busy: boolean;
14
+ error: string | null;
15
+ onChange: (values: ShiftFormValues) => void;
16
+ onSave: () => void;
17
+ }) => {
18
+ const v = p.values;
19
+ /* Change one controlled form value without mutating the caller's draft. */
20
+ const change = (key: keyof ShiftFormValues, value: unknown) =>
21
+ p.onChange({ ...v, [key]: value });
22
+ return (
23
+ <form
24
+ className="v_stretch gap_sm p_12"
25
+ onSubmit={(event) => {
26
+ event.preventDefault();
27
+ p.onSave();
28
+ }}
29
+ >
30
+ {!p.existing ? (
31
+ <label>
32
+ {i18n.t('calendar.kind_' + v.kind)}
33
+ <select
34
+ className="bl w_f p_8 bd_1 bd_bd r_sm"
35
+ value={v.kind}
36
+ onChange={(e) => change('kind', e.target.value)}
37
+ >
38
+ {['SHIFT', 'EVENT', 'BLOCK'].map((kind) => (
39
+ <option
40
+ key={kind}
41
+ value={kind}
42
+ >
43
+ {i18n.t('calendar.kind_' + kind)}
44
+ </option>
45
+ ))}
46
+ </select>
47
+ </label>
48
+ ) : null}
49
+ <label>
50
+ {i18n.t('calendar.title')}
51
+ <input
52
+ required
53
+ className="bl w_f p_8 bd_1 bd_bd r_sm"
54
+ value={v.title}
55
+ onChange={(e) => change('title', e.target.value)}
56
+ />
57
+ </label>
58
+ <label>
59
+ {i18n.t('calendar.room')}
60
+ <select
61
+ className="bl w_f p_8 bd_1 bd_bd r_sm"
62
+ value={v.roomId}
63
+ onChange={(e) => change('roomId', e.target.value)}
64
+ >
65
+ <option value="">{i18n.t('calendar.no_room')}</option>
66
+ {p.rooms.map((room) => (
67
+ <option
68
+ key={room.id}
69
+ value={room.id}
70
+ >
71
+ {room.name}
72
+ </option>
73
+ ))}
74
+ </select>
75
+ </label>
76
+ {(['start', 'end'] as const).map((key) => (
77
+ <label key={key}>
78
+ {i18n.t('calendar.' + key)}
79
+ <input
80
+ required
81
+ type="datetime-local"
82
+ className="bl w_f p_8 bd_1 bd_bd r_sm"
83
+ value={v[key]}
84
+ onChange={(e) => change(key, e.target.value)}
85
+ />
86
+ </label>
87
+ ))}
88
+ {v.kind !== 'SHIFT' ? (
89
+ <label>
90
+ <input
91
+ type="checkbox"
92
+ checked={v.allDay}
93
+ onChange={(e) => change('allDay', e.target.checked)}
94
+ />{' '}
95
+ {i18n.t('calendar.all_day')}
96
+ </label>
97
+ ) : null}
98
+ {(['location', 'note'] as const).map((key) => (
99
+ <label key={key}>
100
+ {i18n.t('calendar.' + key)}
101
+ <input
102
+ className="bl w_f p_8 bd_1 bd_bd r_sm"
103
+ value={v[key]}
104
+ onChange={(e) => change(key, e.target.value)}
105
+ />
106
+ </label>
107
+ ))}
108
+ {v.kind === 'SHIFT' ? (
109
+ <fieldset className="v_stretch gap_sm">
110
+ <legend>{i18n.t('calendar.slots')}</legend>
111
+ {v.slots.map((slot, index) => (
112
+ <div
113
+ className="v_stretch gap_xs bd_1 bd_bd p_8 r_sm"
114
+ key={slot.key}
115
+ >
116
+ <label>
117
+ {i18n.t('calendar.slot_label')}
118
+ <input
119
+ className="w_f p_4 bd_1 bd_bd"
120
+ value={slot.label || ''}
121
+ onChange={(e) =>
122
+ change(
123
+ 'slots',
124
+ v.slots.map((row, i) =>
125
+ i === index ? { ...row, label: e.target.value } : row,
126
+ ),
127
+ )
128
+ }
129
+ />
130
+ </label>
131
+ <label>
132
+ {i18n.t('calendar.required_count')}
133
+ <input
134
+ type="number"
135
+ min="1"
136
+ max="500"
137
+ className="w_f p_4 bd_1 bd_bd"
138
+ value={slot.requiredCount}
139
+ onChange={(e) =>
140
+ change(
141
+ 'slots',
142
+ v.slots.map((row, i) =>
143
+ i === index
144
+ ? { ...row, requiredCount: Number(e.target.value) }
145
+ : row,
146
+ ),
147
+ )
148
+ }
149
+ />
150
+ </label>
151
+ {(['roles', 'qualifications', 'tags'] as const).map((key) => (
152
+ <label key={key}>
153
+ {i18n.t('calendar.predicate_' + key)}
154
+ <input
155
+ className="w_f p_4 bd_1 bd_bd"
156
+ defaultValue={slot.predicate?.[key]?.join(', ') || ''}
157
+ onBlur={(e) =>
158
+ change(
159
+ 'slots',
160
+ v.slots.map((row, i) =>
161
+ i === index
162
+ ? {
163
+ ...row,
164
+ predicate: {
165
+ ...row.predicate,
166
+ [key]: e.target.value
167
+ .split(',')
168
+ .map((part) => part.trim())
169
+ .filter(Boolean),
170
+ },
171
+ }
172
+ : row,
173
+ ),
174
+ )
175
+ }
176
+ />
177
+ </label>
178
+ ))}
179
+ <button
180
+ type="button"
181
+ className="cl_err a_l p_4"
182
+ onClick={() =>
183
+ change(
184
+ 'slots',
185
+ v.slots.filter((_, i) => i !== index),
186
+ )
187
+ }
188
+ >
189
+ {i18n.t('calendar.remove')}
190
+ </button>
191
+ </div>
192
+ ))}
193
+ <button
194
+ type="button"
195
+ className="p_8 bg_alt r_sm"
196
+ onClick={() => {
197
+ let n = v.slots.length + 1;
198
+ while (v.slots.some((slot) => slot.key === `S${n}`)) n++;
199
+ change('slots', [
200
+ ...v.slots,
201
+ {
202
+ key: `S${n}`,
203
+ label: '',
204
+ requiredCount: 1,
205
+ predicate: { roles: [], qualifications: [], tags: [] },
206
+ ordinal: v.slots.length,
207
+ },
208
+ ]);
209
+ }}
210
+ >
211
+ {i18n.t('calendar.add_slot')}
212
+ </button>
213
+ </fieldset>
214
+ ) : null}
215
+ {!p.existing ? (
216
+ <>
217
+ <label>
218
+ {i18n.t('calendar.repeat')}
219
+ <select
220
+ className="bl w_f p_8 bd_1 bd_bd"
221
+ value={v.repeat}
222
+ onChange={(e) => change('repeat', e.target.value)}
223
+ >
224
+ <option value="">{i18n.t('calendar.none')}</option>
225
+ {['DAILY', 'WEEKLY', 'MONTHLY'].map((freq) => (
226
+ <option
227
+ key={freq}
228
+ value={freq}
229
+ >
230
+ {i18n.t('calendar.repeat_' + freq)}
231
+ </option>
232
+ ))}
233
+ </select>
234
+ </label>
235
+ {v.repeat ? (
236
+ <>
237
+ <label>
238
+ {i18n.t('calendar.interval')}
239
+ <input
240
+ type="number"
241
+ min="1"
242
+ max="365"
243
+ value={v.interval}
244
+ onChange={(e) => change('interval', Number(e.target.value))}
245
+ />
246
+ </label>
247
+ {v.repeat === 'WEEKLY' ? (
248
+ <div className="h_item f_wrap gap_sm">
249
+ {Array.from({ length: 7 }, (_, day) => (
250
+ <label key={day}>
251
+ <input
252
+ type="checkbox"
253
+ checked={v.weekdays.includes(day)}
254
+ onChange={(e) =>
255
+ change(
256
+ 'weekdays',
257
+ e.target.checked
258
+ ? [...v.weekdays, day]
259
+ : v.weekdays.filter((value) => value !== day),
260
+ )
261
+ }
262
+ />
263
+ {new Intl.DateTimeFormat(undefined, {
264
+ weekday: 'short',
265
+ timeZone: 'UTC',
266
+ }).format(new Date(Date.UTC(2026, 0, 4 + day)))}
267
+ </label>
268
+ ))}
269
+ </div>
270
+ ) : null}
271
+ {v.repeat === 'MONTHLY' ? (
272
+ <label>
273
+ {i18n.t('calendar.month_day')}
274
+ <input
275
+ type="number"
276
+ min="1"
277
+ max="31"
278
+ value={v.monthDay}
279
+ onChange={(e) =>
280
+ change('monthDay', Number(e.target.value))
281
+ }
282
+ />
283
+ </label>
284
+ ) : null}
285
+ <label>
286
+ {i18n.t('calendar.repeat_count')}
287
+ <input
288
+ type="number"
289
+ min="1"
290
+ value={v.count}
291
+ onChange={(e) => change('count', e.target.value)}
292
+ />
293
+ </label>
294
+ <label>
295
+ {i18n.t('calendar.repeat_until')}
296
+ <input
297
+ type="date"
298
+ value={v.until}
299
+ onChange={(e) => change('until', e.target.value)}
300
+ />
301
+ </label>
302
+ </>
303
+ ) : null}
304
+ </>
305
+ ) : null}
306
+ {p.series ? (
307
+ <label>
308
+ <input
309
+ type="checkbox"
310
+ checked={v.future}
311
+ onChange={(e) => change('future', e.target.checked)}
312
+ />{' '}
313
+ {i18n.t('calendar.series_this_and_future')}
314
+ </label>
315
+ ) : null}
316
+ {p.error ? (
317
+ <p
318
+ role="alert"
319
+ className="cl_err ft_sm"
320
+ >
321
+ {p.error}
322
+ </p>
323
+ ) : null}
324
+ <button
325
+ className="p_12 bg_primary cl_white r_sm"
326
+ disabled={p.busy}
327
+ >
328
+ {i18n.t('calendar.save')}
329
+ </button>
330
+ </form>
331
+ );
332
+ },
333
+ );