2024-08-01 18:09:35 +02:00
|
|
|
import { handleFileUpload } from "$lib/server/utils.js";
|
2024-08-05 14:38:44 +02:00
|
|
|
import type { Actions, PageServerLoad } from "./$types";
|
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-07-31 07:23:32 +02:00
|
|
|
const globalSettingsData = await fetch(
|
2024-07-31 10:29:46 +02:00
|
|
|
`http://localhost:3000/settings?website_id=eq.${params.websiteId}&select=*,media(*)`,
|
2024-07-31 07:23:32 +02:00
|
|
|
{
|
|
|
|
|
method: "GET",
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
Authorization: `Bearer ${cookies.get("session_token")}`,
|
|
|
|
|
Accept: "application/vnd.pgrst.object+json"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const headerData = await fetch(
|
2024-07-31 10:29:46 +02:00
|
|
|
`http://localhost:3000/header?website_id=eq.${params.websiteId}&select=*,media(*)`,
|
2024-07-31 07:23:32 +02:00
|
|
|
{
|
|
|
|
|
method: "GET",
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
Authorization: `Bearer ${cookies.get("session_token")}`,
|
|
|
|
|
Accept: "application/vnd.pgrst.object+json"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2024-07-31 10:29:46 +02:00
|
|
|
const footerData = await fetch(`http://localhost:3000/footer?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-07-31 10:29:46 +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-01 18:09:35 +02:00
|
|
|
footer
|
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;
|
|
|
|
|
const favicon = await handleFileUpload(
|
|
|
|
|
faviconFile,
|
|
|
|
|
params.websiteId,
|
|
|
|
|
locals.user.id,
|
|
|
|
|
cookies.get("session_token"),
|
|
|
|
|
fetch
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (favicon?.success === false) {
|
|
|
|
|
return favicon;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-31 10:29:46 +02:00
|
|
|
const res = await fetch(`http://localhost:3000/settings?website_id=eq.${params.websiteId}`, {
|
|
|
|
|
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: favicon?.content
|
|
|
|
|
})
|
|
|
|
|
});
|
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
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
updateHeader: async ({ request, fetch, cookies, locals, params }) => {
|
|
|
|
|
const data = await request.formData();
|
|
|
|
|
|
|
|
|
|
const logoFile = data.get("logo-image") as File;
|
|
|
|
|
const logo = await handleFileUpload(
|
|
|
|
|
logoFile,
|
|
|
|
|
params.websiteId,
|
|
|
|
|
locals.user.id,
|
|
|
|
|
cookies.get("session_token"),
|
|
|
|
|
fetch
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (logo?.success === false) {
|
|
|
|
|
return logo;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-31 10:29:46 +02:00
|
|
|
const res = await fetch(`http://localhost:3000/header?website_id=eq.${params.websiteId}`, {
|
2024-07-31 07:23:32 +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: logo?.content
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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-07-31 10:29:46 +02:00
|
|
|
const res = await fetch(`http://localhost:3000/home?website_id=eq.${params.websiteId}`, {
|
2024-07-31 07:23:32 +02:00
|
|
|
method: "PATCH",
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
Authorization: `Bearer ${cookies.get("session_token")}`
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
main_content: data.get("main-content")
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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-07-31 10:29:46 +02:00
|
|
|
const res = await fetch(`http://localhost:3000/footer?website_id=eq.${params.websiteId}`, {
|
2024-07-31 07:23:32 +02:00
|
|
|
method: "PATCH",
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
Authorization: `Bearer ${cookies.get("session_token")}`
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
additional_text: data.get("additional-text")
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
};
|