Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Update user list action
- export const UPDATE_USER_LIST = 'UPDATE_USER_LIST';
- export const updateUsers = () => async (dispatch, api) => {
- const res = await api
- .put('/users') // you need to use GET on the URI where you store the users
- .then(res => {
- return res;
- })
- .catch(err => {
- return err.response;
- });
- dispatch({
- type: UPDATE_USER_LIST,
- payload: res.data
- });
- };
- // Delete user action
- export const DELETE_USER = 'DELETE_USER';
- export const deleteUser = (id) => async (dispatch, getState, api) => {
- const res = await api.delete('/user/del/' + id, null)
- .then(function (res) {
- // you should call another action to update the User list
- dispatch({
- type: UPDATE_USER_LIST
- });
- return res;
- })
- .catch(function (err) {
- return err.response;
- });
- dispatch({
- type: DELETE_USER,
- payload: res.data,
- state: getState
- });
- };
- // The reducer
- import {ADD_USER} from '../Actions/Create';
- import {DELETE_USER, UPDATE_USER} from '../Actions/Update';
- export default (state = null, action) => {
- switch (action.type) {
- case ADD_USER:
- return action.payload;
- case DELETE_USER:
- return action.payload;
- case UPDATE_USER:
- return action.payload;
- // add a case where you get the payload and place it in the users state in the store
- case UPDATE_USER_LIST:
- return action.payload
- default:
- return state;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement