mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2025-12-19 10:07:46 -05:00
refactor(client): simplify checks to see if a user has a certification (#64380)
This commit is contained in:
committed by
GitHub
parent
d828d5a076
commit
78b430c197
@@ -18,6 +18,7 @@ import { SolutionDisplayWidget } from '../solution-display-widget';
|
|||||||
import {
|
import {
|
||||||
Certification,
|
Certification,
|
||||||
currentCertifications,
|
currentCertifications,
|
||||||
|
isCertified,
|
||||||
legacyCertifications,
|
legacyCertifications,
|
||||||
upcomingCertifications,
|
upcomingCertifications,
|
||||||
type CertificationFlags
|
type CertificationFlags
|
||||||
@@ -42,69 +43,6 @@ const mapDispatchToProps = {
|
|||||||
openModal
|
openModal
|
||||||
};
|
};
|
||||||
|
|
||||||
const createCertifiedMap = ({
|
|
||||||
is2018DataVisCert,
|
|
||||||
isA2EnglishCert,
|
|
||||||
isApisMicroservicesCert,
|
|
||||||
isJavascriptCertV9,
|
|
||||||
isJsAlgoDataStructCert,
|
|
||||||
isInfosecQaCert,
|
|
||||||
isPythonCertV9,
|
|
||||||
isQaCertV7,
|
|
||||||
isInfosecCertV7,
|
|
||||||
isFrontEndLibsCert,
|
|
||||||
isRespWebDesignCert,
|
|
||||||
isRespWebDesignCertV9,
|
|
||||||
isDataVisCert,
|
|
||||||
isFrontEndCert,
|
|
||||||
isBackEndCert,
|
|
||||||
isSciCompPyCertV7,
|
|
||||||
isDataAnalysisPyCertV7,
|
|
||||||
isMachineLearningPyCertV7,
|
|
||||||
isRelationalDatabaseCertV8,
|
|
||||||
isRelationalDatabaseCertV9,
|
|
||||||
isCollegeAlgebraPyCertV8,
|
|
||||||
isFoundationalCSharpCertV8,
|
|
||||||
isJsAlgoDataStructCertV8
|
|
||||||
}: CertificationFlags): Record<
|
|
||||||
Exclude<Certification, Certification.LegacyFullStack>,
|
|
||||||
boolean
|
|
||||||
> => ({
|
|
||||||
[Certification.RespWebDesign]: isRespWebDesignCert,
|
|
||||||
[Certification.JsAlgoDataStruct]: isJsAlgoDataStructCert,
|
|
||||||
[Certification.FrontEndDevLibs]: isFrontEndLibsCert,
|
|
||||||
[Certification.DataVis]: is2018DataVisCert,
|
|
||||||
[Certification.BackEndDevApis]: isApisMicroservicesCert,
|
|
||||||
[Certification.QualityAssurance]: isQaCertV7,
|
|
||||||
[Certification.InfoSec]: isInfosecCertV7,
|
|
||||||
[Certification.SciCompPy]: isSciCompPyCertV7,
|
|
||||||
[Certification.DataAnalysisPy]: isDataAnalysisPyCertV7,
|
|
||||||
[Certification.MachineLearningPy]: isMachineLearningPyCertV7,
|
|
||||||
[Certification.RelationalDb]: isRelationalDatabaseCertV8,
|
|
||||||
[Certification.CollegeAlgebraPy]: isCollegeAlgebraPyCertV8,
|
|
||||||
[Certification.FoundationalCSharp]: isFoundationalCSharpCertV8,
|
|
||||||
[Certification.LegacyFrontEnd]: isFrontEndCert,
|
|
||||||
[Certification.LegacyDataVis]: isDataVisCert,
|
|
||||||
[Certification.LegacyBackEnd]: isBackEndCert,
|
|
||||||
[Certification.LegacyInfoSecQa]: isInfosecQaCert,
|
|
||||||
// LegacyFullStack cannot be handled by this because there are no projects to
|
|
||||||
// be rendered. The new FullStackDeveloper certification is a normal
|
|
||||||
// certification with projects.
|
|
||||||
[Certification.RespWebDesignV9]: isRespWebDesignCertV9,
|
|
||||||
[Certification.JsV9]: isJavascriptCertV9,
|
|
||||||
[Certification.FrontEndDevLibsV9]: false,
|
|
||||||
[Certification.PythonV9]: isPythonCertV9,
|
|
||||||
[Certification.RelationalDbV9]: isRelationalDatabaseCertV9,
|
|
||||||
[Certification.BackEndDevApisV9]: false,
|
|
||||||
[Certification.FullStackDeveloperV9]: false,
|
|
||||||
[Certification.A2English]: isA2EnglishCert,
|
|
||||||
[Certification.B1English]: false,
|
|
||||||
[Certification.A2Spanish]: false,
|
|
||||||
[Certification.A2Chinese]: false,
|
|
||||||
[Certification.A1Chinese]: false,
|
|
||||||
[Certification.JsAlgoDataStructNew]: isJsAlgoDataStructCertV8
|
|
||||||
});
|
|
||||||
|
|
||||||
const honestyInfoMessage = {
|
const honestyInfoMessage = {
|
||||||
type: 'info',
|
type: 'info',
|
||||||
message: FlashMessages.HonestFirst
|
message: FlashMessages.HonestFirst
|
||||||
@@ -239,7 +177,6 @@ function CertificationSettings(props: CertificationSettingsProps) {
|
|||||||
|
|
||||||
const handleSolutionModalHide = () => initialiseState();
|
const handleSolutionModalHide = () => initialiseState();
|
||||||
|
|
||||||
const isCertifiedMap = createCertifiedMap(props);
|
|
||||||
const getProjectSolution = (projectId: string, projectTitle: string) => {
|
const getProjectSolution = (projectId: string, projectTitle: string) => {
|
||||||
const { completedChallenges, openModal } = props;
|
const { completedChallenges, openModal } = props;
|
||||||
const completedProject = find(
|
const completedProject = find(
|
||||||
@@ -317,7 +254,7 @@ function CertificationSettings(props: CertificationSettingsProps) {
|
|||||||
<tbody>
|
<tbody>
|
||||||
<ProjectsFor
|
<ProjectsFor
|
||||||
certSlug={certSlug}
|
certSlug={certSlug}
|
||||||
isCert={isCertifiedMap[certSlug]}
|
isCert={isCertified(props, certSlug)}
|
||||||
/>
|
/>
|
||||||
</tbody>
|
</tbody>
|
||||||
</Table>
|
</Table>
|
||||||
|
|||||||
@@ -4,7 +4,8 @@ import {
|
|||||||
linkedInCredentialIds,
|
linkedInCredentialIds,
|
||||||
certToTitleMap,
|
certToTitleMap,
|
||||||
certToIdMap,
|
certToIdMap,
|
||||||
certSlugTypeMap
|
certSlugTypeMap,
|
||||||
|
isCertified
|
||||||
} from './certification-settings';
|
} from './certification-settings';
|
||||||
|
|
||||||
describe('linkedInCredentialIds', () => {
|
describe('linkedInCredentialIds', () => {
|
||||||
@@ -49,3 +50,32 @@ describe('certSlugTypeMap', () => {
|
|||||||
expect(uniqueTypes).toEqual(types);
|
expect(uniqueTypes).toEqual(types);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('isCertified', () => {
|
||||||
|
it('should return true if a user has the specified certification', () => {
|
||||||
|
const cert = Certification.RespWebDesignV9;
|
||||||
|
const user = {
|
||||||
|
isRespWebDesignCertV9: true
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(isCertified(user, cert)).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return false if a user does not have the specified certification', () => {
|
||||||
|
const cert = Certification.JsAlgoDataStruct;
|
||||||
|
const user = {
|
||||||
|
isRespWebDesignCertV9: true
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(isCertified(user, cert)).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return false if the certification does not exist', () => {
|
||||||
|
const cert = 'NonExistentCert' as Certification;
|
||||||
|
const user = {
|
||||||
|
isRespWebDesignCertV9: true
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(isCertified(user, cert)).toBe(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
@@ -251,6 +251,14 @@ export type CertificationFlags = {
|
|||||||
[key in UserCertFlag]: boolean;
|
[key in UserCertFlag]: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export function isCertified(
|
||||||
|
user: Partial<CertificationFlags>,
|
||||||
|
cert: Certification
|
||||||
|
): boolean {
|
||||||
|
const certFlag = certSlugTypeMap[cert];
|
||||||
|
return Boolean(user[certFlag]);
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: use i18n keys instead of hardcoded titles
|
// TODO: use i18n keys instead of hardcoded titles
|
||||||
export const certToTitleMap: Record<Certification, string> = {
|
export const certToTitleMap: Record<Certification, string> = {
|
||||||
// Legacy certifications
|
// Legacy certifications
|
||||||
|
|||||||
Reference in New Issue
Block a user