2016-06-22 19:58:23 +00:00
|
|
|
import * as ActionTypes from '../../../constants';
|
2016-07-13 22:53:56 +00:00
|
|
|
import axios from 'axios';
|
2016-07-20 04:51:27 +00:00
|
|
|
import blobUtil from 'blob-util';
|
2016-07-13 22:53:56 +00:00
|
|
|
|
|
|
|
const ROOT_URL = location.href.indexOf('localhost') > 0 ? 'http://localhost:8000/api' : '/api';
|
2016-06-22 19:58:23 +00:00
|
|
|
|
2016-07-07 17:50:52 +00:00
|
|
|
export function updateFileContent(name, content) {
|
2016-06-23 22:29:55 +00:00
|
|
|
return {
|
2016-07-07 17:50:52 +00:00
|
|
|
type: ActionTypes.UPDATE_FILE_CONTENT,
|
2016-06-23 22:29:55 +00:00
|
|
|
name,
|
|
|
|
content
|
|
|
|
};
|
|
|
|
}
|
2016-07-13 20:13:28 +00:00
|
|
|
|
2016-07-20 04:51:27 +00:00
|
|
|
export function getBlobUrl(file) {
|
|
|
|
return (dispatch) => {
|
|
|
|
blobUtil.imgSrcToBlob(file.url, undefined, { crossOrigin: 'Anonymous' })
|
|
|
|
.then(blobUtil.createObjectURL)
|
|
|
|
.then(objectURL => {
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.SET_BLOB_URL,
|
|
|
|
name: file.name,
|
|
|
|
blobURL: objectURL
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-07-13 22:53:56 +00:00
|
|
|
export function createFile(formProps) {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
const state = getState();
|
|
|
|
if (state.project.id) {
|
|
|
|
const postParams = {
|
2016-07-19 20:49:46 +00:00
|
|
|
name: formProps.name,
|
|
|
|
url: formProps.url
|
2016-07-13 22:53:56 +00:00
|
|
|
};
|
|
|
|
axios.post(`${ROOT_URL}/projects/${state.project.id}/files`, postParams, { withCredentials: true })
|
|
|
|
.then(response => {
|
2016-07-20 04:51:27 +00:00
|
|
|
if (response.data.url) {
|
|
|
|
getBlobUrl(response.data)(dispatch);
|
|
|
|
}
|
2016-07-13 22:53:56 +00:00
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.CREATE_FILE,
|
|
|
|
...response.data
|
|
|
|
});
|
2016-07-20 02:29:54 +00:00
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.HIDE_MODAL
|
|
|
|
});
|
2016-07-13 22:53:56 +00:00
|
|
|
})
|
|
|
|
.catch(response => dispatch({
|
|
|
|
type: ActionTypes.ERROR,
|
|
|
|
error: response.data
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
let maxFileId = 0;
|
|
|
|
state.files.forEach(file => {
|
|
|
|
if (parseInt(file.id, 10) > maxFileId) {
|
|
|
|
maxFileId = parseInt(file.id, 10);
|
|
|
|
}
|
|
|
|
});
|
2016-07-20 04:51:27 +00:00
|
|
|
if (formProps.url) {
|
|
|
|
getBlobUrl(formProps)(dispatch);
|
|
|
|
}
|
2016-07-13 22:53:56 +00:00
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.CREATE_FILE,
|
|
|
|
name: formProps.name,
|
2016-07-20 19:33:37 +00:00
|
|
|
id: `${maxFileId + 1}`,
|
|
|
|
url: formProps.url
|
2016-07-13 22:53:56 +00:00
|
|
|
});
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.HIDE_MODAL
|
|
|
|
});
|
|
|
|
}
|
2016-07-13 20:13:28 +00:00
|
|
|
};
|
|
|
|
}
|