AddEditDependentDialog.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <template>
  2. <q-dialog ref="dialogRef" @hide="onDialogHide">
  3. <q-card class="q-dialog-plugin overflow-hidden" style="width: 500px">
  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. :rules="[inputRules.required]"
  11. :label="$t('common.terms.name')"
  12. class="col-12"
  13. />
  14. <DefaultSelect
  15. v-model="form.kinship"
  16. v-model:error="validationErrors.kinship"
  17. :rules="[inputRules.required]"
  18. :label="$t('associado.kinship')"
  19. :options="kinshipOptions"
  20. emit-value
  21. map-options
  22. class="col-12"
  23. />
  24. <DefaultFilePicker
  25. v-if="!dependent"
  26. v-model="documentFile"
  27. :rules="[inputRules.required]"
  28. :error="!!validationErrors.document"
  29. :error-message="validationErrors.document"
  30. :label="$t('associado.dependent_document')"
  31. type="file"
  32. accept="image/*,application/pdf"
  33. class="col-12"
  34. />
  35. <div v-if="!dependent" class="col-12 text-caption text-grey-7">
  36. {{ $t("associado.dependent_document_hint") }}
  37. </div>
  38. </q-card-section>
  39. <q-card-actions>
  40. <q-space />
  41. <q-btn
  42. outline
  43. color="negative"
  44. :label="$t('common.actions.cancel')"
  45. @click="onDialogCancel"
  46. />
  47. <q-btn
  48. color="primary"
  49. :label="dependent ? $t('common.actions.save') : $t('common.actions.add')"
  50. type="submit"
  51. :loading="loading"
  52. />
  53. </q-card-actions>
  54. </q-form>
  55. </q-card>
  56. </q-dialog>
  57. </template>
  58. <script setup>
  59. import { computed, ref, useTemplateRef } from "vue";
  60. import { useInputRules } from "src/composables/useInputRules";
  61. import { useDialogPluginComponent } from "quasar";
  62. import { useI18n } from "vue-i18n";
  63. import { createDependent, updateDependent } from "src/api/profile";
  64. import { useFormUpdateTracker } from "src/composables/useFormUpdateTracker";
  65. import { useSubmitHandler } from "src/composables/useSubmitHandler";
  66. import DefaultDialogHeader from "src/components/defaults/DefaultDialogHeader.vue";
  67. import DefaultInput from "src/components/defaults/DefaultInput.vue";
  68. import DefaultSelect from "src/components/defaults/DefaultSelect.vue";
  69. import DefaultFilePicker from "src/components/defaults/DefaultFilePicker.vue";
  70. defineEmits([...useDialogPluginComponent.emits]);
  71. const { dependent, userId, title } = defineProps({
  72. dependent: { type: Object, default: null },
  73. userId: { type: Number, required: true },
  74. title: { type: Function, default: () => "" },
  75. });
  76. const { t } = useI18n();
  77. const { inputRules } = useInputRules();
  78. const { dialogRef, onDialogHide, onDialogOK, onDialogCancel } = useDialogPluginComponent();
  79. const formRef = useTemplateRef("formRef");
  80. const kinshipOptions = computed(() => [
  81. { label: t("associado.kinship_options.conjuge"), value: "conjuge" },
  82. { label: t("associado.kinship_options.filho"), value: "filho" },
  83. { label: t("associado.kinship_options.filha"), value: "filha" },
  84. { label: t("associado.kinship_options.pai"), value: "pai" },
  85. { label: t("associado.kinship_options.mae"), value: "mae" },
  86. { label: t("associado.kinship_options.irmao"), value: "irmao" },
  87. { label: t("associado.kinship_options.irma"), value: "irma" },
  88. { label: t("associado.kinship_options.outro"), value: "outro" },
  89. ]);
  90. // O documento fica fora do tracker: o File não sobrevive ao deepClone usado no diff.
  91. const documentFile = ref(null);
  92. const { form, getUpdatedFields } = useFormUpdateTracker({
  93. name: dependent?.name ?? "",
  94. kinship: dependent?.kinship ?? "",
  95. responsible_user_id: userId,
  96. });
  97. const { loading, validationErrors, execute: submitForm } = useSubmitHandler({
  98. onSuccess: () => onDialogOK(true),
  99. formRef,
  100. });
  101. const onOKClick = async () => {
  102. if (dependent) {
  103. await submitForm(() => updateDependent(dependent.id, getUpdatedFields.value));
  104. } else {
  105. await submitForm(() => createDependent({ ...form, document: documentFile.value }));
  106. }
  107. };
  108. </script>