All files / client/src/utils favorite.ts

92.3% Statements 12/13
80% Branches 8/10
100% Functions 3/3
92.3% Lines 12/13

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                        608x       12x   12x       12x   3x   12x 3x     12x 9x 3x 3x           12x              
import { ProgramItem } from "shared/types/models/programItem";
import { AppDispatch } from "client/types/reduxTypes";
import { submitUpdateFavorites } from "client/views/my-program-items/myProgramItemsThunks";
 
export interface UpdateFavoriteOpts {
  programItem: ProgramItem;
  action: string;
  favoriteProgramItems: readonly ProgramItem[];
  username: string;
  dispatch: AppDispatch;
}
 
export const updateFavorite = async (
  updateFavoriteOpts: UpdateFavoriteOpts,
): Promise<void> => {
  const { programItem, action, favoriteProgramItems, username, dispatch } =
    updateFavoriteOpts;
 
  Iif (!programItem.programItemId) {
    return;
  }
 
  const programItemIndex = favoriteProgramItems.findIndex(
    (favoriteProgramItem) =>
      favoriteProgramItem.programItemId === programItem.programItemId,
  );
  const favoriteProgramItemIds = favoriteProgramItems.map(
    (favoriteProgramItem) => favoriteProgramItem.programItemId,
  );
 
  if (action === "add" && programItemIndex === -1) {
    favoriteProgramItemIds.push(programItem.programItemId);
  } else Eif (action === "del" && programItemIndex > -1) {
    favoriteProgramItemIds.splice(programItemIndex, 1);
  }
 
  // A failed request is already reported to the user by the request wrapper,
  // and the only error id this endpoint returns is "unknown", so there is
  // nothing here a caller could tell apart or act on
  await dispatch(
    submitUpdateFavorites({
      username,
      favoriteProgramItemIds,
    }),
  );
};