60 lines
1.9 KiB
TypeScript
60 lines
1.9 KiB
TypeScript
import { createReducer } from '@reduxjs/toolkit';
|
|
import { Category } from '@interfaces';
|
|
import { fetchCategories, completeOnboard } from './thunk';
|
|
|
|
// ─── State ────────────────────────────────────────────────────────────────────
|
|
|
|
export interface PreferencesState {
|
|
categories: Category[];
|
|
selectedCategories: string[];
|
|
isLoading: boolean;
|
|
isSubmitting: boolean;
|
|
error: string | null;
|
|
submitError: string | null;
|
|
}
|
|
|
|
const initialState: PreferencesState = {
|
|
categories: [],
|
|
selectedCategories: [],
|
|
isLoading: false,
|
|
isSubmitting: false,
|
|
error: null,
|
|
submitError: null,
|
|
};
|
|
|
|
// ─── Reducer ──────────────────────────────────────────────────────────────────
|
|
|
|
const preferencesReducer = createReducer(initialState, builder => {
|
|
builder
|
|
// Fetch categories
|
|
.addCase(fetchCategories.pending, state => {
|
|
state.isLoading = true;
|
|
state.error = null;
|
|
})
|
|
.addCase(fetchCategories.fulfilled, (state, action) => {
|
|
state.categories = action.payload;
|
|
state.isLoading = false;
|
|
state.error = null;
|
|
})
|
|
.addCase(fetchCategories.rejected, (state, action) => {
|
|
state.isLoading = false;
|
|
state.error = action.payload as string;
|
|
})
|
|
|
|
// Complete onboard
|
|
.addCase(completeOnboard.pending, state => {
|
|
state.isSubmitting = true;
|
|
state.submitError = null;
|
|
})
|
|
.addCase(completeOnboard.fulfilled, state => {
|
|
state.isSubmitting = false;
|
|
state.submitError = null;
|
|
})
|
|
.addCase(completeOnboard.rejected, (state, action) => {
|
|
state.isSubmitting = false;
|
|
state.submitError = action.payload as string;
|
|
});
|
|
});
|
|
|
|
export default preferencesReducer;
|