diff --git a/legalconsenthub/components/CreateOrganizationModal.vue b/legalconsenthub/components/CreateOrganizationModal.vue index b030575..79593b7 100644 --- a/legalconsenthub/components/CreateOrganizationModal.vue +++ b/legalconsenthub/components/CreateOrganizationModal.vue @@ -33,11 +33,12 @@ diff --git a/legalconsenthub/composables/useBetterAuth.ts b/legalconsenthub/composables/useBetterAuth.ts new file mode 100644 index 0000000..9a173df --- /dev/null +++ b/legalconsenthub/composables/useBetterAuth.ts @@ -0,0 +1,49 @@ +const activeOrganization = ref(null) +const selectedOrgId = ref(undefined) + +export function useBetterAuth() { + const toast = useToast() + + async function createOrganization(name: string, slug: string, logo?: string) { + await organization.create( + { name, slug, logo }, + { + onSuccess: () => { + toast.add({ title: 'Organisation erfolgreich erstellt', color: 'success' }) + return Promise.resolve() + }, + onError: (ctx) => { + toast.add({ + title: 'Fehler bei der Erstellung der Organisation', + description: ctx.error.message, + color: 'error' + }) + return Promise.reject() + } + } + ) + } + + async function deleteOrganization() { + await authClient.organization.delete( + { organizationId: activeOrganization.value?.id ?? '' }, + { + onSuccess: () => { + toast.add({ title: 'Organization deleted', color: 'success' }) + activeOrganization.value = null + selectedOrgId.value = undefined + }, + onError: (ctx) => { + toast.add({ title: 'Error deleting organization', description: ctx.error.message, color: 'error' }) + } + } + ) + } + + return { + activeOrganization, + selectedOrgId, + createOrganization, + deleteOrganization + } +} diff --git a/legalconsenthub/pages/administration.vue b/legalconsenthub/pages/administration.vue index 5274c80..1873932 100644 --- a/legalconsenthub/pages/administration.vue +++ b/legalconsenthub/pages/administration.vue @@ -141,10 +141,8 @@ import { useClipboard } from '@vueuse/core' const { copy, copied } = useClipboard() const toast = useToast() -const selectedOrgId = ref(undefined) -const activeOrganization = ref(null) - const organizations = computed(() => useListOrganizations().value.data || []) +const { deleteOrganization: betterAuthDeleteOrganization, activeOrganization, selectedOrgId } = useBetterAuth() const selectItems = computed(() => organizations.value.map((org) => ({ label: org.name, value: org.id }))) @@ -201,18 +199,6 @@ async function deleteOrganization() { if (!confirmed) return - await authClient.organization.delete( - { organizationId: activeOrganization.value.id }, - { - onSuccess: () => { - toast.add({ title: 'Organization deleted', color: 'success' }) - activeOrganization.value = null - selectedOrgId.value = undefined - }, - onError: (ctx) => { - toast.add({ title: 'Error deleting organization', description: ctx.error.message, color: 'error' }) - } - } - ) + await betterAuthDeleteOrganization() }