|
| 1 | +import React, {useEffect, useState} from "react"; |
| 2 | + |
| 3 | +import {Box, Step, StepContent, StepLabel, Stepper, Typography,} from "@mui/material"; |
| 4 | +import {useDispatch, useSelector} from "react-redux"; |
| 5 | +import {RootState} from "../../types/data"; |
| 6 | +import {projectCreated} from "../../actions/project"; |
| 7 | +import {useNavigate} from "react-router-dom"; |
| 8 | +import Layout from "../Layout"; |
| 9 | +import {ErrorModal} from "../errors/ErrorModal"; |
| 10 | +import {CreateProjectModal} from "./CreateProjectModal" |
| 11 | + |
| 12 | +export const CreateProject = (): JSX.Element => { |
| 13 | + const dispatch = useDispatch(); |
| 14 | + const createProject = ( |
| 15 | + formData: FormData, |
| 16 | + ) => dispatch(projectCreated(formData)); |
| 17 | + |
| 18 | + const newProject = useSelector( |
| 19 | + (state: RootState) => state.project.newProject |
| 20 | + ); |
| 21 | + |
| 22 | + const [errorOpen, setErrorOpen] = useState(false); |
| 23 | + |
| 24 | + const [projectRequestForm, setProjectRequestForm] = useState({}); |
| 25 | + const [allowSubmit, setAllowSubmit] = React.useState<boolean>(false); |
| 26 | + |
| 27 | + const history = useNavigate(); |
| 28 | + |
| 29 | + const checkIfFieldsAreRequired = () => { |
| 30 | + const required = false; |
| 31 | + |
| 32 | + return required; |
| 33 | + }; |
| 34 | + |
| 35 | + // step 1 - pick datasets |
| 36 | + const onProjectSave = (formData: any) => { |
| 37 | + setProjectRequestForm(formData); |
| 38 | + |
| 39 | + // If no metadata fields are marked as required, allow user to skip directly to submit |
| 40 | + if (checkIfFieldsAreRequired()) { |
| 41 | + setAllowSubmit(false); |
| 42 | + } else { |
| 43 | + setAllowSubmit(true); |
| 44 | + } |
| 45 | + |
| 46 | + handleNext(); |
| 47 | + }; |
| 48 | + // step 2 - add users |
| 49 | + |
| 50 | + // step |
| 51 | + const [activeStep, setActiveStep] = useState(0); |
| 52 | + const handleNext = () => { |
| 53 | + setActiveStep((prevActiveStep) => prevActiveStep + 1); |
| 54 | + }; |
| 55 | + const handleBack = () => { |
| 56 | + setActiveStep((prevActiveStep) => prevActiveStep - 1); |
| 57 | + }; |
| 58 | + |
| 59 | + const handleFinish = () => { |
| 60 | + createProject(projectRequestForm); |
| 61 | + }; |
| 62 | + |
| 63 | + useEffect(() => { |
| 64 | + if (newProject && newProject.id) { |
| 65 | + history(`/projects/${newProject.id}`); |
| 66 | + } |
| 67 | + }, [newProject]); |
| 68 | + |
| 69 | + return ( |
| 70 | + <Layout> |
| 71 | + <Box className="outer-container"> |
| 72 | + {/*Error Message dialogue*/} |
| 73 | + <ErrorModal errorOpen={errorOpen} setErrorOpen={setErrorOpen}/> |
| 74 | + <Box className="inner-container"> |
| 75 | + <Box> |
| 76 | + <Stepper activeStep={activeStep} orientation="vertical"> |
| 77 | + {/*step 1 Dataset*/} |
| 78 | + <Step key="create-dataset"> |
| 79 | + <StepLabel>Basic Information</StepLabel> |
| 80 | + <StepContent> |
| 81 | + <Typography> |
| 82 | + A project is a collection of datasets and a community of contributors. |
| 83 | + </Typography> |
| 84 | + <Box> |
| 85 | + <CreateProjectModal onSave={onProjectSave}/> |
| 86 | + </Box> |
| 87 | + </StepContent> |
| 88 | + </Step> |
| 89 | + |
| 90 | + <Step key="select-datasets"> |
| 91 | + <StepLabel>Select Datasets</StepLabel> |
| 92 | + <StepContent> |
| 93 | + <Box /> |
| 94 | + </StepContent> |
| 95 | + </Step> |
| 96 | + |
| 97 | + <Step key="invite-users"> |
| 98 | + <StepLabel>Invite Users</StepLabel> |
| 99 | + <StepContent> |
| 100 | + <Box /> |
| 101 | + </StepContent> |
| 102 | + </Step> |
| 103 | + </Stepper> |
| 104 | + </Box> |
| 105 | + </Box> |
| 106 | + </Box> |
| 107 | + </Layout> |
| 108 | + ); |
| 109 | +}; |
0 commit comments