Skip to content

refactor(flat-stores): add countryCode to history & keep first 10 accounts #1994

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,17 @@ Overview.args = {
{
key: "12345678901",
password: "password123",
countryCode: "+86",
},
{
key: "12345678911",
password: "password124",
countryCode: "+65",
},
{
key: "example@mail.com",
password: "password125",
countryCode: null,
},
],
// This means we choose the 'both' account input
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export interface LoginAccountProps {

// history props
password?: string;
accountHistory?: Array<{ key: string; password: string }> | [];
accountHistory?: Array<{ key: string; password: string; countryCode?: string | null }> | [];
onHistoryChange?: (options: any) => void;
}

Expand Down Expand Up @@ -99,7 +99,11 @@ export const LoginAccount: React.FC<LoginAccountProps> = ({
>
{accountHistory.map(account => {
return (
<Select.Option key={account.password} value={account.key}>
<Select.Option
key={account.password}
title={account.countryCode}
value={account.key}
>
{account.key}
</Select.Option>
);
Expand All @@ -115,6 +119,7 @@ export const LoginAccount: React.FC<LoginAccountProps> = ({
<Select
bordered={false}
defaultValue={countryCode}
value={countryCode}
onChange={handleCountryCode}
>
{COUNTRY_CODES.map(code => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ Overview.args = {
{
key: "12345678901",
password: "password123",
countryCode: "+86",
},
{
key: "12345678911",
password: "password124",
countryCode: "+65",
},
{
key: "example@mail.com",
password: "password125",
countryCode: null,
},
],
login: (type: PasswordLoginType, { key }, password: string) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export enum PasswordLoginPageType {
export type LoginKeyType = {
key: string;
originKey: string;
countryCode: string | null;
};

interface LoginFormValues {
Expand All @@ -40,7 +41,7 @@ export interface LoginWithPasswordProps {
buttons?: LoginButtonProviderType[];
privacyURL?: LoginAgreementProps["privacyURL"];
serviceURL?: LoginAgreementProps["serviceURL"];
accountHistory: Array<{ key: string; password: string }> | [];
accountHistory: Array<{ key: string; password: string; countryCode?: string | null }>;
login: (type: PasswordLoginType, key: LoginKeyType, password: string) => Promise<boolean>;
register: () => void;
onClickButton: LoginButtonsProps["onClick"];
Expand Down Expand Up @@ -88,7 +89,10 @@ export const LoginWithPassword: React.FC<LoginWithPasswordProps> = ({
const [agreed, setAgreed] = useState(false);
const [clickedLogin, setClickedLogin] = useState(false);
const [clickedReset, setClickedReset] = useState(false);
const [countryCode, setCountryCode] = useState(defaultCountryCode);
const firstAccount = accountHistory.length > 0 ? accountHistory[0] : null;
const [countryCode, setCountryCode] = useState(
(firstAccount && firstAccount.countryCode) || defaultCountryCode,
);
const [type, setType] = useState(PasswordLoginType.Phone);

const phone = type === PasswordLoginType.Phone;
Expand All @@ -99,8 +103,8 @@ export const LoginWithPassword: React.FC<LoginWithPasswordProps> = ({
const [isAccountValidated, setIsAccountValidated] = useState(false);

const defaultValues = {
key: accountHistory.length > 0 ? accountHistory[0]?.key : "",
password: accountHistory.length > 0 ? accountHistory[0]?.password : "",
key: firstAccount?.key || "",
password: firstAccount?.password || "",
code: undefined,
};
const [password, setPassword] = useState(defaultValues.password);
Expand All @@ -123,6 +127,7 @@ export const LoginWithPassword: React.FC<LoginWithPasswordProps> = ({
{
key: phone ? countryCode + keyValue : keyValue,
originKey: keyValue,
countryCode: phone ? countryCode : null,
},
password,
),
Expand All @@ -149,6 +154,7 @@ export const LoginWithPassword: React.FC<LoginWithPasswordProps> = ({
{
key: phone ? countryCode + keyValue : keyValue,
originKey: keyValue,
countryCode: phone ? countryCode : null,
},
code,
password,
Expand Down Expand Up @@ -230,6 +236,10 @@ export const LoginWithPassword: React.FC<LoginWithPasswordProps> = ({
password={password}
placeholder={t("enter-email-or-phone")}
onHistoryChange={options => {
if (options.title) {
setCountryCode(options.title);
}

form.setFieldsValue({
key: options.value,
password: options.key,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export const RegisterModal: React.FC<RegisterProps> = ({
{
key: phone ? countryCode + keyValue : keyValue,
originKey: keyValue,
countryCode: phone ? countryCode : null,
},
code,
password,
Expand Down
28 changes: 22 additions & 6 deletions packages/flat-pages/src/LoginPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const LoginPage = observer(function LoginPage() {
{...loginProps}
register={(
type: PasswordLoginType,
{ key, originKey }: LoginKeyType,
{ key, originKey, countryCode }: LoginKeyType,
code,
password: string,
) =>
Expand All @@ -93,7 +93,11 @@ export const LoginPage = observer(function LoginPage() {
})
: registerPhone(key, Number(code), password).then(() => {
sp(loginPhoneWithPwd(key, password)).then(authInfo =>
onLoginResult(authInfo, { key: originKey, password }),
onLoginResult(authInfo, {
key: originKey,
password,
countryCode,
}),
);
}),
)
Expand Down Expand Up @@ -137,22 +141,30 @@ export const LoginPage = observer(function LoginPage() {
<LoginWithPassword
{...loginProps}
accountHistory={globalStore.accountHistory}
login={async (type, { key, originKey }: LoginKeyType, password) =>
login={async (
type,
{ key, originKey, countryCode }: LoginKeyType,
password,
) =>
wrap(
type === PasswordLoginType.Email
? loginEmailWithPwd(key, password).then(authInfo =>
onLoginResult(authInfo, { key: originKey, password }),
)
: loginPhoneWithPwd(key, password).then(authInfo =>
onLoginResult(authInfo, { key: originKey, password }),
onLoginResult(authInfo, {
key: originKey,
password,
countryCode,
}),
),
)
}
loginWithVerificationCode={() => setCurrentState("SWITCH_TO_CODE")}
register={() => setCurrentState("SWITCH_TO_REGISTER")}
resetPassword={(
type: PasswordLoginType,
{ key, originKey }: LoginKeyType,
{ key, originKey, countryCode }: LoginKeyType,
code: string,
password: string,
) =>
Expand All @@ -165,7 +177,11 @@ export const LoginPage = observer(function LoginPage() {
})
: resetPwdWithPhone(key, Number(code), password).then(() => {
sp(loginPhoneWithPwd(key, password)).then(authInfo =>
onLoginResult(authInfo, { key: originKey, password }),
onLoginResult(authInfo, {
key: originKey,
password,
countryCode,
}),
);
}),
)
Expand Down
40 changes: 25 additions & 15 deletions packages/flat-stores/src/global-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export type ServerRegionConfig = ServerRegionConfigResult;
export type Account = {
key: string;
password: string;

// if `key` type is phone, countryCode is string
// else countryCode is null
countryCode?: string | null;
};

/**
Expand All @@ -34,7 +38,7 @@ export class GlobalStore {

// login with password
public currentAccount: Account | null = null;
public accountHistory: Account[] | [] = [];
public accountHistory: Account[] = [];

public whiteboardRoomUUID: string | null = null;
public whiteboardRoomToken: string | null = null;
Expand Down Expand Up @@ -219,22 +223,28 @@ export class GlobalStore {
this.currentAccount = null;
};

public updateAccountHistory = ({ key, password }: Account): void => {
public updateAccountHistory = ({ key, password, countryCode }: Account): void => {
const originAccount = this.accountHistory.find(o => o.key === key);
let originCode: string | null = countryCode || null;

// update account password will pass this condition
if (!originCode && originAccount && originAccount.countryCode) {
originCode = originAccount.countryCode;
}

// update current account
this.currentAccount = { key, password };

const hash: Record<string, boolean> = {};
this.accountHistory = [{ key, password }, ...this.accountHistory].reduce(
(history: Account[], next: Account) => {
if (!hash[next.key]) {
hash[next.key] = true;
history.push(next);
this.currentAccount = { key, password, countryCode: originCode };

const hash = new Set();
this.accountHistory = [{ key, password, countryCode: originCode }, ...this.accountHistory]
.filter((next: Account) => {
if (!hash.has(next.key)) {
hash.add(next.key);
return true;
}

return history;
},
[],
);
return false;
})
.slice(0, 10); // keep the first ten accounts
};

public updateServerRegionConfig = (config: ServerRegionConfig | null): void => {
Expand Down