Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b1a6fd6

Browse files
committed
Code to perform remote requests for relationships
1 parent 79e37c5 commit b1a6fd6

File tree

1 file changed

+124
-6
lines changed

1 file changed

+124
-6
lines changed

src/jsonapi.js

Lines changed: 124 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,19 +206,35 @@ export const requireResource = (resourceType, endpoint = resourceType) => {
206206
};
207207
};
208208

209-
export const fetchRelationship = (resource, relationship) => {
210-
return (dispatch, getState) => {
211-
const { axiosConfig } = getState().api.endpoint;
209+
export const fetchRelated = (resource, relationship) => {
210+
return (dispatch) => {
212211
let endpoint;
213212

214-
if (hasOwnProperties(resource, ['relationships', relationship, 'links', 'self'])) {
215-
endpoint = resource.relationships[relationship].links.self;
213+
if (hasOwnProperties(resource, ['relationships', relationship, 'links', 'related'])) {
214+
endpoint = resource.relationships[relationship].links.related;
216215
}
217216

218217
if (!endpoint) {
219-
endpoint = `${resource.type}/${resource.id}/relationships/${relationship}`;
218+
endpoint = `${resource.type}/${resource.id}/${relationship}`;
220219
}
221220

221+
return dispatch(readEndpoint(endpoint));
222+
};
223+
};
224+
225+
const getRelationshipEndpoint = (resource, relationship) => {
226+
if (hasOwnProperties(resource, ['relationships', relationship, 'links', 'self'])) {
227+
return resource.relationships[relationship].links.self;
228+
}
229+
230+
return `${resource.type}/${resource.id}/relationships/${relationship}`;
231+
};
232+
233+
export const fetchRelationship = (resource, relationship) => {
234+
return (dispatch, getState) => {
235+
const { axiosConfig } = getState().api.endpoint;
236+
const endpoint = getRelationshipEndpoint(resource, relationship);
237+
222238
dispatch(apiWillRead(endpoint));
223239

224240
return new Promise((resolve, reject) => {
@@ -238,6 +254,108 @@ export const fetchRelationship = (resource, relationship) => {
238254
};
239255
};
240256

257+
export const replaceRelationship = (resource, relationship, data) => {
258+
return (dispatch, getState) => {
259+
dispatch(apiRelationshipWillUpdate({ resource, relationship }));
260+
261+
const { axiosConfig } = getState().api.endpoint;
262+
const endpoint = getRelationshipEndpoint(resource, relationship);
263+
264+
const options = {
265+
...axiosConfig,
266+
method: 'PATCH',
267+
data: {
268+
data
269+
}
270+
};
271+
272+
return new Promise((resolve, reject) => {
273+
apiRequest(endpoint, options)
274+
.then((json) => {
275+
dispatch(apiRelationshipUpdated(json));
276+
resolve(json);
277+
})
278+
.catch((error) => {
279+
const err = error;
280+
err.resource = resource;
281+
282+
dispatch(apiRelationshipUpdateFailed(err));
283+
reject(err);
284+
});
285+
});
286+
};
287+
};
288+
289+
export const addRelationship = (resource, relationship, data) => {
290+
return (dispatch, getState) => {
291+
dispatch(apiRelationshipWillUpdate({
292+
resource,
293+
relationship
294+
}));
295+
296+
const { axiosConfig } = getState().api.endpoint;
297+
const endpoint = getRelationshipEndpoint(resource, relationship);
298+
299+
const options = {
300+
...axiosConfig,
301+
method: 'POST',
302+
data: {
303+
data
304+
}
305+
};
306+
307+
return new Promise((resolve, reject) => {
308+
apiRequest(endpoint, options)
309+
.then((json) => {
310+
dispatch(apiRelationshipUpdated(json));
311+
resolve(json);
312+
})
313+
.catch((error) => {
314+
const err = error;
315+
err.resource = resource;
316+
317+
dispatch(apiRelationshipUpdateFailed(err));
318+
reject(err);
319+
});
320+
});
321+
};
322+
};
323+
324+
export const deleteRelationship = (resource, relationship, data) => {
325+
return (dispatch, getState) => {
326+
dispatch(apiRelationshipWillDelete({
327+
resource,
328+
relationship
329+
}));
330+
331+
const { axiosConfig } = getState().api.endpoint;
332+
const endpoint = getRelationshipEndpoint(resource, relationship);
333+
334+
const options = {
335+
...axiosConfig,
336+
method: 'DELETE',
337+
data: {
338+
data
339+
}
340+
};
341+
342+
return new Promise((resolve, reject) => {
343+
apiRequest(endpoint, options)
344+
.then((json) => {
345+
dispatch(apiRelationshipDeleted(json));
346+
resolve(json);
347+
})
348+
.catch((error) => {
349+
const err = error;
350+
err.resource = resource;
351+
352+
dispatch(apiRelationshipDeleteFailed(err));
353+
reject(err);
354+
});
355+
});
356+
};
357+
};
358+
241359
// Reducers
242360
export const reducer = handleActions({
243361
[API_SET_AXIOS_CONFIG]: (state, { payload: axiosConfig }) => {

0 commit comments

Comments
 (0)