mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-08-14 11:12:16 +01:00
call api for organization create/update/delete and switch
This commit is contained in:
committed by
Constantin Graf
parent
0c825a691e
commit
e3e45161a1
31
resources/js/utils/apiValidation.ts
Normal file
31
resources/js/utils/apiValidation.ts
Normal 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;
|
||||
}
|
||||
@@ -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,
|
||||
};
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user