-
Notifications
You must be signed in to change notification settings - Fork 828
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(flat-pages): refactor personal page layout & add email/phone…
… binding (#2004)
- Loading branch information
Showing
20 changed files
with
899 additions
and
230 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
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
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
134 changes: 134 additions & 0 deletions
134
packages/flat-pages/src/UserSettingPage/GeneralSettingPage/UpdateEmailModel.tsx
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,134 @@ | ||
import { Button, Form, Modal, message } from "antd"; | ||
import React, { useCallback, useState } from "react"; | ||
import { | ||
LoginAccount, | ||
LoginSendCode, | ||
PasswordLoginType, | ||
codeValidator, | ||
emailValidator, | ||
errorTips, | ||
} from "flat-components"; | ||
import { BindingEmailPayload, bindingEmail, bindingEmailSendCode } from "@netless/flat-server-api"; | ||
import { useLanguage, useTranslate } from "@netless/flat-i18n"; | ||
|
||
import { useSafePromise } from "../../utils/hooks/lifecycle"; | ||
|
||
export interface UpdateEmailModelProps { | ||
visible: boolean; | ||
title: string; | ||
onCancel: () => void; | ||
onConfirm: () => void; | ||
onRefresh: () => void; | ||
} | ||
|
||
export const UpdateEmailModel: React.FC<UpdateEmailModelProps> = ({ | ||
visible, | ||
title, | ||
onCancel, | ||
onConfirm, | ||
onRefresh, | ||
}) => { | ||
const language = useLanguage(); | ||
const t = useTranslate(); | ||
const sp = useSafePromise(); | ||
const emailLanguage = language.startsWith("zh") ? "zh" : "en"; | ||
|
||
const [form] = Form.useForm<BindingEmailPayload>(); | ||
const [isFormValidated, setIsFormValidated] = useState(false); | ||
const [isAccountValidated, setIsAccountValidated] = useState(false); | ||
|
||
const type = PasswordLoginType.Email; | ||
|
||
const defaultValues = { | ||
email: "", | ||
code: "", | ||
}; | ||
|
||
const [loading, setLoading] = useState(false); | ||
|
||
const clearAll = useCallback((): void => { | ||
onRefresh(); | ||
form.resetFields(); | ||
}, [form, onRefresh]); | ||
|
||
const onOk = useCallback(async () => { | ||
const { email, code } = form.getFieldsValue(); | ||
|
||
try { | ||
setLoading(true); | ||
await sp(bindingEmail(email, Number(code))); | ||
|
||
message.success(t("email-set-success")); | ||
setLoading(false); | ||
|
||
setIsFormValidated(false); | ||
clearAll(); | ||
onConfirm(); | ||
} catch (e) { | ||
setLoading(false); | ||
console.error(e); | ||
errorTips(e); | ||
} | ||
}, [clearAll, form, onConfirm, sp, t]); | ||
|
||
const formValidateStatus = useCallback(() => { | ||
setIsFormValidated( | ||
form.getFieldsError().every(field => field.errors.length <= 0) && | ||
Object.values(form.getFieldsValue()).every(v => !!v), | ||
); | ||
|
||
if (form.getFieldValue("email") && !form.getFieldError("email").length) { | ||
setIsAccountValidated(true); | ||
} else { | ||
setIsAccountValidated(false); | ||
} | ||
}, [form]); | ||
|
||
const sendVerificationCode = async (): Promise<any> => { | ||
const { email } = form.getFieldsValue(); | ||
return bindingEmailSendCode(email, emailLanguage); | ||
}; | ||
|
||
return ( | ||
<Modal | ||
centered | ||
forceRender | ||
footer={[ | ||
<Button key="exit-cancel" onClick={onCancel}> | ||
{t("cancel")} | ||
</Button>, | ||
<Button | ||
key="exit-confirm" | ||
disabled={!isFormValidated} | ||
loading={loading} | ||
onClick={onOk} | ||
> | ||
{t("confirm")} | ||
</Button>, | ||
]} | ||
open={visible} | ||
title={title} | ||
width={352} | ||
onCancel={onCancel} | ||
> | ||
<Form | ||
form={form} | ||
initialValues={defaultValues} | ||
name="rebindEmail" | ||
onFieldsChange={formValidateStatus} | ||
> | ||
<Form.Item name="email" rules={[emailValidator]}> | ||
<LoginAccount onlyEmail={true} placeholder={t("enter-email")} /> | ||
</Form.Item> | ||
|
||
<Form.Item name="code" rules={[codeValidator]}> | ||
<LoginSendCode | ||
isAccountValidated={isAccountValidated} | ||
sendVerificationCode={sendVerificationCode} | ||
type={type} | ||
/> | ||
</Form.Item> | ||
</Form> | ||
</Modal> | ||
); | ||
}; |
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
Oops, something went wrong.