| import { createSlice, createAsyncThunk } from '@reduxjs/toolkit'; |
| import sourceService from '../../services/sourceService'; |
|
|
| |
| const initialState = { |
| items: [], |
| loading: false, |
| error: null |
| }; |
|
|
| |
| export const fetchSources = createAsyncThunk( |
| 'sources/fetchSources', |
| async (_, { rejectWithValue }) => { |
| try { |
| const response = await sourceService.getAll(); |
| return response.data.sources; |
| } catch (error) { |
| return rejectWithValue(error.response.data); |
| } |
| } |
| ); |
|
|
| export const addSource = createAsyncThunk( |
| 'sources/addSource', |
| async (sourceData, { rejectWithValue }) => { |
| try { |
| const response = await sourceService.create(sourceData); |
| return response.data; |
| } catch (error) { |
| return rejectWithValue(error.response.data); |
| } |
| } |
| ); |
|
|
| export const deleteSource = createAsyncThunk( |
| 'sources/deleteSource', |
| async (sourceId, { rejectWithValue }) => { |
| try { |
| const response = await sourceService.delete(sourceId); |
| return { ...response.data, sourceId }; |
| } catch (error) { |
| return rejectWithValue(error.response.data); |
| } |
| } |
| ); |
|
|
| export const analyzeKeyword = createAsyncThunk( |
| 'sources/analyzeKeyword', |
| async (keywordData, { rejectWithValue }) => { |
| try { |
| const response = await sourceService.analyzeKeyword(keywordData); |
| return response.data; |
| } catch (error) { |
| return rejectWithValue(error.response.data); |
| } |
| } |
| ); |
|
|
| |
| const sourcesSlice = createSlice({ |
| name: 'sources', |
| initialState, |
| reducers: { |
| clearError: (state) => { |
| state.error = null; |
| } |
| }, |
| extraReducers: (builder) => { |
| |
| builder |
| .addCase(fetchSources.pending, (state) => { |
| state.loading = true; |
| state.error = null; |
| }) |
| .addCase(fetchSources.fulfilled, (state, action) => { |
| state.loading = false; |
| state.items = action.payload; |
| }) |
| .addCase(fetchSources.rejected, (state, action) => { |
| state.loading = false; |
| state.error = action.payload?.message || 'Failed to fetch sources'; |
| }) |
| |
| .addCase(addSource.pending, (state) => { |
| state.loading = true; |
| state.error = null; |
| }) |
| .addCase(addSource.fulfilled, (state, action) => { |
| state.loading = false; |
| |
| |
| if (action.payload && action.payload.source) { |
| state.items = [...state.items, action.payload.source]; |
| } |
| }) |
| .addCase(addSource.rejected, (state, action) => { |
| state.loading = false; |
| state.error = action.payload?.message || 'Failed to add source'; |
| }) |
| |
| .addCase(deleteSource.pending, (state) => { |
| state.loading = true; |
| state.error = null; |
| }) |
| .addCase(deleteSource.fulfilled, (state, action) => { |
| state.loading = false; |
| |
| state.items = state.items.filter(source => source.id !== action.payload.sourceId); |
| }) |
| .addCase(deleteSource.rejected, (state, action) => { |
| state.loading = false; |
| state.error = action.payload?.message || 'Failed to delete source'; |
| }) |
| |
| .addCase(analyzeKeyword.pending, (state) => { |
| state.loading = true; |
| state.error = null; |
| }) |
| .addCase(analyzeKeyword.fulfilled, (state, action) => { |
| state.loading = false; |
| |
| |
| console.log('Keyword analysis completed:', action.payload); |
| }) |
| .addCase(analyzeKeyword.rejected, (state, action) => { |
| state.loading = false; |
| state.error = action.payload?.message || 'Failed to analyze keyword'; |
| }); |
| } |
| }); |
|
|
| export const { clearError } = sourcesSlice.actions; |
| export default sourcesSlice.reducer; |