Przeglądaj źródła

feat(user): enhance userTypes API to accept unitId; update UserTypeSelect and UserActionPage components

ebagabee 1 tydzień temu
rodzic
commit
903ba73384

+ 5 - 2
src/api/user.js

@@ -38,9 +38,12 @@ export const deleteUser = async (id) => {
   return data.payload;
 };
 
-export const userTypes = async (accessScope = null) => {
+export const userTypes = async (accessScope = null, unitId = null) => {
   const { data } = await api.get("/user/all/types", {
-    params: accessScope ? { access_scope: accessScope } : {},
+    params: {
+      ...(accessScope ? { access_scope: accessScope } : {}),
+      ...(unitId ? { unit_id: unitId } : {}),
+    },
   });
   return data.payload;
 };

+ 10 - 4
src/components/selects/UserTypeSelect.vue

@@ -15,7 +15,7 @@
 </template>
 
 <script setup>
-import { onMounted, ref, computed } from "vue";
+import { onMounted, ref, computed, watch } from "vue";
 import { userTypes } from "src/api/user";
 import { useI18n } from "vue-i18n";
 import DefaultSelect from "src/components/defaults/DefaultSelect.vue";
@@ -30,6 +30,7 @@ const props = defineProps({
     default: () => useI18n().t("common.ui.misc.type"),
   },
   accessScope: { type: String, default: null },
+  unitId: { type: Number, default: null },
 });
 
 const { placeholder, label } = props;
@@ -62,9 +63,10 @@ const filterFn = (val, update) => {
   });
 };
 
-onMounted(async () => {
+const loadOptions = async () => {
+  isLoading.value = true;
   try {
-    const response = await userTypes(props.accessScope);
+    const response = await userTypes(props.accessScope, props.unitId);
     userTypeOptions.value = Object.entries(response).map(([key, value]) => ({
       label: value,
       value: key,
@@ -75,5 +77,9 @@ onMounted(async () => {
   } finally {
     isLoading.value = false;
   }
-});
+};
+
+onMounted(loadOptions);
+
+watch(() => [props.accessScope, props.unitId], loadOptions);
 </script>

+ 2 - 0
src/pages/users/UserActionPage.vue

@@ -47,6 +47,7 @@
               :key="form.access_scope"
               v-model="form.user_type"
               :access-scope="form.access_scope"
+              :unit-id="selectedUnitId"
               class="col-6"
               outlined
               label="Tipo de Usuário"
@@ -198,6 +199,7 @@ const canSave = computed(() =>
     isEdit.value ? "edit" : "add",
   ),
 );
+const selectedUnitId = computed(() => form.value.units?.[0]?.value ?? null);
 
 const form = ref({
   access_scope: "unit",