refactor(frontend): extract toActionResult helper and formatDate utility
- Admin page: replace 7 identical error-handling blocks with a single toActionResult() helper — DRY without over-abstraction - New date.ts util: formatDate(isoDate) centralises the T12:00:00 timezone guard and Intl.DateTimeFormat locale config Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
11
frontend/src/lib/utils/date.ts
Normal file
11
frontend/src/lib/utils/date.ts
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
/**
|
||||||
|
* Format an ISO date string (YYYY-MM-DD) for display.
|
||||||
|
* Uses T12:00:00 to avoid UTC timezone off-by-one when converting to local time.
|
||||||
|
*/
|
||||||
|
export function formatDate(isoDate: string): string {
|
||||||
|
return new Intl.DateTimeFormat('de-DE', {
|
||||||
|
day: 'numeric',
|
||||||
|
month: 'long',
|
||||||
|
year: 'numeric'
|
||||||
|
}).format(new Date(isoDate + 'T12:00:00'));
|
||||||
|
}
|
||||||
@@ -2,6 +2,16 @@ import { error, fail } from '@sveltejs/kit';
|
|||||||
import { createApiClient } from '$lib/api.server';
|
import { createApiClient } from '$lib/api.server';
|
||||||
import { getErrorMessage } from '$lib/errors';
|
import { getErrorMessage } from '$lib/errors';
|
||||||
|
|
||||||
|
type ApiResult = { response: Response; error?: unknown };
|
||||||
|
|
||||||
|
function toActionResult(result: ApiResult) {
|
||||||
|
if (!result.response.ok) {
|
||||||
|
const code = (result.error as { code?: string } | undefined)?.code;
|
||||||
|
return fail(result.response.status, { success: false, message: getErrorMessage(code) });
|
||||||
|
}
|
||||||
|
return { success: true };
|
||||||
|
}
|
||||||
|
|
||||||
export async function load({ fetch, locals }) {
|
export async function load({ fetch, locals }) {
|
||||||
const user = locals.user;
|
const user = locals.user;
|
||||||
const hasAdmin = user?.groups?.some((g: { permissions: string[] }) => g.permissions.includes('ADMIN'));
|
const hasAdmin = user?.groups?.some((g: { permissions: string[] }) => g.permissions.includes('ADMIN'));
|
||||||
@@ -35,11 +45,7 @@ export const actions = {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!result.response.ok) {
|
return toActionResult(result);
|
||||||
const code = (result.error as unknown as { code?: string })?.code;
|
|
||||||
return fail(result.response.status, { success: false, message: getErrorMessage(code) });
|
|
||||||
}
|
|
||||||
return { success: true };
|
|
||||||
},
|
},
|
||||||
|
|
||||||
deleteUser: async ({ request, fetch }) => {
|
deleteUser: async ({ request, fetch }) => {
|
||||||
@@ -51,11 +57,7 @@ export const actions = {
|
|||||||
params: { path: { id } }
|
params: { path: { id } }
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!result.response.ok) {
|
return toActionResult(result);
|
||||||
const code = (result.error as unknown as { code?: string })?.code;
|
|
||||||
return fail(result.response.status, { success: false, message: getErrorMessage(code) });
|
|
||||||
}
|
|
||||||
return { success: true };
|
|
||||||
},
|
},
|
||||||
|
|
||||||
updateTag: async ({ request, fetch }) => {
|
updateTag: async ({ request, fetch }) => {
|
||||||
@@ -68,11 +70,7 @@ export const actions = {
|
|||||||
body: { name: data.get('name') as string }
|
body: { name: data.get('name') as string }
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!result.response.ok) {
|
return toActionResult(result);
|
||||||
const code = (result.error as unknown as { code?: string })?.code;
|
|
||||||
return fail(result.response.status, { success: false, message: getErrorMessage(code) });
|
|
||||||
}
|
|
||||||
return { success: true };
|
|
||||||
},
|
},
|
||||||
|
|
||||||
deleteTag: async ({ request, fetch }) => {
|
deleteTag: async ({ request, fetch }) => {
|
||||||
@@ -84,11 +82,7 @@ export const actions = {
|
|||||||
params: { path: { id } }
|
params: { path: { id } }
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!result.response.ok) {
|
return toActionResult(result);
|
||||||
const code = (result.error as unknown as { code?: string })?.code;
|
|
||||||
return fail(result.response.status, { success: false, message: getErrorMessage(code) });
|
|
||||||
}
|
|
||||||
return { success: true };
|
|
||||||
},
|
},
|
||||||
|
|
||||||
createGroup: async ({ request, fetch }) => {
|
createGroup: async ({ request, fetch }) => {
|
||||||
@@ -102,11 +96,7 @@ export const actions = {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!result.response.ok) {
|
return toActionResult(result);
|
||||||
const code = (result.error as unknown as { code?: string })?.code;
|
|
||||||
return fail(result.response.status, { success: false, message: getErrorMessage(code) });
|
|
||||||
}
|
|
||||||
return { success: true };
|
|
||||||
},
|
},
|
||||||
|
|
||||||
updateGroup: async ({ request, fetch }) => {
|
updateGroup: async ({ request, fetch }) => {
|
||||||
@@ -122,11 +112,7 @@ export const actions = {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!result.response.ok) {
|
return toActionResult(result);
|
||||||
const code = (result.error as unknown as { code?: string })?.code;
|
|
||||||
return fail(result.response.status, { success: false, message: getErrorMessage(code) });
|
|
||||||
}
|
|
||||||
return { success: true };
|
|
||||||
},
|
},
|
||||||
|
|
||||||
deleteGroup: async ({ request, fetch }) => {
|
deleteGroup: async ({ request, fetch }) => {
|
||||||
@@ -138,10 +124,6 @@ export const actions = {
|
|||||||
params: { path: { id } }
|
params: { path: { id } }
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!result.response.ok) {
|
return toActionResult(result);
|
||||||
const code = (result.error as unknown as { code?: string })?.code;
|
|
||||||
return fail(result.response.status, { success: false, message: getErrorMessage(code) });
|
|
||||||
}
|
|
||||||
return { success: true };
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user