AddEditCountryDialog.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <template>
  2. <q-dialog ref="dialogRef" @hide="onDialogHide">
  3. <q-card class="q-dialog-plugin overflow-hidden" style="width: 800px">
  4. <DefaultDialogHeader :title="title" @close="onDialogCancel" />
  5. <q-form ref="formRef" @submit="onOKClick">
  6. <q-card-section class="row q-col-gutter-sm">
  7. <q-input
  8. v-model="form.name"
  9. :label="$t('common.terms.name')"
  10. :rules="[inputRules.required]"
  11. class="col-md-6 col-12"
  12. />
  13. <q-input
  14. v-model="form.code"
  15. :label="$t('common.terms.code')"
  16. :rules="[inputRules.required]"
  17. class="col-md-6 col-12"
  18. />
  19. <q-select
  20. v-model="selectedStatus"
  21. :label="$t('common.terms.status')"
  22. :options="statusOptions"
  23. :rules="[inputRules.required]"
  24. class="col-md-6 col-12"
  25. />
  26. </q-card-section>
  27. <q-card-actions align="center">
  28. <q-btn color="primary" label="Cancel" @click="onDialogCancel" />
  29. <q-space />
  30. <q-btn
  31. color="primary"
  32. label="OK"
  33. :type="'submit'"
  34. :loading="loading"
  35. :disable="!hasUpdatedFields"
  36. />
  37. </q-card-actions>
  38. </q-form>
  39. </q-card>
  40. </q-dialog>
  41. </template>
  42. <script setup>
  43. import { ref, useTemplateRef, onMounted, watch } from "vue";
  44. import { useInputRules } from "src/composables/useInputRules";
  45. import { useDialogPluginComponent } from "quasar";
  46. import { useI18n } from "vue-i18n";
  47. import { createCountry, updateCountry } from "src/api/country";
  48. import { useFormUpdateTracker } from "src/composables/useFormUpdateTracker";
  49. import DefaultDialogHeader from "src/components/defaults/DefaultDialogHeader.vue";
  50. defineEmits([
  51. // REQUIRED; need to specify some events that your
  52. // component will emit through useDialogPluginComponent()
  53. ...useDialogPluginComponent.emits,
  54. ]);
  55. const { country, title } = defineProps({
  56. country: {
  57. type: Object,
  58. default: null,
  59. },
  60. title: {
  61. type: Function,
  62. default: () => useI18n().t("common.terms.title"),
  63. },
  64. });
  65. const { t } = useI18n();
  66. const { inputRules } = useInputRules();
  67. const { dialogRef, onDialogHide, onDialogOK, onDialogCancel } =
  68. useDialogPluginComponent();
  69. const formRef = useTemplateRef("formRef");
  70. const { form, getUpdatedFields, hasUpdatedFields } = useFormUpdateTracker({
  71. name: country ? country.name : "",
  72. code: country ? country.code : "",
  73. status: country ? country.status : "ACTIVE",
  74. });
  75. const loading = ref(false);
  76. const selectedStatus = ref({
  77. label: t("common.status.active"),
  78. value: "ACTIVE",
  79. });
  80. const statusOptions = ref([
  81. { label: t("common.status.active"), value: "ACTIVE" },
  82. { label: t("common.status.inactive"), value: "INACTIVE" },
  83. ]);
  84. const onOKClick = async () => {
  85. if (!(await formRef.value.validate())) {
  86. return;
  87. }
  88. if (country) {
  89. // When editing, only send changed fields
  90. loading.value = true;
  91. try {
  92. await updateCountry(country.id, getUpdatedFields.value);
  93. } catch (error) {
  94. console.error(error);
  95. return;
  96. } finally {
  97. loading.value = false;
  98. }
  99. onDialogOK(true);
  100. } else {
  101. // When creating, send all fields
  102. loading.value = true;
  103. try {
  104. await createCountry({ ...form });
  105. } catch (error) {
  106. console.error(error);
  107. return;
  108. } finally {
  109. loading.value = false;
  110. }
  111. onDialogOK(true);
  112. }
  113. };
  114. watch(selectedStatus, () => {
  115. form.status = selectedStatus.value.value;
  116. });
  117. onMounted(() => {
  118. if (country) {
  119. selectedStatus.value = statusOptions.value.find(
  120. (status) => status.value === country.status,
  121. );
  122. }
  123. });
  124. </script>