UNPKG

@lonelyplanet/dotcom-core

Version:

66 lines (57 loc) 1.8 kB
import * as fetch from "isomorphic-fetch"; import { showToastMessage, hideToastMessage } from "./toast"; import { LIST_UPDATE_DONE, LIST_DELETE_DONE, } from "../constants/bookmark"; export const updateList = ({ userId, listId, data }) => async (dispatch) => { try { const response = await fetch(`${window.lp.hosts.profile}/profile/api/${userId}/lists/${listId}.json`, { headers: { accept: "application/json", "Content-Type": "application/json", }, method: "PATCH", credentials: "include", body: JSON.stringify(data), }); if (response.status !== 200) { const errorBody = await response.text(); const errorObj = await JSON.parse(errorBody); return dispatch(showToastMessage(errorObj.message, "error")); } } catch (e) { return dispatch(showToastMessage("There was an error updating your list", "error")); } dispatch({ type: LIST_UPDATE_DONE, payload: { userId, listId, data, }, }); dispatch(showToastMessage("Your list has been updated")); }; const deleteList = ({ userId, listId }) => async (dispatch) => { try { const response = await fetch(`${window.lp.hosts.profile}/profile/api/${userId}/lists/${listId}`, { headers: { accept: "application/json", }, method: "DELETE", credentials: "include", }); if (response.status !== 200) { const errorBody = await response.text(); const errorObj = await JSON.parse(errorBody); return dispatch(showToastMessage(errorObj.message, "error")); } } catch (e) { return dispatch(showToastMessage("There was an error deleting your list", "error")); } dispatch({ type: LIST_DELETE_DONE, }); dispatch(showToastMessage("Your list has been deleted")); };