1 |
|
2 | import md5 from 'spark-md5';
|
3 | import * as types from '../types';
|
4 | import { voteService } from '../services';
|
5 |
|
6 | function increment(id) {
|
7 | return { type: types.INCREMENT_COUNT, id };
|
8 | }
|
9 |
|
10 | function decrement(id) {
|
11 | return { type: types.DECREMENT_COUNT, id };
|
12 | }
|
13 |
|
14 | function destroy(id) {
|
15 | return { type: types.DESTROY_TOPIC, id };
|
16 | }
|
17 |
|
18 | function 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 |
|
27 | function createTopicSuccess() {
|
28 | return {
|
29 | type: types.CREATE_TOPIC_SUCCESS
|
30 | };
|
31 | }
|
32 |
|
33 | function createTopicFailure(data) {
|
34 | return {
|
35 | type: types.CREATE_TOPIC_FAILURE,
|
36 | id: data.id,
|
37 | error: data.error
|
38 | };
|
39 | }
|
40 |
|
41 | function createTopicDuplicate() {
|
42 | return {
|
43 | type: types.CREATE_TOPIC_DUPLICATE
|
44 | };
|
45 | }
|
46 |
|
47 | export function typing(text) {
|
48 | return {
|
49 | type: types.TYPING,
|
50 | newTopic: text
|
51 | };
|
52 | }
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | export function createTopic(text) {
|
59 | return (dispatch, getState) => {
|
60 |
|
61 | if (text.trim().length <= 0) return;
|
62 |
|
63 | const id = md5.hash(text);
|
64 |
|
65 |
|
66 | const { topic } = getState();
|
67 | const data = {
|
68 | count: 1,
|
69 | id,
|
70 | text
|
71 | };
|
72 |
|
73 |
|
74 |
|
75 | if (topic.topics.filter(topicItem => topicItem.id === id).length > 0) {
|
76 |
|
77 |
|
78 |
|
79 | return dispatch(createTopicDuplicate());
|
80 | }
|
81 |
|
82 |
|
83 | dispatch(createTopicRequest(data));
|
84 |
|
85 | return voteService().createTopic({ id, data })
|
86 | .then((res) => {
|
87 | if (res.status === 200) {
|
88 |
|
89 |
|
90 |
|
91 |
|
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 |
|
101 | export 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 |
|
115 | export 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 |
|
129 | export 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 | }
|