add & edit time rezerv
This commit is contained in:
+15
-1
@@ -1,4 +1,5 @@
|
||||
import { api } from './client';
|
||||
import { normalizeHmTime } from '@/utils/format';
|
||||
|
||||
export type AvailabilitySlot = {
|
||||
id?: string;
|
||||
@@ -15,8 +16,21 @@ export function setBusinessAvailability(
|
||||
businessId: string,
|
||||
slots: Omit<AvailabilitySlot, 'id'>[],
|
||||
) {
|
||||
const normalized = slots.map((slot) => {
|
||||
const startTime = normalizeHmTime(String(slot.startTime ?? ''));
|
||||
const endTime = normalizeHmTime(String(slot.endTime ?? ''));
|
||||
if (!startTime || !endTime) {
|
||||
throw new Error('فرمت ساعت نامعتبر است. از انتخابگر ساعت استفاده کنید.');
|
||||
}
|
||||
return {
|
||||
dayOfWeek: Number(slot.dayOfWeek),
|
||||
startTime,
|
||||
endTime,
|
||||
};
|
||||
});
|
||||
|
||||
return api<AvailabilitySlot[]>(`/businesses/${businessId}/availability`, {
|
||||
method: 'PUT',
|
||||
body: JSON.stringify({ slots }),
|
||||
body: JSON.stringify({ slots: normalized }),
|
||||
});
|
||||
}
|
||||
|
||||
+13
-3
@@ -40,9 +40,19 @@ export async function api<T>(path: string, options: ApiOptions = {}): Promise<T>
|
||||
const body = (await res.json().catch(() => null)) as
|
||||
| { message?: string | string[] }
|
||||
| null;
|
||||
const msg = Array.isArray(body?.message)
|
||||
? body.message.join(', ')
|
||||
: body?.message ?? res.statusText;
|
||||
let msg: string;
|
||||
if (Array.isArray(body?.message)) {
|
||||
const lines = body.message.map(String);
|
||||
const timeErrors = lines.filter((l) =>
|
||||
/startTime|endTime/.test(l) && /regular expression|HH:mm/i.test(l),
|
||||
);
|
||||
msg =
|
||||
timeErrors.length > 0
|
||||
? 'فرمت ساعت نامعتبر است. لطفاً دوباره بازه را اضافه و ذخیره کنید.'
|
||||
: lines.join(' · ');
|
||||
} else {
|
||||
msg = body?.message ?? res.statusText;
|
||||
}
|
||||
throw new ApiError(res.status, msg);
|
||||
}
|
||||
|
||||
|
||||
@@ -421,7 +421,12 @@ async function onSave() {
|
||||
message.value = 'ساعات پذیرش ذخیره شد.';
|
||||
await load();
|
||||
} catch (e) {
|
||||
error.value = e instanceof ApiError ? e.message : 'خطا در ذخیره';
|
||||
error.value =
|
||||
e instanceof ApiError
|
||||
? e.message
|
||||
: e instanceof Error
|
||||
? e.message
|
||||
: 'خطا در ذخیره';
|
||||
} finally {
|
||||
saving.value = false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user