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 | 75x 75x 75x 75x 75x 75x 75x 75x 3x 3x 75x 2x 75x 75x 75x 5x 5x 75x 3x 3x 75x 4x 75x 2x 75x 75x 1x | import React, { useState } from "react";
import Modal from "react-bootstrap/Modal";
import { toast } from "react-toastify";
import OurTable, { ButtonColumn } from "main/components/OurTable";
import ConfirmationModal from "main/components/Common/ConfirmationModal";
import SectionsForm from "main/components/Sections/SectionsForm";
import { useBackendMutation } from "main/utils/useBackend";
import { onSectionMutationError } from "main/utils/sectionsUtils";
export default function SectionsTable({
sections,
courseId,
testIdPrefix = "SectionsTable",
showSlackChannel = false,
}) {
const [showEditModal, setShowEditModal] = useState(false);
const [editSection, setEditSection] = useState(null);
const [showDeleteModal, setShowDeleteModal] = useState(false);
const [deleteSection, setDeleteSection] = useState(null);
const sectionsQueryKey = `/api/courses/${courseId}/sections`;
const cellToAxiosParamsEdit = (formData) => ({
url: `/api/courses/${courseId}/sections/${formData.id}`,
method: "PUT",
params: {
section: formData.section,
label: formData.label,
...(showSlackChannel && {
slackChannelName: formData.slackChannelName,
}),
},
});
const cellToAxiosParamsDelete = (section) => ({
url: `/api/courses/${courseId}/sections/${section.id}`,
method: "DELETE",
});
const onEditSuccess = () => {
toast("Section updated successfully.");
setShowEditModal(false);
};
const onDeleteSuccess = () => {
toast("Section deleted successfully.");
};
const editMutation = useBackendMutation(
cellToAxiosParamsEdit,
{ onSuccess: onEditSuccess, onError: onSectionMutationError },
[sectionsQueryKey],
);
const deleteMutation = useBackendMutation(
cellToAxiosParamsDelete,
{ onSuccess: onDeleteSuccess },
[sectionsQueryKey],
);
const editCallback = (cell) => {
setEditSection(cell.row.original);
setShowEditModal(true);
};
const deleteCallback = (cell) => {
setDeleteSection(cell.row.original);
setShowDeleteModal(true);
};
const submitEditForm = (data) => {
editMutation.mutate(data);
};
const confirmDelete = () => {
deleteMutation.mutate(deleteSection);
};
const columns = [
{
header: "id",
accessorKey: "id",
id: "id",
},
{
header: "Section",
accessorKey: "section",
id: "section",
},
{
header: "Label",
accessorKey: "label",
id: "label",
},
...(showSlackChannel
? [
{
header: "Slack Channel Name",
accessorKey: "slackChannelName",
id: "slackChannelName",
},
]
: []),
ButtonColumn("Edit", "primary", editCallback, testIdPrefix),
ButtonColumn("Delete", "danger", deleteCallback, testIdPrefix),
];
return (
<>
<Modal
show={showEditModal}
onHide={() => setShowEditModal(false)}
centered={true}
data-testid={`${testIdPrefix}-edit-modal`}
>
<Modal.Header closeButton>
<Modal.Title>Edit Section</Modal.Title>
</Modal.Header>
<Modal.Body data-testid={`${testIdPrefix}-edit-modal-body`}>
<SectionsForm
initialContents={editSection}
submitAction={submitEditForm}
buttonLabel="Update"
showSlackChannel={showSlackChannel}
/>
</Modal.Body>
</Modal>
<ConfirmationModal
showModal={showDeleteModal}
setShowModal={setShowDeleteModal}
onYes={confirmDelete}
>
<span data-testid={`${testIdPrefix}-delete-confirmation-message`}>
Are you sure you want to delete section {deleteSection?.section} (
{deleteSection?.label})?
</span>
</ConfirmationModal>
<OurTable data={sections} columns={columns} testid={testIdPrefix} />
</>
);
}
|