-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add public-samples/refunds-wyx7mz/src/web/src/store/slices/ui.slice.ts
- Loading branch information
1 parent
32f3f73
commit 097e76f
Showing
1 changed file
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import { createSlice, PayloadAction } from '@reduxjs/toolkit'; // @reduxjs/toolkit ^1.9.5 | ||
import { ApiStatus } from '../../types/common.types'; | ||
|
||
/** | ||
* Interface defining the shape of the UI state managed by this slice | ||
*/ | ||
interface UIState { | ||
isLoading: boolean; | ||
sidebarOpen: boolean; | ||
modals: Record<string, boolean>; | ||
activeView: string; | ||
theme: string; | ||
} | ||
|
||
/** | ||
* Initial state for the UI slice | ||
*/ | ||
const initialState: UIState = { | ||
isLoading: false, | ||
sidebarOpen: true, // Default to open | ||
modals: {}, | ||
activeView: 'dashboard', // Default view | ||
theme: 'light', // Default theme | ||
}; | ||
|
||
/** | ||
* Redux slice for managing UI-related state in the Refunds Service application. | ||
* Handles common UI elements like loading indicators, modal visibility, sidebar state, | ||
* theme settings, and active view tracking. | ||
*/ | ||
const uiSlice = createSlice({ | ||
name: 'ui', | ||
initialState, | ||
reducers: { | ||
/** | ||
* Sets the global loading state | ||
* Can be used with ApiStatus.LOADING or ApiStatus.IDLE for consistent state management | ||
*/ | ||
setLoading: (state, action: PayloadAction<boolean>) => { | ||
state.isLoading = action.payload; | ||
}, | ||
|
||
/** | ||
* Shows a modal by ID | ||
*/ | ||
showModal: (state, action: PayloadAction<string>) => { | ||
state.modals[action.payload] = true; | ||
}, | ||
|
||
/** | ||
* Hides a modal by ID | ||
*/ | ||
hideModal: (state, action: PayloadAction<string>) => { | ||
state.modals[action.payload] = false; | ||
}, | ||
|
||
/** | ||
* Toggles the sidebar open/closed state | ||
*/ | ||
toggleSidebar: (state) => { | ||
state.sidebarOpen = !state.sidebarOpen; | ||
}, | ||
|
||
/** | ||
* Sets the active view or screen | ||
*/ | ||
setActiveView: (state, action: PayloadAction<string>) => { | ||
state.activeView = action.payload; | ||
}, | ||
|
||
/** | ||
* Sets the theme (light/dark) | ||
*/ | ||
setTheme: (state, action: PayloadAction<string>) => { | ||
state.theme = action.payload; | ||
}, | ||
}, | ||
}); | ||
|
||
// Extract the action creators | ||
export const uiActions = uiSlice.actions; | ||
|
||
// Extract the reducer | ||
export const uiReducer = uiSlice.reducer; | ||
|
||
/** | ||
* Selector function to get the global loading state from the Redux store | ||
*/ | ||
export const selectLoading = (state: { ui: UIState }): boolean => state.ui.isLoading; | ||
|
||
/** | ||
* Selector function to check if a specific modal is currently visible | ||
*/ | ||
export const selectModalVisible = (state: { ui: UIState }, modalId: string): boolean => | ||
Boolean(state.ui.modals[modalId]); | ||
|
||
/** | ||
* Selector function to get the sidebar open state | ||
*/ | ||
export const selectSidebarOpen = (state: { ui: UIState }): boolean => state.ui.sidebarOpen; | ||
|
||
/** | ||
* Selector function to get the currently active view or screen | ||
*/ | ||
export const selectActiveView = (state: { ui: UIState }): string => state.ui.activeView; | ||
|
||
/** | ||
* Selector function to get the current theme setting | ||
*/ | ||
export const selectTheme = (state: { ui: UIState }): string => state.ui.theme; | ||
|
||
// Default export for the slice | ||
export default uiSlice; |