-
Notifications
You must be signed in to change notification settings - Fork 11
/
sagas.js
159 lines (142 loc) · 3.89 KB
/
sagas.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* eslint-disable */
import { takeLatest } from 'redux-saga';
import { take, call, put, select, fork, cancel } from 'redux-saga/effects';
import Cosmic from 'cosmicjs';
import async from 'async';
import config from '../../config';
import request from '../../utils/request';
import cosmic from '../../utils/cosmic';
const READ_KEY = config.bucket.read_key;
const WRITE_KEY = config.bucket.write_key;
const SLUG = config.bucket.slug;
const HOST = 'https://api.cosmicjs.com/v1';
import {
GET_NOTE_GROUPS,
ADD_NOTE_GROUP,
EDIT_NOTE_GROUP,
DELETE_NOTE_GROUP,
} from './constants';
import {
getNoteGroupsSuccess,
getNoteGroupsFail,
addNoteGroupSuccess,
addNoteGroupFail,
editNoteGroupSuccess,
editNoteGroupFail,
deleteNoteGroupSuccess,
deleteNoteGroupFail
} from './actions';
export function* getNoteGroups() {
const params = {
type_slug: 'groups',
};
try {
const groups = yield call(cosmic, "GET_TYPE",params);
yield put(getNoteGroupsSuccess(groups||[]));
} catch(err) {
yield put(getNoteGroupsFail(groups.err));
}
}
export function* addNoteGroup(action) {
const params = {
write_key: config.bucket.write_key,
type_slug: "groups",
title: action.group.title,
content: action.group.content,
metafields: [{
value: action.group.color,
type: 'text',
key: "color",
title: "Color",
}],
};
const group = yield call(cosmic, "ADD", params);
if(!group.err) {
yield put(addNoteGroupSuccess(group.object));
} else {
yield put(addNoteGroupFail(group.err));
}
}
export function* editNoteGroup(action) {
const params = {
write_key: config.bucket.write_key,
type_slug: "groups",
slug: action.slug,
title: action.group.title,
content: action.group.content,
metafields: [{
type: 'text',
value: action.group.color,
key: "color",
title: "Color",
}]
};
const group = yield call(cosmic, "EDIT", params);
if(!group.err) {
yield put(editNoteGroupSuccess(group.object, action.index));
} else {
yield put(editNoteGroupFail(group.err));
}
}
export function* deleteNoteGroup(action) {
const params = {
write_key: config.bucket.write_key,
slug: action.group.slug,
};
const notes = yield call(getNotes, action.group.slug);
async.eachSeries(notes, (note, callback) => {
const params = {
write_key: config.bucket.write_key,
slug: note.slug,
};
if (!!note.metafields && !!note.metafields[1]) {
deleteMedia(note.metafields[1].id);
}
if (!!note.metafields && !!note.metafields[2]) {
deleteMedia(note.metafields[2].id);
}
Cosmic.deleteObject(config, params, (err, res)=> {
callback();
})
});
const response = yield call(cosmic, "DELETE", params);
if(!response.err) {
yield put(deleteNoteGroupSuccess(action.index));
} else {
yield put(deleteNoteGroupFail(response.err));
}
}
function deleteMedia(id) {
const requestURL = `${HOST}/${SLUG}/media/${id}`;
const options = {
method: 'DELETE',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
write_key: WRITE_KEY,
})
};
request(requestURL, options);
}
function* getNotes(slug) {
const requestURL = `${HOST}/${SLUG}/object-type/notes/search?metafield_key=group&metafield_object_slug=${slug}&read_key=${READ_KEY}`;
try {
const notes = yield call(request, requestURL);
return notes.data.objects||[];
} catch (err) {
return err
}
}
/**
* Watches for LOAD_REPOS actions and calls getRepos when one comes in.
* By using `takeLatest` only the result of the latest API call is applied.
*/
export function* homeSagas() {
yield fork(takeLatest, GET_NOTE_GROUPS, getNoteGroups);
yield fork(takeLatest, ADD_NOTE_GROUP, addNoteGroup);
yield fork(takeLatest, EDIT_NOTE_GROUP, editNoteGroup);
yield fork(takeLatest, DELETE_NOTE_GROUP, deleteNoteGroup);
}
// Bootstrap sagas
export default homeSagas;