2019-07-09 08:35:24 +00:00
|
|
|
import axios from 'axios';
|
|
|
|
import * as ActionTypes from '../../../constants';
|
|
|
|
import { startLoader, stopLoader } from './loader';
|
2019-09-09 16:53:54 +00:00
|
|
|
import { setToastText, showToast } from './toast';
|
2019-07-09 08:35:24 +00:00
|
|
|
|
|
|
|
const __process = (typeof global !== 'undefined' ? global : window).process;
|
|
|
|
const ROOT_URL = __process.env.API_URL;
|
|
|
|
|
2019-09-09 16:53:54 +00:00
|
|
|
const TOAST_DISPLAY_TIME_MS = 1500;
|
|
|
|
|
2019-07-09 08:35:24 +00:00
|
|
|
// eslint-disable-next-line
|
|
|
|
export function getCollections(username) {
|
|
|
|
return (dispatch) => {
|
|
|
|
dispatch(startLoader());
|
|
|
|
let url;
|
|
|
|
if (username) {
|
|
|
|
url = `${ROOT_URL}/${username}/collections`;
|
|
|
|
} else {
|
|
|
|
url = `${ROOT_URL}/collections`;
|
|
|
|
}
|
|
|
|
axios.get(url, { withCredentials: true })
|
|
|
|
.then((response) => {
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.SET_COLLECTIONS,
|
|
|
|
collections: response.data
|
|
|
|
});
|
|
|
|
dispatch(stopLoader());
|
|
|
|
})
|
|
|
|
.catch((response) => {
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.ERROR,
|
|
|
|
error: response.data
|
|
|
|
});
|
|
|
|
dispatch(stopLoader());
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
2019-07-09 16:24:09 +00:00
|
|
|
|
|
|
|
export function createCollection(collection) {
|
|
|
|
return (dispatch) => {
|
|
|
|
dispatch(startLoader());
|
|
|
|
const url = `${ROOT_URL}/collections`;
|
|
|
|
return axios.post(url, collection, { withCredentials: true })
|
|
|
|
.then((response) => {
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.CREATE_COLLECTION
|
|
|
|
});
|
|
|
|
dispatch(stopLoader());
|
|
|
|
|
2019-09-09 16:53:54 +00:00
|
|
|
const collectionName = response.data.name;
|
|
|
|
dispatch(setToastText(`Created "${collectionName}"`));
|
|
|
|
dispatch(showToast(TOAST_DISPLAY_TIME_MS));
|
|
|
|
|
2019-07-09 16:24:09 +00:00
|
|
|
return response.data;
|
|
|
|
})
|
|
|
|
.catch((response) => {
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.ERROR,
|
|
|
|
error: response.data
|
|
|
|
});
|
|
|
|
dispatch(stopLoader());
|
|
|
|
|
|
|
|
return response.data;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function addToCollection(collectionId, projectId) {
|
|
|
|
return (dispatch) => {
|
|
|
|
dispatch(startLoader());
|
|
|
|
const url = `${ROOT_URL}/collections/${collectionId}/${projectId}`;
|
|
|
|
return axios.post(url, { withCredentials: true })
|
|
|
|
.then((response) => {
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.ADD_TO_COLLECTION,
|
|
|
|
payload: response.data
|
|
|
|
});
|
|
|
|
dispatch(stopLoader());
|
|
|
|
|
2019-09-09 16:53:54 +00:00
|
|
|
const collectionName = response.data.name;
|
|
|
|
|
|
|
|
dispatch(setToastText(`Added to "${collectionName}`));
|
|
|
|
dispatch(showToast(TOAST_DISPLAY_TIME_MS));
|
|
|
|
|
2019-07-09 16:24:09 +00:00
|
|
|
return response.data;
|
|
|
|
})
|
|
|
|
.catch((response) => {
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.ERROR,
|
|
|
|
error: response.data
|
|
|
|
});
|
|
|
|
dispatch(stopLoader());
|
|
|
|
|
|
|
|
return response.data;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function removeFromCollection(collectionId, projectId) {
|
|
|
|
return (dispatch) => {
|
|
|
|
dispatch(startLoader());
|
|
|
|
const url = `${ROOT_URL}/collections/${collectionId}/${projectId}`;
|
|
|
|
return axios.delete(url, { withCredentials: true })
|
|
|
|
.then((response) => {
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.REMOVE_FROM_COLLECTION,
|
|
|
|
payload: response.data
|
|
|
|
});
|
|
|
|
dispatch(stopLoader());
|
|
|
|
|
2019-09-09 16:53:54 +00:00
|
|
|
const collectionName = response.data.name;
|
|
|
|
|
|
|
|
dispatch(setToastText(`Removed from "${collectionName}`));
|
|
|
|
dispatch(showToast(TOAST_DISPLAY_TIME_MS));
|
|
|
|
|
2019-07-09 16:24:09 +00:00
|
|
|
return response.data;
|
|
|
|
})
|
|
|
|
.catch((response) => {
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.ERROR,
|
|
|
|
error: response.data
|
|
|
|
});
|
|
|
|
dispatch(stopLoader());
|
|
|
|
|
|
|
|
return response.data;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
2019-09-16 20:58:32 +00:00
|
|
|
|
|
|
|
export function editCollection(collectionId, { name, description }) {
|
|
|
|
return (dispatch) => {
|
|
|
|
const url = `${ROOT_URL}/collections/${collectionId}`;
|
|
|
|
return axios.patch(url, { name, description }, { withCredentials: true })
|
|
|
|
.then((response) => {
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.EDIT_COLLECTION,
|
|
|
|
payload: response.data
|
|
|
|
});
|
|
|
|
return response.data;
|
|
|
|
})
|
|
|
|
.catch((response) => {
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.ERROR,
|
|
|
|
error: response.data
|
|
|
|
});
|
|
|
|
|
|
|
|
return response.data;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|