ProfileEditDialog.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <template>
  2. <q-dialog ref="dialogRef" persistent maximized transition-show="slide-left" transition-hide="slide-right">
  3. <q-card class="bg-white full-width full-height">
  4. <div class="row items-center q-px-md q-pt-md q-pb-sm bg-white shadow-profile">
  5. <q-btn flat round dense icon="mdi-chevron-left" color="primary" @click="onDialogCancel" />
  6. <q-space />
  7. <span class="text-subtitle1 text-weight-bold text-primary">{{ $t('profile.edit_data') }}</span>
  8. <q-space />
  9. <div style="width: 32px"></div>
  10. </div>
  11. <div v-if="loading" class="flex flex-center q-pa-xl">
  12. <q-spinner color="primary" size="3em" />
  13. </div>
  14. <template v-else>
  15. <q-scroll-area class="col" style="height: calc(100vh - 72px)">
  16. <div class="column items-center q-mt-xl q-mb-md">
  17. <q-avatar size="140px" color="indigo-1" text-color="indigo-4" class="text-weight-bold text-h2 shadow-1">
  18. {{ form.name ? form.name.charAt(0).toUpperCase() : '' }}
  19. </q-avatar>
  20. <q-btn flat no-caps color="grey-6" class="q-mt-sm" :label="$t('profile.change_photo')" />
  21. </div>
  22. <div class="q-px-xl q-gutter-y-lg">
  23. <div>
  24. <div class="text-weight-bold text-grey-8 q-mb-sm">{{ $t('profile.full_name') }}</div>
  25. <q-input
  26. v-model="form.name"
  27. outlined
  28. dense
  29. input-class="text-text"
  30. :placeholder="$t('profile.placeholder_name')"
  31. />
  32. </div>
  33. <div>
  34. <div class="text-weight-bold text-grey-8 q-mb-sm">{{ $t('profile.email') }}</div>
  35. <q-input
  36. v-model="form.email"
  37. outlined
  38. dense
  39. input-class="text-text"
  40. :placeholder="$t('profile.placeholder_email')"
  41. />
  42. </div>
  43. <div>
  44. <div class="text-weight-bold text-grey-8 q-mb-sm">{{ $t('profile.phone') }}</div>
  45. <q-input
  46. v-model="form.phone"
  47. outlined
  48. dense
  49. input-class="text-text"
  50. mask="(##) #####-####"
  51. unmasked-value
  52. :placeholder="$t('profile.placeholder_phone')"
  53. />
  54. </div>
  55. </div>
  56. <q-space/>
  57. <div class="q-pa-xl q-mt-md">
  58. <q-btn
  59. unelevated
  60. rounded
  61. no-caps
  62. padding="8px 16px"
  63. class="full-width q-py-md text-weight-bold"
  64. :label="$t('profile.update')"
  65. :color="hasUpdatedFields ? 'primary' : 'grey-4'"
  66. :disable="!hasUpdatedFields"
  67. :loading="submitting"
  68. @click="submitUpdate"
  69. />
  70. </div>
  71. </q-scroll-area>
  72. </template>
  73. </q-card>
  74. </q-dialog>
  75. </template>
  76. <script setup>
  77. import { ref, onMounted } from 'vue';
  78. import { useDialogPluginComponent } from 'quasar';
  79. import { updateUser } from 'src/api/user';
  80. import { useFormUpdateTracker } from 'src/composables/useFormUpdateTracker';
  81. const props = defineProps({
  82. userData: {
  83. type: Object,
  84. default: null
  85. }
  86. });
  87. defineEmits([...useDialogPluginComponent.emits]);
  88. const { dialogRef, onDialogOK, onDialogCancel } = useDialogPluginComponent();
  89. const loading = ref(false);
  90. const submitting = ref(false);
  91. const userId = ref(null);
  92. const { form, hasUpdatedFields, setUpdateFormAsOriginal } = useFormUpdateTracker({
  93. name: '',
  94. email: '',
  95. phone: ''
  96. });
  97. const submitUpdate = async () => {
  98. if (!hasUpdatedFields.value) return;
  99. submitting.value = true;
  100. try {
  101. const data = await updateUser({
  102. name: form.name,
  103. email: form.email,
  104. phone: form.phone
  105. }, userId.value);
  106. console.log(data)
  107. setUpdateFormAsOriginal(data);
  108. onDialogOK(data);
  109. } catch (error) {
  110. console.error('Erro ao atualizar perfil:', error);
  111. } finally {
  112. submitting.value = false;
  113. }
  114. };
  115. onMounted(async () => {
  116. if (props.userData) {
  117. const data = props.userData;
  118. userId.value = data.id;
  119. form.name = data.name || '';
  120. form.email = data.email || '';
  121. form.phone = data.phone || '';
  122. setUpdateFormAsOriginal(data);
  123. return;
  124. }
  125. loading.value = true;
  126. });
  127. </script>
  128. <style scoped lang="scss">
  129. :deep(.q-field--outlined .q-field__control) {
  130. border-radius: 8px;
  131. &::before { border: 1px solid #e0e0e0; }
  132. }
  133. </style>