Przeglądaj źródła

feat: adiciona crud de tipo de usuarios e adiciona usuario para franquia

ebagabee 1 miesiąc temu
rodzic
commit
de61a447e5

+ 5 - 0
src/api/user.js

@@ -26,6 +26,11 @@ export const createUser = async (user) => {
 };
 
 export const updateUser = async (user, id) => {
+  if (user instanceof FormData) {
+    user.append("_method", "PUT");
+    const { data } = await api.post(`/user/${id}`, user);
+    return data.payload;
+  }
   const { data } = await api.put(`/user/${id}`, user);
   return data.payload;
 };

+ 15 - 35
src/components/selects/UserTypeSelect.vue

@@ -1,6 +1,6 @@
 <template>
   <DefaultSelect
-    v-model="selectedUserType"
+    v-model="selectedOption"
     v-bind="$attrs"
     use-input
     hide-selected
@@ -15,7 +15,7 @@
 </template>
 
 <script setup>
-import { onMounted, ref, watch } from "vue";
+import { onMounted, ref, computed } from "vue";
 import { userTypes } from "src/api/user";
 import { useI18n } from "vue-i18n";
 import DefaultSelect from "src/components/defaults/DefaultSelect.vue";
@@ -29,10 +29,6 @@ const props = defineProps({
     type: String,
     default: () => useI18n().t("common.ui.misc.type"),
   },
-  type: {
-    type: String,
-    default: null,
-  },
   excludeTypes: {
     type: Array,
     default: () => [],
@@ -41,18 +37,23 @@ const props = defineProps({
 
 const { placeholder, label } = props;
 
-const selectedUserType = defineModel({
-  type: Object,
-});
 const userTypeOptions = ref([]);
 const filteredOptions = ref([]);
 const isLoading = ref(true);
 
-const selectUserByValue = (value) => {
-  selectedUserType.value = userTypeOptions.value.find(
-    (option) => option.value === value,
-  );
-};
+const modelValue = defineModel({ type: String, default: null });
+
+const selectedOption = computed({
+  get() {
+    return (
+      userTypeOptions.value.find((opt) => opt.value === modelValue.value) ??
+      null
+    );
+  },
+  set(option) {
+    modelValue.value = option?.value ?? null;
+  },
+});
 
 const filterFn = (val, update) => {
   update(() => {
@@ -74,31 +75,10 @@ onMounted(async () => {
       .map(([key, value]) => ({ label: value, value: key }))
       .filter((opt) => !props.excludeTypes.includes(opt.value));
     filteredOptions.value = userTypeOptions.value;
-
-    if (props.type) {
-      selectedUserType.value = userTypeOptions.value.find(
-        (option) => option.value === props.type,
-      );
-    }
   } catch (error) {
     console.error("Failed to load user types:", error);
   } finally {
     isLoading.value = false;
   }
 });
-
-watch(
-  () => props.type,
-  (newType) => {
-    if (newType && userTypeOptions.value.length > 0) {
-      selectedUserType.value = userTypeOptions.value.find(
-        (option) => option.value === newType,
-      );
-    }
-  },
-);
-
-defineExpose({
-  selectUserByValue,
-});
 </script>

+ 4 - 6
src/pages/users/UserActionPage.vue

@@ -35,7 +35,6 @@
             outlined
             label="Função"
             :rules="[inputRules.required]"
-            :type="editUserTypeValue"
             :disable="!canChangeUserType"
             :exclude-types="['ADMIN']"
           />
@@ -118,7 +117,6 @@ const { user } = storeToRefs(userStore());
 const formRef = ref(null);
 const avatarRef = ref(null);
 const avatarFile = ref(null);
-const editUserTypeValue = ref(null);
 const originalUserType = ref(null);
 
 const isEdit = computed(() => !!route.params.id);
@@ -147,8 +145,8 @@ onMounted(async () => {
     form.value.email = data.email;
     form.value.cpf = data.cpf;
     form.value.phone = data.phone;
-    editUserTypeValue.value = data.user_type?.value ?? data.user_type;
-    originalUserType.value = editUserTypeValue.value;
+    form.value.user_type = data.user_type;
+    originalUserType.value = data.user_type;
     if (data.avatar_url) avatarRef.value?.setImageUrl(data.avatar_url);
   } catch (error) {
     console.error("Failed to load user:", error);
@@ -161,7 +159,7 @@ function buildPayload() {
   if (avatarFile.value) fd.append("avatar", avatarFile.value);
 
   if (user.value?.unit_id) fd.append("unit_id", user.value.unit_id);
-  if (form.value.user_type?.value) fd.append("user_type", form.value.user_type.value);
+  if (form.value.user_type) fd.append("user_type", form.value.user_type);
   if (form.value.cpf) fd.append("cpf", form.value.cpf);
   if (form.value.phone) fd.append("phone", form.value.phone);
   if (form.value.name) fd.append("name", form.value.name);
@@ -172,7 +170,7 @@ function buildPayload() {
 }
 
 async function onSave() {
-  if (isEditingSelf.value && form.value.user_type?.value !== originalUserType.value) {
+  if (isEditingSelf.value && form.value.user_type !== originalUserType.value) {
     $q.notify({
       type: "warning",
       message: "Não é possível alterar o seu próprio tipo de usuário. Entre em contato com o suporte.",