2024-08-05 14:38:44 +02:00
|
|
|
import type { Actions, PageServerLoad } from "./$types";
|
2024-08-19 20:33:23 +02:00
|
|
|
import { API_BASE_PREFIX } from "$lib/server/utils";
|
2024-07-31 07:23:32 +02:00
|
|
|
|
2024-08-05 14:38:44 +02:00
|
|
|
export const load: PageServerLoad = async ({ params, fetch, cookies, url }) => {
|
2024-08-14 19:33:41 +02:00
|
|
|
const globalSettingsData = await fetch(
|
|
|
|
|
`${API_BASE_PREFIX}/settings?website_id=eq.${params.websiteId}`,
|
|
|
|
|
{
|
|
|
|
|
method: "GET",
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
Authorization: `Bearer ${cookies.get("session_token")}`,
|
|
|
|
|
Accept: "application/vnd.pgrst.object+json"
|
|
|
|
|
}
|
2024-07-31 07:23:32 +02:00
|
|
|
}
|
2024-08-14 19:33:41 +02:00
|
|
|
);
|
2024-08-13 22:14:47 +02:00
|
|
|
|
2024-08-14 19:33:41 +02:00
|
|
|
const headerData = await fetch(`${API_BASE_PREFIX}/header?website_id=eq.${params.websiteId}`, {
|
2024-08-13 22:14:47 +02:00
|
|
|
method: "GET",
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
Authorization: `Bearer ${cookies.get("session_token")}`,
|
|
|
|
|
Accept: "application/vnd.pgrst.object+json"
|
2024-07-31 07:23:32 +02:00
|
|
|
}
|
2024-08-13 22:14:47 +02:00
|
|
|
});
|
|
|
|
|
|
2024-08-14 19:33:41 +02:00
|
|
|
const footerData = await fetch(`${API_BASE_PREFIX}/footer?website_id=eq.${params.websiteId}`, {
|
2024-08-13 22:14:47 +02:00
|
|
|
method: "GET",
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
Authorization: `Bearer ${cookies.get("session_token")}`,
|
|
|
|
|
Accept: "application/vnd.pgrst.object+json"
|
2024-07-31 07:23:32 +02:00
|
|
|
}
|
2024-08-13 22:14:47 +02:00
|
|
|
});
|
2024-07-31 07:23:32 +02:00
|
|
|
|
|
|
|
|
const globalSettings = await globalSettingsData.json();
|
|
|
|
|
const header = await headerData.json();
|
|
|
|
|
const footer = await footerData.json();
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
globalSettings,
|
|
|
|
|
header,
|
2024-08-19 20:33:23 +02:00
|
|
|
footer,
|
|
|
|
|
API_BASE_PREFIX
|
2024-07-31 07:23:32 +02:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2024-08-05 14:38:44 +02:00
|
|
|
export const actions: Actions = {
|
2024-07-31 07:23:32 +02:00
|
|
|
updateGlobal: async ({ request, fetch, cookies, params, locals }) => {
|
|
|
|
|
const data = await request.formData();
|
|
|
|
|
const faviconFile = data.get("favicon") as File;
|
2024-08-10 17:09:12 +02:00
|
|
|
|
2024-08-14 19:33:41 +02:00
|
|
|
const uploadedImageData = await fetch(`${API_BASE_PREFIX}/rpc/upload_file`, {
|
2024-08-13 22:14:47 +02:00
|
|
|
method: "POST",
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/octet-stream",
|
|
|
|
|
Authorization: `Bearer ${cookies.get("session_token")}`,
|
|
|
|
|
Accept: "application/vnd.pgrst.object+json",
|
|
|
|
|
"X-Website-Id": params.websiteId,
|
|
|
|
|
"X-Mimetype": faviconFile.type,
|
|
|
|
|
"X-Original-Filename": faviconFile.name
|
|
|
|
|
},
|
|
|
|
|
body: await faviconFile.arrayBuffer()
|
|
|
|
|
});
|
2024-08-10 17:09:12 +02:00
|
|
|
|
|
|
|
|
const uploadedImage = await uploadedImageData.json();
|
|
|
|
|
|
2024-08-10 22:20:57 +02:00
|
|
|
if (!uploadedImageData.ok && faviconFile.size > 0) {
|
2024-08-10 17:09:12 +02:00
|
|
|
return { success: false, message: uploadedImage.message };
|
2024-07-31 07:23:32 +02:00
|
|
|
}
|
|
|
|
|
|
2024-08-14 19:33:41 +02:00
|
|
|
const res = await fetch(`${API_BASE_PREFIX}/settings?website_id=eq.${params.websiteId}`, {
|
2024-08-13 22:14:47 +02:00
|
|
|
method: "PATCH",
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
Authorization: `Bearer ${cookies.get("session_token")}`
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
accent_color_light_theme: data.get("accent-color-light"),
|
|
|
|
|
accent_color_dark_theme: data.get("accent-color-dark"),
|
|
|
|
|
favicon_image: uploadedImage.file_id
|
|
|
|
|
})
|
|
|
|
|
});
|
2024-07-31 07:23:32 +02:00
|
|
|
|
|
|
|
|
if (!res.ok) {
|
|
|
|
|
const response = await res.json();
|
|
|
|
|
return { success: false, message: response.message };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
success: true,
|
2024-08-01 18:09:35 +02:00
|
|
|
message: "Successfully updated global settings"
|
2024-07-31 07:23:32 +02:00
|
|
|
};
|
|
|
|
|
},
|
2024-08-10 17:09:12 +02:00
|
|
|
updateHeader: async ({ request, fetch, cookies, params }) => {
|
2024-07-31 07:23:32 +02:00
|
|
|
const data = await request.formData();
|
2024-08-10 17:09:12 +02:00
|
|
|
const logoImage = data.get("logo-image") as File;
|
|
|
|
|
|
2024-08-14 19:33:41 +02:00
|
|
|
const uploadedImageData = await fetch(`${API_BASE_PREFIX}/rpc/upload_file`, {
|
2024-08-13 22:14:47 +02:00
|
|
|
method: "POST",
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/octet-stream",
|
|
|
|
|
Authorization: `Bearer ${cookies.get("session_token")}`,
|
|
|
|
|
Accept: "application/vnd.pgrst.object+json",
|
|
|
|
|
"X-Website-Id": params.websiteId,
|
|
|
|
|
"X-Mimetype": logoImage.type,
|
|
|
|
|
"X-Original-Filename": logoImage.name
|
|
|
|
|
},
|
|
|
|
|
body: await logoImage.arrayBuffer()
|
|
|
|
|
});
|
2024-08-10 17:09:12 +02:00
|
|
|
|
|
|
|
|
const uploadedImage = await uploadedImageData.json();
|
2024-07-31 07:23:32 +02:00
|
|
|
|
2024-08-10 22:20:57 +02:00
|
|
|
if (!uploadedImageData.ok && logoImage.size > 0) {
|
2024-08-10 17:09:12 +02:00
|
|
|
return { success: false, message: uploadedImage.message };
|
2024-07-31 07:23:32 +02:00
|
|
|
}
|
|
|
|
|
|
2024-08-14 19:33:41 +02:00
|
|
|
const res = await fetch(`${API_BASE_PREFIX}/header?website_id=eq.${params.websiteId}`, {
|
2024-08-13 22:14:47 +02:00
|
|
|
method: "PATCH",
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
Authorization: `Bearer ${cookies.get("session_token")}`
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
logo_type: data.get("logo-type"),
|
|
|
|
|
logo_text: data.get("logo-text"),
|
|
|
|
|
logo_image: uploadedImage.file_id
|
|
|
|
|
})
|
|
|
|
|
});
|
2024-07-31 07:23:32 +02:00
|
|
|
|
|
|
|
|
if (!res.ok) {
|
|
|
|
|
const response = await res.json();
|
|
|
|
|
return { success: false, message: response.message };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
success: true,
|
2024-08-01 18:09:35 +02:00
|
|
|
message: "Successfully updated header"
|
2024-07-31 07:23:32 +02:00
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
updateHome: async ({ request, fetch, cookies, params }) => {
|
|
|
|
|
const data = await request.formData();
|
|
|
|
|
|
2024-08-14 19:33:41 +02:00
|
|
|
const res = await fetch(`${API_BASE_PREFIX}/home?website_id=eq.${params.websiteId}`, {
|
2024-08-13 22:14:47 +02:00
|
|
|
method: "PATCH",
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
Authorization: `Bearer ${cookies.get("session_token")}`
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
main_content: data.get("main-content")
|
|
|
|
|
})
|
|
|
|
|
});
|
2024-07-31 07:23:32 +02:00
|
|
|
|
|
|
|
|
if (!res.ok) {
|
|
|
|
|
const response = await res.json();
|
|
|
|
|
return { success: false, message: response.message };
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-01 18:09:35 +02:00
|
|
|
return { success: true, message: "Successfully updated home" };
|
2024-07-31 07:23:32 +02:00
|
|
|
},
|
|
|
|
|
updateFooter: async ({ request, fetch, cookies, params }) => {
|
|
|
|
|
const data = await request.formData();
|
|
|
|
|
|
2024-08-14 19:33:41 +02:00
|
|
|
const res = await fetch(`${API_BASE_PREFIX}/footer?website_id=eq.${params.websiteId}`, {
|
2024-08-13 22:14:47 +02:00
|
|
|
method: "PATCH",
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
Authorization: `Bearer ${cookies.get("session_token")}`
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
additional_text: data.get("additional-text")
|
|
|
|
|
})
|
|
|
|
|
});
|
2024-07-31 07:23:32 +02:00
|
|
|
|
|
|
|
|
if (!res.ok) {
|
|
|
|
|
const response = await res.json();
|
|
|
|
|
return { success: false, message: response.message };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
success: true,
|
2024-08-01 18:09:35 +02:00
|
|
|
message: "Successfully updated footer"
|
2024-07-31 07:23:32 +02:00
|
|
|
};
|
2024-08-19 19:31:41 +02:00
|
|
|
},
|
|
|
|
|
pasteImage: async ({ request, fetch, cookies, params }) => {
|
|
|
|
|
const data = await request.formData();
|
|
|
|
|
const file = data.get("file") as File;
|
|
|
|
|
|
|
|
|
|
const fileData = await fetch(`${API_BASE_PREFIX}/rpc/upload_file`, {
|
|
|
|
|
method: "POST",
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/octet-stream",
|
|
|
|
|
Authorization: `Bearer ${cookies.get("session_token")}`,
|
|
|
|
|
Accept: "application/vnd.pgrst.object+json",
|
|
|
|
|
"X-Website-Id": params.websiteId,
|
|
|
|
|
"X-Mimetype": file.type,
|
|
|
|
|
"X-Original-Filename": file.name
|
|
|
|
|
},
|
|
|
|
|
body: await file.arrayBuffer()
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const fileJSON = await fileData.json();
|
|
|
|
|
|
|
|
|
|
if (!fileData.ok) {
|
|
|
|
|
return { success: false, message: fileJSON.message };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return { fileId: fileJSON.file_id };
|
2024-07-31 07:23:32 +02:00
|
|
|
}
|
|
|
|
|
};
|