Skip to content

Commit

Permalink
refactor(client): simplify page creation (freeCodeCamp#55720)
Browse files Browse the repository at this point in the history
  • Loading branch information
ojeytonwilliams authored Aug 13, 2024
1 parent 90b2cfb commit 041a841
Show file tree
Hide file tree
Showing 15 changed files with 39 additions and 58 deletions.
8 changes: 1 addition & 7 deletions client/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ exports.onCreateNode = function onCreateNode({ node, actions, getNode }) {
if (node.internal.type === 'MarkdownRemark') {
const slug = createFilePath({ node, getNode });
if (!slug.includes('LICENSE')) {
const {
frontmatter: { component = '' }
} = node;
createNodeField({ node, name: 'slug', value: slug });
createNodeField({ node, name: 'component', value: component });
}
}
};
Expand Down Expand Up @@ -74,6 +70,7 @@ exports.createPages = async function createPages({
) {
edges {
node {
id
challenge {
block
blockType
Expand Down Expand Up @@ -122,17 +119,14 @@ exports.createPages = async function createPages({
fields {
slug
nodeIdentity
component
}
frontmatter {
certification
block
superBlock
title
}
htmlAst
id
excerpt
}
}
}
Expand Down
17 changes: 0 additions & 17 deletions client/src/redux/prop-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,15 @@ export type CurrentCert = {
};

export type MarkdownRemark = {
fields: [{ component: string; nodeIdentity: string; slug: string }];
fileAbsolutePath: string;
frontmatter: {
block: string;
superBlock: SuperBlocks;
// TODO: make enum like superBlock
certification: string;
title: CertTitle;
};
headings: [
{
depth: number;
value: string;
id: string;
}
];
html: string;
htmlAst: Record<string, unknown>;
id: string;
rawMarkdownBody: string;
timeToRead: number;
wordCount: {
paragraphs: number;
sentences: number;
words: number;
};
};

export type MultipleChoiceAnswer = {
Expand Down
4 changes: 2 additions & 2 deletions client/src/templates/Challenges/classic/show.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -532,8 +532,8 @@ ShowClassic.displayName = 'ShowClassic';
export default connect(mapStateToProps, mapDispatchToProps)(ShowClassic);

export const query = graphql`
query ClassicChallenge($slug: String!) {
challengeNode(challenge: { fields: { slug: { eq: $slug } } }) {
query ClassicChallenge($id: String!) {
challengeNode(id: { eq: $id }) {
challenge {
block
demoType
Expand Down
4 changes: 2 additions & 2 deletions client/src/templates/Challenges/codeally/show.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -426,8 +426,8 @@ export default connect(

// GraphQL
export const query = graphql`
query CodeAllyChallenge($slug: String!) {
challengeNode(challenge: { fields: { slug: { eq: $slug } } }) {
query CodeAllyChallenge($id: String!) {
challengeNode(id: { eq: $id }) {
challenge {
block
fields {
Expand Down
4 changes: 2 additions & 2 deletions client/src/templates/Challenges/dialogue/show.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,8 @@ export default connect(
)(withTranslation()(ShowDialogue));

export const query = graphql`
query Dialogue($slug: String!) {
challengeNode(challenge: { fields: { slug: { eq: $slug } } }) {
query Dialogue($id: String!) {
challengeNode(id: { eq: $id }) {
challenge {
videoId
title
Expand Down
4 changes: 2 additions & 2 deletions client/src/templates/Challenges/exam/show.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -596,8 +596,8 @@ export default connect(

// GraphQL
export const query = graphql`
query ExamChallenge($slug: String!) {
challengeNode(challenge: { fields: { slug: { eq: $slug } } }) {
query ExamChallenge($id: String!) {
challengeNode(id: { eq: $id }) {
challenge {
block
challengeType
Expand Down
4 changes: 2 additions & 2 deletions client/src/templates/Challenges/fill-in-the-blank/show.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 +429,8 @@ export default connect(
)(withTranslation()(ShowFillInTheBlank));

export const query = graphql`
query FillInTheBlankChallenge($slug: String!) {
challengeNode(challenge: { fields: { slug: { eq: $slug } } }) {
query FillInTheBlankChallenge($id: String!) {
challengeNode(id: { eq: $id }) {
challenge {
title
description
Expand Down
4 changes: 2 additions & 2 deletions client/src/templates/Challenges/ms-trophy/show.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ export default connect(
)(withTranslation()(MsTrophy));

export const query = graphql`
query MsTrophyChallenge($slug: String!) {
challengeNode(challenge: { fields: { slug: { eq: $slug } } }) {
query MsTrophyChallenge($id: String!) {
challengeNode(id: { eq: $id }) {
challenge {
title
description
Expand Down
4 changes: 2 additions & 2 deletions client/src/templates/Challenges/odin/show.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -452,8 +452,8 @@ export default connect(
)(withTranslation()(ShowOdin));

export const query = graphql`
query TheOdinProject($slug: String!) {
challengeNode(challenge: { fields: { slug: { eq: $slug } } }) {
query TheOdinProject($id: String!) {
challengeNode(id: { eq: $id }) {
challenge {
videoId
videoLocaleIds {
Expand Down
4 changes: 2 additions & 2 deletions client/src/templates/Challenges/projects/backend/show.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,8 @@ export default connect(
)(withTranslation()(BackEnd));

export const query = graphql`
query BackendChallenge($slug: String!) {
challengeNode(challenge: { fields: { slug: { eq: $slug } } }) {
query BackendChallenge($id: String!) {
challengeNode(id: { eq: $id }) {
challenge {
forumTopicId
title
Expand Down
4 changes: 2 additions & 2 deletions client/src/templates/Challenges/projects/frontend/show.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@ export default connect(
)(withTranslation()(Project));

export const query = graphql`
query ProjectChallenge($slug: String!) {
challengeNode(challenge: { fields: { slug: { eq: $slug } } }) {
query ProjectChallenge($id: String!) {
challengeNode(id: { eq: $id }) {
challenge {
forumTopicId
title
Expand Down
4 changes: 2 additions & 2 deletions client/src/templates/Challenges/video/show.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -359,8 +359,8 @@ export default connect(
)(withTranslation()(ShowVideo));

export const query = graphql`
query VideoChallenge($slug: String!) {
challengeNode(challenge: { fields: { slug: { eq: $slug } } }) {
query VideoChallenge($id: String!) {
challengeNode(id: { eq: $id }) {
challenge {
videoId
videoLocaleIds {
Expand Down
4 changes: 2 additions & 2 deletions client/src/templates/Introduction/intro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ IntroductionPage.displayName = 'IntroductionPage';
export default IntroductionPage;

export const query = graphql`
query IntroPageBySlug($slug: String!, $block: String!) {
markdownRemark(fields: { slug: { eq: $slug } }) {
query IntroPageBySlug($id: String!, $block: String!) {
markdownRemark(id: { eq: $id }) {
frontmatter {
block
superBlock
Expand Down
4 changes: 2 additions & 2 deletions client/src/templates/Introduction/super-block-intro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,8 @@ export default connect(
)(withTranslation()(memo(SuperBlockIntroductionPage)));

export const query = graphql`
query SuperBlockIntroPageBySlug($slug: String!, $superBlock: String!) {
markdownRemark(fields: { slug: { eq: $slug } }) {
query SuperBlockIntroPageBySlug($id: String!, $superBlock: String!) {
markdownRemark(id: { eq: $id }) {
frontmatter {
certification
superBlock
Expand Down
24 changes: 14 additions & 10 deletions client/utils/gatsby/challenge-page-creator.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function getNextBlock(id, edges) {
}

exports.createChallengePages = function (createPage) {
return function ({ node: { challenge } }, index, allChallengeEdges) {
return function ({ node }, index, allChallengeEdges) {
const {
dashedName,
disableLoopProtectTests,
Expand All @@ -112,7 +112,7 @@ exports.createChallengePages = function (createPage) {
template,
challengeType,
id
} = challenge;
} = node.challenge;
// TODO: challengeType === 7 and isPrivate are the same, right? If so, we
// should remove one of them.

Expand All @@ -136,8 +136,11 @@ exports.createChallengePages = function (createPage) {
prevChallengePath: getPrevChallengePath(index, allChallengeEdges),
id
},
projectPreview: getProjectPreviewConfig(challenge, allChallengeEdges),
slug
projectPreview: getProjectPreviewConfig(
node.challenge,
allChallengeEdges
),
id: node.id
}
});
};
Expand Down Expand Up @@ -176,15 +179,16 @@ exports.createBlockIntroPages = function (createPage) {
return function (edge) {
const {
fields: { slug },
frontmatter: { block }
frontmatter: { block },
id
} = edge.node;

createPage({
path: slug,
component: intro,
context: {
block,
slug
id
}
});
};
Expand All @@ -194,7 +198,8 @@ exports.createSuperBlockIntroPages = function (createPage) {
return function (edge) {
const {
fields: { slug },
frontmatter: { superBlock, certification }
frontmatter: { superBlock, certification },
id
} = edge.node;

if (!certification) {
Expand All @@ -210,9 +215,8 @@ exports.createSuperBlockIntroPages = function (createPage) {
path: slug,
component: superBlockIntro,
context: {
certification,
superBlock,
slug
id,
superBlock
}
});
};
Expand Down

0 comments on commit 041a841

Please sign in to comment.