Repository: freeCodeCamp/freeCodeCamp
File: client/src/redux/selectors.js (JavaScript)

Code before:
export const showMultipleProgressModalsSelector = state =>
  state[MainApp].showMultipleProgressModals;
export const sessionChallNumProgressModalShownSelector = state =>
  state[MainApp].sessionChallNumProgressModalShown;
export const isDonatingSelector = state => userSelector(state).isDonating;
export const isOnlineSelector = state => state[MainApp].isOnline;
export const isServerOnlineSelector = state => state[MainApp].isServerOnline;
export const isSignedInSelector = state => !!state[MainApp].appUsername;
export const isDonationModalOpenSelector = state =>
  state[MainApp].showDonationModal;
export const isSignoutModalOpenSelector = state =>
  state[MainApp].showSignoutModal;
export const recentlyClaimedBlockSelector = state =>
  state[MainApp].recentlyClaimedBlock;
export const donationFormStateSelector = state =>
  state[MainApp].donationFormState;
export const signInLoadingSelector = state =>
  userFetchStateSelector(state).pending;
export const showCertSelector = state => state[MainApp].showCert;
export const showCertFetchStateSelector = state =>
  state[MainApp].showCertFetchState;
export const shouldRequestDonationSelector = state => {
  const completedChallengesLength = completedChallengesSelector(state).length;
  const completionCount = completionCountSelector(state);
  const sessionChallNumProgressModalShown =
    sessionChallNumProgressModalShownSelector(state);
  const isDonating = isDonatingSelector(state);
  const recentlyClaimedBlock = recentlyClaimedBlockSelector(state);
  const showMultipleProgressModals = showMultipleProgressModalsSelector(state);

  // don't request donation if already donating
  if (isDonating) return false;

  // a block has been completed
  if (recentlyClaimedBlock) return true;

  /* 
  When AB testing for showing multiple progress modals is active,
  show a donation modal every 30 challenges after the first 50
   */
  if (
    showMultipleProgressModals &&
    sessionChallNumProgressModalShown !== null &&
    completedChallengesLength > 50 &&
    sessionChallNumProgressModalShown + 20 >= completionCount
  )
    return true;

  // a donation has already been requested
  if (sessionChallNumProgressModalShown !== null) return false;


Review comment (suggestion):
```suggestion
  const progressDonationModalShown = progressDonationModalShownSelector(state);
  const lastCompletionCount =
    completionCountWhenShownProgressModalSelector(state);
```

Code after:
export const showMultipleProgressModalsSelector = state =>
  state[MainApp].showMultipleProgressModals;
export const completionCountWhenShownProgressModalSelector = state =>
  state[MainApp].completionCountWhenShownProgressModal;
export const progressDonationModalShownSelector = state =>
  state[MainApp].progressDonationModalShown;
export const isDonatingSelector = state => userSelector(state).isDonating;
export const isOnlineSelector = state => state[MainApp].isOnline;
export const isServerOnlineSelector = state => state[MainApp].isServerOnline;
export const isSignedInSelector = state => !!state[MainApp].appUsername;
export const isDonationModalOpenSelector = state =>
  state[MainApp].showDonationModal;
export const isSignoutModalOpenSelector = state =>
  state[MainApp].showSignoutModal;
export const recentlyClaimedBlockSelector = state =>
  state[MainApp].recentlyClaimedBlock;
export const donationFormStateSelector = state =>
  state[MainApp].donationFormState;
export const signInLoadingSelector = state =>
  userFetchStateSelector(state).pending;
export const showCertSelector = state => state[MainApp].showCert;
export const showCertFetchStateSelector = state =>
  state[MainApp].showCertFetchState;
export const shouldRequestDonationSelector = state => {
  const completedChallengesLength = completedChallengesSelector(state).length;
  const completionCount = completionCountSelector(state);
  const lastCompletionCount =
    completionCountWhenShownProgressModalSelector(state);
  const progressDonationModalShown = progressDonationModalShownSelector(state);
  const isDonating = isDonatingSelector(state);
  const recentlyClaimedBlock = recentlyClaimedBlockSelector(state);
  const showMultipleProgressModals = showMultipleProgressModalsSelector(state);

  // don't request donation if already donating
  if (isDonating) return false;

  // a block has been completed
  if (recentlyClaimedBlock) return true;

  /* 
  When AB testing for showing multiple progress modals is active,
  show a donation modal every 30 challenges after the first 50
   */
  if (
    showMultipleProgressModals &&
    progressDonationModalShown &&
    completedChallengesLength > 50 &&
    completionCount - lastCompletionCount >= 30
  )
    return true;