feat: user sessions state
This commit is contained in:
57
web/src/store/admin/userSessions.ts
Normal file
57
web/src/store/admin/userSessions.ts
Normal file
@ -0,0 +1,57 @@
|
||||
import {
|
||||
adminGetUserSessionsApi,
|
||||
adminRevokeUserSessionApi,
|
||||
} from "@/api/admin/sessions";
|
||||
import type { UserSession } from "@/types";
|
||||
import { create } from "zustand";
|
||||
|
||||
export const ADMIN_USER_SESSIONS_PAGE_SIZE = 10;
|
||||
|
||||
export interface IUserSessionsState {
|
||||
items: UserSession[];
|
||||
totalPages: number;
|
||||
page: number;
|
||||
|
||||
loading: boolean;
|
||||
|
||||
revokingId: string | null;
|
||||
|
||||
fetch: (page: number) => Promise<void>;
|
||||
revoke: (id: string) => Promise<void>;
|
||||
}
|
||||
|
||||
export const useUserSessions = create<IUserSessionsState>((set) => ({
|
||||
items: [],
|
||||
totalPages: 0,
|
||||
page: 1,
|
||||
loading: false,
|
||||
revokingId: null,
|
||||
|
||||
fetch: async (page) => {
|
||||
set({ loading: true, page });
|
||||
|
||||
try {
|
||||
const response = await adminGetUserSessionsApi({
|
||||
page,
|
||||
size: ADMIN_USER_SESSIONS_PAGE_SIZE,
|
||||
});
|
||||
set({ items: response.items, totalPages: response.total_pages });
|
||||
} catch (err) {
|
||||
console.log("ERR: Failed to fetch admin user sessions:", err);
|
||||
} finally {
|
||||
set({ loading: false });
|
||||
}
|
||||
},
|
||||
|
||||
revoke: async (id) => {
|
||||
set({ revokingId: id });
|
||||
|
||||
try {
|
||||
await adminRevokeUserSessionApi(id);
|
||||
} catch (err) {
|
||||
console.log("ERR: Failed to revoke user sessions:", err);
|
||||
} finally {
|
||||
set({ revokingId: null });
|
||||
}
|
||||
},
|
||||
}));
|
Reference in New Issue
Block a user