All files / utils commonsUtils.js

100% Statements 64/64
100% Branches 15/15
100% Functions 5/5
100% Lines 64/64

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 701x   1x 3x 3x 3x   1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 86x 86x 86x 86x 86x 66x 66x 86x 4x 4x 16x 16x   1x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 90x 90x 45x 45x 45x   1x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 31x 31x 17x 17x 17x  
import { toast } from "react-toastify";
 
export function onDeleteSuccess(message) {
  console.log(message);
  toast(message);
}
 
export function cellToAxiosParamsDelete(cell) {
  return {
    url: "/api/commons",
    method: "DELETE",
    params: {
      id: cell.row.values["commons.id"],
    },
  };
}
 
// A commons with no course is visible to everyone. A course-linked commons
// is only visible to users enrolled in that course (as a student or staff
// member), or to admins. See issue #251.
export function isEligibleForCourseLinkedCommons(
  commons,
  myCourseIds,
  isAdmin,
) {
  if (commons.courseId == null) {
    return true;
  }
  if (isAdmin) {
    return true;
  }
  return myCourseIds.includes(commons.courseId);
}
 
export function filterCommonsNotJoinedAndNotHidden(
  commons,
  commonsJoined,
  // Stryker disable next-line ArrayDeclaration : any placeholder array is
  // behaviorally equivalent to [] here, since .includes() on a real numeric
  // courseId will never match Stryker's string placeholder either way.
  myCourseIds = [],
  isAdmin = false,
) {
  const joinedIdList = commonsJoined.map((c) => c.id);
  return commons.filter(
    (f) =>
      !f.hidden &&
      !joinedIdList.includes(f.id) &&
      isEligibleForCourseLinkedCommons(f, myCourseIds, isAdmin),
  );
}
 
export function filterCommonsJoinedAndNotHidden(
  commons,
  commonsJoined,
  // Stryker disable next-line ArrayDeclaration : any placeholder array is
  // behaviorally equivalent to [] here, since .includes() on a real numeric
  // courseId will never match Stryker's string placeholder either way.
  myCourseIds = [],
  isAdmin = false,
) {
  const joinedIdList = commonsJoined.map((c) => c.id);
  return commons.filter(
    (f) =>
      !f.hidden &&
      joinedIdList.includes(f.id) &&
      isEligibleForCourseLinkedCommons(f, myCourseIds, isAdmin),
  );
}