add & edit time rezerv

This commit is contained in:
2026-05-25 20:22:51 +03:30
parent 2a25727ef1
commit 833b7589d2
3 changed files with 34 additions and 5 deletions
+15 -1
View File
@@ -1,4 +1,5 @@
import { api } from './client'; import { api } from './client';
import { normalizeHmTime } from '@/utils/format';
export type AvailabilitySlot = { export type AvailabilitySlot = {
id?: string; id?: string;
@@ -15,8 +16,21 @@ export function setBusinessAvailability(
businessId: string, businessId: string,
slots: Omit<AvailabilitySlot, 'id'>[], 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`, { return api<AvailabilitySlot[]>(`/businesses/${businessId}/availability`, {
method: 'PUT', method: 'PUT',
body: JSON.stringify({ slots }), body: JSON.stringify({ slots: normalized }),
}); });
} }
+13 -3
View File
@@ -40,9 +40,19 @@ export async function api<T>(path: string, options: ApiOptions = {}): Promise<T>
const body = (await res.json().catch(() => null)) as const body = (await res.json().catch(() => null)) as
| { message?: string | string[] } | { message?: string | string[] }
| null; | null;
const msg = Array.isArray(body?.message) let msg: string;
? body.message.join(', ') if (Array.isArray(body?.message)) {
: body?.message ?? res.statusText; 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); throw new ApiError(res.status, msg);
} }
@@ -421,7 +421,12 @@ async function onSave() {
message.value = 'ساعات پذیرش ذخیره شد.'; message.value = 'ساعات پذیرش ذخیره شد.';
await load(); await load();
} catch (e) { } catch (e) {
error.value = e instanceof ApiError ? e.message : 'خطا در ذخیره'; error.value =
e instanceof ApiError
? e.message
: e instanceof Error
? e.message
: 'خطا در ذخیره';
} finally { } finally {
saving.value = false; saving.value = false;
} }