Added saves state of dashboard and reloads it now
This commit is contained in:
@@ -19,7 +19,9 @@ const useAuthStore = create((set) => ({
|
||||
firstName: res.data.first_name,
|
||||
lastName: res.data.last_name,
|
||||
email: res.data.email,
|
||||
fullName: [res.data.first_name, res.data.last_name].filter(Boolean).join(' ')
|
||||
fullName: [res.data.first_name, res.data.last_name].filter(Boolean).join(' '),
|
||||
// Catch the layout and map data on fresh login!
|
||||
preferences: res.data.preferences || {}
|
||||
}
|
||||
});
|
||||
} catch (err) {
|
||||
@@ -42,7 +44,7 @@ const useAuthStore = create((set) => ({
|
||||
checkAuth: async () => {
|
||||
try {
|
||||
// Hit the new lightweight endpoint
|
||||
const res = await api.get('session');
|
||||
const res = await api.get('session/');
|
||||
|
||||
// Rehydrate the store with the user's info!
|
||||
set({
|
||||
@@ -53,13 +55,37 @@ const useAuthStore = create((set) => ({
|
||||
firstName: res.data.first_name,
|
||||
lastName: res.data.last_name,
|
||||
email: res.data.email,
|
||||
fullName: [res.data.first_name, res.data.last_name].filter(Boolean).join(' ')
|
||||
fullName: [res.data.first_name, res.data.last_name].filter(Boolean).join(' '),
|
||||
// Catch the layout and map data on page refresh!
|
||||
preferences: res.data.preferences || {}
|
||||
}
|
||||
});
|
||||
} catch {
|
||||
// If it fails, ensure the store is completely wiped
|
||||
set({ isAuthenticated: false, isLoading: false, user: null });
|
||||
}
|
||||
},
|
||||
|
||||
// Allow the UI to save widget placement
|
||||
saveDashboardLayout: async (newLayout) => {
|
||||
try {
|
||||
// 1. Optimistically update React state for zero UI lag
|
||||
set((state) => ({
|
||||
user: {
|
||||
...state.user,
|
||||
preferences: {
|
||||
...state.user.preferences,
|
||||
dashboard_layout: newLayout
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
// 2. Quietly push the save command to Django
|
||||
await api.post('dashboard/save/', { layout: newLayout });
|
||||
|
||||
} catch (error) {
|
||||
console.error("Failed to save layout to database", error);
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user