Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3432 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
3719 stevensc 1
import { axios } from '../../utils';
2
import { feedActionTypes } from './feed.types';
3
 
4
export const setTimelineUrl = (url) => ({
5
  type: feedActionTypes.SET_TIMELINE_URL,
6
  payload: url
7
});
8
 
9
export const loadFeeds = () => ({
10
  type: feedActionTypes.LOAD_FEEDS
11
});
12
 
13
export const loadFeedsSuccess = (feeds, currentPage, pages) => ({
14
  type: feedActionTypes.LOAD_FEEDS_SUCCESS,
15
  payload: { feeds, currentPage, pages }
16
});
17
 
18
export const addFeed = (url, feed, shareId) => {
19
  return async (dispatch) => {
20
    const form = new FormData();
21
    const feedFields = Object.keys(feed);
22
 
23
    feedFields.forEach((field) => form.append(field, feed[field]));
24
 
25
    const response = await axios.post(url, form);
26
    const { data, success } = response.data;
27
 
28
    if (!success) {
29
      const error = typeof data === 'string' ? data : 'Error al publicar';
30
      throw new Error(error);
31
    }
32
 
33
    dispatch(addFeedSuccess(data, shareId));
34
  };
35
};
36
 
37
export const addFeedSuccess = (feed, feedSharedId = '') => ({
38
  type: feedActionTypes.ADD_FEED,
39
  payload: { feed, feedSharedId }
40
});
41
 
42
export const deleteFeed = (url, id) => {
43
  return async (dispatch) => {
44
    const response = await axios.post(url);
45
    const { data, success } = response.data;
46
 
47
    if (!success) {
48
      const err = typeof data === 'string' ? data : 'Error al borrar la publicación';
49
      throw new Error(err);
50
    }
51
 
52
    dispatch(deleteFeedSuccess(id));
53
    return data;
54
  };
55
};
56
 
57
export const deleteFeedSuccess = (feedId) => ({
58
  type: feedActionTypes.DELETE_FEED,
59
  payload: feedId
60
});
61
 
62
export const updateFeed = ({ feed, uuid }) => ({
63
  type: feedActionTypes.UPDATE_FEED,
64
  payload: { feed, uuid }
65
});
66
 
67
export const setCurrentPage = (page) => ({
68
  type: feedActionTypes.SET_CURRENT_PAGE,
69
  payload: page
70
});
71
 
72
export const fetchFeeds = (url, page) => {
73
  return (dispatch) => {
74
    if (!url || url.includes('undefined')) return;
75
 
76
    dispatch(loadFeeds());
77
 
78
    axios.get(url + '?page=' + page).then((response) => {
79
      const { data, success } = response.data;
80
 
81
      if (!success) {
82
        throw new Error('Error al obtener las publicaciones, por favor intente más tarde');
83
      }
84
 
85
      const feeds = data.current.items;
86
      const currentPage = data.current.page;
87
      const pages = data.total.pages;
88
 
89
      dispatch(loadFeedsSuccess(feeds, currentPage, pages));
90
    });
91
  };
92
};
93
 
94
export const removeComment = ({ feedId, commentId }) => ({
95
  type: feedActionTypes.REMOVE_COMMENT,
96
  payload: { feedId, commentId }
97
});
98
 
99
export const addComment = ({ feedId, comment }) => ({
100
  type: feedActionTypes.ADD_COMMENT,
101
  payload: { feedId, comment }
102
});