Added loading state to create calendar form

This commit is contained in:
Alexis
2024-08-24 21:38:15 +02:00
parent 99e478951d
commit 245b46661a
2 changed files with 24 additions and 8 deletions

View File

@@ -15,7 +15,7 @@ function onChangedName(newName: string) {
const emit = defineEmits(['on-close']) const emit = defineEmits(['on-close'])
function handleCancel() { function handleClose() {
emit('on-close') emit('on-close')
} }
</script> </script>
@@ -34,11 +34,11 @@ function handleCancel() {
</span> </span>
</UiAlertDialogTitle> </UiAlertDialogTitle>
<UiButton size="icon" variant="ghost" class="absolute top-4 right-4" title="Fermer la fenêtre" @click="handleCancel"> <UiButton size="icon" variant="ghost" class="absolute top-4 right-4" title="Fermer la fenêtre" @click="handleClose">
<PhX size="20" /> <PhX size="20" />
</UiButton> </UiButton>
<CalendarFormCreate @on-changed-name="onChangedName" @on-cancel="handleCancel" /> <CalendarFormCreate @on-changed-name="onChangedName" @on-close="handleClose" />
</UiAlertDialogContent> </UiAlertDialogContent>
</UiAlertDialog> </UiAlertDialog>
</template> </template>

View File

@@ -1,6 +1,6 @@
<script lang="ts" setup> <script lang="ts" setup>
import type { Calendar } from '~/models/CalendarConfig'; import type { Calendar } from '~/models/CalendarConfig';
import { PhAlarm, PhCalendarDots, PhWrench } from '@phosphor-icons/vue'; import { PhAlarm, PhCalendarDots, PhCircleNotch, PhWrench } from '@phosphor-icons/vue';
const defaultSkeleton: Calendar = { name: '', today: { day: 1, month: 0, year: 0 }, months: [], events: []} const defaultSkeleton: Calendar = { name: '', today: { day: 1, month: 0, year: 0 }, months: [], events: []}
const calendarSkeleton = ref<Calendar>({ ...defaultSkeleton }) const calendarSkeleton = ref<Calendar>({ ...defaultSkeleton })
@@ -35,8 +35,19 @@ const validSkeletonGeneral = computed(() => calendarSkeleton.value.name)
const validSkeleton = computed(() => validSkeletonGeneral.value && validSkeletonMonths.value) const validSkeleton = computed(() => validSkeletonGeneral.value && validSkeletonMonths.value)
/** Send the data to the store for validation */ /** Send the data to the store for validation */
const isCreatingCalendar = ref<boolean>(false)
async function handleSubmit() { async function handleSubmit() {
await $fetch(`/api/calendars/create`, { method: 'POST', body: {...calendarSkeleton.value, worldId: 1 } }) try {
isCreatingCalendar.value = true
await $fetch(`/api/calendars/create`, { method: 'POST', body: {...calendarSkeleton.value, worldId: 1 } })
emit('on-close')
} catch (err) {
console.log(err)
} finally {
isCreatingCalendar.value = false
}
} }
/** /**
@@ -46,7 +57,7 @@ const emit = defineEmits<{
// eslint-disable-next-line no-unused-vars // eslint-disable-next-line no-unused-vars
(e: 'on-changed-name', calendarName: string): void (e: 'on-changed-name', calendarName: string): void
// eslint-disable-next-line no-unused-vars // eslint-disable-next-line no-unused-vars
(e: 'on-cancel'): void (e: 'on-close'): void
}>() }>()
/** Hook to emit a debounced event for the changed skeleton name */ /** Hook to emit a debounced event for the changed skeleton name */
@@ -55,7 +66,7 @@ const handleNameChange = useDebounceFn(() => {
}, 400) }, 400)
function handleFormCancel() { function handleFormCancel() {
emit('on-cancel') emit('on-close')
} }
</script> </script>
@@ -107,7 +118,12 @@ function handleFormCancel() {
<UiButton type="button" variant="destructive" @click="handleFormCancel"> <UiButton type="button" variant="destructive" @click="handleFormCancel">
Annuler Annuler
</UiButton> </UiButton>
<UiButton type="submit" :disabled="!validSkeleton">
<UiButton type="submit" :disabled="!validSkeleton || isCreatingCalendar">
<Transition name="fade">
<PhCircleNotch v-if="isCreatingCalendar" size="20" class="opacity-50 animate-spin"/>
</Transition>
Créer Créer
</UiButton> </UiButton>
</footer> </footer>