2020-04-10 19:58:55 +02:00
|
|
|
import { browserHistory } from 'react-router';
|
2020-06-08 12:29:24 +02:00
|
|
|
import apiClient from '../../../utils/apiClient';
|
2019-07-09 10:35:24 +02:00
|
|
|
import * as ActionTypes from '../../../constants';
|
|
|
|
import { startLoader, stopLoader } from './loader';
|
2019-09-09 18:53:54 +02:00
|
|
|
import { setToastText, showToast } from './toast';
|
2019-07-09 10:35:24 +02:00
|
|
|
|
|
|
|
|
2019-09-09 18:53:54 +02:00
|
|
|
const TOAST_DISPLAY_TIME_MS = 1500;
|
|
|
|
|
2019-07-09 10:35:24 +02:00
|
|
|
// eslint-disable-next-line
|
|
|
|
export function getCollections(username) {
|
|
|
|
return (dispatch) => {
|
|
|
|
dispatch(startLoader());
|
|
|
|
let url;
|
|
|
|
if (username) {
|
2020-06-08 12:29:24 +02:00
|
|
|
url = `/${username}/collections`;
|
2019-07-09 10:35:24 +02:00
|
|
|
} else {
|
2020-06-08 12:29:24 +02:00
|
|
|
url = '/collections';
|
2019-07-09 10:35:24 +02:00
|
|
|
}
|
2020-07-31 14:04:48 +02:00
|
|
|
console.log(url);
|
2020-06-08 12:29:24 +02:00
|
|
|
apiClient.get(url)
|
2019-07-09 10:35:24 +02:00
|
|
|
.then((response) => {
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.SET_COLLECTIONS,
|
|
|
|
collections: response.data
|
|
|
|
});
|
|
|
|
dispatch(stopLoader());
|
|
|
|
})
|
2020-04-25 16:48:39 +02:00
|
|
|
.catch((error) => {
|
|
|
|
const { response } = error;
|
2019-07-09 10:35:24 +02:00
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.ERROR,
|
|
|
|
error: response.data
|
|
|
|
});
|
|
|
|
dispatch(stopLoader());
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
2019-07-09 18:24:09 +02:00
|
|
|
|
|
|
|
export function createCollection(collection) {
|
|
|
|
return (dispatch) => {
|
|
|
|
dispatch(startLoader());
|
2020-06-08 12:29:24 +02:00
|
|
|
const url = '/collections';
|
|
|
|
return apiClient.post(url, collection)
|
2019-07-09 18:24:09 +02:00
|
|
|
.then((response) => {
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.CREATE_COLLECTION
|
|
|
|
});
|
|
|
|
dispatch(stopLoader());
|
|
|
|
|
2020-04-10 19:58:55 +02:00
|
|
|
const newCollection = response.data;
|
|
|
|
dispatch(setToastText(`Created "${newCollection.name}"`));
|
2019-09-09 18:53:54 +02:00
|
|
|
dispatch(showToast(TOAST_DISPLAY_TIME_MS));
|
|
|
|
|
2020-04-10 19:58:55 +02:00
|
|
|
const pathname = `/${newCollection.owner.username}/collections/${newCollection.id}`;
|
|
|
|
const location = { pathname, state: { skipSavingPath: true } };
|
|
|
|
|
|
|
|
browserHistory.push(location);
|
2019-07-09 18:24:09 +02:00
|
|
|
})
|
2020-04-25 16:48:39 +02:00
|
|
|
.catch((error) => {
|
|
|
|
const { response } = error;
|
2020-04-10 19:58:55 +02:00
|
|
|
console.error('Error creating collection', response.data);
|
2019-07-09 18:24:09 +02:00
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.ERROR,
|
|
|
|
error: response.data
|
|
|
|
});
|
|
|
|
dispatch(stopLoader());
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function addToCollection(collectionId, projectId) {
|
|
|
|
return (dispatch) => {
|
|
|
|
dispatch(startLoader());
|
2020-06-08 12:29:24 +02:00
|
|
|
const url = `/collections/${collectionId}/${projectId}`;
|
|
|
|
return apiClient.post(url)
|
2019-07-09 18:24:09 +02:00
|
|
|
.then((response) => {
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.ADD_TO_COLLECTION,
|
|
|
|
payload: response.data
|
|
|
|
});
|
|
|
|
dispatch(stopLoader());
|
|
|
|
|
2019-09-09 18:53:54 +02:00
|
|
|
const collectionName = response.data.name;
|
|
|
|
|
|
|
|
dispatch(setToastText(`Added to "${collectionName}`));
|
|
|
|
dispatch(showToast(TOAST_DISPLAY_TIME_MS));
|
|
|
|
|
2019-07-09 18:24:09 +02:00
|
|
|
return response.data;
|
|
|
|
})
|
2020-04-25 16:48:39 +02:00
|
|
|
.catch((error) => {
|
|
|
|
const { response } = error;
|
2019-07-09 18:24:09 +02:00
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.ERROR,
|
|
|
|
error: response.data
|
|
|
|
});
|
|
|
|
dispatch(stopLoader());
|
|
|
|
|
|
|
|
return response.data;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function removeFromCollection(collectionId, projectId) {
|
|
|
|
return (dispatch) => {
|
|
|
|
dispatch(startLoader());
|
2020-06-08 12:29:24 +02:00
|
|
|
const url = `/collections/${collectionId}/${projectId}`;
|
|
|
|
return apiClient.delete(url)
|
2019-07-09 18:24:09 +02:00
|
|
|
.then((response) => {
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.REMOVE_FROM_COLLECTION,
|
|
|
|
payload: response.data
|
|
|
|
});
|
|
|
|
dispatch(stopLoader());
|
|
|
|
|
2019-09-09 18:53:54 +02:00
|
|
|
const collectionName = response.data.name;
|
|
|
|
|
|
|
|
dispatch(setToastText(`Removed from "${collectionName}`));
|
|
|
|
dispatch(showToast(TOAST_DISPLAY_TIME_MS));
|
|
|
|
|
2019-07-09 18:24:09 +02:00
|
|
|
return response.data;
|
|
|
|
})
|
2020-04-25 16:48:39 +02:00
|
|
|
.catch((error) => {
|
|
|
|
const { response } = error;
|
2019-07-09 18:24:09 +02:00
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.ERROR,
|
|
|
|
error: response.data
|
|
|
|
});
|
|
|
|
dispatch(stopLoader());
|
|
|
|
|
|
|
|
return response.data;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
2019-09-16 22:58:32 +02:00
|
|
|
|
|
|
|
export function editCollection(collectionId, { name, description }) {
|
|
|
|
return (dispatch) => {
|
2020-06-08 12:29:24 +02:00
|
|
|
const url = `/collections/${collectionId}`;
|
|
|
|
return apiClient.patch(url, { name, description })
|
2019-09-16 22:58:32 +02:00
|
|
|
.then((response) => {
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.EDIT_COLLECTION,
|
|
|
|
payload: response.data
|
|
|
|
});
|
|
|
|
return response.data;
|
|
|
|
})
|
2020-04-25 16:48:39 +02:00
|
|
|
.catch((error) => {
|
|
|
|
const { response } = error;
|
2019-09-16 22:58:32 +02:00
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.ERROR,
|
|
|
|
error: response.data
|
|
|
|
});
|
|
|
|
|
|
|
|
return response.data;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
2019-10-20 15:31:20 +02:00
|
|
|
|
|
|
|
export function deleteCollection(collectionId) {
|
|
|
|
return (dispatch) => {
|
2020-06-08 12:29:24 +02:00
|
|
|
const url = `/collections/${collectionId}`;
|
|
|
|
return apiClient.delete(url)
|
2019-10-20 15:31:20 +02:00
|
|
|
.then((response) => {
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.DELETE_COLLECTION,
|
|
|
|
payload: response.data,
|
|
|
|
collectionId,
|
|
|
|
});
|
|
|
|
return response.data;
|
|
|
|
})
|
2020-04-25 16:48:39 +02:00
|
|
|
.catch((error) => {
|
|
|
|
const { response } = error;
|
2019-10-20 15:31:20 +02:00
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.ERROR,
|
|
|
|
error: response.data
|
|
|
|
});
|
|
|
|
|
|
|
|
return response.data;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|