|
| 1 | +import React, { useMemo } from 'react'; |
| 2 | +import { useTranslation } from 'react-i18next'; |
| 3 | +import { Spacer } from '@freecodecamp/ui'; |
| 4 | + |
| 5 | +import { |
| 6 | + certificationCollectionSuperBlocks, |
| 7 | + chapterBasedSuperBlocks, |
| 8 | + SuperBlocks |
| 9 | +} from '../../../../../shared-dist/config/curriculum'; |
| 10 | +import type { CertTitle } from '../../../../config/cert-and-project-map'; |
| 11 | +import type { |
| 12 | + ChapterBasedSuperBlockStructure, |
| 13 | + ClaimedCertifications, |
| 14 | + User |
| 15 | +} from '../../../redux/prop-types'; |
| 16 | +import type { |
| 17 | + BlockLabel, |
| 18 | + BlockLayouts |
| 19 | +} from '../../../../../shared-dist/config/blocks'; |
| 20 | +import { SuperBlockIcon } from '../../../assets/superblock-icon'; |
| 21 | +import { Link } from '../../../components/helpers'; |
| 22 | +import { |
| 23 | + certSlugTypeMap, |
| 24 | + certificationRequirements, |
| 25 | + superBlockToCertMap |
| 26 | +} from '../../../../../shared-dist/config/certification-settings'; |
| 27 | +import CheckMark from './check-mark'; |
| 28 | + |
| 29 | +import Block from './block'; |
| 30 | +import CertChallenge from './cert-challenge'; |
| 31 | +import { SuperBlockAccordion } from './super-block-accordion'; |
| 32 | +import './super-block-accordion.css'; |
| 33 | + |
| 34 | +type Challenge = { |
| 35 | + block: string; |
| 36 | + blockLabel: BlockLabel; |
| 37 | + blockLayout: BlockLayouts; |
| 38 | + challengeType: number; |
| 39 | + dashedName: string; |
| 40 | + fields: { slug: string }; |
| 41 | + id: string; |
| 42 | + module: string; |
| 43 | + order: number; |
| 44 | + superBlock: SuperBlocks; |
| 45 | + title: string; |
| 46 | +}; |
| 47 | + |
| 48 | +type SuperBlockMapProps = { |
| 49 | + certification: string; |
| 50 | + completedChallengeIds: string[]; |
| 51 | + disabledBlocks: string[]; |
| 52 | + initialExpandedBlock: string; |
| 53 | + showCertification: boolean; |
| 54 | + structure?: ChapterBasedSuperBlockStructure; |
| 55 | + superBlock: SuperBlocks; |
| 56 | + superBlockChallenges: Challenge[]; |
| 57 | + title: CertTitle; |
| 58 | + user: User | null; |
| 59 | +}; |
| 60 | + |
| 61 | +const BlockList = ({ |
| 62 | + certification, |
| 63 | + disabledBlocks, |
| 64 | + showCertification, |
| 65 | + superBlock, |
| 66 | + superBlockChallenges, |
| 67 | + title, |
| 68 | + user |
| 69 | +}: { |
| 70 | + certification: string; |
| 71 | + disabledBlocks: string[]; |
| 72 | + showCertification: boolean; |
| 73 | + superBlock: SuperBlocks; |
| 74 | + superBlockChallenges: Challenge[]; |
| 75 | + title: CertTitle; |
| 76 | + user: User | null; |
| 77 | +}) => { |
| 78 | + const visibleBlocks = useMemo(() => { |
| 79 | + const uniqueBlocks = Array.from( |
| 80 | + new Set(superBlockChallenges.map(({ block }) => block)) |
| 81 | + ); |
| 82 | + |
| 83 | + return uniqueBlocks.filter(block => !disabledBlocks.includes(block)); |
| 84 | + }, [disabledBlocks, superBlockChallenges]); |
| 85 | + |
| 86 | + return ( |
| 87 | + <div className='block-ui'> |
| 88 | + {visibleBlocks.map(block => { |
| 89 | + const blockChallenges = superBlockChallenges.filter( |
| 90 | + challenge => challenge.block === block |
| 91 | + ); |
| 92 | + const blockLabel = blockChallenges[0]?.blockLabel ?? null; |
| 93 | + |
| 94 | + if (!blockChallenges.length) return null; |
| 95 | + |
| 96 | + return ( |
| 97 | + <Block |
| 98 | + key={block} |
| 99 | + block={block} |
| 100 | + blockLabel={blockLabel} |
| 101 | + challenges={blockChallenges} |
| 102 | + superBlock={superBlock} |
| 103 | + /> |
| 104 | + ); |
| 105 | + })} |
| 106 | + {showCertification && !!user && ( |
| 107 | + <CertChallenge |
| 108 | + certification={certification} |
| 109 | + superBlock={superBlock} |
| 110 | + title={title} |
| 111 | + user={user} |
| 112 | + /> |
| 113 | + )} |
| 114 | + </div> |
| 115 | + ); |
| 116 | +}; |
| 117 | + |
| 118 | +export const SuperBlockMap = ({ |
| 119 | + certification, |
| 120 | + completedChallengeIds, |
| 121 | + disabledBlocks, |
| 122 | + initialExpandedBlock, |
| 123 | + showCertification, |
| 124 | + structure, |
| 125 | + superBlock, |
| 126 | + superBlockChallenges, |
| 127 | + title, |
| 128 | + user |
| 129 | +}: SuperBlockMapProps) => { |
| 130 | + const { t } = useTranslation(); |
| 131 | + if (chapterBasedSuperBlocks.includes(superBlock)) { |
| 132 | + if (!structure) return null; |
| 133 | + |
| 134 | + const getRequirementItems = () => { |
| 135 | + const certificationForSuperBlock = superBlockToCertMap[superBlock]; |
| 136 | + const requirementsLookup = certificationRequirements as Partial< |
| 137 | + Record<string, SuperBlocks[]> |
| 138 | + >; |
| 139 | + const requirements: SuperBlocks[] = |
| 140 | + (certificationForSuperBlock && |
| 141 | + requirementsLookup[certificationForSuperBlock]) ?? |
| 142 | + []; |
| 143 | + |
| 144 | + const requirementItems = requirements.map((requirement: SuperBlocks) => { |
| 145 | + const requirementTitle = t(`intro:${requirement}.title`); |
| 146 | + const requirementLink = `/learn/${requirement}/`; |
| 147 | + |
| 148 | + const certSlug = superBlockToCertMap[requirement]; |
| 149 | + const certFlagLookup = certSlugTypeMap as Record< |
| 150 | + string, |
| 151 | + keyof ClaimedCertifications |
| 152 | + >; |
| 153 | + const certFlagKey = certSlug ? certFlagLookup[certSlug] : undefined; |
| 154 | + const isRequirementComplete = Boolean( |
| 155 | + certFlagKey && user?.[certFlagKey] |
| 156 | + ); |
| 157 | + |
| 158 | + return ( |
| 159 | + <li className='chapter requirement' key={requirement}> |
| 160 | + <Link |
| 161 | + className='chapter-button' |
| 162 | + data-playwright-test-label='requirement-button' |
| 163 | + to={requirementLink} |
| 164 | + > |
| 165 | + <div className='chapter-button-left'> |
| 166 | + <span className='checkmark-wrap chapter-checkmark-wrap'> |
| 167 | + <CheckMark isCompleted={isRequirementComplete} /> |
| 168 | + </span> |
| 169 | + <SuperBlockIcon className='map-icon' superBlock={requirement} /> |
| 170 | + {requirementTitle} |
| 171 | + </div> |
| 172 | + </Link> |
| 173 | + </li> |
| 174 | + ); |
| 175 | + }); |
| 176 | + |
| 177 | + return requirementItems; |
| 178 | + }; |
| 179 | + |
| 180 | + return ( |
| 181 | + <> |
| 182 | + {certificationCollectionSuperBlocks.includes(superBlock) && ( |
| 183 | + <> |
| 184 | + <ul className='super-block-accordion requirement-list'> |
| 185 | + {getRequirementItems()} |
| 186 | + </ul> |
| 187 | + <Spacer size='m' /> |
| 188 | + <h2 className='text-center big-subheading'> |
| 189 | + {t(`intro:misc-text.courses`)} |
| 190 | + </h2> |
| 191 | + <Spacer size='m' /> |
| 192 | + </> |
| 193 | + )} |
| 194 | + |
| 195 | + <SuperBlockAccordion |
| 196 | + challenges={superBlockChallenges} |
| 197 | + superBlock={superBlock} |
| 198 | + structure={structure} |
| 199 | + chosenBlock={initialExpandedBlock} |
| 200 | + completedChallengeIds={completedChallengeIds} |
| 201 | + /> |
| 202 | + </> |
| 203 | + ); |
| 204 | + } |
| 205 | + |
| 206 | + return ( |
| 207 | + <BlockList |
| 208 | + certification={certification} |
| 209 | + disabledBlocks={disabledBlocks} |
| 210 | + showCertification={showCertification} |
| 211 | + superBlock={superBlock} |
| 212 | + superBlockChallenges={superBlockChallenges} |
| 213 | + title={title} |
| 214 | + user={user} |
| 215 | + /> |
| 216 | + ); |
| 217 | +}; |
| 218 | + |
| 219 | +SuperBlockMap.displayName = 'SuperBlockMap'; |
| 220 | + |
| 221 | +export default SuperBlockMap; |
0 commit comments