chore: release [ci skip]
This commit is contained in:
+71
-47
@@ -177,9 +177,12 @@ export function ShiftProvider({ children }: { children: ReactNode }) {
|
||||
client: eateryClient,
|
||||
});
|
||||
|
||||
const [mutateDeleteShift] = useMutation<{ deleteShift: boolean }>(DELETE_SHIFT, {
|
||||
client: eateryClient,
|
||||
});
|
||||
const [mutateDeleteShift] = useMutation<{ deleteShift: boolean }>(
|
||||
DELETE_SHIFT,
|
||||
{
|
||||
client: eateryClient,
|
||||
},
|
||||
);
|
||||
|
||||
const [mutateRegisterShift] = useMutation<
|
||||
{ registerShift: { staffId: string } },
|
||||
@@ -285,9 +288,13 @@ export function ShiftProvider({ children }: { children: ReactNode }) {
|
||||
const weekKeys = new Set(weekDates.map(formatDate));
|
||||
return shifts
|
||||
.filter(
|
||||
(s) => weekKeys.has(dateKey(s.date)) && (s.registeredStaff ?? []).length > 0,
|
||||
(s) =>
|
||||
weekKeys.has(dateKey(s.date)) && (s.registeredStaff ?? []).length > 0,
|
||||
)
|
||||
.reduce((sum, s) => sum + (s.wage ?? 0) * (s.registeredStaff ?? []).length, 0);
|
||||
.reduce(
|
||||
(sum, s) => sum + (s.wage ?? 0) * (s.registeredStaff ?? []).length,
|
||||
0,
|
||||
);
|
||||
}, [shifts, getWeekDates]);
|
||||
|
||||
// ── Shift actions ───────────────────────────────────────────────────────
|
||||
@@ -311,7 +318,13 @@ export function ShiftProvider({ children }: { children: ReactNode }) {
|
||||
|
||||
if (
|
||||
shift.date &&
|
||||
hasConflict(shift.date, shift.startTime, shift.endTime, staffId, shiftId)
|
||||
hasConflict(
|
||||
shift.date,
|
||||
shift.startTime,
|
||||
shift.endTime,
|
||||
staffId,
|
||||
shiftId,
|
||||
)
|
||||
) {
|
||||
return {
|
||||
success: false,
|
||||
@@ -338,7 +351,9 @@ export function ShiftProvider({ children }: { children: ReactNode }) {
|
||||
setShifts((prev) =>
|
||||
prev.map((s) => {
|
||||
if (s.id !== shiftId) return s;
|
||||
const updated = (s.registeredStaff ?? []).filter((rs) => rs.id !== staffId);
|
||||
const updated = (s.registeredStaff ?? []).filter(
|
||||
(rs) => rs.id !== staffId,
|
||||
);
|
||||
return {
|
||||
...s,
|
||||
registeredStaff: updated,
|
||||
@@ -347,51 +362,60 @@ export function ShiftProvider({ children }: { children: ReactNode }) {
|
||||
);
|
||||
}, []);
|
||||
|
||||
const createShift = useCallback(async (shift: Omit<ShiftEntity, "id">) => {
|
||||
try {
|
||||
const { data } = await mutateCreateShift({
|
||||
variables: {
|
||||
shiftInput: {
|
||||
date: toLocalDateISO(shift.date ?? new Date()),
|
||||
startTime: shift.startTime,
|
||||
endTime: shift.endTime,
|
||||
maxStaff: shift.maxStaff,
|
||||
wage: shift.wage,
|
||||
const createShift = useCallback(
|
||||
async (shift: Omit<ShiftEntity, "id">) => {
|
||||
try {
|
||||
const { data } = await mutateCreateShift({
|
||||
variables: {
|
||||
shiftInput: {
|
||||
date: toLocalDateISO(shift.date ?? new Date()),
|
||||
startTime: shift.startTime,
|
||||
endTime: shift.endTime,
|
||||
maxStaff: shift.maxStaff,
|
||||
wage: shift.wage,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
if (data) {
|
||||
// Optimistic: dùng date từ server nếu có, fallback sang local
|
||||
const serverDate = data.createShift.date
|
||||
? new Date(data.createShift.date as unknown as string)
|
||||
: shift.date;
|
||||
setShifts((prev) => [...prev, {
|
||||
...data.createShift,
|
||||
date: serverDate,
|
||||
wage: data.createShift.wage ?? shift.wage,
|
||||
registeredStaff: [],
|
||||
}]);
|
||||
// Refetch để đồng bộ với server
|
||||
refetch();
|
||||
if (data) {
|
||||
// Optimistic: dùng date từ server nếu có, fallback sang local
|
||||
const serverDate = data.createShift.date
|
||||
? new Date(data.createShift.date as unknown as string)
|
||||
: shift.date;
|
||||
setShifts((prev) => [
|
||||
...prev,
|
||||
{
|
||||
...data.createShift,
|
||||
date: serverDate,
|
||||
wage: data.createShift.wage ?? shift.wage,
|
||||
registeredStaff: [],
|
||||
},
|
||||
]);
|
||||
// Refetch để đồng bộ với server
|
||||
refetch();
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("[createShift] mutation failed:", err);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("[createShift] mutation failed:", err);
|
||||
}
|
||||
}, [mutateCreateShift, refetch]);
|
||||
},
|
||||
[mutateCreateShift, refetch],
|
||||
);
|
||||
|
||||
const deleteShift = useCallback(async (shiftId: string) => {
|
||||
try {
|
||||
const { data } = await mutateDeleteShift({
|
||||
variables: { id: shiftId },
|
||||
});
|
||||
if (data?.deleteShift) {
|
||||
setShifts((prev) => prev.filter((s) => s.id !== shiftId));
|
||||
const deleteShift = useCallback(
|
||||
async (shiftId: string) => {
|
||||
try {
|
||||
const { data } = await mutateDeleteShift({
|
||||
variables: { id: shiftId },
|
||||
});
|
||||
if (data?.deleteShift) {
|
||||
setShifts((prev) => prev.filter((s) => s.id !== shiftId));
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("[deleteShift] mutation failed:", err);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("[deleteShift] mutation failed:", err);
|
||||
}
|
||||
}, [mutateDeleteShift]);
|
||||
},
|
||||
[mutateDeleteShift],
|
||||
);
|
||||
|
||||
return (
|
||||
<ShiftContext.Provider
|
||||
|
||||
Reference in New Issue
Block a user