-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9673d43
commit 1787eb3
Showing
3 changed files
with
156 additions
and
19 deletions.
There are no files selected for viewing
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
143 changes: 143 additions & 0 deletions
143
frontend/react/src/components/signup/SignupCustomerForm.jsx
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import React from 'react'; | ||
import {Formik, Form, useField} from 'formik'; | ||
import * as Yup from 'yup'; | ||
import {Alert, AlertIcon, Box, Button, FormLabel, Input, Select, Stack} from "@chakra-ui/react"; | ||
import {signup} from "../../services/client.js"; | ||
import {errorNotification, successNotification} from "../../services/notification.js"; | ||
|
||
const MyTextInput = ({label, ...props}) => { | ||
const [field, meta] = useField(props); | ||
return ( | ||
<Box> | ||
<FormLabel htmlFor={props.id || props.name}>{label}</FormLabel> | ||
<Input className="text-input" {...field} {...props} /> | ||
{meta.touched && meta.error ? ( | ||
<Alert className="error" status={"error"} mt={2}> | ||
<AlertIcon/> | ||
{meta.error} | ||
</Alert> | ||
) : null} | ||
</Box> | ||
); | ||
}; | ||
|
||
const MySelect = ({label, ...props}) => { | ||
const [field, meta] = useField(props); | ||
return ( | ||
<Box> | ||
<FormLabel htmlFor={props.id || props.name}>{label}</FormLabel> | ||
<Select {...field} {...props} /> | ||
{meta.touched && meta.error ? ( | ||
<Alert className="error" status={"error"} mt={2}> | ||
<AlertIcon/> | ||
{meta.error} | ||
</Alert> | ||
) : null} | ||
</Box> | ||
); | ||
}; | ||
|
||
const SignupCustomerForm = ({onSuccess}) => { | ||
return ( | ||
<> | ||
<Formik | ||
initialValues={{ | ||
name: '', | ||
email: '', | ||
age: 0, | ||
password: '', | ||
gender: '', | ||
}} | ||
validationSchema={Yup.object({ | ||
name: Yup.string() | ||
.max(15, 'Must be 15 characters or less') | ||
.required('Required'), | ||
email: Yup.string() | ||
.email('Invalid email address') | ||
.required('Required'), | ||
age: Yup.number() | ||
.min(16, 'Must be at least 16 years of age') | ||
.max(100, 'Must at less than 100 years of age') | ||
.required('Required'), | ||
password: Yup.string() | ||
.min(4, 'Must be 4 characters or more') | ||
.max(15, 'Must be 15 characters or less') | ||
.required('Required'), | ||
gender: Yup.string() | ||
.oneOf( | ||
['MALE', 'FEMALE'], | ||
'Invalid Gender Type' | ||
) | ||
.required('Required'), | ||
})} | ||
onSubmit={(customer, {setSubmitting}) => { | ||
setSubmitting(true) | ||
signup(customer).then(res => { | ||
console.log(res); | ||
successNotification( | ||
"Customer saved", | ||
`${customer.name} was successfully saved` | ||
) | ||
onSuccess(res.headers["authorization"]); | ||
}).catch(err => { | ||
console.log(err) | ||
errorNotification( | ||
err.code, | ||
err.response.data.message | ||
) | ||
}).finally(() => { | ||
setSubmitting(false) | ||
}) | ||
}} | ||
> | ||
{({isValid, isSubmitting}) => ( | ||
<Form> | ||
<Stack spacing={"24px"}> | ||
<MyTextInput | ||
label="Name" | ||
name="name" | ||
type="text" | ||
placeholder="Jane" | ||
/> | ||
|
||
<MyTextInput | ||
label="Email Address" | ||
name="email" | ||
type="email" | ||
placeholder="[email protected]" | ||
/> | ||
|
||
<MyTextInput | ||
label="Age" | ||
name="age" | ||
type="number" | ||
placeholder="20" | ||
/> | ||
|
||
<MyTextInput | ||
label="Password" | ||
name="password" | ||
type="password" | ||
placeholder="******" | ||
/> | ||
|
||
<MySelect label="Gender" name="gender"> | ||
<option value="">Select gender type</option> | ||
<option value="MALE">Male</option> | ||
<option value="FEMALE">Female</option> | ||
</MySelect> | ||
|
||
<Button disabled={!isValid || isSubmitting} type="submit" | ||
bgGradient={{sm: 'linear(to-r, blue.600, purple.600)'}} color={"white"} | ||
_hover={false}> | ||
Submit | ||
</Button> | ||
</Stack> | ||
</Form> | ||
)} | ||
</Formik> | ||
</> | ||
); | ||
}; | ||
|
||
export default SignupCustomerForm; |
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