# Redux Integration Guide This application uses Redux Toolkit for centralized state management across the entire platform. ## Architecture Overview The Redux store is configured in `/src/store/store.ts` and provides the following slices: ### State Slices #### 1. Auth Slice (`/src/store/slices/authSlice.ts`) Manages authentication state and user information. **State:** - `user`: Current authenticated user object - `isAuthenticated`: Boolean flag for auth status - `currentEntity`: Selected organizational entity **Actions:** - `login(user)`: Authenticates user and stores user data - `logout()`: Clears user session - `setCurrentEntity(entity)`: Switches between organizational entities #### 2. UI Slice (`/src/store/slices/uiSlice.ts`) Manages global UI state. **State:** - `currentView`: Active view/page in the application - `searchQuery`: Global search query string - `sidebarCollapsed`: Sidebar visibility state **Actions:** - `setCurrentView(view)`: Navigate to different views - `setSearchQuery(query)`: Update search query - `toggleSidebar()`: Toggle sidebar collapsed state #### 3. Timesheets Slice (`/src/store/slices/timesheetsSlice.ts`) Manages timesheet data. **State:** - `timesheets`: Array of timesheet objects - `loading`: Loading state for async operations **Actions:** - `setTimesheets(timesheets)`: Replace all timesheets - `addTimesheet(timesheet)`: Add new timesheet - `updateTimesheet(timesheet)`: Update existing timesheet - `deleteTimesheet(id)`: Remove timesheet by ID - `setLoading(boolean)`: Set loading state #### 4. Invoices Slice (`/src/store/slices/invoicesSlice.ts`) Manages invoice data. **State:** - `invoices`: Array of invoice objects - `loading`: Loading state for async operations **Actions:** - `setInvoices(invoices)`: Replace all invoices - `addInvoice(invoice)`: Add new invoice - `updateInvoice(invoice)`: Update existing invoice - `deleteInvoice(id)`: Remove invoice by ID - `setLoading(boolean)`: Set loading state #### 5. Payroll Slice (`/src/store/slices/payrollSlice.ts`) Manages payroll run data. **State:** - `payrollRuns`: Array of payroll run objects - `loading`: Loading state for async operations **Actions:** - `setPayrollRuns(runs)`: Replace all payroll runs - `addPayrollRun(run)`: Add new payroll run - `updatePayrollRun(run)`: Update existing payroll run - `setLoading(boolean)`: Set loading state #### 6. Compliance Slice (`/src/store/slices/complianceSlice.ts`) Manages compliance document data. **State:** - `documents`: Array of compliance document objects - `loading`: Loading state for async operations **Actions:** - `setComplianceDocs(docs)`: Replace all compliance documents - `addComplianceDoc(doc)`: Add new compliance document - `updateComplianceDoc(doc)`: Update existing compliance document - `deleteComplianceDoc(id)`: Remove compliance document by ID - `setLoading(boolean)`: Set loading state #### 7. Expenses Slice (`/src/store/slices/expensesSlice.ts`) Manages expense data. **State:** - `expenses`: Array of expense objects - `loading`: Loading state for async operations **Actions:** - `setExpenses(expenses)`: Replace all expenses - `addExpense(expense)`: Add new expense - `updateExpense(expense)`: Update existing expense - `deleteExpense(id)`: Remove expense by ID - `setLoading(boolean)`: Set loading state #### 8. Notifications Slice (`/src/store/slices/notificationsSlice.ts`) Manages system notifications. **State:** - `notifications`: Array of notification objects - `unreadCount`: Number of unread notifications **Actions:** - `setNotifications(notifications)`: Replace all notifications - `addNotification(notification)`: Add new notification - `markAsRead(id)`: Mark notification as read - `markAllAsRead()`: Mark all notifications as read - `deleteNotification(id)`: Remove notification by ID ## Using Redux in Components ### 1. Import the typed hooks ```typescript import { useAppSelector, useAppDispatch } from '@/store/hooks' ``` ### 2. Access state with useAppSelector ```typescript const user = useAppSelector(state => state.auth.user) const isAuthenticated = useAppSelector(state => state.auth.isAuthenticated) const timesheets = useAppSelector(state => state.timesheets.timesheets) ``` ### 3. Dispatch actions with useAppDispatch ```typescript import { login, logout } from '@/store/slices/authSlice' import { setCurrentView } from '@/store/slices/uiSlice' const dispatch = useAppDispatch() // Dispatch actions dispatch(login({ id: '1', email: 'user@example.com', name: 'User', role: 'Admin' })) dispatch(setCurrentView('dashboard')) dispatch(logout()) ``` ## Custom Hooks with Redux We've created several custom hooks that wrap Redux logic for cleaner component code: ### useAuth Hook (`/src/hooks/use-auth.ts`) ```typescript import { useAuth } from '@/hooks/use-auth' function MyComponent() { const { user, isAuthenticated, currentEntity, logout } = useAuth() return (
Welcome, {user?.name}!
}Unread: {unreadCount}
{notifications.map(n => (