All files / src/app/teacher-page/page/assignments/new-assignment Step2.jsx

0% Statements 0/24
0% Branches 0/13
0% Functions 0/11
0% Lines 0/19

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                                                                                                                                                                                                                                                                             
import * as React from "react";
import PropTypes from "prop-types";
import Tabs from "@mui/material/Tabs";
import Tab from "@mui/material/Tab";
import Box from "@mui/material/Box";
import SearchQuestions from "./search-questions/SearchQuestions";
import SelectedQuestions from "./selected-questions/SelectedQuestions";
import { Alert, Button, Card, CardContent, Chip, Divider } from "@mui/material";
import { useState } from "react";
import ArrowBackIosIcon from "@mui/icons-material/ArrowBackIos";
import ArrowForwardIosIcon from "@mui/icons-material/ArrowForwardIos";
import { useSelector } from "react-redux";
import { useEffect } from "react";
import { enqueueSnackbar } from "notistack";
 
function TabPanel(props) {
    const { value, index, children, ...other } = props;
    return (
        <div
            role="tabpanel"
            hidden={value !== index}
            id={`full-width-tabpanel-${index}`}
            aria-labelledby={`full-width-tab-${index}`}
            {...other}
        >
            {value === index && <Box sx={{ padding: "5% 0" }}>{children}</Box>}
        </div>
    );
}
 
TabPanel.propTypes = {
    value: PropTypes.number.isRequired,
    index: PropTypes.number.isRequired,
    children: PropTypes.node,
};
 
export default function TabsComponent({ stepController, handleBack, isReuse = false }) {
    const { assignmentData } = useSelector((state) => state.TeacherCreateAssignment);
 
    const [selectedQuestions, setSelectedQuestions] = useState([]);
 
    const [value, setValue] = useState(0);
 
    const clickNextStep = () => {
        if (isReuse || selectedQuestions.length > 0) {
            stepController(1, {
                selectedQuestions: selectedQuestions,
            });
        } else {
            enqueueSnackbar("Select 1 or more questions to proceed", {
                variant: "error",
            });
        }
    };
 
    const handleChange = (event, newValue) => {
        setValue(newValue);
    };
 
    const addNewItem = (newItem) => {
        if (!selectedQuestions.includes(newItem)) setSelectedQuestions((prevItems) => [...prevItems, newItem]);
    };
 
    const removeItem = (question) => {
        setSelectedQuestions((prevItems) => prevItems.filter((item) => item._id !== question._id));
    };
 
    useEffect(() => {
        setSelectedQuestions(assignmentData.step2?.selectedQuestions ?? []);
    }, []);
 
    return (
        <Box
            sx={{
                mt: 5,
                display: "flex",
                flexDirection: "row",
                justifyContent: "space-between",
                gap: "2%",
            }}
        >
            <Button
                id="teacher-class-assignment-step2-back"
                sx={{ mt: 5, height: 40 }}
                variant="contained"
                color="primary"
                onClick={handleBack}
            >
                <ArrowBackIosIcon />
            </Button>
            <Card sx={{ width: "100%", mb: 10 }}>
                <CardContent>
                    {isReuse ? (
                        <>
                            <Alert severity="info" sx={{ mb: 2 }}>
                                This is a staff assignment. Questions are read-only and cannot be added or removed.
                            </Alert>
                            <SelectedQuestions
                                selectedQuestions={selectedQuestions}
                                removeQuestion={removeItem}
                                hideButtonRemove
                            />
                        </>
                    ) : (
                        <>
                            <Box sx={{ display: "flex", alignItems: "center" }}>
                                <Tabs value={value} onChange={handleChange}>
                                    <Tab label="Search Questions" />
                                    <Tab label="Selected Questions" />
                                </Tabs>
                                <Chip label={selectedQuestions.length} color="primary" />
                            </Box>
                            <Divider />
                            <TabPanel value={value} index={0}>
                                <SearchQuestions selectedQuestions={selectedQuestions} setSelectedQuestions={addNewItem} />
                            </TabPanel>
                            <TabPanel value={value} index={1}>
                                <SelectedQuestions selectedQuestions={selectedQuestions} removeQuestion={removeItem} />
                            </TabPanel>
                        </>
                    )}
                </CardContent>
            </Card>
            <Button
                id="teacher-class-assignment-step2-next"
                sx={{ mt: 5, height: 40 }}
                variant="contained"
                onClick={clickNextStep}
            >
                <ArrowForwardIosIcon />
            </Button>
        </Box>
    );
}