|
| 1 | +import { createSlice } from '@reduxjs/toolkit'; |
| 2 | +import { PURGE } from 'redux-persist'; |
| 3 | +import { |
| 4 | + extractBarcode, |
| 5 | + extractDataFromText, |
| 6 | + extractText, |
| 7 | +} from '@store/thunks'; |
| 8 | + |
| 9 | +export interface AIState { |
| 10 | + text?: string; |
| 11 | + data?: Record<string, unknown>; |
| 12 | + barCodes?: string[]; |
| 13 | + loadingText: boolean; |
| 14 | + loadingData: boolean; |
| 15 | + loadingBarCodes: boolean; |
| 16 | +} |
| 17 | + |
| 18 | +const initialState = { |
| 19 | + text: undefined, |
| 20 | + data: undefined, |
| 21 | + barCodes: undefined, |
| 22 | + loadingText: true, |
| 23 | + loadingData: true, |
| 24 | + loadingBarCodes: true, |
| 25 | +} satisfies AIState as AIState; |
| 26 | + |
| 27 | +const aiSlice = createSlice({ |
| 28 | + name: 'ai', |
| 29 | + initialState, |
| 30 | + reducers: {}, |
| 31 | + extraReducers: (builder) => { |
| 32 | + // Add reducers for additional action types here, and handle loading state as needed |
| 33 | + builder |
| 34 | + .addCase(extractText.fulfilled, (state, action) => { |
| 35 | + // Add user to the state array |
| 36 | + state.text = action.payload; |
| 37 | + state.loadingText = false; |
| 38 | + }) |
| 39 | + .addCase(extractText.pending, (state) => { |
| 40 | + // Add user to the state array |
| 41 | + state.loadingText = true; |
| 42 | + }) |
| 43 | + .addCase(extractBarcode.fulfilled, (state, action) => { |
| 44 | + // Add user to the state array |
| 45 | + state.barCodes = action.payload; |
| 46 | + state.loadingBarCodes = false; |
| 47 | + }) |
| 48 | + .addCase(extractBarcode.pending, (state) => { |
| 49 | + // Add user to the state array |
| 50 | + state.loadingBarCodes = true; |
| 51 | + }) |
| 52 | + .addCase(extractDataFromText.fulfilled, (state, action) => { |
| 53 | + // Add user to the state array |
| 54 | + state.data = action.payload; |
| 55 | + state.loadingData = false; |
| 56 | + }) |
| 57 | + .addCase(extractDataFromText.pending, (state) => { |
| 58 | + // Add user to the state array |
| 59 | + state.loadingData = true; |
| 60 | + }) |
| 61 | + .addCase(PURGE, (state) => { |
| 62 | + state.data = initialState.data; |
| 63 | + state.text = initialState.text; |
| 64 | + state.loadingData = initialState.loadingData; |
| 65 | + state.loadingText = initialState.loadingText; |
| 66 | + state.loadingBarCodes = initialState.loadingBarCodes; |
| 67 | + }); |
| 68 | + }, |
| 69 | +}); |
| 70 | + |
| 71 | +export const reducerAI = aiSlice.reducer; |
0 commit comments