-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial new chicken form and storage
- Loading branch information
1 parent
ab0c3cc
commit beb322f
Showing
4 changed files
with
197 additions
and
5 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,38 @@ | ||
import { createSlice, createAsyncThunk } from '@reduxjs/toolkit'; | ||
import * as chickenApi from '../../api/chickenApi'; | ||
|
||
export const addChicken = createAsyncThunk( | ||
'chickens/addChicken', | ||
async (chickenData, { getState }) => { | ||
const { auth } = getState(); | ||
const farmId = auth.user.farmId; | ||
const response = await chickenApi.addChicken(farmId, chickenData); | ||
return response; | ||
} | ||
); | ||
|
||
const chickenSlice = createSlice({ | ||
name: 'chickens', | ||
initialState: { | ||
chickens: [], | ||
loading: false, | ||
error: null | ||
}, | ||
reducers: {}, | ||
extraReducers: builder => { | ||
builder | ||
.addCase(addChicken.pending, state => { | ||
state.loading = true; | ||
}) | ||
.addCase(addChicken.fulfilled, (state, action) => { | ||
state.chickens.push(action.payload); | ||
state.loading = false; | ||
}) | ||
.addCase(addChicken.rejected, (state, action) => { | ||
state.loading = false; | ||
state.error = action.error.message; | ||
}); | ||
} | ||
}); | ||
|
||
export default chickenSlice.reducer; |