Skip to content

Commit

Permalink
refactor(homepage): decouple page process and page fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
ben196888 committed Oct 19, 2023
1 parent 6451884 commit 51bcde0
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
5 changes: 1 addition & 4 deletions homepage/src/lib/fetchPage.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import fs from 'fs';
import { join } from 'path';
import matter from 'gray-matter';
import { componentMapper } from './componentMapper';

const pagesDirectory = join(process.cwd(), '_pages');

Expand All @@ -16,10 +15,8 @@ export function fetchPage(lang, page) {
const fileContent = fs.readFileSync(filePath, 'utf8');
const { data } = matter(fileContent);

const contentList = data['layout_list']?.map(componentMapper);

return {
contentList,
data,
};
} catch (error) {
console.log(error);
Expand Down
9 changes: 8 additions & 1 deletion homepage/src/pages/cards.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import contentMapper from '../layouts/contentMapper';
import { fetchPage } from '../lib/fetchPage';
import { fetchCards } from '../lib/fetchCards';
import { getNavigationList } from '../lib/getNavigationList';
import { componentMapper } from '../lib/componentMapper';

/**
*
Expand Down Expand Up @@ -36,6 +37,10 @@ export const getStaticProps = async ({ locale }) => {

const page = fetchPage(locale, 'cards');

const contentList = page.data['layout_list']?.map((layout) =>
componentMapper(layout, rawCards),
);

const title = {
en: 'Card Introduction',
'zh-tw': '卡片介紹',
Expand Down Expand Up @@ -94,7 +99,9 @@ export const getStaticProps = async ({ locale }) => {
'open data': projectCardSubtitle['open data'][locale],
'open source': projectCardSubtitle['open source'][locale],
},
page,
page: {
contentList,
},
},
};
};
Expand Down
6 changes: 5 additions & 1 deletion homepage/src/pages/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Script from 'next/script';
import { fetchPage } from '../lib/fetchPage';
import { getNavigationList } from '../lib/getNavigationList';
import contentMapper from '../layouts/contentMapper';
import { componentMapper } from '../lib/componentMapper';

/**
*
Expand All @@ -23,6 +24,7 @@ export const getStaticProps = async ({ locale }) => {
};

const page = fetchPage(locale, 'index');
const contentList = page.data['layout_list']?.map(componentMapper);

return {
props: {
Expand All @@ -31,7 +33,9 @@ export const getStaticProps = async ({ locale }) => {
title: headInfo.title[locale],
description: headInfo.description[locale],
},
page,
page: {
contentList,
},
},
};
};
Expand Down
6 changes: 5 additions & 1 deletion homepage/src/pages/resource.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import contentMapper from '../layouts/contentMapper';
import { componentMapper } from '../lib/componentMapper';
import { fetchPage } from '../lib/fetchPage';
import { getNavigationList } from '../lib/getNavigationList';

Expand All @@ -8,11 +9,14 @@ import { getNavigationList } from '../lib/getNavigationList';
*/
export const getStaticProps = async ({ locale }) => {
const page = fetchPage(locale, 'resource');
const contentList = page.data['layout_list']?.map(componentMapper);
const navigationList = await getNavigationList(locale);

return {
props: {
page,
page: {
contentList,
},
navigationList,
},
};
Expand Down

0 comments on commit 51bcde0

Please sign in to comment.