diff --git a/frontend/src/lib/components/admin/ServicesManagement.svelte b/frontend/src/lib/components/admin/ServicesManagement.svelte index 6fe707c..1459332 100644 --- a/frontend/src/lib/components/admin/ServicesManagement.svelte +++ b/frontend/src/lib/components/admin/ServicesManagement.svelte @@ -33,7 +33,10 @@ minimum_age_required: 0, }); let editingService = $state(null); - let loading = $state(true); + let servicesLoading = $state(true); + let servicesUpdating = $state>({}); + let showServiceModal = $state(false); + let creatingService = $state(false); let serviceErrors = $state>({}); function validatePrice(price: any): string { @@ -90,14 +93,7 @@ name: validateName(newService.name), price: validatePrice(newService.price), duration_minutes: validateDuration(newService.duration_minutes, 'duration_minutes'), - patch_test_duration_hours: validateDuration( - newService.patch_test_duration_hours, - 'patch_test_duration_hours' - ), - minimum_age_required: validateDuration( - newService.minimum_age_required, - 'minimum_age_required' - ) + minimum_age_required: validateDuration(newService.minimum_age_required, 'minimum_age_required') }; } @@ -125,102 +121,6 @@ !serviceErrors.minimum_age_required ); - let creatingService = $state(false); - - async function createService() { - updateAllErrors(); - - const hasErrors = Object.values(serviceErrors).some((error) => error !== ''); - if (hasErrors) { - toast.error('Please fix the validation errors before submitting'); - return; - } - - if (!isFormValid) { - toast.error('Form validation failed'); - return; - } - - creatingService = true; - const loadingToast = toast.loading('Creating service...'); - - try { - const payload = { - name: newService.name.trim(), - description: newService.description.trim() || undefined, - price: Number(newService.price), - duration_minutes: Number(newService.duration_minutes), - minimum_age_required: Number(newService.minimum_age_required) - }; - - const response = await fetch('/api/admin/services', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - Authorization: `Bearer ${authStore.currentToken}` - }, - body: JSON.stringify(payload) - }); - - if (response.ok) { - await response.json(); - toast.success('Service created successfully!', { id: loadingToast }); - - resetServiceForm(); - showServiceModal = false; - - await fetchServices(); - } else if (response.status === 409) { - toast.error('A service with this name already exists', { id: loadingToast }); - } else if (response.status === 400) { - const errorText = await response.text(); - toast.error(`Validation error: ${errorText}`, { id: loadingToast }); - } else { - const errorText = await response.text(); - toast.error(`Failed to create service: ${errorText}`, { id: loadingToast }); - } - } catch (err) { - console.error('Error creating service:', err); - toast.error('Network error creating service', { id: loadingToast }); - } finally { - creatingService = false; - } - } - - } catch (err) { - console.error('Error creating service:', err); - toast.error('Network error creating service', { id: loadingToast }); - } finally { - creatingService = false; - } - } - - function resetServiceForm() { - newService = { - name: '', - description: '', - price: 0, - duration_minutes: 60, - is_active: true, - minimum_age_required: 0 - }; - serviceErrors = { - name: '', - price: '', - duration_minutes: '', - minimum_age_required: '' - }; - } - - - function validatePatchTestField() { - serviceErrors.patch_test_duration_hours = validateDuration( - newService.patch_test_duration_hours, - 'patch_test_duration_hours' - ); - } - - // =============== API Functions =============== async function fetchServices() { servicesLoading = true; @@ -327,10 +227,9 @@ const payload = { name: newService.name.trim(), description: newService.description.trim() || undefined, - price: parseFloat(newService.price), - duration_minutes: newService.duration_minutes, - patch_test_duration_hours: newService.patch_test_duration_hours, - minimum_age_required: newService.minimum_age_required + price: Number(newService.price), + duration_minutes: Number(newService.duration_minutes), + minimum_age_required: Number(newService.minimum_age_required) }; const response = await fetch('/api/admin/services', { @@ -371,16 +270,15 @@ newService = { name: '', description: '', - price: '', + price: 0, duration_minutes: 60, - patch_test_duration_hours: 0, + is_active: true, minimum_age_required: 0 }; serviceErrors = { name: '', price: '', duration_minutes: '', - patch_test_duration_hours: '', minimum_age_required: '' }; } @@ -714,4 +612,4 @@ - + \ No newline at end of file