-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e1f1a9f
commit 570fd00
Showing
7 changed files
with
209 additions
and
126 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
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
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
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,64 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { createAsyncThunk, createSlice, PayloadAction } from '@reduxjs/toolkit'; | ||
import { DynamicData } from '../../@types/DynamicData'; | ||
import API from '../../utils/api'; | ||
|
||
interface fetchUserType { | ||
page?: number; | ||
search?: string; | ||
} | ||
|
||
interface UsersState { | ||
data: DynamicData[]; | ||
isLoading: boolean; | ||
error: string | null; | ||
} | ||
|
||
const initialState: UsersState = { | ||
data: [], | ||
isLoading: false, | ||
error: null, | ||
}; | ||
|
||
export const getUserPag = createAsyncThunk( | ||
'fetchUser', | ||
async ({ page, search }: fetchUserType, { rejectWithValue }) => { | ||
try { | ||
const { data } = await API.get('/users', { | ||
params: { page, search: search }, | ||
}); | ||
return data; | ||
} catch (error) { | ||
return rejectWithValue(error); | ||
} | ||
}, | ||
); | ||
|
||
const getUserPagSlice = createSlice({ | ||
name: 'users', | ||
initialState, | ||
reducers: {}, | ||
extraReducers: (builder) => { | ||
builder.addCase(getUserPag.pending, (state) => { | ||
state.isLoading = true; | ||
state.error = null; | ||
}); | ||
builder.addCase( | ||
getUserPag.fulfilled, | ||
(state, action: PayloadAction<any>) => { | ||
state.isLoading = false; | ||
state.data = [...state.data, action.payload]; | ||
state.error = null; | ||
}, | ||
); | ||
builder.addCase( | ||
getUserPag.rejected, | ||
(state, action: PayloadAction<any>) => { | ||
state.isLoading = false; | ||
state.error = action.payload; | ||
}, | ||
); | ||
}, | ||
}); | ||
|
||
export default getUserPagSlice.reducer; |
Oops, something went wrong.