363 lines
14 KiB
TypeScript
363 lines
14 KiB
TypeScript
import { createDirectusConnection } from "@/lib/directus";
|
|
import { print } from 'graphql';
|
|
import { formatDate } from "@/lib/dates";
|
|
import getAllPages from "@/graphql/pages/getAllPages.graphql";
|
|
import getPage from "@/graphql/pages/getPage.graphql";
|
|
import { getImageUrl } from "@/lib/images";
|
|
|
|
export function dataToPage(pageRecord: any): WebPage {
|
|
let dates: string[] = [
|
|
pageRecord["date_created"],
|
|
pageRecord["date_updated"],
|
|
pageRecord["search_engine"][0]["date_created"],
|
|
pageRecord["search_engine"][0]["date_updated"]
|
|
];
|
|
|
|
const searchEngine = pageRecord["search_engine"][0];
|
|
|
|
let components: WebpageComponent[] = [];
|
|
|
|
pageRecord["components"].forEach((componentRecord: any) => {
|
|
const component = componentRecord["item"];
|
|
|
|
switch (componentRecord["item"]["__typename"]) {
|
|
case "Hero":
|
|
let heroComponent: HeroComponent = {
|
|
component: "Hero",
|
|
id: component["hero_id"],
|
|
title: component["hero_title"],
|
|
text: component["hero_text"],
|
|
backgroundImage: {
|
|
url: getImageUrl(component["background_image"]["filename_disk"]),
|
|
width: component["background_image"]["width"],
|
|
height: component["background_image"]["height"]
|
|
}
|
|
};
|
|
|
|
components.push(heroComponent);
|
|
dates.push(component["hero_created"]);
|
|
dates.push(component["hero_updated"]);
|
|
dates.push(component["background_image"]["created_on"]);
|
|
|
|
break;
|
|
case "Text_With_Side_Image":
|
|
let textWithImageComponent: TextWithImageComponent = {
|
|
component: "TextWithImage",
|
|
id: component["twsi_id"],
|
|
title: component["twsi_title"],
|
|
text: component["twsi_text"],
|
|
imageSide: component["twsi_image_side"],
|
|
imageSize: component["twsi_image_size"],
|
|
image: {
|
|
url: getImageUrl(component["image"]["filename_disk"]),
|
|
width: component["image"]["width"],
|
|
height: component["image"]["height"]
|
|
}
|
|
};
|
|
|
|
components.push(textWithImageComponent);
|
|
dates.push(component["twsi_created"]);
|
|
dates.push(component["twsi_updated"]);
|
|
dates.push(component["image"]["created_on"]);
|
|
|
|
break;
|
|
case "Wall_Of_Text":
|
|
let wallOfTextComponent: WallOfTextComponent = {
|
|
component: "WallOfText",
|
|
id: component["wot_id"],
|
|
title: component["wot_title"],
|
|
text: component["wot_text"]
|
|
};
|
|
|
|
components.push(wallOfTextComponent);
|
|
dates.push(component["wot_created"]);
|
|
dates.push(component["wot_updated"]);
|
|
|
|
break;
|
|
case "Frequently_Asked_Questions":
|
|
let faqComponent: FrequentlyAskedQuestionsComponent = {
|
|
component: "FrequentlyAskedQuestions",
|
|
id: component["faq_id"],
|
|
title: component["faq_title"],
|
|
text: component["faq_text"],
|
|
questions: []
|
|
};
|
|
|
|
component["questions"].forEach((faqQuestionRecord: any) => {
|
|
faqComponent.questions.push({
|
|
id: faqQuestionRecord["id"],
|
|
question: faqQuestionRecord["question"],
|
|
answer: faqQuestionRecord["answer"]
|
|
});
|
|
|
|
dates.push(faqQuestionRecord["date_created"]);
|
|
dates.push(faqQuestionRecord["date_updated"]);
|
|
});
|
|
|
|
components.push(faqComponent);
|
|
dates.push(component["faq_created"]);
|
|
dates.push(component["faq_updated"]);
|
|
|
|
break;
|
|
case "Upcoming_Events":
|
|
let upcomingEventsComponent: UpcomingEventsComponent = {
|
|
component: "UpcomingEvents",
|
|
id: component["ue_id"],
|
|
title: component["ue_title"],
|
|
text: component["ue_text"],
|
|
events: []
|
|
};
|
|
|
|
component["events"].forEach((eventRecord: any) => {
|
|
upcomingEventsComponent.events.push({
|
|
id: eventRecord["id"],
|
|
title: eventRecord["title"],
|
|
description: eventRecord["description"],
|
|
location: eventRecord["location"],
|
|
mapLocation: [
|
|
eventRecord["map_location"]["coordinates"][1],
|
|
eventRecord["map_location"]["coordinates"][0]
|
|
],
|
|
thumbnail: {
|
|
url: getImageUrl(eventRecord["thumbnail"]["filename_disk"]),
|
|
width: eventRecord["thumbnail"]["width"],
|
|
height: eventRecord["thumbnail"]["height"]
|
|
},
|
|
startDate: eventRecord["start_date"],
|
|
endDate: eventRecord["end_date"]
|
|
});
|
|
|
|
dates.push(eventRecord["date_created"]);
|
|
dates.push(eventRecord["date_updated"]);
|
|
});
|
|
|
|
components.push(upcomingEventsComponent);
|
|
dates.push(component["ue_created"]);
|
|
dates.push(component["ue_updated"]);
|
|
|
|
break;
|
|
case "Equipment_Table":
|
|
let equipmentTableComponent: EquipmentTableComponent = {
|
|
component: "EquipmentTable",
|
|
id: component["et_id"],
|
|
title: component["et_title"],
|
|
text: component["et_text"],
|
|
items: []
|
|
};
|
|
|
|
component["items"].forEach((itemRecord: any) => {
|
|
equipmentTableComponent.items.push({
|
|
id: itemRecord["id"],
|
|
title: itemRecord["title"],
|
|
text: itemRecord["text"],
|
|
icon: {
|
|
url: getImageUrl(itemRecord["icon"]["filename_disk"]),
|
|
width: itemRecord["icon"]["width"],
|
|
height: itemRecord["icon"]["height"]
|
|
}
|
|
});
|
|
|
|
dates.push(itemRecord["date_created"]);
|
|
dates.push(itemRecord["date_updated"]);
|
|
dates.push(itemRecord["icon"]["created_on"]);
|
|
});
|
|
|
|
components.push(equipmentTableComponent);
|
|
dates.push(component["et_created"]);
|
|
dates.push(component["et_updated"]);
|
|
|
|
break;
|
|
case "Review_List":
|
|
let reviewsComponent: ReviewListComponent = {
|
|
component: "Reviews",
|
|
id: component["rl_id"],
|
|
title: component["rl_title"],
|
|
text: component["rl_text"],
|
|
reviews: []
|
|
};
|
|
|
|
component["reviews"].forEach((reviewRecord: any) => {
|
|
reviewsComponent.reviews.push({
|
|
id: reviewRecord["id"],
|
|
name: reviewRecord["name"],
|
|
review: reviewRecord["review"],
|
|
stars: reviewRecord["stars"],
|
|
date: reviewRecord["date"],
|
|
thumbnail: {
|
|
url: getImageUrl(reviewRecord["thumbnail"]["filename_disk"]),
|
|
width: reviewRecord["thumbnail"]["width"],
|
|
height: reviewRecord["thumbnail"]["height"]
|
|
}
|
|
});
|
|
|
|
dates.push(reviewRecord["date_created"]);
|
|
dates.push(reviewRecord["date_updated"]);
|
|
dates.push(reviewRecord["thumbnail"]["created_on"]);
|
|
});
|
|
|
|
components.push(reviewsComponent);
|
|
dates.push(component["rl_created"]);
|
|
dates.push(component["rl_updated"]);
|
|
|
|
break;
|
|
case "Contact":
|
|
let contactComponent: ContactComponent = {
|
|
component: "Contact",
|
|
id: component["c_id"],
|
|
title: component["c_title"],
|
|
text: component["c_text"],
|
|
methods: []
|
|
};
|
|
|
|
component["methods"].forEach((contactMethodRecord: any) => {
|
|
contactComponent.methods.push({
|
|
id: contactMethodRecord["id"],
|
|
title: contactMethodRecord["title"],
|
|
url: contactMethodRecord["url"],
|
|
color: contactMethodRecord["color"],
|
|
icon: {
|
|
url: getImageUrl(contactMethodRecord["icon"]["filename_disk"]),
|
|
width: contactMethodRecord["icon"]["width"],
|
|
height: contactMethodRecord["icon"]["height"]
|
|
}
|
|
});
|
|
|
|
dates.push(contactMethodRecord["date_created"]);
|
|
dates.push(contactMethodRecord["date_updated"]);
|
|
dates.push(contactMethodRecord["icon"]["created_on"]);
|
|
});
|
|
|
|
components.push(contactComponent);
|
|
dates.push(component["c_created"]);
|
|
dates.push(component["c_updated"]);
|
|
|
|
break;
|
|
case "Last_Blogs":
|
|
let lastBlogsComponent: LastBlogsComponent = {
|
|
component: "LastBlogs",
|
|
id: component["lb_id"],
|
|
title: component["lb_title"],
|
|
readMoreButtonText: component["lb_read_more_button_text"],
|
|
amount: component["lb_amount"]
|
|
};
|
|
|
|
components.push(lastBlogsComponent);
|
|
dates.push(component["lb_created"]);
|
|
dates.push(component["lb_updated"]);
|
|
|
|
break;
|
|
case "Last_Projects":
|
|
let lastProjectsComponent: LastProjectsComponent = {
|
|
component: "LastProjects",
|
|
id: component["lp_id"],
|
|
title: component["lp_title"],
|
|
readMoreButtonText: component["lp_read_more_button_text"],
|
|
amount: component["lp_amount"]
|
|
};
|
|
|
|
components.push(lastProjectsComponent);
|
|
dates.push(component["lp_created"]);
|
|
dates.push(component["lp_updated"]);
|
|
|
|
break;
|
|
case "Last_Galleries":
|
|
let lastGalleriesComponent: LastGalleriesComponent = {
|
|
component: "LastGalleries",
|
|
id: component["lg_id"],
|
|
title: component["lg_title"],
|
|
readMoreButtonText: component["lg_read_more_button_text"],
|
|
amount: component["lg_amount"]
|
|
};
|
|
|
|
components.push(lastGalleriesComponent);
|
|
dates.push(component["lg_created"]);
|
|
dates.push(component["lg_updated"]);
|
|
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
});
|
|
|
|
let lastModified: Date;
|
|
|
|
if (dates.filter(e => e !== null).length === 0) {
|
|
lastModified = new Date();
|
|
}
|
|
else {
|
|
const sortedDates: string[] = dates.sort((a: string, b: string) => {
|
|
return new Date(b).getTime() - new Date(a).getTime();
|
|
});
|
|
|
|
lastModified = new Date(sortedDates[0]);
|
|
}
|
|
|
|
let page: WebPage = {
|
|
type: "Webpage",
|
|
exists: true,
|
|
id: pageRecord["id"],
|
|
lastModified: lastModified,
|
|
url: pageRecord["url"],
|
|
searchEngine: {
|
|
title: searchEngine["title"],
|
|
description: searchEngine["description"],
|
|
canonical: searchEngine["canonical"],
|
|
allowCrawlers: searchEngine["allow_crawler"],
|
|
priority: searchEngine["priority"],
|
|
thumbnail: {
|
|
url: getImageUrl(searchEngine["thumbnail"]["filename_disk"]),
|
|
height: searchEngine["thumbnail"]["height"],
|
|
width: searchEngine["thumbnail"]["width"]
|
|
}
|
|
},
|
|
components: components
|
|
}
|
|
|
|
return page;
|
|
}
|
|
|
|
export async function getAllWebpages(): Promise<WebPage[]> {
|
|
const client = await createDirectusConnection();
|
|
const result = await client.query(print(getAllPages), {
|
|
date: formatDate(new Date(), "%Y-%M-%D")
|
|
});
|
|
|
|
let pages: WebPage[] = [];
|
|
|
|
result["Pages"].forEach((pageRecord: any) => {
|
|
pages.push(dataToPage(pageRecord));
|
|
});
|
|
|
|
return pages;
|
|
}
|
|
|
|
export async function getWebpage(route: string): Promise<WebPage | null> {
|
|
const client = await createDirectusConnection();
|
|
const result = await client.query(print(getPage), {
|
|
date: formatDate(new Date(), "%Y-%M-%D"),
|
|
route: route
|
|
});
|
|
|
|
if (result["Pages"].length === 0) {
|
|
return {
|
|
type: "Webpage",
|
|
exists: false
|
|
};
|
|
}
|
|
|
|
const page = dataToPage(result["Pages"][0]);
|
|
|
|
if (!page.exists) {
|
|
return {
|
|
type: "Webpage",
|
|
exists: false
|
|
};
|
|
}
|
|
|
|
return {
|
|
...page,
|
|
type: "Webpage",
|
|
exists: true
|
|
}
|
|
}
|