-
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): add merge account (#1997)
refactor(flat-pages): update type refactor(flat-pages): refactor rebinding name
- Loading branch information
Showing
12 changed files
with
289 additions
and
16 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
29 changes: 29 additions & 0 deletions
29
...t-components/src/components/LoginPage/RebindingPhonePanel/RebindingPhonePanel.stories.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,29 @@ | ||
import { Meta, Story } from "@storybook/react"; | ||
import { message } from "antd"; | ||
import React from "react"; | ||
import { RebindingPhonePanelProps, RebindingPhonePanel } from "."; | ||
|
||
const storyMeta: Meta = { | ||
title: "LoginPage/RebindingPhonePanel", | ||
component: RebindingPhonePanel, | ||
}; | ||
|
||
export default storyMeta; | ||
|
||
export const Overview: Story<RebindingPhonePanelProps> = props => { | ||
return <RebindingPhonePanel {...props} />; | ||
}; | ||
|
||
Overview.args = { | ||
cancelRebindingPhone: () => { | ||
message.info("back to previous step"); | ||
}, | ||
rebindingPhone: (countryCode: string, phone: string, code: string) => { | ||
message.info("merge phone with " + countryCode + " " + phone + " " + code); | ||
return new Promise(resolve => setTimeout(() => resolve(false), 1000)); | ||
}, | ||
sendRebindingPhoneCode: (countryCode: string, phone: string) => { | ||
message.info("send verification code with " + countryCode + " " + phone); | ||
return new Promise(resolve => setTimeout(() => resolve(false), 1000)); | ||
}, | ||
}; |
9 changes: 9 additions & 0 deletions
9
packages/flat-components/src/components/LoginPage/RebindingPhonePanel/index.less
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,9 @@ | ||
.login-with-phone-rebinding { | ||
padding-top: 184px; | ||
padding-bottom: 32px; | ||
} | ||
|
||
.login-countdown { | ||
user-select: none; | ||
-webkit-user-select: none; | ||
} |
120 changes: 120 additions & 0 deletions
120
packages/flat-components/src/components/LoginPage/RebindingPhonePanel/index.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,120 @@ | ||
import "./index.less"; | ||
|
||
import { useTranslate } from "@netless/flat-i18n"; | ||
import { Button, message, Form } from "antd"; | ||
import React, { useCallback, useState } from "react"; | ||
|
||
import { useSafePromise } from "../../../utils/hooks"; | ||
import { LoginTitle } from "../LoginTitle"; | ||
import { LoginAccount, PasswordLoginType, defaultCountryCode } from "../LoginAccount"; | ||
import { LoginSendCode } from "../LoginSendCode"; | ||
import { codeValidator } from "../LoginWithCode/validators"; | ||
import { phoneValidator } from "../LoginWithPassword/validators"; | ||
|
||
export interface RebindingPhonePanelProps { | ||
cancelRebindingPhone: () => void; | ||
rebindingPhone: (countryCode: string, phone: string, code: string) => Promise<boolean>; | ||
sendRebindingPhoneCode: (countryCode: string, phone: string) => Promise<any>; | ||
} | ||
|
||
interface RebindingFormValues { | ||
phone: string; | ||
code: string; | ||
} | ||
|
||
export const RebindingPhonePanel: React.FC<RebindingPhonePanelProps> = ({ | ||
sendRebindingPhoneCode, | ||
rebindingPhone, | ||
cancelRebindingPhone, | ||
}) => { | ||
const sp = useSafePromise(); | ||
const t = useTranslate(); | ||
|
||
const [form] = Form.useForm<RebindingFormValues>(); | ||
const [isFormValidated, setIsFormValidated] = useState(false); | ||
const [isAccountValidated, setIsAccountValidated] = useState(false); | ||
const type = PasswordLoginType.Phone; | ||
|
||
const defaultValues = { | ||
phone: "", | ||
code: "", | ||
}; | ||
|
||
const [countryCode, setCountryCode] = useState(defaultCountryCode); | ||
const [clickedRebinding, setClickedRebinding] = useState(false); | ||
|
||
const handleRebindingPhone = useCallback(async () => { | ||
if (isFormValidated && rebindingPhone) { | ||
setClickedRebinding(true); | ||
const { phone, code } = form.getFieldsValue(); | ||
const success = await sp(rebindingPhone(countryCode, phone, code)); | ||
if (success) { | ||
await sp(new Promise(resolve => setTimeout(resolve, 60000))); | ||
} else { | ||
message.error(t("rebinding-phone-failed")); | ||
} | ||
setClickedRebinding(false); | ||
} | ||
}, [isFormValidated, form, sp, countryCode, rebindingPhone, t]); | ||
|
||
const sendVerificationCode = async (): Promise<any> => { | ||
const { phone } = form.getFieldsValue(); | ||
return sendRebindingPhoneCode(countryCode, phone); | ||
}; | ||
|
||
const formValidateStatus = useCallback(() => { | ||
setIsFormValidated( | ||
form.getFieldsError().every(field => field.errors.length <= 0) && | ||
Object.values(form.getFieldsValue()).every(v => !!v), | ||
); | ||
|
||
if (form.getFieldValue("phone") && !form.getFieldError("phone").length) { | ||
setIsAccountValidated(true); | ||
} else { | ||
setIsAccountValidated(false); | ||
} | ||
}, [form]); | ||
|
||
return ( | ||
<div className="login-with-phone-rebinding"> | ||
<div className="login-width-limiter"> | ||
<LoginTitle subtitle=" " title={t("rebinding-phone")} /> | ||
|
||
<Form | ||
form={form} | ||
initialValues={defaultValues} | ||
name="rebindingPhoneForm" | ||
onFieldsChange={formValidateStatus} | ||
> | ||
<Form.Item name="phone" rules={[phoneValidator]}> | ||
<LoginAccount | ||
countryCode={countryCode} | ||
handleCountryCode={code => setCountryCode(code)} | ||
placeholder={t("enter-phone")} | ||
/> | ||
</Form.Item> | ||
|
||
<Form.Item name="code" rules={[codeValidator]}> | ||
<LoginSendCode | ||
isAccountValidated={isAccountValidated} | ||
sendVerificationCode={sendVerificationCode} | ||
type={type} | ||
/> | ||
</Form.Item> | ||
</Form> | ||
<Button | ||
className="login-big-button" | ||
disabled={!isFormValidated} | ||
loading={clickedRebinding} | ||
type="primary" | ||
onClick={handleRebindingPhone} | ||
> | ||
{t("confirm")} | ||
</Button> | ||
<Button className="login-btn-back" type="link" onClick={cancelRebindingPhone}> | ||
{t("back")} | ||
</Button> | ||
</div> | ||
</div> | ||
); | ||
}; |
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
Oops, something went wrong.