Advertisement
Guest User

Untitled

a guest
May 24th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. import {
  2. GET_CONTACTS,
  3. DELETE_CONTACT,
  4. ADD_CONTACT,
  5. GET_CONTACT,
  6. UPDATE_CONTACT
  7. } from './types';
  8. import axios from 'axios';
  9.  
  10. export const getContacts = () => async dispatch => {
  11. const res = await axios.get('https://jsonplaceholder.typicode.com/users');
  12. dispatch({
  13. type: GET_CONTACTS,
  14. payload: res.data
  15. });
  16. };
  17.  
  18. export const getContact = id => async dispatch => {
  19. const res = await axios.get(
  20. `https://jsonplaceholder.typicode.com/users/${id}`
  21. );
  22. dispatch({
  23. type: GET_CONTACT,
  24. payload: res.data
  25. });
  26. };
  27.  
  28. export const deleteContact = id => async dispatch => {
  29. try {
  30. await axios.delete(`https://jsonplaceholder.typicode.com/users/${id}`);
  31. dispatch({
  32. type: DELETE_CONTACT,
  33. payload: id
  34. });
  35. } catch (e) {
  36. dispatch({
  37. type: DELETE_CONTACT,
  38. payload: id
  39. });
  40. }
  41. };
  42.  
  43. export const addContact = contact => async dispatch => {
  44. const res = await axios.post(
  45. 'https://jsonplaceholder.typicode.com/users',
  46. contact
  47. );
  48. dispatch({
  49. type: ADD_CONTACT,
  50. payload: res.data
  51. });
  52. };
  53.  
  54. export const updateContact = contact => async dispatch => {
  55. const res = await axios.put(
  56. `https://jsonplaceholder.typicode.com/users/${contact.id}`,
  57. contact
  58. );
  59. dispatch({
  60. type: UPDATE_CONTACT,
  61. payload: res.data
  62. });
  63. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement