Files
archtika/web-app/src/routes/(authenticated)/website/[websiteId]/+page.server.ts

176 lines
5.0 KiB
TypeScript
Raw Normal View History

import type { Actions, PageServerLoad } from "./$types";
import { API_BASE_PREFIX } from "$lib/server/utils";
2024-09-25 21:45:01 +02:00
import { apiRequest } from "$lib/server/utils";
import type { Settings, Header, Footer } from "$lib/db-schema";
2024-07-31 07:23:32 +02:00
2024-09-25 21:45:01 +02:00
export const load: PageServerLoad = async ({ params, fetch }) => {
const globalSettings: Settings = (
await apiRequest(
fetch,
`${API_BASE_PREFIX}/settings?website_id=eq.${params.websiteId}`,
"GET",
{
headers: {
Accept: "application/vnd.pgrst.object+json"
},
returnData: true
}
)
).data;
const header: Header = (
await apiRequest(fetch, `${API_BASE_PREFIX}/header?website_id=eq.${params.websiteId}`, "GET", {
headers: {
Accept: "application/vnd.pgrst.object+json"
2024-09-25 21:45:01 +02:00
},
returnData: true
})
).data;
2024-07-31 07:23:32 +02:00
2024-09-25 21:45:01 +02:00
const footer: Footer = (
await apiRequest(fetch, `${API_BASE_PREFIX}/footer?website_id=eq.${params.websiteId}`, "GET", {
headers: {
Accept: "application/vnd.pgrst.object+json"
},
returnData: true
})
).data;
2024-07-31 07:23:32 +02:00
return {
globalSettings,
header,
footer,
API_BASE_PREFIX
2024-07-31 07:23:32 +02:00
};
};
export const actions: Actions = {
2024-09-25 21:45:01 +02:00
updateGlobal: async ({ request, fetch, params }) => {
2024-07-31 07:23:32 +02:00
const data = await request.formData();
const faviconFile = data.get("favicon") as File;
const headers: Record<string, string> = {
"Content-Type": "application/octet-stream",
Accept: "application/vnd.pgrst.object+json",
"X-Website-Id": params.websiteId
};
if (faviconFile) {
headers["X-Mimetype"] = faviconFile.type;
headers["X-Original-Filename"] = faviconFile.name;
}
2024-09-25 21:45:01 +02:00
const uploadedImage = await apiRequest(fetch, `${API_BASE_PREFIX}/rpc/upload_file`, "POST", {
headers,
2024-09-25 21:45:01 +02:00
body: faviconFile ? await faviconFile.arrayBuffer() : null,
returnData: true
});
2024-07-31 07:23:32 +02:00
2024-09-25 21:45:01 +02:00
if (!uploadedImage.success && (faviconFile?.size ?? 0 > 0)) {
return uploadedImage;
2024-07-31 07:23:32 +02:00
}
2024-09-25 21:45:01 +02:00
return await apiRequest(
fetch,
`${API_BASE_PREFIX}/settings?website_id=eq.${params.websiteId}`,
"PATCH",
{
body: {
accent_color_dark_theme: data.get("accent-color-dark"),
accent_color_light_theme: data.get("accent-color-light"),
background_color_dark_theme: data.get("background-color-dark"),
background_color_light_theme: data.get("background-color-light"),
2024-09-25 21:45:01 +02:00
favicon_image: uploadedImage.data?.file_id
},
successMessage: "Successfully updated global settings"
}
);
2024-07-31 07:23:32 +02:00
},
2024-09-25 21:45:01 +02:00
updateHeader: async ({ request, fetch, params }) => {
2024-07-31 07:23:32 +02:00
const data = await request.formData();
const logoImage = data.get("logo-image") as File;
const headers: Record<string, string> = {
"Content-Type": "application/octet-stream",
Accept: "application/vnd.pgrst.object+json",
"X-Website-Id": params.websiteId
};
if (logoImage) {
headers["X-Mimetype"] = logoImage.type;
headers["X-Original-Filename"] = logoImage.name;
}
2024-09-25 21:45:01 +02:00
const uploadedImage = await apiRequest(fetch, `${API_BASE_PREFIX}/rpc/upload_file`, "POST", {
headers,
2024-09-25 21:45:01 +02:00
body: logoImage ? await logoImage.arrayBuffer() : null,
returnData: true
});
2024-09-25 21:45:01 +02:00
if (!uploadedImage.success && (logoImage?.size ?? 0 > 0)) {
return { success: false, message: uploadedImage.message };
2024-07-31 07:23:32 +02:00
}
2024-09-25 21:45:01 +02:00
return await apiRequest(
fetch,
`${API_BASE_PREFIX}/header?website_id=eq.${params.websiteId}`,
"PATCH",
{
body: {
logo_type: data.get("logo-type"),
logo_text: data.get("logo-text"),
logo_image: uploadedImage.data?.file_id
},
successMessage: "Successfully updated header"
}
);
2024-07-31 07:23:32 +02:00
},
2024-09-25 21:45:01 +02:00
updateHome: async ({ request, fetch, params }) => {
2024-07-31 07:23:32 +02:00
const data = await request.formData();
2024-09-25 21:45:01 +02:00
return await apiRequest(
fetch,
`${API_BASE_PREFIX}/home?website_id=eq.${params.websiteId}`,
"PATCH",
{
body: {
main_content: data.get("main-content")
},
successMessage: "Successfully updated home"
}
);
2024-07-31 07:23:32 +02:00
},
2024-09-25 21:45:01 +02:00
updateFooter: async ({ request, fetch, params }) => {
2024-07-31 07:23:32 +02:00
const data = await request.formData();
2024-09-25 21:45:01 +02:00
return await apiRequest(
fetch,
`${API_BASE_PREFIX}/footer?website_id=eq.${params.websiteId}`,
"PATCH",
{
body: {
additional_text: data.get("additional-text")
},
successMessage: "Successfully updated footer"
}
);
},
2024-09-25 21:45:01 +02:00
pasteImage: async ({ request, fetch, params }) => {
const data = await request.formData();
const file = data.get("file") as File;
2024-09-25 21:45:01 +02:00
return await apiRequest(fetch, `${API_BASE_PREFIX}/rpc/upload_file`, "POST", {
headers: {
"Content-Type": "application/octet-stream",
Accept: "application/vnd.pgrst.object+json",
"X-Website-Id": params.websiteId,
"X-Mimetype": file.type,
"X-Original-Filename": file.name
},
2024-09-25 21:45:01 +02:00
body: await file.arrayBuffer(),
successMessage: "Successfully uploaded image",
returnData: true
});
2024-07-31 07:23:32 +02:00
}
};