From 6016813f4c23611746099c342e56f6cf6d17c624 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=98=99=E2=97=A6=20The=20Tablet=20=E2=9D=80=20GamerGirla?= =?UTF-8?q?ndCo=20=E2=97=A6=E2=9D=A7?= Date: Tue, 9 Jul 2024 20:53:50 -0400 Subject: [PATCH] refactor(components): update story form add `draftData` prop to distinguish whether we're editing a draft or a published story actually renumber chapter indexes after reordering use draftData prop in form change callback --- components/story/create/storyForm.vue | 68 +++++++++++++++++---------- 1 file changed, 44 insertions(+), 24 deletions(-) diff --git a/components/story/create/storyForm.vue b/components/story/create/storyForm.vue index ce36ef8..b80c0db 100644 --- a/components/story/create/storyForm.vue +++ b/components/story/create/storyForm.vue @@ -5,7 +5,7 @@ import { Field, FieldArray, useForm, useFieldArray } from "vee-validate"; import { ASpin } from "#components"; import { storySchema } from "@client/storyFormSchema"; - import { FormStory, defaultChapter } from "@client/types/form/story"; + import { FormStory, defaultChapter, FormChapter } from "@client/types/form/story"; import { autoEdit, autoSave, debouncedAutoEdit, debouncedAutoSave } from "@client/utils"; import findUser from "~/components/findUser.vue"; @@ -20,17 +20,16 @@ endpoint: string; endpointMethod: "put" | "post"; submitText?: string; + draftData?: { + endpoint: string; + endpointMethod: "put" | "post"; + }; }>(); - let w; - onMounted(() => { - w = window; - }); const dc = defaultChapter; - // data: FormStory; const sdata = defineModel("data", { required: true, }); - let drag = false; + let drag: boolean = false; const expandos = ref([]); function logSubmit(values, actions) { @@ -46,27 +45,32 @@ otherBtnInvoked.value = false; await autoSave(values); } else { - const { data: dat } = await useApiFetch(`/story/new`, { + const { data: dat } = await useApiFetch(`/story/new`, { method: "post", body: values, }); - if (dat.success) { - await router.push(`/story/${dat.story._id}/1`); + if (dat.value.success) { + await router.push(`/story/${dat.value.story._id}/1`); } } } else { await autoEdit(values, props.endpoint, props.endpointMethod); } } - function inval({ values, errors, results }) { - logSubmit(values, undefined); - } + const { values, setFieldValue, handleSubmit } = useForm({ keepValuesOnUnmount: true, validationSchema: storySchema, initialValues: sdata.value, }); - // const { push, remove, move, fields } = useFieldArray("chapters"); + function renumber(update: (idx: number, value: FormChapter) => void) { + for (let i = 0; i < values.chapters.length; i++) { + const nv = values.chapters[i]; + nv.index = i + 1; + update(i, nv); + sdata.value.chapters[i].index = i + 1; + } + } const subCb = handleSubmit(onSubmit); const pushHOF = (push) => (e) => { @@ -96,7 +100,18 @@ };