import { find, map, forEach } from "lodash"; // import { FetchedData } from "../fetchPublicData"; import getLocales from "../helpers/getLocales"; import createPaths from "../helpers/createPaths"; import createFilenames from "../helpers/createFilenames"; import templatePaths from "../helpers/templatePaths"; import chunksByPage from "../helpers/chunksByPage"; import createCommonConfig from "../helpers/createCommonConfig"; import getPageAndGlobalTranslations from "../helpers/getPageAndGlobalTranslations"; import formatDate from "../helpers/formatDate"; import getUncategorizedCategoryProperties from "../helpers/getUncategorizedCategoryProperties"; const HtmlWebpackPlugin = require("html-webpack-plugin"); type CategoryData = { name: string; locale: string; slug: string; posts: { title: string; description: string; updatedAt: string; link: string; }[]; availableLocales: string[]; }; const categories = (responses: FetchedData): typeof HtmlWebpackPlugin[] => { const { postsResponse, categoriesResponse } = responses; const posts = postsResponse.data.items; const categories = categoriesResponse.data.items; const categoriesData: CategoryData[] = []; forEach(categories, category => { forEach(getLocales(), locale => { const translation = find(category.translations, { locale }); if (!translation) return; const postsData: CategoryData["posts"] = []; forEach(posts, post => { if (post.category && post.category === category.id) { const postTranslation = find(post.translations, { locale }); if (postTranslation) { postsData.push({ title: postTranslation.title, description: postTranslation.metaDescription, link: `${createPaths.getPublicUrl()}/${createPaths.post(locale, category.slug, post.slug)}`, updatedAt: formatDate(postTranslation.updatedAt, locale) }); } } }); categoriesData.push({ locale, name: translation.name, slug: category.slug, posts: postsData, availableLocales: map(category.translations, translation => translation.locale) }); }); }); // uncategorized category pages forEach(getLocales(), locale => { const uncategorizedProps = getUncategorizedCategoryProperties(locale); const postsData: CategoryData["posts"] = []; forEach(posts, post => { if (!post.category) { const postTranslation = find(post.translations, { locale }); if (postTranslation) { postsData.push({ title: postTranslation.title, description: postTranslation.metaDescription, link: `${createPaths.getPublicUrl()}/${createPaths.post( locale, uncategorizedProps.slug, post.slug )}`, updatedAt: formatDate(postTranslation.updatedAt, locale) }); } } }); categoriesData.push({ locale, name: uncategorizedProps.name, slug: uncategorizedProps.slug, posts: postsData, availableLocales: getLocales() }); }); return map( categoriesData, ({ name, slug, locale, posts, availableLocales }) => new HtmlWebpackPlugin({ filename: createFilenames.category(locale, slug), template: templatePaths.category, chunks: chunksByPage.category, templateParameters: { ...createCommonConfig(locale, availableLocales, locale => createPaths.category(locale, slug)), // TODO: available locales name, slug, locale, posts, i18n: getPageAndGlobalTranslations(locale, "category") } }) ); }; export default categories;