From 424711fcad0b9456831474ff8480dc9be62460a3 Mon Sep 17 00:00:00 2001 From: Oliver Eyton-Williams Date: Tue, 21 Feb 2023 16:40:01 +0100 Subject: [PATCH] fix(client): provide return types for get-completion-percentage (#49458) fix: provide return types I also moved the undefined check closer to the source, so it's clearer what is missing --- .../src/templates/Challenges/redux/selectors.js | 13 ++++--------- client/src/utils/get-completion-percentage.ts | 15 ++++++++------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/client/src/templates/Challenges/redux/selectors.js b/client/src/templates/Challenges/redux/selectors.js index dfb14bc9cd6..00bfa7bb5c3 100644 --- a/client/src/templates/Challenges/redux/selectors.js +++ b/client/src/templates/Challenges/redux/selectors.js @@ -94,30 +94,25 @@ export const challengeDataSelector = state => { export const currentBlockIdsSelector = state => { const { block, certification, challengeType } = challengeMetaSelector(state); const allChallengesInfo = allChallengesInfoSelector(state); - const currentBlockIds = getCurrentBlockIds( + + return getCurrentBlockIds( allChallengesInfo, block, certification, challengeType ); - - if (!currentBlockIds) { - return []; - } - - return currentBlockIds; }; export const completedChallengesInBlockSelector = state => { const completedChallengesIds = completedChallengesIdsSelector(state); const currentBlockIds = currentBlockIdsSelector(state); const { id } = challengeMetaSelector(state); - const completedChallengesInBlock = getCompletedChallengesInBlock( + + return getCompletedChallengesInBlock( completedChallengesIds, currentBlockIds, id ); - return completedChallengesInBlock; }; export const completedPercentageSelector = state => { diff --git a/client/src/utils/get-completion-percentage.ts b/client/src/utils/get-completion-percentage.ts index 1371604d2d4..392644c48a5 100644 --- a/client/src/utils/get-completion-percentage.ts +++ b/client/src/utils/get-completion-percentage.ts @@ -23,7 +23,7 @@ export function getCompletedChallengesInBlock( completedChallengesIds: string[], currentBlockIds: string[], currentChallengeId: string -) { +): number { const oldCompletionCount = completedChallengesIds.filter(challengeId => currentBlockIds.includes(challengeId) ).length; @@ -39,13 +39,14 @@ export const getCurrentBlockIds = ( block: string, certification: string, challengeType: number -) => { +): string[] => { const { challengeEdges, certificateNodes } = allChallengesInfo; - const currentCertificateIds = certificateNodes - .filter( - node => dasherize(node.challenge.certification) === certification - )[0] - ?.challenge.tests.map(test => test.id); + const currentCertificateIds = + certificateNodes + .filter( + node => dasherize(node.challenge.certification) === certification + )[0] + ?.challenge.tests.map(test => test.id) ?? []; const currentBlockIds = challengeEdges .filter(edge => edge.node.challenge.block === block) .map(edge => edge.node.challenge.id);