remote-todo.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import axios from "axios";
  2. import * as types from "../actionTypes/actionTypes";
  3. import rmFiels from "../utils/rmPropFromObject";
  4. const URL = "https://test-request-4b9d7.firebaseio.com/posts";
  5. export const changeInputValue = payload => ({
  6. type: types.CHANGE_INPUT_VALUE,
  7. payload
  8. });
  9. export const handleChangeEditForm = payload => ({
  10. type: types.HANDLE_CHANGE_EDIT_FORM,
  11. payload
  12. });
  13. export const resetPostForm = () => ({
  14. type: types.RESET_POSTS_FORM
  15. });
  16. export const showEditForm = payload => ({
  17. type: types.SHOW_EDIT_FORM,
  18. payload
  19. });
  20. export const cancelUpdate = payload => ({
  21. type: types.CANCEL_UPDATE,
  22. payload
  23. });
  24. const postRequest = payload => ({
  25. type: types.POST_REQUEST,
  26. payload
  27. });
  28. const postRequestSuccess = payload => ({
  29. type: types.POST_REQUEST_SUCCESS,
  30. payload
  31. });
  32. const postRequestFail = payload => ({
  33. type: types.POST_REQUEST_FAIL,
  34. payload
  35. });
  36. export const createPost = payload => {
  37. return dispatch => {
  38. dispatch(postRequest());
  39. return axios
  40. .post(URL + ".json", payload)
  41. .then(res => dispatch(postRequestSuccess(res)))
  42. .catch(err => dispatch(postRequestFail(err)));
  43. };
  44. };
  45. const getRequest = payload => ({
  46. type: types.GET_REQUEST,
  47. payload
  48. });
  49. const getRequestSuccess = payload => ({
  50. type: types.GET_REQUEST_SUCCESS,
  51. payload
  52. });
  53. const getRequestFail = payload => ({
  54. type: types.GET_REQUEST_FAIL,
  55. payload
  56. });
  57. export const getPosts = () => dispatch => {
  58. dispatch(getRequest());
  59. return axios
  60. .get(URL + ".json")
  61. .then(res => dispatch(getRequestSuccess(res)))
  62. .catch(err => dispatch(getRequestFail(err)));
  63. };
  64. const removeRequest = payload => ({
  65. type: types.REMOVE_REQUEST,
  66. payload
  67. });
  68. const removeRequestSuccess = payload => ({
  69. type: types.REMOVE_REQUEST_SUCCESS,
  70. payload
  71. });
  72. const removeRequestFail = payload => ({
  73. type: types.REMOVE_REQUEST_FAIL,
  74. payload
  75. });
  76. export const removePost = id => dispatch => {
  77. dispatch(removeRequest());
  78. return axios
  79. .delete(URL + "/" + id + ".json")
  80. .then(res => dispatch(removeRequestSuccess({ res, id })))
  81. .catch(err => dispatch(removeRequestFail(err)));
  82. };
  83. const updatePost = payload => ({
  84. type: types.UPDATE_REQUEST,
  85. payload
  86. });
  87. const updatePostSuccess = payload => ({
  88. type: types.UPDATE_REQUEST_SUCCESS,
  89. payload
  90. });
  91. const updatePostFail = payload => ({
  92. type: types.UPDATE_REQUEST_FAIL,
  93. payload
  94. });
  95. export const update = payload => dispatch => {
  96. const data = rmFiels(payload, "id");
  97. dispatch(updatePost());
  98. return axios
  99. .put(URL + "/" + payload.id + ".json", data)
  100. .then(res => dispatch(updatePostSuccess({ res, id: payload.id })))
  101. .catch(err => dispatch(updatePostFail(err)));
  102. };