call api for organization create/update/delete and switch

This commit is contained in:
Gregor Vostrak
2026-06-09 00:12:55 +02:00
committed by Constantin Graf
parent e8f632f330
commit 6c0041d249
8 changed files with 452 additions and 69 deletions

View File

@@ -0,0 +1,31 @@
import axios, { type AxiosError } from 'axios';
type ApiValidationResponse = {
message?: string;
errors?: Record<string, string[]>;
};
export function isApiValidationError(error: unknown): error is AxiosError<ApiValidationResponse> {
return axios.isAxiosError<ApiValidationResponse>(error) && error.response?.status === 422;
}
export function getApiValidationFieldErrors(error: unknown): Record<string, string> {
if (!isApiValidationError(error)) {
return {};
}
const fieldErrors: Record<string, string> = {};
for (const [field, messages] of Object.entries(error.response?.data?.errors ?? {})) {
if (Array.isArray(messages) && messages[0]) {
fieldErrors[field] = messages[0];
}
}
return fieldErrors;
}
export function getApiValidationMessage(error: unknown, fallback: string): string {
if (!isApiValidationError(error)) {
return fallback;
}
return error.response?.data?.message ?? fallback;
}

View File

@@ -11,23 +11,29 @@ import { useNotificationsStore } from '@/utils/notification';
import { getCurrentOrganizationId } from '@/utils/useUser';
import { api } from '@/packages/api/src';
export function switchOrganization(organizationId: string) {
// Clear Inertia's prefetch cache to prevent stale pages from the old
// organization being served when navigating after the switch.
router.flushAll();
export async function switchOrganization(organizationId: string) {
const { handleApiRequestNotifications } = useNotificationsStore();
try {
await handleApiRequestNotifications(
() => api.updateMyCurrentOrganization({ organization_id: organizationId }),
undefined,
'Failed to switch organization'
);
} catch {
// The error notification is surfaced by the request handler.
return;
}
router.put(
route('current-team.update'),
{
team_id: organizationId,
// The current organization changed server-side. Clear Inertia's prefetch
// cache and reload into the dashboard so the new organization context
// (auth.user.current_team) is picked up everywhere.
router.flushAll();
router.visit(route('dashboard'), {
preserveState: false,
onSuccess: () => {
initializeStores();
},
{
preserveState: false,
onSuccess: () => {
initializeStores();
},
}
);
});
}
export const useOrganizationStore = defineStore('organization', () => {
@@ -67,9 +73,33 @@ export const useOrganizationStore = defineStore('organization', () => {
}
}
async function createOrganization(name: string): Promise<Organization | null> {
const response = await api.createOrganization({ name });
return response?.data ?? null;
}
async function deleteOrganization(organizationId: string) {
await handleApiRequestNotifications(
() =>
api.deleteOrganization(undefined, {
params: {
organization: organizationId,
},
}),
'Organization deleted successfully',
'Failed to delete organization'
);
}
const organization = computed<Organization | null>(() => {
return organizationResponse.value?.data || null;
});
return { organization, fetchOrganization, updateOrganization };
return {
organization,
fetchOrganization,
updateOrganization,
createOrganization,
deleteOrganization,
};
});