Reapply "fix(user): refresh profile on sidebar open"
This reverts commit f4d0f76466.
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
|
import { useEffect } from "react";
|
||||||
import { LogOut } from "lucide-react";
|
import { LogOut } from "lucide-react";
|
||||||
import { signOut } from "next-auth/react";
|
import { signOut } from "next-auth/react";
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
@@ -29,6 +30,18 @@ export function SidebarScreen() {
|
|||||||
const auth = useAuthState();
|
const auth = useAuthState();
|
||||||
const authDispatch = useAuthDispatch();
|
const authDispatch = useAuthDispatch();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!auth.hasInitialized || auth.isLoading) return;
|
||||||
|
if (auth.loginStatus === "notLoggedIn") return;
|
||||||
|
|
||||||
|
userDispatch({ type: "UserFetch" });
|
||||||
|
}, [
|
||||||
|
auth.hasInitialized,
|
||||||
|
auth.isLoading,
|
||||||
|
auth.loginStatus,
|
||||||
|
userDispatch,
|
||||||
|
]);
|
||||||
|
|
||||||
const handleLogoutClick = () => {
|
const handleLogoutClick = () => {
|
||||||
userDispatch({ type: "UserClearLocal" });
|
userDispatch({ type: "UserClearLocal" });
|
||||||
authDispatch({ type: "AuthLogoutSubmitted" });
|
authDispatch({ type: "AuthLogoutSubmitted" });
|
||||||
|
|||||||
@@ -103,11 +103,10 @@ describe("userMachine", () => {
|
|||||||
actor.stop();
|
actor.stop();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("updates user profile fields synchronously", () => {
|
it("updates user profile synchronously", () => {
|
||||||
const actor = createActor(createTestUserMachine()).start();
|
const actor = createActor(createTestUserMachine()).start();
|
||||||
|
|
||||||
actor.send({ type: "UserUpdate", user: makeUser({ username: "Before" }) });
|
actor.send({ type: "UserUpdate", user: makeUser({ username: "After" }) });
|
||||||
actor.send({ type: "UserUpdateUsername", username: "After" });
|
|
||||||
|
|
||||||
expect(actor.getSnapshot().context.currentUser?.username).toBe("After");
|
expect(actor.getSnapshot().context.currentUser?.username).toBe("After");
|
||||||
|
|
||||||
|
|||||||
@@ -7,8 +7,5 @@ export type UserEvent =
|
|||||||
| { type: "UserInit" }
|
| { type: "UserInit" }
|
||||||
| { type: "UserFetch" }
|
| { type: "UserFetch" }
|
||||||
| { type: "UserUpdate"; user: UserView }
|
| { type: "UserUpdate"; user: UserView }
|
||||||
| { type: "UserUpdateUsername"; username: string }
|
|
||||||
| { type: "UserClearLocal" }
|
| { type: "UserClearLocal" }
|
||||||
| { type: "UserLogout" }
|
| { type: "UserLogout" };
|
||||||
| { type: "UserDeleteChatHistory" }
|
|
||||||
| { type: "UserDeleteAccount" };
|
|
||||||
|
|||||||
@@ -40,13 +40,6 @@ export const userMachine = setup({
|
|||||||
};
|
};
|
||||||
}),
|
}),
|
||||||
|
|
||||||
updateUsername: assign(({ context, event }) => {
|
|
||||||
if (event.type !== "UserUpdateUsername" || !context.currentUser) return {};
|
|
||||||
return {
|
|
||||||
currentUser: { ...context.currentUser, username: event.username },
|
|
||||||
};
|
|
||||||
}),
|
|
||||||
|
|
||||||
clearUser: assign(() => ({
|
clearUser: assign(() => ({
|
||||||
currentUser: null,
|
currentUser: null,
|
||||||
avatarUrl: null,
|
avatarUrl: null,
|
||||||
@@ -66,15 +59,10 @@ export const userMachine = setup({
|
|||||||
UserUpdate: {
|
UserUpdate: {
|
||||||
actions: "updateUser",
|
actions: "updateUser",
|
||||||
},
|
},
|
||||||
UserUpdateUsername: {
|
|
||||||
actions: "updateUsername",
|
|
||||||
},
|
|
||||||
UserClearLocal: {
|
UserClearLocal: {
|
||||||
actions: "clearUser",
|
actions: "clearUser",
|
||||||
},
|
},
|
||||||
UserLogout: "loggingOut",
|
UserLogout: "loggingOut",
|
||||||
UserDeleteChatHistory: {},
|
|
||||||
UserDeleteAccount: {},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user