Files
freeCodeCamp/client/src/components/Map/index.js
Oliver Eyton-Williams e118dda13a fix: order imports and remove circular dependencies (#41824)
* fix: remove circular dependency

redux depended on templates/Challenges/redux and vice versa.  This
meant that import order mattered and confusing bugs could arise.

(cherry picked from commit 7d67a4e70922bbb3051f2f9982dcc69e240d43dc)

* feat: require imports to be in alphabetical order

Import order generally does not matter, but there are edge cases
(circular  imports and css imports, for example) where changing order
changes behaviour

(cherry picked from commit b8d1393a91ec6e068caf8e8498a5c95df68c2b2c)

* chore: order imports

* fix: lift up challenge description + title comps

This brings the classic Show closer to the others as they
now all create the description and title components

* fix: remove donation-saga/index circular import

(cherry picked from commit 51a44ca668a700786d2744feffeae4fdba5fd207)

* refactor: extract action-types from settings

(cherry picked from commit 25e26124d691c84a0d0827d41dafb761c686fadd)

* fix: lint errors

* feat: prevent useless renames
2021-08-02 08:39:40 -05:00

159 lines
4.5 KiB
JavaScript

import { graphql, useStaticQuery } from 'gatsby';
import i18next from 'i18next';
import PropTypes from 'prop-types';
import React from 'react';
import envData from '../../../../config/env.json';
import { isAuditedCert } from '../../../../utils/is-audited';
import { generateIconComponent } from '../../assets/icons';
import LinkButton from '../../assets/icons/link-button';
import { Link, Spacer } from '../helpers';
import './map.css';
const { curriculumLocale } = envData;
const propTypes = {
currentSuperBlock: PropTypes.string,
forLanding: PropTypes.bool
};
function createSuperBlockTitle(superBlock) {
const superBlockTitle = i18next.t(`intro:${superBlock}.title`);
return superBlock === 'coding-interview-prep'
? i18next.t('learn.cert-map-estimates.coding-prep', {
title: superBlockTitle
})
: i18next.t('learn.cert-map-estimates.certs', { title: superBlockTitle });
}
const linkSpacingStyle = {
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center'
};
function renderLandingMap(nodes) {
nodes = nodes.filter(node => node.superBlock !== 'coding-interview-prep');
return (
<ul data-test-label='certifications'>
{nodes.map((node, i) => (
<li key={i}>
<Link
className='btn link-btn btn-lg'
to={`/learn/${node.superBlock}/`}
>
<div style={linkSpacingStyle}>
{generateIconComponent(node.superBlock, 'map-icon')}
{i18next.t(`intro:${node.superBlock}.title`)}
</div>
<LinkButton />
</Link>
</li>
))}
</ul>
);
}
function renderLearnMap(nodes, currentSuperBlock = '') {
nodes = nodes.filter(node => node.superBlock !== currentSuperBlock);
return curriculumLocale === 'english' ? (
<ul data-test-label='learn-curriculum-map'>
{nodes.map((node, i) => (
<li key={i}>
<Link
className='btn link-btn btn-lg'
to={`/learn/${node.superBlock}/`}
>
<div style={linkSpacingStyle}>
{generateIconComponent(node.superBlock, 'map-icon')}
{createSuperBlockTitle(node.superBlock)}
</div>
</Link>
</li>
))}
</ul>
) : (
<ul data-test-label='learn-curriculum-map'>
{nodes
.filter(node => isAuditedCert(curriculumLocale, node.superBlock))
.map((node, i) => (
<li key={i}>
<Link
className='btn link-btn btn-lg'
to={`/learn/${node.superBlock}/`}
>
<div style={linkSpacingStyle}>
{generateIconComponent(node.superBlock, 'map-icon')}
{createSuperBlockTitle(node.superBlock)}
</div>
</Link>
</li>
))}
<hr />
<div style={{ textAlign: 'center' }}>
<p style={{ marginBottom: 0 }}>{i18next.t('learn.help-translate')} </p>
<Link
external={true}
sameTab={false}
to={i18next.t('links:help-translate-link-url')}
>
{i18next.t('learn.help-translate-link')}
</Link>
<Spacer />
</div>
{nodes
.filter(node => !isAuditedCert(curriculumLocale, node.superBlock))
.map((node, i) => (
<li key={i}>
<Link
className='btn link-btn btn-lg'
to={`/learn/${node.superBlock}/`}
>
<div style={linkSpacingStyle}>
{generateIconComponent(node.superBlock, 'map-icon')}
{createSuperBlockTitle(node.superBlock)}
</div>
</Link>
</li>
))}
</ul>
);
}
export function Map({ forLanding = false, currentSuperBlock = '' }) {
/*
* this query gets the first challenge from each block and the second block
* from each superblock, leaving you with one challenge from each
* superblock
*/
const data = useStaticQuery(graphql`
query SuperBlockNodes {
allChallengeNode(
sort: { fields: [superOrder] }
filter: { order: { eq: 2 }, challengeOrder: { eq: 1 } }
) {
nodes {
superBlock
dashedName
}
}
}
`);
let nodes = data.allChallengeNode.nodes;
return (
<div className='map-ui' data-test-label='learn-curriculum-map'>
{forLanding
? renderLandingMap(nodes)
: renderLearnMap(nodes, currentSuperBlock)}
</div>
);
}
Map.displayName = 'Map';
Map.propTypes = propTypes;
export default Map;