Skip to content
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

refactor(flat-stores): reset pmi status when user logout #2078

Merged
merged 1 commit into from
Nov 7, 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 @@ -129,10 +129,10 @@ export const GeneralSettingPage = observer(function GeneralSettingPage() {
onOk: async () => {
try {
await sp(deleteAccount());
globalStore.updateUserInfo(null);
globalStore.updatePmi(null);
globalStore.updatePmiRoomList([]);
globalStore.deleteCurrentAccountFromHistory();

// reset user status
globalStore.deleteAccount();

pushHistory(RouteNameType.LoginPage);
} catch (err) {
errorTips(err);
Expand Down
6 changes: 4 additions & 2 deletions packages/flat-server-api/src/pmi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export interface CreateOrGetPmiResult {
pmi: string;
}

export function createOrGetPmi(payload: CreateOrGetPmiPayload): Promise<CreateOrGetPmiResult> {
return postV2<CreateOrGetPmiPayload, CreateOrGetPmiResult>("user/pmi", payload);
export function createOrGetPmi(payload: CreateOrGetPmiPayload): Promise<string> {
return postV2<CreateOrGetPmiPayload, CreateOrGetPmiResult>("user/pmi", payload).then(
e => e.pmi,
);
}
41 changes: 22 additions & 19 deletions packages/flat-stores/src/global-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class GlobalStore {
public isTurnOffDeviceTest = false;
public userInfo: UserInfo | null = null;
public pmi: string | null = null;
public pmiRoomList: PmiRoom[] = [];
public pmiRoomList: PmiRoom[] | null = [];

// login with password
public currentAccount: Account | null = null;
Expand Down Expand Up @@ -88,11 +88,11 @@ export class GlobalStore {
public hideAvatarsRoomUUIDs: string[] | undefined = undefined;

public get pmiRoomExist(): boolean {
return this.pmiRoomList.length > 0;
return (this.pmiRoomList && this.pmiRoomList.length > 0) || false;
}

public get pmiRoomUUID(): string {
return this.pmiRoomList[0]?.roomUUID;
return (this.pmiRoomList && this.pmiRoomList[0]?.roomUUID) || "";
}

public get userUUID(): string | undefined {
Expand Down Expand Up @@ -132,15 +132,11 @@ export class GlobalStore {
});
}

public updatePmi = async (pmi?: string | null): Promise<void> => {
if (pmi) {
public updatePmi = async (): Promise<void> => {
const pmi = (await createOrGetPmi({ create: true })) || null;
runInAction(() => {
this.pmi = pmi;
} else {
const pmi = (await createOrGetPmi({ create: true }))?.pmi || null;
runInAction(() => {
this.pmi = pmi;
});
}
});
};

public updatePmiRoomListByRoomUUID = (roomUUID: string): void => {
Expand All @@ -149,15 +145,11 @@ export class GlobalStore {
}
};

public updatePmiRoomList = async (pmiRoomList?: PmiRoom[]): Promise<void> => {
if (pmiRoomList) {
public updatePmiRoomList = async (): Promise<void> => {
const pmiRoomList = (await listPmi()) || [];
runInAction(() => {
this.pmiRoomList = pmiRoomList;
} else {
const pmiRoomList = (await listPmi()) || [];
runInAction(() => {
this.pmiRoomList = pmiRoomList;
});
}
});
};

public updateUserInfo = (userInfo: UserInfo | null): void => {
Expand Down Expand Up @@ -243,12 +235,23 @@ export class GlobalStore {
}
};

public deleteAccount = (): void => {
globalStore.updateUserInfo(null);
globalStore.deleteCurrentAccountFromHistory();

this.pmi = null;
this.pmiRoomList = null;
this.roomHistory = [];
};

public logout = (): void => {
this.userInfo = null;
this.currentAccount = null;
this.lastLoginCheck = null;
this.onStageRoomUUIDs = [];
this.roomHistory = [];
this.pmi = null;
this.pmiRoomList = null;
document.cookie = "flatJWTToken=; SameSite=Lax; domain=whiteboard.agora.io; max-age=0";
};

Expand Down