1 | import { IBookmark } from "@lonelyplanet/open-planet-node/dist/resources/bookmark";
|
2 | import { IBookmarkList } from "@lonelyplanet/open-planet-node/dist/resources/bookmarkList";
|
3 | import "isomorphic-fetch";
|
4 | import { ThunkAction } from "redux-thunk";
|
5 | import { ADD_BOOKMARK_DONE, ADD_BOOKMARK_START, LIST_CREATE_DONE, LIST_CREATE_START, LIST_DELETE_DONE, LIST_DELETE_START, LIST_UPDATE_DONE, LIST_UPDATE_START, UPDATE_BOOKMARK_DONE, UPDATE_BOOKMARK_START } from "../constants/bookmark";
|
6 | import { showToastMessage } from "./toast";
|
7 |
|
8 | const messages = {
|
9 | addBookmarkToListError: "There was an error updating your list",
|
10 | addBookmarkToListSuccess: "Your item has been added.",
|
11 | createListSuccess: "Your list has been successfully created.",
|
12 | createListError: "There was an error creating your list",
|
13 | updateListError: "There was an error updating your list",
|
14 | updateListSuccess: "Your list has been updated",
|
15 | deleteListError: "There was an error deleting your list",
|
16 | deleteListSuccess: "Your list has been deleted",
|
17 | updateBookmarkSuccess: "Your bookmark has been updated",
|
18 | updateBookmarkError: "There was an error updating your bookmark",
|
19 | }
|
20 |
|
21 | export type CreateListParams = {
|
22 | userId: string;
|
23 | data: IBookmarkList,
|
24 | onDone: Function,
|
25 | };
|
26 |
|
27 | export const createList = ({ userId, data, onDone }: CreateListParams): ThunkAction<void, {}, {}, {
|
28 | type: string,
|
29 | }> =>
|
30 | async (dispatch) => {
|
31 | dispatch({
|
32 | type: LIST_CREATE_START,
|
33 | });
|
34 |
|
35 | let payload;
|
36 |
|
37 | try {
|
38 | const response = await fetch(`${window.lp.hosts.profile}/profile/api/${userId}/lists`, {
|
39 | headers: {
|
40 | "Accept": "application/json",
|
41 | "Content-Type": "application/json",
|
42 | },
|
43 | method: "POST",
|
44 | credentials: "include",
|
45 | body: JSON.stringify(data),
|
46 | });
|
47 |
|
48 | if (response.status !== 200) {
|
49 | const errorBody = await response.text();
|
50 | const errorObj = await JSON.parse(errorBody);
|
51 | return dispatch(showToastMessage(errorObj.message, "error"));
|
52 | }
|
53 |
|
54 | payload = await response.json();
|
55 | } catch (e) {
|
56 | return dispatch(showToastMessage(messages.createListError, "error"));
|
57 | }
|
58 |
|
59 | dispatch({
|
60 | type: LIST_CREATE_DONE,
|
61 | payload,
|
62 | });
|
63 |
|
64 | dispatch(showToastMessage(messages.createListSuccess));
|
65 |
|
66 | if (onDone) {
|
67 | onDone(payload.id);
|
68 | }
|
69 | };
|
70 |
|
71 | export const updateList = ({ userId, listId, data }: {
|
72 | userId: string;
|
73 | listId: string;
|
74 | entryId: string;
|
75 | data: IBookmarkList,
|
76 | }): ThunkAction<void, {}, {}, { type: string }> => async (dispatch) => {
|
77 | dispatch({
|
78 | type: LIST_UPDATE_START,
|
79 | });
|
80 |
|
81 | let payload;
|
82 |
|
83 | try {
|
84 | const response = await fetch(`${window.lp.hosts.profile}/profile/api/${userId}/lists/${listId}.json`, {
|
85 | headers: {
|
86 | "Accept": "application/json",
|
87 | "Content-Type": "application/json",
|
88 | },
|
89 | method: "PATCH",
|
90 | credentials: "include",
|
91 | body: JSON.stringify(data),
|
92 | });
|
93 |
|
94 | if (response.status !== 200) {
|
95 | const errorBody = await response.text();
|
96 | const errorObj = await JSON.parse(errorBody);
|
97 | return dispatch(showToastMessage(errorObj.message, "error"));
|
98 | }
|
99 |
|
100 | payload = await response.json();
|
101 | } catch (e) {
|
102 | return dispatch(showToastMessage(messages.updateListError, "error"));
|
103 | }
|
104 |
|
105 | dispatch({
|
106 | type: LIST_UPDATE_DONE,
|
107 | payload,
|
108 | });
|
109 |
|
110 | dispatch(showToastMessage(messages.updateListSuccess));
|
111 | };
|
112 |
|
113 | export const deleteList = ({ userId, listId }: {
|
114 | userId: string;
|
115 | listId: string;
|
116 | }): ThunkAction<Promise<void>, {}, {}, { type: string }> => async (dispatch) => {
|
117 | dispatch({
|
118 | type: LIST_DELETE_START,
|
119 | });
|
120 |
|
121 | try {
|
122 | const response = await fetch(`${window.lp.hosts.profile}/profile/api/${userId}/lists/${listId}`, {
|
123 | headers: {
|
124 | Accept: "application/json",
|
125 | },
|
126 | method: "DELETE",
|
127 | credentials: "include",
|
128 | });
|
129 |
|
130 | if (response.status !== 204) {
|
131 | const errorBody = await response.text();
|
132 | const errorObj = await JSON.parse(errorBody);
|
133 | return dispatch(showToastMessage(errorObj.message, "error"));
|
134 | }
|
135 | } catch (e) {
|
136 | return dispatch(showToastMessage(messages.deleteListError, "error"));
|
137 | }
|
138 |
|
139 | dispatch({
|
140 | type: LIST_DELETE_DONE,
|
141 | });
|
142 |
|
143 | dispatch(showToastMessage(messages.deleteListSuccess));
|
144 | };
|
145 |
|
146 | export const addBookmarkToList = ({ userId, listId, data }: {
|
147 | userId: string;
|
148 | listId: string;
|
149 | data: IBookmark,
|
150 | }): ThunkAction<Promise<void>, {}, {}, { type: string }> => async (dispatch) => {
|
151 | dispatch({
|
152 | type: ADD_BOOKMARK_START,
|
153 | payload: data,
|
154 | });
|
155 |
|
156 | let payload;
|
157 |
|
158 | try {
|
159 | const response = await fetch(`${window.lp.hosts.profile}/profile/api/${userId}/lists/${listId}/entries.json`, {
|
160 | headers: {
|
161 | "Accept": "application/json",
|
162 | "Content-Type": "application/json",
|
163 | },
|
164 | method: "POST",
|
165 | credentials: "include",
|
166 | body: JSON.stringify(data),
|
167 | });
|
168 |
|
169 | if (response.status !== 200) {
|
170 | const errorBody = await response.text();
|
171 | const errorObj = await JSON.parse(errorBody);
|
172 | return dispatch(showToastMessage(errorObj.message, "error"));
|
173 | }
|
174 |
|
175 | payload = await response.json();
|
176 | } catch (e) {
|
177 | return dispatch(showToastMessage(messages.addBookmarkToListError, "error"));
|
178 | }
|
179 |
|
180 | dispatch({
|
181 | type: ADD_BOOKMARK_DONE,
|
182 | payload: {
|
183 | listId,
|
184 | bookmark: payload,
|
185 | },
|
186 | });
|
187 |
|
188 | dispatch(showToastMessage(messages.addBookmarkToListSuccess));
|
189 | };
|
190 |
|
191 | export interface IUpdateBookmarkDoneAction {
|
192 | type: typeof UPDATE_BOOKMARK_DONE;
|
193 | payload: {
|
194 | listId: string,
|
195 | bookmark: IBookmark,
|
196 | };
|
197 | }
|
198 |
|
199 | export const updateBookmark = ({ userId, listId, entryId, data }: {
|
200 | userId: string;
|
201 | listId: string;
|
202 | entryId: string;
|
203 | data: IBookmark,
|
204 | }): ThunkAction<Promise<void>, {}, {}, { type: string }> => async (dispatch) => {
|
205 | dispatch({
|
206 | type: UPDATE_BOOKMARK_START,
|
207 | });
|
208 |
|
209 | let payload;
|
210 |
|
211 | try {
|
212 | const response = await fetch(
|
213 | `${window.lp.hosts.profile}/profile/api/${userId}/lists/${listId}/entries/${entryId}.json`,
|
214 | {
|
215 | headers: {
|
216 | "Accept": "application/json",
|
217 | "Content-Type": "application/json",
|
218 | },
|
219 | method: "PATCH",
|
220 | credentials: "include",
|
221 | body: JSON.stringify(data),
|
222 | });
|
223 |
|
224 | if (response.status !== 200) {
|
225 | const errorBody = await response.text();
|
226 | const errorObj = await JSON.parse(errorBody);
|
227 | return dispatch(showToastMessage(errorObj.message, "error"));
|
228 | }
|
229 |
|
230 | payload = await response.json();
|
231 | } catch (e) {
|
232 | return dispatch(showToastMessage(messages.updateBookmarkError, "error"));
|
233 | }
|
234 |
|
235 | const action: IUpdateBookmarkDoneAction = {
|
236 | type: UPDATE_BOOKMARK_DONE,
|
237 | payload: {
|
238 | listId,
|
239 | bookmark: payload,
|
240 | }
|
241 | };
|
242 |
|
243 | dispatch(action);
|
244 |
|
245 | dispatch(showToastMessage(messages.updateBookmarkSuccess));
|
246 | };
|