All files / client/src/views/all-program-items allProgramItemsSlice.ts

100% Statements 32/32
50% Branches 1/2
100% Functions 16/16
100% Lines 32/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147                                            532x         532x               424x                   424x                   43x   43x   43x 43x   43x                 532x   532x       532x 9551x   532x     393x                 393x 393x   2751x 17673x     334x       532x     393x                       393x 3537x 7063x           532x     393x   393x           393x   1572x 2543x     328x      
import { PayloadAction, createSelector, createSlice } from "@reduxjs/toolkit";
import { unique } from "remeda";
import {
  AgeGroup,
  Language,
  ProgramItem,
  Tag,
} from "shared/types/models/programItem";
import {
  AllProgramItemsState,
  ProgramItemDirectSignups,
  RootState,
} from "client/types/reduxTypes";
 
interface DirectSignupUpdate {
  programItemId: string;
  updates: {
    username: string;
    signupMessage: string;
  }[];
}
 
const initialState: AllProgramItemsState = {
  programItems: [],
  directSignups: [],
};
 
const allProgramItemsSlice = createSlice({
  name: "allProgramItems",
  initialState,
  reducers: {
    submitGetProgramItemsAsync(
      state,
      action: PayloadAction<ProgramItem[]>,
    ): AllProgramItemsState {
      return {
        ...state,
        programItems: action.payload,
      };
    },
 
    submitGetDirectSignupsAsync(
      state,
      action: PayloadAction<ProgramItemDirectSignups[]>,
    ): AllProgramItemsState {
      return {
        ...state,
        directSignups: action.payload,
      };
    },
 
    submitUpdateDirectSignupAsync(
      state,
      action: PayloadAction<DirectSignupUpdate>,
    ): AllProgramItemsState {
      const index = state.directSignups.findIndex(
        (directSignup) =>
          directSignup.programItemId === action.payload.programItemId,
      );
      Eif (index !== -1) {
        state.directSignups[index].users = action.payload.updates;
      }
      return state;
    },
  },
});
 
export const {
  submitGetProgramItemsAsync,
  submitGetDirectSignupsAsync,
  submitUpdateDirectSignupAsync,
} = allProgramItemsSlice.actions;
 
export const allProgramItemsReducer = allProgramItemsSlice.reducer;
 
// SELECTORS
 
export const selectProgramItems = (state: RootState): readonly ProgramItem[] =>
  state.allProgramItems.programItems;
 
export const selectTags = createSelector(
  [selectProgramItems],
  (programItems) => {
    const ignoredTags = new Set([
      Tag.GUEST_OF_HONOR,
      Tag.THEME,
      Tag.LGBT,
      Tag.PRE_CONVENTION_WEEK,
      Tag.K16, // This is entryCondition, not age limit
      Tag.USES_GEN_AI,
    ]);
 
    const tags = unique([Tag.BEGINNER_FRIENDLY, ...Object.values(Tag)]);
    return tags
      .filter((tag) => {
        return programItems.some((programItem) =>
          programItem.tags.includes(tag),
        );
      })
      .filter((tag) => !ignoredTags.has(tag));
  },
);
 
export const selectAgeGroups = createSelector(
  [selectProgramItems],
  (programItems) => {
    const ageGroups = unique([
      AgeGroup.EVERYONE,
      AgeGroup.FAMILIES,
      AgeGroup.SMALL_KIDS,
      AgeGroup.KIDS,
      AgeGroup.TEENS,
      AgeGroup.YOUNG_ADULTS,
      AgeGroup.ADULTS_AND_YOUTH,
      AgeGroup.ADULTS,
      AgeGroup.ONLY_ADULTS,
      ...Object.values(AgeGroup),
    ]);
    return ageGroups.filter((ageGroup) =>
      programItems.some((programItem) =>
        programItem.ageGroups.includes(ageGroup),
      ),
    );
  },
);
 
export const selectLanguages = createSelector(
  [selectProgramItems],
  (programItems) => {
    const ignoredLanguages = new Set([Language.LANGUAGE_FREE]);
 
    const languages = unique([
      Language.FINNISH,
      Language.ENGLISH,
      Language.SWEDISH,
      ...Object.values(Language),
    ]);
    return languages
      .filter((language) => {
        return programItems.some((programItem) =>
          programItem.languages.includes(language),
        );
      })
      .filter((language) => !ignoredLanguages.has(language));
  },
);