Created
June 8, 2023 17:09
-
-
Save phenixita/fc34b28f261f16b858a10bc2c6f03659 to your computer and use it in GitHub Desktop.
AthleteSubmissionForm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { AdapterDateFns } from "@mui/x-date-pickers/AdapterDateFns"; | |
import { LocalizationProvider } from "@mui/x-date-pickers"; | |
import Button from "@mui/material/Button"; | |
import { Formik, Form } from "formik"; | |
import * as React from "react"; | |
import useFetchWithMsal from "../hooks/useFetchWithMsal"; | |
import { protectedResources } from "../authConfig"; | |
import { Stack } from "@mui/system"; | |
import { DatePicker } from "@mui/x-date-pickers"; | |
import TextField from "@mui/material/TextField"; | |
export default function AthleteSubmissionForm(props, { setAthletes }) { | |
const { error, execute } = useFetchWithMsal({ | |
scopes: protectedResources.apiTodoList.scopes.read, | |
}); | |
const handleAthleteSubmission = (e) => { | |
e.preventDefault(); | |
execute("POST", protectedResources.athletes.endpoint, { | |
firstName: firstName, | |
lastName: lastName, | |
beltRank: belt, | |
}).then((response) => { | |
console.log(response); | |
reset(); | |
setAthletes(null); | |
}); | |
}; | |
const validate = (values) => { | |
const errors = {}; | |
if (!values.firstName) { | |
errors.firstName = "Required"; | |
} else if (values.firstName.length > 15) { | |
errors.firstName = "Must be 15 characters or less"; | |
} | |
return errors; | |
}; | |
return ( | |
<LocalizationProvider dateAdapter={AdapterDateFns}> | |
<Formik | |
initialValues={{ | |
firstName: "", | |
lastName: "", | |
beltRank: "", | |
birthDate: new Date(), | |
}} | |
validate={validate} | |
onSubmit={(values, { setSubmitting }) => { | |
setTimeout(() => { | |
setSubmitting(false); | |
alert(JSON.stringify(values, null, 2)); | |
}, 500); | |
}} | |
> | |
{({ submitForm, isSubmitting, getFieldProps }) => ( | |
<Form> | |
<Stack> | |
<TextField | |
label="First Name" | |
name="firstName" | |
{...getFieldProps("firstName")} | |
/> | |
<TextField | |
label="Last Name" | |
name="lastName" | |
{...getFieldProps("lastName")} | |
/> | |
<TextField | |
label="Belt Rank" | |
name="beltRank" | |
{...getFieldProps("beltRank")} | |
/> | |
<DatePicker | |
disableFuture | |
{...getFieldProps("birthDate")} | |
label="Date" | |
/> | |
<Button disabled={isSubmitting} onClick={submitForm}> | |
Submit | |
</Button> | |
</Stack> | |
</Form> | |
)} | |
</Formik> | |
</LocalizationProvider> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment