2022-01-28 12:34:20 +00:00
import { TextField , Typography } from "@material-ui/core" ;
2019-09-02 15:56:59 +00:00
import Form from "@saleor/components/Form" ;
import FormSpacer from "@saleor/components/FormSpacer" ;
2022-01-28 12:34:20 +00:00
import { Button } from "@saleor/macaw-ui" ;
2021-12-17 11:10:54 +00:00
import { SetPasswordData } from "@saleor/sdk" ;
2020-04-07 13:29:31 +00:00
import getAccountErrorMessage from "@saleor/utils/errors/account" ;
2020-05-14 09:30:32 +00:00
import React from "react" ;
import { FormattedMessage , useIntl } from "react-intl" ;
2019-09-02 15:56:59 +00:00
2022-01-28 12:34:20 +00:00
import useStyles from "../styles" ;
2019-09-02 15:56:59 +00:00
export interface NewPasswordPageFormData {
password : string ;
confirmPassword : string ;
}
export interface NewPasswordPageProps {
2019-09-02 19:23:37 +00:00
disabled : boolean ;
2021-12-17 11:10:54 +00:00
errors : SetPasswordData [ "errors" ] ;
2019-09-02 15:56:59 +00:00
onSubmit : ( data : NewPasswordPageFormData ) = > void ;
}
const initialForm : NewPasswordPageFormData = {
confirmPassword : "" ,
password : ""
} ;
const NewPasswordPage : React.FC < NewPasswordPageProps > = props = > {
2020-04-07 13:29:31 +00:00
const { disabled , errors , onSubmit } = props ;
2019-09-02 15:56:59 +00:00
const classes = useStyles ( props ) ;
const intl = useIntl ( ) ;
2020-04-07 13:29:31 +00:00
const error = getAccountErrorMessage (
errors . find ( err = > err . field === "password" ) ,
intl
) ;
2019-09-02 15:56:59 +00:00
return (
< Form initial = { initialForm } onSubmit = { onSubmit } >
{ ( { change : handleChange , data , submit : handleSubmit } ) = > {
const passwordError =
data . password !== data . confirmPassword && data . password . length > 0 ;
return (
2019-09-03 13:42:15 +00:00
< >
2022-01-28 12:34:20 +00:00
< Typography variant = "h3" className = { classes . header } >
< FormattedMessage
defaultMessage = "Set up new password"
description = "page title"
/ >
< / Typography >
{ ! ! error && < div className = { classes . panel } > { error } < / div > }
< Typography variant = "caption" color = "textSecondary" >
< FormattedMessage defaultMessage = "Please set up a new password for your account. Repeat your new password to make sure you will be able to remember it." / >
2019-09-02 15:56:59 +00:00
< / Typography >
< FormSpacer / >
< TextField
autoFocus
fullWidth
autoComplete = "none"
2019-09-02 19:23:37 +00:00
disabled = { disabled }
2019-09-02 15:56:59 +00:00
label = { intl . formatMessage ( {
defaultMessage : "New Password"
} ) }
name = "password"
onChange = { handleChange }
type = "password"
value = { data . password }
inputProps = { {
2020-07-02 10:59:09 +00:00
"data-test" : "password"
2019-09-02 15:56:59 +00:00
} }
/ >
< FormSpacer / >
< TextField
fullWidth
error = { passwordError }
autoComplete = "none"
2019-09-02 19:23:37 +00:00
disabled = { disabled }
2019-09-02 15:56:59 +00:00
label = { intl . formatMessage ( {
defaultMessage : "Confirm Password"
} ) }
name = "confirmPassword"
onChange = { handleChange }
type = "password"
value = { data . confirmPassword }
helperText = {
passwordError &&
intl . formatMessage ( {
defaultMessage : "Passwords do not match"
} )
}
inputProps = { {
2020-07-02 10:59:09 +00:00
"data-test" : "confirm-password"
2019-09-02 15:56:59 +00:00
} }
/ >
< FormSpacer / >
< Button
2021-06-07 11:35:53 +00:00
data - test = "button-bar-confirm"
2019-09-02 15:56:59 +00:00
className = { classes . submit }
2019-09-02 19:23:37 +00:00
disabled = { ( passwordError && data . password . length > 0 ) || disabled }
2022-01-28 12:34:20 +00:00
variant = "primary"
2019-09-02 15:56:59 +00:00
onClick = { handleSubmit }
type = "submit"
>
< FormattedMessage
defaultMessage = "Set new password"
description = "button"
/ >
< / Button >
2019-09-03 13:42:15 +00:00
< / >
2019-09-02 15:56:59 +00:00
) ;
} }
< / Form >
) ;
} ;
NewPasswordPage . displayName = "NewPasswordPage" ;
export default NewPasswordPage ;