2023-12-29 17:21:02 -05:00
|
|
|
<script setup lang="ts">
|
|
|
|
import draggable from "vuedraggable";
|
|
|
|
import { v4 } from "uuid";
|
|
|
|
import lmove from "lodash-move";
|
2024-03-15 19:05:23 -04:00
|
|
|
import { Field, FieldArray, useForm, useFieldArray } from "vee-validate";
|
2023-12-29 17:21:02 -05:00
|
|
|
import { storySchema } from "@client/storyFormSchema";
|
2023-12-29 20:11:07 -05:00
|
|
|
import { FormStory, defaultChapter } from "@client/types/form/story";
|
|
|
|
import { autoEdit, autoSave, debouncedAutoEdit, debouncedAutoSave } from "@client/utils";
|
2023-12-29 17:21:02 -05:00
|
|
|
|
|
|
|
import findUser from "~/components/findUser.vue";
|
|
|
|
|
|
|
|
import singleChapter from "./singleChapter.vue";
|
|
|
|
import icon from "~/components/icon.vue";
|
|
|
|
|
2024-03-16 15:29:21 -04:00
|
|
|
const router = useRouter();
|
|
|
|
|
2023-12-29 17:21:02 -05:00
|
|
|
const props = defineProps<{
|
|
|
|
canDraft?: boolean;
|
|
|
|
endpoint: string;
|
|
|
|
endpointMethod: "put" | "post";
|
|
|
|
submitText?: string;
|
|
|
|
}>();
|
2024-03-15 19:05:23 -04:00
|
|
|
let w;
|
|
|
|
onMounted(() => {
|
|
|
|
w = window;
|
|
|
|
});
|
2024-01-04 15:19:08 -05:00
|
|
|
const dc = defaultChapter;
|
|
|
|
// data: FormStory;
|
2024-03-15 19:05:23 -04:00
|
|
|
const sdata = defineModel<FormStory>("data", {
|
2024-01-04 15:19:08 -05:00
|
|
|
required: true,
|
|
|
|
});
|
2023-12-29 17:21:02 -05:00
|
|
|
let drag = false;
|
|
|
|
const expandos = ref<string[]>([]);
|
|
|
|
|
|
|
|
function logSubmit(values, actions) {
|
|
|
|
console.debug("VALUE");
|
|
|
|
console.debug(values);
|
|
|
|
console.debug(actions);
|
|
|
|
}
|
|
|
|
const otherBtnInvoked = ref<boolean>(false);
|
|
|
|
async function onSubmit(values, actions) {
|
|
|
|
logSubmit(values, actions);
|
|
|
|
if (props.canDraft) {
|
|
|
|
if (otherBtnInvoked.value) {
|
|
|
|
otherBtnInvoked.value = false;
|
|
|
|
await autoSave(values);
|
|
|
|
} else {
|
2024-01-04 15:19:08 -05:00
|
|
|
const { data: dat } = await useApiFetch(`/story/new`, {
|
2023-12-29 17:21:02 -05:00
|
|
|
method: "post",
|
|
|
|
body: values,
|
|
|
|
});
|
2024-03-16 15:29:21 -04:00
|
|
|
if (dat.success) {
|
|
|
|
await router.push(`/story/${dat.story._id}/1`);
|
|
|
|
}
|
2023-12-29 17:21:02 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
await autoEdit(values, props.endpoint, props.endpointMethod);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function inval({ values, errors, results }) {
|
|
|
|
logSubmit(values, undefined);
|
|
|
|
}
|
2024-01-04 15:19:08 -05:00
|
|
|
const { values, setFieldValue, handleSubmit } = useForm<FormStory>({
|
2023-12-29 17:21:02 -05:00
|
|
|
keepValuesOnUnmount: true,
|
|
|
|
validationSchema: storySchema,
|
2024-03-15 19:05:23 -04:00
|
|
|
initialValues: sdata.value,
|
2023-12-29 17:21:02 -05:00
|
|
|
});
|
2024-03-15 19:05:23 -04:00
|
|
|
// const { push, remove, move, fields } = useFieldArray<FormChapter>("chapters");
|
2023-12-29 17:21:02 -05:00
|
|
|
const subCb = handleSubmit(onSubmit);
|
2024-03-15 19:05:23 -04:00
|
|
|
|
|
|
|
const pushHOF = (push) => (e) => {
|
|
|
|
if (!Array.isArray(values.chapters)) {
|
|
|
|
// noinspection TypeScriptValidateTypes
|
|
|
|
setFieldValue("chapters", []);
|
|
|
|
}
|
|
|
|
const chaps = [...toRaw(values.chapters)];
|
|
|
|
let lastIndex = chaps.length - 1;
|
|
|
|
if (lastIndex < 0) lastIndex = 0;
|
|
|
|
let lastChapter = chaps[lastIndex];
|
|
|
|
// log.debug('chaptrs->', chaps);
|
|
|
|
// log.debug('lastIndex->', lastIndex, lastChapter);
|
|
|
|
let newChapter = Object.assign({}, defaultChapter, {
|
|
|
|
summary: lastChapter?.summary || "",
|
|
|
|
index: (lastChapter?.index || 0) + 1,
|
|
|
|
bands: lastChapter?.bands || [],
|
|
|
|
characters: lastChapter?.characters || [],
|
|
|
|
relationships: lastChapter?.relationships || [],
|
|
|
|
uuidKey: v4(),
|
|
|
|
genre: lastChapter?.genre || [],
|
|
|
|
});
|
|
|
|
// console.debug("nc->", newChapter);
|
|
|
|
// console.debug("pushi -> ", push);
|
|
|
|
push(newChapter);
|
|
|
|
sdata.value.chapters.push(newChapter);
|
|
|
|
};
|
2023-12-29 17:21:02 -05:00
|
|
|
</script>
|
|
|
|
<template>
|
2024-03-15 19:05:23 -04:00
|
|
|
<form data-testid="storyform" @submit="subCb" @change="() => (canDraft ? debouncedAutoSave(values) : debouncedAutoEdit(values, endpoint, endpointMethod))">
|
2023-12-29 17:21:02 -05:00
|
|
|
<!-- <a-form v-bind:model="acData"> -->
|
2024-03-15 19:05:23 -04:00
|
|
|
|
2023-12-29 17:21:02 -05:00
|
|
|
<Field name="title" v-slot="{ value, field, errorMessage }">
|
2023-12-29 20:11:07 -05:00
|
|
|
<a-form-item label="Title" :help="errorMessage" :validate-status="!!errorMessage ? 'error' : ''">
|
2024-03-15 19:05:23 -04:00
|
|
|
<a-input :data-testid="`storyform.${field.name}`" v-bind="field" :value="value" />
|
2023-12-29 17:21:02 -05:00
|
|
|
</a-form-item>
|
|
|
|
</Field>
|
|
|
|
<a-form-item label="Co-author (optional)">
|
2024-03-15 19:05:23 -04:00
|
|
|
<find-user data-testid="storyform.coauthor" :initial-option="sdata.coAuthor" fieldName="coAuthor" :multi="false" />
|
2023-12-29 17:21:02 -05:00
|
|
|
</a-form-item>
|
2023-12-29 20:11:07 -05:00
|
|
|
<Field :unchecked-value="false" :value="true" type="checkbox" name="completed" v-slot="{ value, field, errorMessage }">
|
2024-03-15 19:05:23 -04:00
|
|
|
<a-checkbox :data-testid="`storyform.${field.name}`" v-bind="field"> Complete </a-checkbox>
|
2023-12-29 17:21:02 -05:00
|
|
|
</Field>
|
|
|
|
<a-divider />
|
|
|
|
<!-- <test1/> -->
|
|
|
|
<field-array name="chapters" v-slot="{ fields, push, remove, move }">
|
2024-03-16 15:29:21 -04:00
|
|
|
<client-only>
|
|
|
|
<div>
|
|
|
|
<div v-for="(element, index) in data.chapters">
|
|
|
|
<client-only>
|
|
|
|
<Teleport :to="`#chapter-\\[${element.uuidKey}\\]`">
|
|
|
|
<a-collapse v-model:active-key="expandos" collapsible="icon">
|
|
|
|
<template #expandIcon="{ isActive }">
|
|
|
|
<span :data-testid="`storyform.chapters[${index}].collapse`"> <RightOutlined :rotate="isActive ? 90 : undefined" /></span>
|
2024-03-15 19:05:23 -04:00
|
|
|
</template>
|
2024-03-16 15:29:21 -04:00
|
|
|
<a-collapse-panel :key="`${element.uuidKey}`">
|
|
|
|
<template #header>
|
|
|
|
<div :data-testid="`storyform.chapters[${index}].header`" style="display: flex; justify-content: space-between">
|
|
|
|
<span :data-testid="`storyform.chapters[${index}].titleEl`">{{ values.chapters[index]?.chapterTitle || "Untitled" }}</span>
|
|
|
|
<a-button
|
|
|
|
@click="
|
|
|
|
(e) => {
|
|
|
|
// let localFields = toRaw(fields);
|
|
|
|
// log.debug(`${index} | ${element.index}`);
|
|
|
|
// log.debug('fields->', localFields);
|
|
|
|
data.chapters.splice(index, 1);
|
|
|
|
remove(index);
|
|
|
|
// todo renumber
|
|
|
|
// renumber()
|
|
|
|
}
|
|
|
|
"
|
|
|
|
>
|
|
|
|
<icon istyle="regular" name="trash" />
|
|
|
|
</a-button>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
<single-chapter :data="element" :name="`chapters[${index}]`" />
|
|
|
|
</a-collapse-panel>
|
|
|
|
</a-collapse>
|
|
|
|
</Teleport>
|
|
|
|
</client-only>
|
|
|
|
</div>
|
2024-03-15 19:05:23 -04:00
|
|
|
</div>
|
2024-03-16 15:29:21 -04:00
|
|
|
</client-only>
|
|
|
|
<client-only>
|
|
|
|
<draggable
|
|
|
|
:component-data="{ type: 'transtion-group', 'data-testid': 'storyform.chapters' }"
|
|
|
|
@start="drag = true"
|
|
|
|
@end="drag = false"
|
|
|
|
v-model="values.chapters"
|
|
|
|
tag="div"
|
|
|
|
data-testid="storyform.chapters"
|
|
|
|
item-key="uuidKey"
|
|
|
|
@change="
|
|
|
|
(e) => {
|
|
|
|
console.log(e);
|
|
|
|
if (e.moved) {
|
|
|
|
console.debug(e.moved);
|
|
|
|
move(e.moved.oldIndex, e.moved.newIndex);
|
|
|
|
data.chapters = lmove(data.chapters, e.moved.oldIndex, e.moved.newIndex);
|
|
|
|
// w.tinymce.remove();
|
|
|
|
// log.debug(toRaw(acData.chapters.map((a) => toRaw(a))));
|
|
|
|
}
|
2023-12-29 17:21:02 -05:00
|
|
|
}
|
2024-03-16 15:29:21 -04:00
|
|
|
"
|
|
|
|
>
|
|
|
|
<template #item="{ element, index }">
|
|
|
|
<div :id="`chapter-[${element.uuidKey}]`"></div>
|
|
|
|
</template>
|
|
|
|
<template #footer>
|
|
|
|
<a-button data-testid="storyform.addChapter" :onClick="pushHOF(push)"> Add chapter </a-button>
|
|
|
|
</template>
|
|
|
|
</draggable>
|
|
|
|
</client-only>
|
2023-12-29 17:21:02 -05:00
|
|
|
</field-array>
|
2023-12-29 20:11:07 -05:00
|
|
|
<a-button type="primary" html-type="submit">{{ submitText || "Post" }}</a-button>
|
|
|
|
<a-button html-type="submit" v-if="canDraft" @click="() => (otherBtnInvoked = true)"> Save for Later </a-button>
|
2023-12-29 17:21:02 -05:00
|
|
|
</form>
|
|
|
|
</template>
|