feat: register flow

:%s
This commit is contained in:
lytrankieio123
2021-09-29 14:40:06 +07:00
parent ba6517ef39
commit 275f10c5e7
8 changed files with 37 additions and 27 deletions

View File

@@ -7,15 +7,7 @@ interface Props {
onRemove?: (id: number) => void onRemove?: (id: number) => void
} }
const MessageCommon = memo(({ messages, onRemove }: Props) => { const MessageCommon = memo(({ messages, onRemove }: Props) => {
useEffect(() => {
console.log("this fun change; onRemove")
}, [onRemove])
const handleRemove = (id: number) => {
onRemove && onRemove(id)
}
return ( return (
<div className={s.messageCommon}> <div className={s.messageCommon}>
{messages.reverse().map((item) => ( {messages.reverse().map((item) => (
@@ -23,7 +15,7 @@ const MessageCommon = memo(({ messages, onRemove }: Props) => {
key={item.id} key={item.id}
id={item.id} id={item.id}
content={item.content} content={item.content}
onRemove={handleRemove} onRemove={onRemove}
/> />
))} ))}
</div> </div>

View File

@@ -8,6 +8,8 @@
border-radius: 0.8rem; border-radius: 0.8rem;
transition: all .5s; transition: all .5s;
animation: showMessage .5s; animation: showMessage .5s;
width: max-content;
margin: 0 1.6rem;
&.hide { &.hide {
display: none; display: none;

View File

@@ -2,10 +2,12 @@ import classNames from 'classnames'
import { Form, Formik } from 'formik' import { Form, Formik } from 'formik'
import React, { useEffect, useRef } from 'react' import React, { useEffect, useRef } from 'react'
import { import {
ButtonCommon, ButtonCommon,
InputFiledInForm, InputFiledInForm,
InputPasswordFiledInForm InputPasswordFiledInForm,
} from 'src/components/common' } from 'src/components/common'
import { useMessage } from 'src/components/contexts'
import { LANGUAGE } from 'src/utils/language.utils'
import { CustomInputCommon } from 'src/utils/type.utils' import { CustomInputCommon } from 'src/utils/type.utils'
import * as Yup from 'yup' import * as Yup from 'yup'
import { useSignup } from '../../../../hooks' import { useSignup } from '../../../../hooks'
@@ -32,6 +34,7 @@ const DisplayingErrorMessagesSchema = Yup.object().shape({
const FormRegister = ({ onSwitch, isHide }: Props) => { const FormRegister = ({ onSwitch, isHide }: Props) => {
const emailRef = useRef<CustomInputCommon>(null) const emailRef = useRef<CustomInputCommon>(null)
const { loading, signup, error } = useSignup() const { loading, signup, error } = useSignup()
const { showMessageSuccess, showMessageError } = useMessage()
useEffect(() => { useEffect(() => {
if (!isHide) { if (!isHide) {
@@ -40,9 +43,15 @@ const FormRegister = ({ onSwitch, isHide }: Props) => {
}, [isHide]) }, [isHide])
const onSignup = (values: { email: string; password: string }) => { const onSignup = (values: { email: string; password: string }) => {
signup({ email: values.email, password: values.password }) signup({ email: values.email, password: values.password }, onSignupCallBack)
// TODO: flow }
alert('User created. Please verify your email')
const onSignupCallBack = (isSuccess: boolean, message?: string) => {
if (isSuccess) {
showMessageSuccess("Create account successfully. Please verify your email to login.")
} else {
showMessageError(message || LANGUAGE.MESSAGE.ERROR)
}
} }
useEffect(() => { useEffect(() => {

View File

@@ -17,7 +17,7 @@ export function MessageProvider({ children }: Props) {
) => { ) => {
const item: MessageItemProps = { const item: MessageItemProps = {
id: currentId + 1, id: currentId + 1,
content: currentId + 1 + content, content,
type, type,
timeout, timeout,
} }

View File

@@ -42,6 +42,7 @@ const useLogin = () => {
throw CommonError.create(data.login.message, data.login.errorCode) throw CommonError.create(data.login.message, data.login.errorCode)
} }
const authToken = headers.get('vendure-auth-token') const authToken = headers.get('vendure-auth-token')
console.log("auth token: ", authToken)
if (authToken != null) { if (authToken != null) {
localStorage.setItem('token', authToken) localStorage.setItem('token', authToken)
return mutate() return mutate()

View File

@@ -32,7 +32,10 @@ const useSignup = () => {
const [error, setError] = useState<Error | null>(null) const [error, setError] = useState<Error | null>(null)
const { mutate } = useActiveCustomer() const { mutate } = useActiveCustomer()
const signup = ({ firstName, lastName, email, password }: SignupInput) => { const signup = (
{ firstName, lastName, email, password }: SignupInput,
fCallBack: (isSuccess: boolean, message?: string) => void
) => {
setError(null) setError(null)
setLoading(true) setLoading(true)
fetcher<SignupMutation>({ fetcher<SignupMutation>({
@@ -53,11 +56,15 @@ const useSignup = () => {
data.registerCustomerAccount.errorCode data.registerCustomerAccount.errorCode
) )
} }
console.log(data)
mutate() mutate()
fCallBack(true)
return data return data
}) })
.catch(setError) .catch((error) => {
setError(error)
fCallBack(false, error.message)
})
.finally(() => setLoading(false)) .finally(() => setLoading(false))
} }

View File

@@ -25,7 +25,7 @@ const useVerifyCustomer = () => {
query: VERIFY_CUSTOMER_ACCOUNT, query: VERIFY_CUSTOMER_ACCOUNT,
variables: options, variables: options,
}) })
.then(({ data, headers }) => { .then(({ data }) => {
if (data.verifyCustomerAccount.__typename !== 'CurrentUser') { if (data.verifyCustomerAccount.__typename !== 'CurrentUser') {
throw CommonError.create( throw CommonError.create(
data.verifyCustomerAccount.message, data.verifyCustomerAccount.message,
@@ -33,12 +33,8 @@ const useVerifyCustomer = () => {
) )
} }
fCallBack && fCallBack(true) fCallBack && fCallBack(true)
mutate()
const authToken = headers.get('vendure-auth-token') return data
if (authToken != null) {
localStorage.setItem('token', authToken)
return mutate()
}
}) })
.catch((err) => { .catch((err) => {
setError(err) setError(err)

View File

@@ -9,5 +9,8 @@ export const LANGUAGE = {
}, },
PLACE_HOLDER: { PLACE_HOLDER: {
SEARCH: 'Search', SEARCH: 'Search',
},
MESSAGE: {
ERROR: 'Something went wrong! Please try again!'
} }
} }