Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Update user list action
  2. export const UPDATE_USER_LIST = 'UPDATE_USER_LIST';
  3. export const updateUsers = () => async (dispatch, api) => {
  4.     const res = await api
  5.         .put('/users') // you need to use GET on the URI where you store the users
  6.         .then(res => {
  7.             return res;
  8.         })
  9.         .catch(err => {
  10.             return err.response;
  11.         });
  12.  
  13.     dispatch({
  14.         type: UPDATE_USER_LIST,
  15.         payload: res.data
  16.     });
  17. };
  18.  
  19. // Delete user action
  20. export const DELETE_USER = 'DELETE_USER';
  21. export const deleteUser = (id) => async (dispatch, getState, api) => {
  22. const res = await api.delete('/user/del/' + id, null)
  23.     .then(function (res) {
  24.         // you should call another action to update the User list
  25.         dispatch({
  26.             type: UPDATE_USER_LIST
  27.         });
  28.         return res;
  29.     })
  30.     .catch(function (err) {
  31.         return err.response;
  32.     });
  33.  
  34.     dispatch({
  35.         type: DELETE_USER,
  36.         payload: res.data,
  37.         state: getState
  38.     });
  39. };
  40.  
  41. // The reducer
  42. import {ADD_USER} from '../Actions/Create';
  43. import {DELETE_USER, UPDATE_USER} from '../Actions/Update';
  44.  
  45. export default (state = null, action) => {
  46.  
  47.     switch (action.type) {
  48.         case ADD_USER:
  49.             return action.payload;
  50.         case DELETE_USER:
  51.             return action.payload;
  52.         case UPDATE_USER:
  53.             return action.payload;
  54.             // add a case where you get the payload and place it in the users state in the store
  55.         case UPDATE_USER_LIST:
  56.             return action.payload
  57.         default:
  58.             return state;
  59.        }
  60.  
  61.    };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement