Files
freeCodeCamp/client/utils/gatsby/challenge-page-creator.js

168 lines
4.1 KiB
JavaScript
Raw Normal View History

2018-11-29 12:12:15 +00:00
const path = require('path');
const { createPoly } = require('../../../utils/polyvinyl');
const { dasherize } = require('../../../utils/slugs');
const { sortChallengeFiles } = require('../../../utils/sort-challengefiles');
const { viewTypes } = require('../challenge-types');
2018-11-29 12:12:15 +00:00
const backend = path.resolve(
__dirname,
'../../src/templates/Challenges/projects/backend/Show.tsx'
2018-11-29 12:12:15 +00:00
);
const classic = path.resolve(
__dirname,
'../../src/templates/Challenges/classic/show.tsx'
2018-11-29 12:12:15 +00:00
);
const frontend = path.resolve(
2018-11-29 12:12:15 +00:00
__dirname,
'../../src/templates/Challenges/projects/frontend/Show.tsx'
2018-11-29 12:12:15 +00:00
);
const codeally = path.resolve(
__dirname,
'../../src/templates/Challenges/codeally/show.tsx'
);
2018-11-29 12:12:15 +00:00
const intro = path.resolve(
__dirname,
feat(client): ts-migrate multiple files (#43262) * feat(client): ts-migrate rename files * feat(client): ts-migrate client/src/templates/Introduction/* * feat(client): ts-migrate client/src/components/formHelpers/form* * fix: import * Update client/src/components/formHelpers/form-validators.tsx Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * Update client/src/components/formHelpers/form-fields.tsx Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * Update client/src/components/formHelpers/form-fields.tsx Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * fix: types in client/src/components/formHelpers/index.tsx * fix: types in client/src/templates/Introduction/super-block-intro.tsx * fix: types in client/src/components/formHelpers/* * fix: signInLoading and value types * Update client/src/templates/Introduction/super-block-intro.tsx Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com> * Update client/src/components/formHelpers/index.tsx Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com> * Update client/src/components/formHelpers/index.tsx Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com> * Update client/src/components/formHelpers/index.tsx Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com> * chore(deps): update dependency rollup to v2.58.1 * fix: rename variables and fix imports for ts-migrate * fix: remove `Type` suffix from the type definition. * Update client/src/components/formHelpers/form.tsx Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com> Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com> Co-authored-by: Renovate Bot <bot@renovateapp.com>
2021-10-25 23:15:36 +05:30
'../../src/templates/Introduction/intro.tsx'
2018-11-29 12:12:15 +00:00
);
const superBlockIntro = path.resolve(
__dirname,
feat(client): ts-migrate multiple files (#43262) * feat(client): ts-migrate rename files * feat(client): ts-migrate client/src/templates/Introduction/* * feat(client): ts-migrate client/src/components/formHelpers/form* * fix: import * Update client/src/components/formHelpers/form-validators.tsx Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * Update client/src/components/formHelpers/form-fields.tsx Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * Update client/src/components/formHelpers/form-fields.tsx Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * fix: types in client/src/components/formHelpers/index.tsx * fix: types in client/src/templates/Introduction/super-block-intro.tsx * fix: types in client/src/components/formHelpers/* * fix: signInLoading and value types * Update client/src/templates/Introduction/super-block-intro.tsx Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com> * Update client/src/components/formHelpers/index.tsx Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com> * Update client/src/components/formHelpers/index.tsx Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com> * Update client/src/components/formHelpers/index.tsx Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com> * chore(deps): update dependency rollup to v2.58.1 * fix: rename variables and fix imports for ts-migrate * fix: remove `Type` suffix from the type definition. * Update client/src/components/formHelpers/form.tsx Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com> Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com> Co-authored-by: Renovate Bot <bot@renovateapp.com>
2021-10-25 23:15:36 +05:30
'../../src/templates/Introduction/super-block-intro.tsx'
2018-11-29 12:12:15 +00:00
);
const video = path.resolve(
__dirname,
'../../src/templates/Challenges/video/Show.tsx'
);
2018-11-29 12:12:15 +00:00
const views = {
backend,
classic,
modern: classic,
frontend,
video,
codeally
2018-11-29 12:12:15 +00:00
// quiz: Quiz
};
function getNextChallengePath(_node, index, nodeArray) {
2018-11-29 12:12:15 +00:00
const next = nodeArray[index + 1];
return next ? next.node.challenge.fields.slug : '/learn';
}
function getPrevChallengePath(_node, index, nodeArray) {
const prev = nodeArray[index - 1];
return prev ? prev.node.challenge.fields.slug : '/learn';
}
function getTemplateComponent(challengeType) {
return views[viewTypes[challengeType]];
}
2018-11-29 12:12:15 +00:00
exports.createChallengePages = function (createPage) {
return function ({ node: { challenge } }, index, allChallengeEdges) {
const {
superBlock,
block,
fields: { slug },
required = [],
template,
challengeType,
id
} = challenge;
// TODO: challengeType === 7 and isPrivate are the same, right? If so, we
// should remove one of them.
2018-11-29 12:12:15 +00:00
createPage({
path: slug,
component: getTemplateComponent(challengeType),
context: {
challengeMeta: {
superBlock,
block,
template,
required,
nextChallengePath: getNextChallengePath(
challenge,
index,
allChallengeEdges
),
prevChallengePath: getPrevChallengePath(
challenge,
index,
allChallengeEdges
),
id
},
projectPreview: getProjectPreviewConfig(challenge, allChallengeEdges),
slug
}
});
};
};
2018-11-29 12:12:15 +00:00
function getProjectPreviewConfig(challenge, allChallengeEdges) {
const { block, challengeOrder, usesMultifileEditor } = challenge;
const challengesInBlock = allChallengeEdges
.filter(({ node: { challenge } }) => challenge.block === block)
.map(({ node: { challenge } }) => challenge);
const lastChallenge = challengesInBlock[challengesInBlock.length - 1];
const solutionToLastChallenge = sortChallengeFiles(
lastChallenge.solutions[0] ?? []
);
const lastChallengeFiles = sortChallengeFiles(
lastChallenge.challengeFiles ?? []
);
const projectPreviewChallengeFiles = lastChallengeFiles.map((file, id) =>
createPoly({
...file,
contents: solutionToLastChallenge[id]?.contents ?? file.contents
})
);
return {
showProjectPreview: challengeOrder === 0 && usesMultifileEditor,
challengeData: {
challengeType: lastChallenge.challengeType,
challengeFiles: projectPreviewChallengeFiles,
required: lastChallenge.required,
template: lastChallenge.template
}
};
}
exports.createBlockIntroPages = function (createPage) {
return function (edge) {
const {
fields: { slug },
frontmatter: { block }
} = edge.node;
2018-11-29 12:12:15 +00:00
createPage({
path: slug,
component: intro,
context: {
block: dasherize(block),
slug
}
});
};
2018-11-29 12:12:15 +00:00
};
exports.createSuperBlockIntroPages = function (createPage) {
return function (edge) {
const {
fields: { slug },
frontmatter: { superBlock }
} = edge.node;
2018-11-29 12:12:15 +00:00
createPage({
path: slug,
component: superBlockIntro,
context: {
superBlock,
slug
}
});
};
2018-11-29 12:12:15 +00:00
};