| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- <template>
- <q-dialog ref="dialogRef" @hide="onDialogHide">
- <q-card class="q-dialog-plugin overflow-hidden" style="width: 800px">
- <DefaultDialogHeader :title="title" @close="onDialogCancel" />
- <q-form ref="formRef" @submit="onOKClick">
- <q-card-section class="row q-col-gutter-sm">
- <q-input
- v-model="form.name"
- :label="$t('common.terms.name')"
- :rules="[inputRules.required]"
- class="col-md-6 col-12"
- />
- <q-input
- v-model="form.code"
- :label="$t('common.terms.code')"
- :rules="[inputRules.required]"
- class="col-md-6 col-12"
- />
- <q-select
- v-model="selectedStatus"
- :label="$t('common.terms.status')"
- :options="statusOptions"
- :rules="[inputRules.required]"
- class="col-md-6 col-12"
- />
- </q-card-section>
- <q-card-actions align="center">
- <q-btn color="primary" label="Cancel" @click="onDialogCancel" />
- <q-space />
- <q-btn
- color="primary"
- label="OK"
- :type="'submit'"
- :loading="loading"
- :disable="!hasUpdatedFields"
- />
- </q-card-actions>
- </q-form>
- </q-card>
- </q-dialog>
- </template>
- <script setup>
- import { ref, useTemplateRef, onMounted, watch } from "vue";
- import { useInputRules } from "src/composables/useInputRules";
- import { useDialogPluginComponent } from "quasar";
- import { useI18n } from "vue-i18n";
- import { createCountry, updateCountry } from "src/api/country";
- import { useFormUpdateTracker } from "src/composables/useFormUpdateTracker";
- import DefaultDialogHeader from "src/components/defaults/DefaultDialogHeader.vue";
- defineEmits([
- // REQUIRED; need to specify some events that your
- // component will emit through useDialogPluginComponent()
- ...useDialogPluginComponent.emits,
- ]);
- const { country, title } = defineProps({
- country: {
- type: Object,
- default: null,
- },
- title: {
- type: Function,
- default: () => useI18n().t("common.terms.title"),
- },
- });
- const { t } = useI18n();
- const { inputRules } = useInputRules();
- const { dialogRef, onDialogHide, onDialogOK, onDialogCancel } =
- useDialogPluginComponent();
- const formRef = useTemplateRef("formRef");
- const { form, getUpdatedFields, hasUpdatedFields } = useFormUpdateTracker({
- name: country ? country.name : "",
- code: country ? country.code : "",
- status: country ? country.status : "ACTIVE",
- });
- const loading = ref(false);
- const selectedStatus = ref({
- label: t("common.status.active"),
- value: "ACTIVE",
- });
- const statusOptions = ref([
- { label: t("common.status.active"), value: "ACTIVE" },
- { label: t("common.status.inactive"), value: "INACTIVE" },
- ]);
- const onOKClick = async () => {
- if (!(await formRef.value.validate())) {
- return;
- }
- if (country) {
- // When editing, only send changed fields
- loading.value = true;
- try {
- await updateCountry(country.id, getUpdatedFields.value);
- } catch (error) {
- console.error(error);
- return;
- } finally {
- loading.value = false;
- }
- onDialogOK(true);
- } else {
- // When creating, send all fields
- loading.value = true;
- try {
- await createCountry({ ...form });
- } catch (error) {
- console.error(error);
- return;
- } finally {
- loading.value = false;
- }
- onDialogOK(true);
- }
- };
- watch(selectedStatus, () => {
- form.status = selectedStatus.value.value;
- });
- onMounted(() => {
- if (country) {
- selectedStatus.value = statusOptions.value.find(
- (status) => status.value === country.status,
- );
- }
- });
- </script>
|