feat(improvement): add pagination to endpoints and pages + sync navbar when data changes
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { createContext, useContext, useState, useEffect, type ReactNode } from 'react';
|
||||
import { api } from '../api';
|
||||
import { getUserProfile } from '../api/users';
|
||||
import { fetchWorkspaces } from '../api/workspaces';
|
||||
|
||||
interface User {
|
||||
id: string;
|
||||
@@ -30,23 +31,26 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
|
||||
|
||||
const fetchInitialData = async () => {
|
||||
try {
|
||||
const [userRes, wsRes] = await Promise.all([
|
||||
api.get('/api/users/me/'),
|
||||
api.get('/api/workspaces/')
|
||||
const [userData, wsData] = await Promise.all([
|
||||
getUserProfile(),
|
||||
fetchWorkspaces() // fetchWorkspaces({ limit: 50 })
|
||||
]);
|
||||
|
||||
setUser(userRes.data);
|
||||
setWorkspaces(wsRes.data.results || wsRes.data);
|
||||
setUser(userData);
|
||||
|
||||
const workspacesList = Array.isArray(wsData.data) ? wsData.data : (wsData?.data?.results || []);
|
||||
|
||||
setWorkspaces(workspacesList);
|
||||
|
||||
const savedWsId = localStorage.getItem('active_workspace');
|
||||
const targetWs = wsRes.data.find((w: Workspace) => w.id === savedWsId) || wsRes.data[0];
|
||||
const targetWs = workspacesList.find((w: Workspace) => w.id === savedWsId) || workspacesList[0];
|
||||
|
||||
if (targetWs) {
|
||||
setActiveWorkspace(targetWs);
|
||||
localStorage.setItem('active_workspace', targetWs.id);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
console.error("Failed to fetch initial context data:", error);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -38,12 +38,14 @@ export const WorkspaceProvider = ({ children }: { children: ReactNode }) => {
|
||||
|
||||
const loadWorkspaces = async () => {
|
||||
try {
|
||||
const data = await fetchWorkspaces()
|
||||
const response = await fetchWorkspaces()
|
||||
|
||||
const data = Array.isArray(response) ? response : (response?.results || [])
|
||||
setWorkspaces(data)
|
||||
|
||||
if (data.length > 0) {
|
||||
const storedId = localStorage.getItem("activeWorkspaceId")
|
||||
const stored = data.find((w) => w.id === storedId)
|
||||
const stored = data.find((w: Workspace) => w.id === storedId)
|
||||
if (stored) {
|
||||
setActiveWorkspaceState(stored)
|
||||
} else {
|
||||
@@ -69,7 +71,7 @@ export const WorkspaceProvider = ({ children }: { children: ReactNode }) => {
|
||||
const addWorkspace = async (name: string) => {
|
||||
try {
|
||||
setIsCreatingFirst(true)
|
||||
const newWs = await createWorkspace(name)
|
||||
const newWs = await createWorkspace({ name, description: "" })
|
||||
setWorkspaces((prev) => [...prev, newWs])
|
||||
setActiveWorkspace(newWs)
|
||||
toast.success(t.workspace?.createSuccess || "Workspace created!")
|
||||
|
||||
Reference in New Issue
Block a user