diff --git a/src/components/manage-productions/manage-productions.tsx b/src/components/manage-productions/manage-productions.tsx new file mode 100644 index 00000000..de7901f5 --- /dev/null +++ b/src/components/manage-productions/manage-productions.tsx @@ -0,0 +1,187 @@ +import { ErrorMessage } from "@hookform/error-message"; +import { SubmitHandler, useForm } from "react-hook-form"; +import { useEffect, useState } from "react"; +import styled from "@emotion/styled"; +import { DisplayContainerHeader } from "../landing-page/display-container-header"; +import { + ActionButton, + DecorativeLabel, + FormContainer, + FormInput, + FormLabel, + StyledWarningMessage, +} from "../landing-page/form-elements"; +import { Spinner } from "../loader/loader"; +import { API } from "../../api/api"; +import { useGlobalState } from "../../global-state/context-provider"; +import { useFetchProduction } from "../landing-page/use-fetch-production"; +import { darkText, errorColour } from "../../css-helpers/defaults"; + +type FormValue = { + productionId: string; +}; + +const RemoveConfirmation = styled.div` + background: #91fa8c; + padding: 1rem; + border-radius: 0.5rem; + border: 1px solid #b2ffa1; + color: #1a1a1a; +`; + +const FetchErrorMessage = styled.div` + background: ${errorColour}; + color: ${darkText}; + padding: 0.5rem; + margin: 1rem 0; +`; + +const VerifyBtnWrapper = styled.div` + display: flex; +`; + +export const ManageProductions = () => { + const [loading, setLoading] = useState(false); + const [deleteDone, setDeleteDone] = useState(false); + const [verifyRemove, setVerifyRemove] = useState(false); + const [removeProductionId, setRemoveProductionId] = useState( + null + ); + const [, dispatch] = useGlobalState(); + const { + reset, + formState, + formState: { errors, isSubmitSuccessful }, + register, + handleSubmit, + } = useForm(); + + const { onChange, onBlur, name, ref } = register("productionId", { + required: "Production ID is required", + min: 1, + }); + + const { error: productionFetchError, production } = + useFetchProduction(removeProductionId); + + useEffect(() => { + if (formState.isSubmitSuccessful) { + reset({ + productionId: "", + }); + setRemoveProductionId(null); + setVerifyRemove(false); + } + }, [formState.isSubmitSuccessful, isSubmitSuccessful, reset]); + + const onSubmit: SubmitHandler = (value) => { + setLoading(true); + API.deleteProduction(parseInt(value.productionId, 10)) + .then(() => { + setDeleteDone(true); + setLoading(false); + setVerifyRemove(false); + }) + .catch((error) => { + dispatch({ + type: "ERROR", + payload: error, + }); + setLoading(false); + }); + }; + // TODO return button + + return ( + + Remove Production + + Production ID + { + setDeleteDone(false); + onChange(ev); + + const pid = parseInt(ev.target.value, 10); + + setRemoveProductionId(Number.isNaN(pid) ? null : pid); + }} + name={name} + ref={ref} + onBlur={onBlur} + type="number" + autoComplete="off" + placeholder="Production ID" + /> + + {productionFetchError && ( + + The production ID could not be fetched. {productionFetchError.name}{" "} + {productionFetchError.message}. + + )} + + {production ? ( + production && ( + <> + + Production name: {production.name} + + {!verifyRemove && ( + setVerifyRemove(true)} + > + Remove + {loading && } + + )} + {verifyRemove && ( + <> +

Are you sure?

+ + + Yes + {loading && } + + { + setRemoveProductionId(null); + setVerifyRemove(false); + reset({ + productionId: "", + }); + }} + > + Go back + {loading && } + + + + )} + + ) + ) : ( + + Please enter a production id + + )} + {deleteDone && ( + + The production {production?.name} has been removed + + )} +
+ ); +};