AddEditCityDialog.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. <CountrySelect
  14. ref="countrySelectRef"
  15. v-model="selectedCountry"
  16. :label="$t('ui.navigation.country')"
  17. :rules="[inputRules.required]"
  18. class="col-md-6 col-12"
  19. />
  20. <StateSelect
  21. v-model="selectedState"
  22. :country="selectedCountry"
  23. :label="$t('ui.navigation.state')"
  24. :rules="[inputRules.required]"
  25. class="col-md-6 col-12"
  26. @selected-country-id="countrySelectRef.selectCountryById($event)"
  27. />
  28. <q-select
  29. v-model="selectedStatus"
  30. :label="$t('common.terms.status')"
  31. :options="statusOptions"
  32. :rules="[inputRules.required]"
  33. class="col-md-6 col-12"
  34. />
  35. </q-card-section>
  36. <q-card-actions align="center">
  37. <q-btn color="primary" label="Cancel" @click="onDialogCancel" />
  38. <q-space />
  39. <q-btn
  40. color="primary"
  41. label="OK"
  42. :type="'submit'"
  43. :loading="loading"
  44. :disable="!hasUpdatedFields"
  45. />
  46. </q-card-actions>
  47. </q-form>
  48. </q-card>
  49. </q-dialog>
  50. </template>
  51. <script setup>
  52. import { ref, useTemplateRef, onMounted, watch } from "vue";
  53. import { useInputRules } from "src/composables/useInputRules";
  54. import { useDialogPluginComponent } from "quasar";
  55. import { useI18n } from "vue-i18n";
  56. import { createCity, updateCity } from "src/api/city";
  57. import { useFormUpdateTracker } from "src/composables/useFormUpdateTracker";
  58. import DefaultDialogHeader from "src/components/defaults/DefaultDialogHeader.vue";
  59. import CountrySelect from "src/components/regions/CountrySelect.vue";
  60. import StateSelect from "src/components/regions/StateSelect.vue";
  61. defineEmits([
  62. // REQUIRED; need to specify some events that your
  63. // component will emit through useDialogPluginComponent()
  64. ...useDialogPluginComponent.emits,
  65. ]);
  66. const { city, title } = defineProps({
  67. city: {
  68. type: Object,
  69. default: null,
  70. },
  71. title: {
  72. type: Function,
  73. default: () => useI18n().t("common.terms.title"),
  74. },
  75. });
  76. const { t } = useI18n();
  77. const { inputRules } = useInputRules();
  78. const { dialogRef, onDialogHide, onDialogOK, onDialogCancel } =
  79. useDialogPluginComponent();
  80. const formRef = useTemplateRef("formRef");
  81. const countrySelectRef = useTemplateRef("countrySelectRef");
  82. const { form, getUpdatedFields, hasUpdatedFields } = useFormUpdateTracker({
  83. name: city ? city.name : "",
  84. country_id: city ? city.country_id : null,
  85. state_id: city ? city.state_id : null,
  86. status: city ? city.status : "ACTIVE",
  87. });
  88. const loading = ref(false);
  89. const selectedCountry = ref(null);
  90. const selectedState = ref(null);
  91. const selectedStatus = ref({
  92. label: t("common.status.active"),
  93. value: "ACTIVE",
  94. });
  95. const statusOptions = ref([
  96. { label: t("common.status.active"), value: "ACTIVE" },
  97. { label: t("common.status.inactive"), value: "INACTIVE" },
  98. ]);
  99. const onOKClick = async () => {
  100. if (!(await formRef.value.validate())) {
  101. return;
  102. }
  103. if (city) {
  104. loading.value = true;
  105. try {
  106. await updateCity(city.id, getUpdatedFields.value);
  107. } catch (error) {
  108. console.error(error);
  109. return;
  110. } finally {
  111. loading.value = false;
  112. }
  113. onDialogOK(true);
  114. } else {
  115. loading.value = true;
  116. try {
  117. await createCity({ ...form });
  118. } catch (error) {
  119. console.error(error);
  120. return;
  121. } finally {
  122. loading.value = false;
  123. }
  124. onDialogOK(true);
  125. }
  126. };
  127. watch(selectedStatus, () => {
  128. form.status = selectedStatus.value?.value;
  129. });
  130. watch(selectedCountry, () => {
  131. form.country_id = selectedCountry.value?.value;
  132. });
  133. watch(selectedState, () => {
  134. form.state_id = selectedState.value?.value;
  135. });
  136. onMounted(async () => {
  137. if (city) {
  138. selectedStatus.value = statusOptions.value.find(
  139. (status) => status.value === city.status,
  140. );
  141. }
  142. });
  143. </script>