AddEditCountryDialog.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 q-pt-none">
  7. <DefaultInput
  8. v-model="form.name"
  9. v-model:error="validationErrors.name"
  10. :label="$t('common.terms.name')"
  11. :placeholder="'Nome completo do pais'"
  12. :rules="[inputRules.required]"
  13. class="col-md-6 col-12"
  14. />
  15. <DefaultInput
  16. v-model="form.code"
  17. v-model:error="validationErrors.code"
  18. :label="$t('common.terms.code')"
  19. :placeholder="'Ex. BR, PT...'"
  20. :rules="[inputRules.required]"
  21. class="col-md-6 col-12"
  22. />
  23. <DefaultSelect
  24. v-model="selectedStatus"
  25. v-model:error="validationErrors.status"
  26. :options="statusOptions"
  27. :rules="[inputRules.required]"
  28. :label="$t('common.terms.status')"
  29. :placeholder="$t('common.terms.status')"
  30. class="col-md-6 col-12"
  31. />
  32. </q-card-section>
  33. <q-card-actions>
  34. <q-space />
  35. <q-btn
  36. outline
  37. color="negative"
  38. :label="$t('common.actions.cancel')"
  39. @click="onDialogCancel"
  40. />
  41. <q-btn
  42. color="primary"
  43. :label="
  44. country ? $t('common.actions.save') : $t('common.actions.add')
  45. "
  46. :type="'submit'"
  47. :loading="loading"
  48. :disable="!hasUpdatedFields"
  49. />
  50. </q-card-actions>
  51. </q-form>
  52. </q-card>
  53. </q-dialog>
  54. </template>
  55. <script setup>
  56. import { ref, useTemplateRef, onMounted, watch } from "vue";
  57. import { useInputRules } from "src/composables/useInputRules";
  58. import { useDialogPluginComponent } from "quasar";
  59. import { useI18n } from "vue-i18n";
  60. import { createCountry, updateCountry } from "src/api/country";
  61. import { useFormUpdateTracker } from "src/composables/useFormUpdateTracker";
  62. import { useSubmitHandler } from "src/composables/useSubmitHandler";
  63. import DefaultDialogHeader from "src/components/defaults/DefaultDialogHeader.vue";
  64. import DefaultInput from "src/components/defaults/DefaultInput.vue";
  65. import DefaultSelect from "src/components/defaults/DefaultSelect.vue";
  66. defineEmits([
  67. // REQUIRED; need to specify some events that your
  68. // component will emit through useDialogPluginComponent()
  69. ...useDialogPluginComponent.emits,
  70. ]);
  71. const { country, title } = defineProps({
  72. country: {
  73. type: Object,
  74. default: null,
  75. },
  76. title: {
  77. type: Function,
  78. default: () => useI18n().t("common.terms.title"),
  79. },
  80. });
  81. const { t } = useI18n();
  82. const { inputRules } = useInputRules();
  83. const { dialogRef, onDialogHide, onDialogOK, onDialogCancel } =
  84. useDialogPluginComponent();
  85. const formRef = useTemplateRef("formRef");
  86. const { form, getUpdatedFields, hasUpdatedFields } = useFormUpdateTracker({
  87. name: country ? country?.name : "",
  88. code: country ? country?.code : "",
  89. status: country ? country?.status : "ACTIVE",
  90. });
  91. const {
  92. loading,
  93. validationErrors,
  94. execute: submitForm,
  95. } = useSubmitHandler({
  96. onSuccess: () => onDialogOK(true),
  97. formRef: formRef,
  98. });
  99. const selectedStatus = ref({
  100. label: t("common.status.active"),
  101. value: "ACTIVE",
  102. });
  103. const statusOptions = ref([
  104. { label: t("common.status.active"), value: "ACTIVE" },
  105. { label: t("common.status.inactive"), value: "INACTIVE" },
  106. ]);
  107. const onOKClick = async () => {
  108. if (country) {
  109. await submitForm(() => updateCountry(getUpdatedFields.value, country.id));
  110. } else {
  111. await submitForm(() => createCountry({ ...form }));
  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>