106 lines
3.6 KiB
Vue
106 lines
3.6 KiB
Vue
<script setup lang="ts">
|
|||
|
|
import { SaveOutlined } from "@ant-design/icons-vue";
|
||
|
|
import { isSectionKey, SECTION_LABELS, type SectionKey, sectionSchemas } from "@imen/shared";
|
||
|
|
import { message } from "ant-design-vue";
|
||
|
|
import { type Component, computed, ref } from "vue";
|
||
|
|
import { useRoute } from "vue-router";
|
||
|
|
import { api, errorMessage } from "@/api/http";
|
||
|
|
import type { SectionResponse } from "@/api/types";
|
||
|
|
import PageHeader from "@/components/PageHeader.vue";
|
||
|
|
import { useZodForm } from "@/composables/useZodForm";
|
||
|
|
import { jalali, SITE_URL } from "@/utils/format";
|
||
|
|
import AboutForm from "@/sections/AboutForm.vue";
|
||
|
|
import ContactForm from "@/sections/ContactForm.vue";
|
||
|
|
import FooterForm from "@/sections/FooterForm.vue";
|
||
|
|
import HeroForm from "@/sections/HeroForm.vue";
|
||
|
|
import IndustriesForm from "@/sections/IndustriesForm.vue";
|
||
|
|
import ProductsIntroForm from "@/sections/ProductsIntroForm.vue";
|
||
|
|
import ServicesForm from "@/sections/ServicesForm.vue";
|
||
|
|
import SettingsForm from "@/sections/SettingsForm.vue";
|
||
|
|
import StandardsForm from "@/sections/StandardsForm.vue";
|
||
|
|
import StatsForm from "@/sections/StatsForm.vue";
|
||
|
|
import TapeForm from "@/sections/TapeForm.vue";
|
||
|
|
import TestimonialsForm from "@/sections/TestimonialsForm.vue";
|
||
|
|
|
||
|
|
const FORMS: Record<SectionKey, Component> = {
|
||
|
|
settings: SettingsForm,
|
||
|
|
hero: HeroForm,
|
||
|
|
tape: TapeForm,
|
||
|
|
stats: StatsForm,
|
||
|
|
productsIntro: ProductsIntroForm,
|
||
|
|
about: AboutForm,
|
||
|
|
services: ServicesForm,
|
||
|
|
standards: StandardsForm,
|
||
|
|
industries: IndustriesForm,
|
||
|
|
testimonials: TestimonialsForm,
|
||
|
|
contact: ContactForm,
|
||
|
|
footer: FooterForm,
|
||
|
|
};
|
||
|
|
|
||
|
|
const route = useRoute();
|
||
|
|
const key = String(route.params.key);
|
||
|
|
const valid = isSectionKey(key);
|
||
|
|
const sectionKey = (valid ? key : "hero") as SectionKey;
|
||
|
|
|
||
|
|
// the page is re-created per key (RouterView keys by path), so one form instance per section
|
||
|
|
const { model, validate, handleError, reset } = useZodForm(
|
||
|
|
sectionSchemas[sectionKey] as (typeof sectionSchemas)["hero"],
|
||
|
|
{} as never,
|
||
|
|
);
|
||
|
|
const loaded = ref(false);
|
||
|
|
const saving = ref(false);
|
||
|
|
const updatedAt = ref<string | null>(null);
|
||
|
|
const label = computed(() => (valid ? SECTION_LABELS[sectionKey] : null));
|
||
|
|
|
||
|
|
async function load() {
|
||
|
|
try {
|
||
|
|
const res = await api<SectionResponse>(`/admin/sections/${sectionKey}`);
|
||
|
|
reset(res.data as never);
|
||
|
|
updatedAt.value = res.updatedAt;
|
||
|
|
loaded.value = true;
|
||
|
|
} catch (e) {
|
||
|
|
void message.error(errorMessage(e));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async function save() {
|
||
|
|
const data = validate();
|
||
|
|
if (!data) return;
|
||
|
|
saving.value = true;
|
||
|
|
try {
|
||
|
|
const res = await api<SectionResponse>(`/admin/sections/${sectionKey}`, { method: "PUT", body: data });
|
||
|
|
reset(res.data as never);
|
||
|
|
updatedAt.value = res.updatedAt;
|
||
|
|
void message.success("ذخیره شد؛ سایت تا چند لحظه دیگر بهروز میشود");
|
||
|
|
} catch (e) {
|
||
|
|
handleError(e);
|
||
|
|
} finally {
|
||
|
|
saving.value = false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if (valid) void load();
|
||
|
|
</script>
|
||
|
|
|
||
|
|
<template>
|
||
|
|
<a-result v-if="!valid" status="404" title="بخش پیدا نشد" />
|
||
|
|
<template v-else>
|
||
|
|
<PageHeader
|
||
|
|
:title="label!.title"
|
||
|
|
:subtitle="updatedAt ? `آخرین ذخیره: ${jalali(updatedAt)}` : label!.desc"
|
||
|
|
back="/sections"
|
||
|
|
>
|
||
|
|
<a :href="SITE_URL" target="_blank" rel="noopener"><a-button>مشاهده سایت</a-button></a>
|
||
|
|
<a-button type="primary" :loading="saving" :disabled="!loaded" @click="save">
|
||
|
|
<template #icon><SaveOutlined /></template>
|
||
|
|
ذخیره
|
||
|
|
</a-button>
|
||
|
|
</PageHeader>
|
||
|
|
<a-spin :spinning="!loaded">
|
||
|
|
<a-form v-if="loaded" layout="vertical" @submit.prevent="save">
|
||
|
|
<component :is="FORMS[sectionKey]" v-model="model" />
|
||
|
|
</a-form>
|
||
|
|
</a-spin>
|
||
|
|
</template>
|
||
|
|
</template>
|