fix(account): persist profile actions through backend

This commit is contained in:
zetaloop
2026-04-25 14:13:55 +08:00
parent d7cc6b0141
commit e3572bf86b
2 changed files with 108 additions and 30 deletions
+44 -6
View File
@@ -13,6 +13,9 @@ import {
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"
import { Sheet, SheetContent, SheetHeader, SheetTitle, SheetTrigger } from "@/components/ui/sheet"
import { getCurrentUserForLogin, logout as logoutRequest, switchCurrentRole } from "@/lib/api"
import { toApiError } from "@/lib/errors"
import { notifyInfo } from "@/lib/toast"
import type { UserRole } from "@/lib/types"
import { cn } from "@/lib/utils"
import { useAuthStore } from "@/store/auth"
@@ -46,7 +49,14 @@ export function Header() {
const [mobileOpen, setMobileOpen] = useState(false)
const pathname = usePathname()
const router = useRouter()
const { isAuthenticated, currentRole, verifiedRoles, switchRole, logout, user } = useAuthStore()
const {
isAuthenticated,
currentRole,
verifiedRoles,
login,
logout: clearAuth,
user,
} = useAuthStore()
const ownerShop = useShopStore((state) =>
user ? state.shops.find((shop) => shop.owner.id === user.id) : undefined,
)
@@ -73,11 +83,39 @@ export function Header() {
)
const handleRoleSwitch = (role: UserRole) => {
switchRole(role)
if (pathname.startsWith("/dashboard") && !canAccessDashboard(role, pathname)) {
router.push(role === "consumer" ? "/" : "/dashboard")
if (role === currentRole) {
setMobileOpen(false)
return
}
setMobileOpen(false)
void (async () => {
try {
await switchCurrentRole(role)
const updated = await getCurrentUserForLogin()
login(updated, updated.verifiedRoles ?? [updated.role])
if (pathname.startsWith("/dashboard") && !canAccessDashboard(role, pathname)) {
router.push(role === "consumer" ? "/" : "/dashboard")
}
} catch (error) {
notifyInfo(toApiError(error).msg)
} finally {
setMobileOpen(false)
}
})()
}
const handleLogout = () => {
void (async () => {
try {
await logoutRequest()
} catch (error) {
notifyInfo(toApiError(error).msg)
} finally {
clearAuth()
setMobileOpen(false)
router.push("/")
}
})()
}
const unreadCount = useNotificationStore(
@@ -226,7 +264,7 @@ export function Header() {
</DropdownMenuItem>
</DropdownMenuGroup>
<DropdownMenuSeparator />
<DropdownMenuItem onClick={logout}>
<DropdownMenuItem onClick={handleLogout}>
<LogOut className="mr-2 h-4 w-4" />
退
</DropdownMenuItem>