All files / components/Courses/TabComponent ConceptTabComponent.jsx

100% Statements 36/36
100% Branches 0/0
100% Functions 13/13
100% Lines 35/35

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                    2x 2x     32x 32x 32x 32x 32x 32x 32x         32x             32x                       32x       1x 1x           32x 1x                     1x 1x 1x           32x 1x           1x 1x 1x           32x 1x 1x     32x 1x 1x     32x       1x                 1x           1x             1x                        
import React, { useState } from "react";
import { Button } from "react-bootstrap";
import { toast } from "react-toastify";
import ConceptDeleteModal from "main/components/Concept/ConceptDeleteModal";
import ConceptModal from "main/components/Concept/ConceptModal";
import ConceptTable from "main/components/Concept/ConceptTable";
import { useBackend, useBackendMutation } from "main/utils/useBackend";
 
// Suppress error toasts when the concept list fetch fails (e.g. network error);
// background fetches should not interrupt the user with toast messages.
const suppressFetchToasts = true;
const DEFAULT_NEW_CONCEPT_POSITION = { x: 0, y: 0 };
 
export default function ConceptTabComponent({ courseId, testIdPrefix }) {
  const [showCreateModal, setShowCreateModal] = useState(false);
  const [showEditModal, setShowEditModal] = useState(false);
  const [showDeleteModal, setShowDeleteModal] = useState(false);
  const [editingConcept, setEditingConcept] = useState(null);
  const [deletingConceptId, setDeletingConceptId] = useState(null);
  const conceptsPath = `/api/concepts/course?courseId=${courseId}`;
  const conceptRelatedQueries = [
    conceptsPath,
    "/api/concepts/top-level",
    "/api/concepts/subconcepts",
  ];
  const { data: concepts } = useBackend(
    [conceptsPath],
    { method: "GET", url: conceptsPath },
    [],
    suppressFetchToasts,
  );
 
  const objectToAxiosParams = (concept) => ({
    url: "/api/concept",
    method: "POST",
    data: {
      courseId,
      label: concept.label,
      description: concept.description,
      example: concept.example,
      ...DEFAULT_NEW_CONCEPT_POSITION,
    },
  });
 
  const createConceptMutation = useBackendMutation(
    objectToAxiosParams,
    {
      onSuccess: (concept) => {
        toast(`Concept ${concept.label} created`);
        setShowCreateModal(false);
      },
    },
    conceptRelatedQueries,
  );
 
  const updateConceptMutation = useBackendMutation(
    (concept) => ({
      url: `/api/concept/put?conceptId=${editingConcept.id}`,
      method: "PUT",
      data: {
        label: concept.label,
        description: concept.description,
        example: concept.example,
      },
    }),
    {
      onSuccess: (concept) => {
        toast(`Concept ${concept.label} updated`);
        setShowEditModal(false);
        setEditingConcept(null);
      },
    },
    conceptRelatedQueries,
  );
 
  const deleteConceptMutation = useBackendMutation(
    () => ({
      url: `/api/concept/delete?conceptId=${deletingConceptId}`,
      method: "DELETE",
    }),
    {
      onSuccess: () => {
        toast("Concept deleted");
        setShowDeleteModal(false);
        setDeletingConceptId(null);
      },
    },
    conceptRelatedQueries,
  );
 
  const editCallback = (cell) => {
    setEditingConcept(cell.row.original);
    setShowEditModal(true);
  };
 
  const deleteCallback = (cell) => {
    setDeletingConceptId(cell.row.original.id);
    setShowDeleteModal(true);
  };
 
  return (
    <div className="tabComponent" data-testid={`${testIdPrefix}-conceptTab`}>
      <h2>Concepts</h2>
      <Button
        onClick={() => setShowCreateModal(true)}
        data-testid={`${testIdPrefix}-post-button`}
        className="mb-3"
      >
        Create Concept
      </Button>
      <ConceptModal
        showModal={showCreateModal}
        toggleShowModal={setShowCreateModal}
        onSubmitAction={(concept) => createConceptMutation.mutate(concept)}
      />
      <ConceptModal
        showModal={showEditModal}
        toggleShowModal={setShowEditModal}
        initialContents={editingConcept}
        onSubmitAction={(concept) => updateConceptMutation.mutate(concept)}
        buttonText="Update"
        modalTitle="Edit Concept"
      />
      <ConceptDeleteModal
        showModal={showDeleteModal}
        toggleShowModal={setShowDeleteModal}
        onSubmitAction={() => deleteConceptMutation.mutate({})}
      />
      <ConceptTable
        concepts={concepts}
        showButtons={true}
        editCallback={editCallback}
        deleteCallback={deleteCallback}
        testId={`${testIdPrefix}-ConceptTable`}
      />
    </div>
  );
}