UnitDataTab.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <template>
  2. <div class="q-pa-md">
  3. <q-form ref="formRef">
  4. <div class="column justify-center items-center q-mb-lg">
  5. <AvatarImageComponent ref="avatarRef" @update:file="onAvatarChange" />
  6. <div class="row full-width q-mt-md q-col-gutter-sm">
  7. <DefaultInput
  8. :model-value="form.social_reason"
  9. class="col-12"
  10. disable
  11. label="Razão Social"
  12. outlined
  13. />
  14. <DefaultInput
  15. :model-value="form.fantasy_name"
  16. class="col-12"
  17. disable
  18. label="Nome Fantasia"
  19. outlined
  20. />
  21. <DefaultInput
  22. :model-value="form.cnpj"
  23. class="col-4"
  24. disable
  25. label="CNPJ"
  26. outlined
  27. />
  28. <DefaultInput
  29. :model-value="form.state_registration"
  30. class="col-4"
  31. disable
  32. label="Inscrição Estadual"
  33. outlined
  34. />
  35. <DefaultInput
  36. :model-value="form.name_responsible"
  37. class="col-4"
  38. disable
  39. label="Responsável"
  40. outlined
  41. />
  42. <DefaultInput
  43. :model-value="form.postal_code"
  44. class="col-3"
  45. disable
  46. label="CEP"
  47. outlined
  48. />
  49. <DefaultInput
  50. :model-value="form.street"
  51. class="col-6"
  52. disable
  53. label="Endereço"
  54. outlined
  55. />
  56. <DefaultInput
  57. :model-value="form.address_number"
  58. class="col-3"
  59. disable
  60. label="Número"
  61. outlined
  62. />
  63. <DefaultInput
  64. :model-value="form.neighborhood"
  65. class="col-4"
  66. disable
  67. label="Bairro"
  68. outlined
  69. />
  70. <DefaultInput
  71. :model-value="stateName"
  72. class="col-4"
  73. disable
  74. label="Estado"
  75. outlined
  76. />
  77. <DefaultInput
  78. :model-value="cityName"
  79. class="col-4"
  80. disable
  81. label="Cidade"
  82. outlined
  83. />
  84. <DefaultInput
  85. :model-value="form.complement"
  86. class="col-12"
  87. disable
  88. label="Complemento"
  89. outlined
  90. />
  91. <DefaultInput
  92. :model-value="form.email"
  93. class="col-6"
  94. disable
  95. label="E-mail Principal"
  96. outlined
  97. />
  98. <DefaultInput
  99. :model-value="form.secondary_email"
  100. class="col-6"
  101. disable
  102. label="E-mail Secundário"
  103. outlined
  104. />
  105. <DefaultInput
  106. :model-value="form.phone_number"
  107. class="col-6"
  108. disable
  109. label="Telefone"
  110. outlined
  111. />
  112. <DefaultInput
  113. :model-value="form.cell_number"
  114. class="col-6"
  115. disable
  116. label="Celular"
  117. outlined
  118. />
  119. <div class="col-12 q-mt-sm">
  120. <div class="text-subtitle2 text-grey-7 q-mb-sm">Alterar Senha</div>
  121. </div>
  122. <DefaultPasswordInput
  123. v-model="password"
  124. v-model:error="validationErrors.password"
  125. class="col-6"
  126. label="Nova Senha"
  127. outlined
  128. :rules="password ? [inputRules.password] : []"
  129. />
  130. <DefaultPasswordInput
  131. v-model="passwordConfirmation"
  132. v-model:error="validationErrors.password_confirmation"
  133. class="col-6"
  134. label="Confirmar Nova Senha"
  135. outlined
  136. :rules="password ? [inputRules.samePassword(password)] : []"
  137. />
  138. </div>
  139. <div class="row justify-end q-mt-md items-end full-width q-px-xs">
  140. <q-btn
  141. color="primary-2"
  142. label="Salvar"
  143. :disable="!hasChanges"
  144. :loading="loading"
  145. @click="onSave"
  146. />
  147. </div>
  148. </div>
  149. </q-form>
  150. </div>
  151. </template>
  152. <script setup>
  153. import { computed, onMounted, ref } from "vue";
  154. import { getUnitMe, updateUnitMe } from "src/api/unit";
  155. import { updateUserMe } from "src/api/user.js";
  156. import { useInputRules } from "src/composables/useInputRules";
  157. import { useSubmitHandler } from "src/composables/useSubmitHandler";
  158. import DefaultInput from "src/components/defaults/DefaultInput.vue";
  159. import DefaultPasswordInput from "src/components/defaults/DefaultPasswordInput.vue";
  160. import AvatarImageComponent from "src/components/shared/AvatarImageComponent.vue";
  161. const props = defineProps({
  162. getFormAsFormData: { type: Function, required: true },
  163. setUpdateFormAsOriginal: { type: Function, required: true },
  164. unitId: { type: Number, default: null },
  165. });
  166. const form = defineModel("form", { type: Object, required: true });
  167. const { inputRules } = useInputRules();
  168. const avatarRef = ref(null);
  169. const cityName = ref("");
  170. const formRef = ref(null);
  171. const newAvatarFile = ref(null);
  172. const password = ref(null);
  173. const passwordConfirmation = ref(null);
  174. const stateName = ref("");
  175. const hasChanges = computed(() => !!newAvatarFile.value || !!password.value);
  176. const { loading, validationErrors, execute } = useSubmitHandler({ formRef });
  177. function onAvatarChange(file) {
  178. newAvatarFile.value = file;
  179. }
  180. async function onSave() {
  181. await execute(async () => {
  182. if (newAvatarFile.value) {
  183. const formData = new FormData();
  184. formData.append("avatar", newAvatarFile.value);
  185. await updateUnitMe(formData);
  186. newAvatarFile.value = null;
  187. }
  188. if (password.value) {
  189. await updateUserMe({ password: password.value });
  190. password.value = null;
  191. passwordConfirmation.value = null;
  192. }
  193. });
  194. }
  195. onMounted(async () => {
  196. try {
  197. const unit = await getUnitMe();
  198. form.value.fantasy_name = unit.fantasy_name;
  199. form.value.social_reason = unit.social_reason;
  200. form.value.cnpj = unit.cnpj;
  201. form.value.state_registration = unit.state_registration;
  202. form.value.name_responsible = unit.name_responsible;
  203. form.value.street = unit.street;
  204. form.value.address_number = unit.address_number;
  205. form.value.postal_code = unit.postal_code;
  206. form.value.neighborhood = unit.neighborhood;
  207. form.value.complement = unit.complement;
  208. form.value.email = unit.email;
  209. form.value.secondary_email = unit.secondary_email;
  210. form.value.phone_number = unit.phone_number;
  211. form.value.cell_number = unit.cell_number;
  212. stateName.value = unit.state?.name ?? "";
  213. cityName.value = unit.city?.name ?? "";
  214. if (unit.avatar_url) {
  215. avatarRef.value?.setImageUrl(unit.avatar_url);
  216. }
  217. props.setUpdateFormAsOriginal();
  218. } catch (e) {
  219. console.error(e);
  220. }
  221. });
  222. </script>