Home Reference Source

application/redux/actions/channel-actions.js

  1. import BaseService from './../../services/base-service';
  2.  
  3. class ChannelService extends BaseService {
  4.  
  5. fetch(opts) {
  6. return this.get('/v3/channel', opts);
  7. }
  8.  
  9. fetchOne(id) {
  10. return this.get('/v3/channel/' + id);
  11. }
  12.  
  13. create(data) {
  14. return this.post('/v3/channel', data);
  15. }
  16.  
  17. update(id, data) {
  18. return this.put('/v3/channel/' + id, data);
  19. }
  20.  
  21. remove(uuid) {
  22. return this.delete('/v3/channel/' + uuid);
  23. }
  24. }
  25.  
  26. const service = new ChannelService;
  27.  
  28. /**
  29. * Action types
  30. */
  31. export const CHANNELS_REQUEST = 'CHANNELS_REQUEST';
  32. export const CHANNELS_RECEIVE = 'CHANNELS_RECEIVE';
  33. export const CHANNEL_RECEIVE = 'CHANNEL_RECEIVE';
  34. export const CHANNEL_SELECTED = 'CHANNEL_SELECTED';
  35. export const CHANNELS_INVALIDATE = 'CHANNELS_INVALIDATE';
  36.  
  37. export const CHANNELS_RECEIVE_PAGINATION = 'CHANNELS_RECEIVE_PAGINATION';
  38.  
  39. /**
  40. * Action creators
  41. */
  42. export function channelsReceivePagination(pagination) {
  43. return {
  44. type: CHANNELS_RECEIVE_PAGINATION,
  45. payload: {
  46. before: pagination.get('before'),
  47. current: pagination.get('current'),
  48. first: pagination.get('first'),
  49. last: pagination.get('last'),
  50. limit: pagination.get('limit'),
  51. next: pagination.get('next'),
  52. total_items: pagination.get('total_items'),
  53. total_pages: pagination.get('total_pages'),
  54. }
  55. };
  56. }
  57.  
  58. export function channelsRequest(params = {}) {
  59. return {
  60. type: CHANNELS_REQUEST,
  61. payload: {
  62. params: params
  63. }
  64. };
  65. }
  66.  
  67. export function channelsReceive(items) {
  68.  
  69. return {
  70. type: CHANNELS_RECEIVE,
  71. payload: {
  72. channels: items ? items : []
  73. }
  74. };
  75. }
  76.  
  77. export function channelReceive(channel) {
  78. return {
  79. type: CHANNEL_RECEIVE,
  80. payload: {
  81. channel: channel ? channel : {}
  82. }
  83. };
  84. }
  85.  
  86. export function channelSetSelected(channel) {
  87. return {
  88. type: CHANNEL_SELECTED,
  89. payload: {
  90. selected: channel ? channel : false
  91. }
  92. };
  93. }
  94.  
  95. export function channelsInvalidate() {
  96. return {
  97. type: CHANNELS_INVALIDATE,
  98. payload: {
  99. }
  100. };
  101. }
  102.  
  103. export function channelUpdate(id, data) {
  104. return function(dispatch, getState) {
  105. dispatch(channelsRequest({}));
  106.  
  107. return service.update(id, data)
  108. .then(response => {
  109. return dispatch(channelReceive(response.first()));
  110. });
  111. }
  112. }
  113.  
  114. export function channelCreate(data) {
  115. return function(dispatch, getState) {
  116. dispatch(channelsRequest({}));
  117.  
  118. return service.create(data)
  119. .then(response => {
  120. dispatch(channelSetSelected(response.first().get('uuid')));
  121. return dispatch(channelReceive(response.first()));
  122. });
  123. }
  124. }
  125.  
  126. export function channelsShouldFetch(state) {
  127. const channels = state.channels;
  128. if (!channels.items.length) {
  129. return true;
  130. } else if (channels.isFetching) {
  131. return false;
  132. }
  133.  
  134. return channels.didInvalidate;
  135. }
  136.  
  137. export function channelsMaybeFetch(params = {}) {
  138. return function(dispatch, getState) {
  139. const channels = getState().channels.items;
  140.  
  141. if (getState().channels.isFetching) {
  142. return Promise.resolve();
  143. }
  144.  
  145. if (!channels.items || !channels.items.size) {
  146. return dispatch(channelsFetch(params));
  147. }
  148.  
  149. return Promise.resolve();
  150. }
  151. };
  152.  
  153. export function channelsFetch(params = {}) {
  154. return function(dispatch, getState) {
  155. dispatch(channelsRequest(params));
  156.  
  157. params.per_page = params.per_page ? params.per_page : 20;
  158. params.order = params.order ? params.order : "name";
  159. params.dir = params.dir ? params.dir : "asc";
  160.  
  161. // default page
  162. let page = 0;
  163.  
  164. if (params.page) {
  165. // specific page is requested
  166. page = params.page;
  167. } else if (getState().channels.pagination.current) {
  168. // existing pagination
  169. page = getState().channels.pagination.current;
  170. }
  171.  
  172. if (page) {
  173. params.page = page;
  174. }
  175.  
  176. return service.fetch(params)
  177. .then(response => {
  178. dispatch(channelsReceivePagination(response));
  179. return dispatch(channelsReceive(response.get('items')));
  180. });
  181. };
  182. }
  183.  
  184. export function channelsFetchOne(id) {
  185. return function (dispatch, getState) {
  186. dispatch(channelsRequest({}));
  187.  
  188. return service.fetchOne(id)
  189. .then(response => {
  190. return dispatch(channelReceive(response.first()));
  191. });
  192. }
  193. }
  194.  
  195. export function channelsRemove(uuid) {
  196. return function (dispatch, getState) {
  197. dispatch(channelsInvalidate());
  198.  
  199. return service.remove(uuid);
  200. }
  201. }