Files
tehran_tala/apps/admin-panel/src/views/city/createCity.vue
T

102 lines
3.2 KiB
Vue
Raw Normal View History

<template>
<!--
Ported from legacy henza_admin/src/views/city/createCity.vue.
Converted from a standalone page to a modal (BaseFormModal): the /city/create
route still exists and still renders this component directly for a route-based
fallback (a-form as a plain page), but cityList.vue now opens it as a modal via
the `open`/`id` props instead of navigating.
-->
<BaseFormModal
v-if="asModal"
:open="Boolean(open)"
:id="id"
add-title="افزودن شهر"
edit-title="افزودن شهر"
:loading="loading"
:submitting="loading"
width="sm"
@update:open="$emit('update:open', $event)"
@submit="handleSubmit"
>
<div class="grid-2">
<a-form-item label="نام شهر" :validate-status="err.title ? 'error' : ''" :help="err.title">
<a-input v-model:value="form.title" />
</a-form-item>
<a-form-item label="استان">
<a-input v-model:value="form.province" />
</a-form-item>
</div>
</BaseFormModal>
<!-- route-based fallback for direct links to /city/create -->
<a-spin v-else :spinning="loading">
<a-form layout="vertical" @submit.prevent="handleSubmit">
<a-card title="افزودن شهر">
<div class="grid-2">
<a-form-item label="نام شهر" :validate-status="err.title ? 'error' : ''" :help="err.title">
<a-input v-model:value="form.title" />
</a-form-item>
<a-form-item label="استان">
<a-input v-model:value="form.province" />
</a-form-item>
</div>
<a-button type="primary" @click="handleSubmit">ثبت اطلاعات</a-button>
</a-card>
</a-form>
</a-spin>
</template>
<script setup lang="ts">
import { reactive, ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { message } from 'ant-design-vue';
import BaseFormModal from '../../components/common/BaseFormModal.vue';
import api from '../../api/client';
const props = defineProps<{
/** Present -> rendered as a modal by the parent list page. */
open?: boolean;
id?: string;
}>();
const emit = defineEmits<{
(e: 'update:open', value: boolean): void;
(e: 'success'): void;
}>();
// `asModal` is fixed at mount: the parent decides which way this component is
// used (modal vs the plain /city/create route) by passing `open` or not.
const asModal = props.open !== undefined;
const route = useRoute();
const router = useRouter();
const loading = ref(false);
const form = reactive<Record<string, any>>({ title: '', province: '' });
const err = reactive<Record<string, string>>({});
async function handleSubmit() {
Object.keys(err).forEach((k) => delete err[k]);
if (!form.title) {
err.title = 'لطفا مقدار را وارد کنید';
return;
}
try {
loading.value = true;
await api.post('cities', form);
message.info('اطلاعات با موفقیت به ثبت رسید.');
if (asModal) {
emit('success');
emit('update:open', false);
} else {
router.push('/city/list');
}
} catch (e: any) {
message.error(e?.response?.data?.message ?? 'خطا در ثبت اطلاعات.');
} finally {
loading.value = false;
}
}
</script>
<!-- .grid-2 now comes from the global utilities.less (dedup) -->