UNPKG

3.61 kBJavaScriptView Raw
1/* eslint consistent-return: 0, no-else-return: 0*/
2import md5 from 'spark-md5';
3import * as types from '../types';
4import { voteService } from '../services';
5
6function increment(id) {
7 return { type: types.INCREMENT_COUNT, id };
8}
9
10function decrement(id) {
11 return { type: types.DECREMENT_COUNT, id };
12}
13
14function destroy(id) {
15 return { type: types.DESTROY_TOPIC, id };
16}
17
18function createTopicRequest(data) {
19 return {
20 type: types.CREATE_TOPIC_REQUEST,
21 id: data.id,
22 count: data.count,
23 text: data.text
24 };
25}
26
27function createTopicSuccess() {
28 return {
29 type: types.CREATE_TOPIC_SUCCESS
30 };
31}
32
33function createTopicFailure(data) {
34 return {
35 type: types.CREATE_TOPIC_FAILURE,
36 id: data.id,
37 error: data.error
38 };
39}
40
41function createTopicDuplicate() {
42 return {
43 type: types.CREATE_TOPIC_DUPLICATE
44 };
45}
46
47export function typing(text) {
48 return {
49 type: types.TYPING,
50 newTopic: text
51 };
52}
53
54// This action creator returns a function,
55// which will get executed by Redux-Thunk middleware
56// This function does not need to be pure, and thus allowed
57// to have side effects, including executing asynchronous API calls.
58export function createTopic(text) {
59 return (dispatch, getState) => {
60 // If the text box is empty
61 if (text.trim().length <= 0) return;
62
63 const id = md5.hash(text);
64 // Redux thunk's middleware receives the store methods `dispatch`
65 // and `getState` as parameters
66 const { topic } = getState();
67 const data = {
68 count: 1,
69 id,
70 text
71 };
72
73 // Conditional dispatch
74 // If the topic already exists, make sure we emit a dispatch event
75 if (topic.topics.filter(topicItem => topicItem.id === id).length > 0) {
76 // Currently there is no reducer that changes state for this
77 // For production you would ideally have a message reducer that
78 // notifies the user of a duplicate topic
79 return dispatch(createTopicDuplicate());
80 }
81
82 // First dispatch an optimistic update
83 dispatch(createTopicRequest(data));
84
85 return voteService().createTopic({ id, data })
86 .then((res) => {
87 if (res.status === 200) {
88 // We can actually dispatch a CREATE_TOPIC_SUCCESS
89 // on success, but I've opted to leave that out
90 // since we already did an optimistic update
91 // We could return res.json();
92 return dispatch(createTopicSuccess());
93 }
94 })
95 .catch(() => {
96 return dispatch(createTopicFailure({ id, error: 'Oops! Something went wrong and we couldn\'t create your topic'}));
97 });
98 };
99}
100
101export function incrementCount(id) {
102 return (dispatch) => {
103 return voteService().updateTopic({
104 id,
105 data: {
106 isFull: false,
107 isIncrement: true
108 }
109 })
110 .then(() => dispatch(increment(id)))
111 .catch(() => dispatch(createTopicFailure({id, error: 'Oops! Something went wrong and we couldn\'t add your vote'})));
112 };
113}
114
115export function decrementCount(id) {
116 return (dispatch) => {
117 return voteService().updateTopic({
118 id,
119 data: {
120 isFull: false,
121 isIncrement: false
122 }
123 })
124 .then(() => dispatch(decrement(id)))
125 .catch(() => dispatch(createTopicFailure({id, error: 'Oops! Something went wrong and we couldn\'t add your vote'})));
126 };
127}
128
129export function destroyTopic(id) {
130 return (dispatch) => {
131 return voteService().deleteTopic({ id })
132 .then(() => dispatch(destroy(id)))
133 .catch(() => dispatch(createTopicFailure({id,
134 error: 'Oops! Something went wrong and we couldn\'t add your vote'})));
135 };
136}