redone admin
added branches
This commit is contained in:
@@ -6,15 +6,26 @@ const { $api } = useNuxtApp()
|
||||
|
||||
const id = route.params.id as string
|
||||
const profile = ref<any>(null)
|
||||
const branches = ref<any[]>([])
|
||||
const pending = ref(true)
|
||||
const saving = ref(false)
|
||||
|
||||
async function fetchBranches() {
|
||||
try {
|
||||
branches.value = await useEntities("branches").select()
|
||||
} catch (err) {
|
||||
console.error('[fetchBranches]', err)
|
||||
branches.value = []
|
||||
}
|
||||
}
|
||||
|
||||
/** Profil laden **/
|
||||
async function fetchProfile() {
|
||||
pending.value = true
|
||||
try {
|
||||
profile.value = await $api(`/api/profiles/${id}`)
|
||||
ensureWorkingHoursStructure()
|
||||
ensureBranchStructure()
|
||||
} catch (err: any) {
|
||||
console.error('[fetchProfile]', err)
|
||||
toast.add({
|
||||
@@ -27,6 +38,45 @@ async function fetchProfile() {
|
||||
}
|
||||
}
|
||||
|
||||
function ensureBranchStructure() {
|
||||
if (!profile.value) return
|
||||
|
||||
profile.value.branch_id = profile.value.branch_id ?? profile.value.branch?.id ?? null
|
||||
|
||||
if (!Array.isArray(profile.value.branch_ids)) {
|
||||
if (Array.isArray(profile.value.branches)) {
|
||||
profile.value.branch_ids = profile.value.branches
|
||||
.map((entry: any) => entry?.id ?? entry)
|
||||
.filter((entry: any) => entry != null)
|
||||
} else {
|
||||
profile.value.branch_ids = []
|
||||
}
|
||||
}
|
||||
|
||||
if (profile.value.branch_id && !profile.value.branch_ids.includes(profile.value.branch_id)) {
|
||||
profile.value.branch_ids = [...profile.value.branch_ids, profile.value.branch_id]
|
||||
}
|
||||
}
|
||||
|
||||
const updatePrimaryBranch = (value: number | null) => {
|
||||
if (!profile.value) return
|
||||
profile.value.branch_id = value
|
||||
|
||||
if (value && !profile.value.branch_ids.includes(value)) {
|
||||
profile.value.branch_ids = [...profile.value.branch_ids, value]
|
||||
}
|
||||
}
|
||||
|
||||
const updateBranchMemberships = (values: number[]) => {
|
||||
if (!profile.value) return
|
||||
|
||||
profile.value.branch_ids = values || []
|
||||
|
||||
if (profile.value.branch_id && !profile.value.branch_ids.includes(profile.value.branch_id)) {
|
||||
profile.value.branch_id = null
|
||||
}
|
||||
}
|
||||
|
||||
/** Profil speichern **/
|
||||
async function saveProfile() {
|
||||
if (saving.value) return
|
||||
@@ -129,7 +179,9 @@ const checkZip = async () => {
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(fetchProfile)
|
||||
onMounted(async () => {
|
||||
await Promise.all([fetchBranches(), fetchProfile()])
|
||||
})
|
||||
|
||||
</script>
|
||||
|
||||
@@ -255,6 +307,33 @@ onMounted(fetchProfile)
|
||||
|
||||
</UCard>
|
||||
|
||||
<UCard v-if="!pending && profile" class="mt-3">
|
||||
<USeparator label="Niederlassungen" />
|
||||
|
||||
<UForm :state="profile" @submit.prevent="saveProfile" class="grid grid-cols-1 md:grid-cols-2 gap-6 mt-4">
|
||||
<UFormField label="Primäre Niederlassung">
|
||||
<USelectMenu
|
||||
:model-value="profile.branch_id"
|
||||
:items="branches"
|
||||
label-key="name"
|
||||
value-key="id"
|
||||
@update:model-value="updatePrimaryBranch"
|
||||
/>
|
||||
</UFormField>
|
||||
|
||||
<UFormField label="Weitere Niederlassungen">
|
||||
<USelectMenu
|
||||
:model-value="profile.branch_ids"
|
||||
:items="branches"
|
||||
label-key="name"
|
||||
value-key="id"
|
||||
multiple
|
||||
@update:model-value="updateBranchMemberships"
|
||||
/>
|
||||
</UFormField>
|
||||
</UForm>
|
||||
</UCard>
|
||||
|
||||
<UCard v-if="!pending && profile" class="mt-3">
|
||||
<USeparator label="Adresse & Standort" />
|
||||
|
||||
|
||||
@@ -12,7 +12,8 @@
|
||||
id: profile?.id || null,
|
||||
employee_number: profile?.employee_number || '',
|
||||
full_name: profile?.full_name || user?.full_name || user?.email || 'Ohne Profil',
|
||||
email: user?.email || profile?.email || ''
|
||||
email: user?.email || profile?.email || '',
|
||||
branch_name: profile?.branch?.name || ''
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,6 +49,9 @@
|
||||
},{
|
||||
key: "email",
|
||||
label: "E-Mail",
|
||||
},{
|
||||
key: "branch_name",
|
||||
label: "Niederlassung",
|
||||
}
|
||||
]
|
||||
const selectedColumns = ref(templateColumns)
|
||||
|
||||
Reference in New Issue
Block a user