@replyke/core
Version:
Replyke: Build interactive apps with social features like comments, votes, feeds, user lists, notifications, and more.
66 lines • 2.26 kB
JavaScript
import { createSlice } from '@reduxjs/toolkit';
const initialState = {
user: null,
loading: false,
updating: false,
currentProjectId: undefined,
error: null,
};
const userSlice = createSlice({
name: 'user',
initialState,
reducers: {
// User data management
setUser: (state, action) => {
state.user = action.payload;
state.error = null;
},
clearUser: (state) => {
state.user = null;
state.error = null;
},
// Project context
setProjectContext: (state, action) => {
if (state.currentProjectId !== action.payload) {
state.currentProjectId = action.payload;
}
},
// Loading states
setLoading: (state, action) => {
state.loading = action.payload;
},
setUpdating: (state, action) => {
state.updating = action.payload;
},
// Error handling
setError: (state, action) => {
state.error = action.payload;
},
clearError: (state) => {
state.error = null;
},
// Optimistic updates (will be used by RTK Query)
updateUserOptimistic: (state, action) => {
if (state.user) {
state.user = { ...state.user, ...action.payload };
}
},
},
});
// Actions
export const { setUser, clearUser, setProjectContext, setLoading, setUpdating, setError, clearError, updateUserOptimistic, } = userSlice.actions;
// Selectors - use namespaced state for dual-mode support
export const selectUser = (state) => state.replyke.user.user;
export const selectUserLoading = (state) => state.replyke.user.loading;
export const selectUserUpdating = (state) => state.replyke.user.updating;
export const selectCurrentProjectId = (state) => state.replyke.user.currentProjectId;
export const selectUserError = (state) => state.replyke.user.error;
// Complex selectors
export const selectUserById = (userId) => (state) => {
const user = selectUser(state);
return user?.id === userId ? user : null;
};
// Reducer
export const userReducer = userSlice.reducer;
export default userSlice;
//# sourceMappingURL=userSlice.js.map