2016-06-22 19:58:23 +00:00
|
|
|
import * as ActionTypes from '../../../constants';
|
2016-06-23 22:29:55 +00:00
|
|
|
import { browserHistory } from 'react-router';
|
|
|
|
import axios from 'axios';
|
2016-07-15 17:11:50 +00:00
|
|
|
import JSZip from 'jszip';
|
2016-07-20 19:33:37 +00:00
|
|
|
import JSZipUtils from 'jszip-utils';
|
2016-07-15 17:11:50 +00:00
|
|
|
import { saveAs } from 'file-saver';
|
2016-07-20 04:51:27 +00:00
|
|
|
import { getBlobUrl } from './files';
|
2016-07-20 19:33:37 +00:00
|
|
|
import async from 'async';
|
2016-06-20 17:29:32 +00:00
|
|
|
|
|
|
|
const ROOT_URL = location.href.indexOf('localhost') > 0 ? 'http://localhost:8000/api' : '/api';
|
|
|
|
|
2016-07-19 22:26:46 +00:00
|
|
|
export function getProjectBlobUrls() {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
const state = getState();
|
|
|
|
state.files.forEach(file => {
|
|
|
|
if (file.url) {
|
2016-07-20 04:51:27 +00:00
|
|
|
getBlobUrl(file)(dispatch);
|
2016-07-19 22:26:46 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-06-20 17:29:32 +00:00
|
|
|
export function getProject(id) {
|
2016-07-19 22:26:46 +00:00
|
|
|
return (dispatch, getState) => {
|
2016-06-23 22:29:55 +00:00
|
|
|
axios.get(`${ROOT_URL}/projects/${id}`, { withCredentials: true })
|
|
|
|
.then(response => {
|
2016-08-17 22:13:17 +00:00
|
|
|
// browserHistory.push(`/projects/${id}`);
|
2016-06-23 22:29:55 +00:00
|
|
|
dispatch({
|
2016-06-28 23:35:56 +00:00
|
|
|
type: ActionTypes.SET_PROJECT,
|
2016-06-29 16:52:16 +00:00
|
|
|
project: response.data,
|
2016-07-08 18:57:22 +00:00
|
|
|
files: response.data.files,
|
2016-07-15 15:54:47 +00:00
|
|
|
selectedFile: response.data.selectedFile,
|
|
|
|
owner: response.data.user
|
2016-06-23 22:29:55 +00:00
|
|
|
});
|
2016-07-19 22:26:46 +00:00
|
|
|
getProjectBlobUrls()(dispatch, getState);
|
2016-06-23 22:29:55 +00:00
|
|
|
})
|
|
|
|
.catch(response => dispatch({
|
|
|
|
type: ActionTypes.ERROR,
|
|
|
|
error: response.data
|
|
|
|
}));
|
|
|
|
};
|
2016-06-20 17:29:32 +00:00
|
|
|
}
|
|
|
|
|
2016-08-15 16:42:13 +00:00
|
|
|
export function setProjectName(name) {
|
2016-06-23 22:29:55 +00:00
|
|
|
return {
|
|
|
|
type: ActionTypes.SET_PROJECT_NAME,
|
|
|
|
name
|
|
|
|
};
|
2016-06-20 17:29:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function saveProject() {
|
2016-06-23 22:29:55 +00:00
|
|
|
return (dispatch, getState) => {
|
|
|
|
const state = getState();
|
2016-08-12 17:31:34 +00:00
|
|
|
if (state.user.id && state.project.owner && state.project.owner.id !== state.user.id) {
|
|
|
|
return;
|
|
|
|
}
|
2016-06-23 22:29:55 +00:00
|
|
|
const formParams = Object.assign({}, state.project);
|
2016-07-08 18:57:22 +00:00
|
|
|
formParams.files = [...state.files];
|
2016-06-28 23:35:56 +00:00
|
|
|
if (state.project.id) {
|
|
|
|
axios.put(`${ROOT_URL}/projects/${state.project.id}`, formParams, { withCredentials: true })
|
2016-06-23 22:29:55 +00:00
|
|
|
.then(() => {
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.PROJECT_SAVE_SUCCESS
|
2016-06-28 23:35:56 +00:00
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch((response) => dispatch({
|
|
|
|
type: ActionTypes.PROJECT_SAVE_FAIL,
|
|
|
|
error: response.data
|
|
|
|
}));
|
2016-06-23 22:29:55 +00:00
|
|
|
} else {
|
2016-07-08 18:57:22 +00:00
|
|
|
// this might be unnecessary, but to prevent collisions in mongodb
|
|
|
|
formParams.files.map(file => {
|
|
|
|
const newFile = Object.assign({}, file);
|
|
|
|
delete newFile.id;
|
|
|
|
return newFile;
|
|
|
|
});
|
2016-06-23 22:29:55 +00:00
|
|
|
axios.post(`${ROOT_URL}/projects`, formParams, { withCredentials: true })
|
|
|
|
.then(response => {
|
|
|
|
browserHistory.push(`/projects/${response.data.id}`);
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.NEW_PROJECT,
|
|
|
|
name: response.data.name,
|
|
|
|
id: response.data.id,
|
2016-07-15 15:54:47 +00:00
|
|
|
owner: response.data.user,
|
2016-07-08 18:57:22 +00:00
|
|
|
selectedFile: response.data.selectedFile,
|
2016-07-06 21:29:07 +00:00
|
|
|
files: response.data.files
|
2016-06-23 22:29:55 +00:00
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(response => dispatch({
|
|
|
|
type: ActionTypes.PROJECT_SAVE_FAIL,
|
|
|
|
error: response.data
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
};
|
2016-06-20 17:29:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function createProject() {
|
2016-06-23 22:29:55 +00:00
|
|
|
return (dispatch) => {
|
|
|
|
axios.post(`${ROOT_URL}/projects`, {}, { withCredentials: true })
|
|
|
|
.then(response => {
|
|
|
|
browserHistory.push(`/projects/${response.data.id}`);
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.NEW_PROJECT,
|
|
|
|
name: response.data.name,
|
|
|
|
id: response.data.id,
|
2016-07-15 15:54:47 +00:00
|
|
|
owner: response.data.user,
|
2016-07-08 18:57:22 +00:00
|
|
|
selectedFile: response.data.selectedFile,
|
2016-07-06 21:29:07 +00:00
|
|
|
files: response.data.files
|
2016-06-23 22:29:55 +00:00
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(response => dispatch({
|
|
|
|
type: ActionTypes.PROJECT_SAVE_FAIL,
|
|
|
|
error: response.data
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
}
|
2016-07-15 17:11:50 +00:00
|
|
|
|
|
|
|
export function exportProjectAsZip() {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
const state = getState();
|
|
|
|
const zip = new JSZip();
|
2016-07-20 19:33:37 +00:00
|
|
|
async.each(state.files, (file, cb) => {
|
|
|
|
console.log(file);
|
|
|
|
if (file.url) {
|
|
|
|
JSZipUtils.getBinaryContent(file.url, (err, data) => {
|
|
|
|
zip.file(file.name, data, { binary: true });
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
zip.file(file.name, file.content);
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
}, err => {
|
|
|
|
if (err) console.log(err);
|
|
|
|
zip.generateAsync({ type: 'blob' }).then((content) => {
|
|
|
|
saveAs(content, `${state.project.name}.zip`);
|
|
|
|
});
|
2016-07-15 17:11:50 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-08-12 16:45:26 +00:00
|
|
|
export function newProject() {
|
|
|
|
browserHistory.push('/');
|
|
|
|
return {
|
|
|
|
type: ActionTypes.RESET_PROJECT
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-07-15 17:36:33 +00:00
|
|
|
export function cloneProject() {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
const state = getState();
|
|
|
|
const formParams = Object.assign({}, { name: state.project.name }, { files: state.files });
|
|
|
|
axios.post(`${ROOT_URL}/projects`, formParams, { withCredentials: true })
|
|
|
|
.then(response => {
|
|
|
|
browserHistory.push(`/projects/${response.data.id}`);
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.NEW_PROJECT,
|
|
|
|
name: response.data.name,
|
|
|
|
id: response.data.id,
|
|
|
|
owner: response.data.user,
|
|
|
|
selectedFile: response.data.selectedFile,
|
|
|
|
files: response.data.files
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(response => dispatch({
|
|
|
|
type: ActionTypes.PROJECT_SAVE_FAIL,
|
|
|
|
error: response.data
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-08-15 16:42:13 +00:00
|
|
|
export function showEditProjectName() {
|
|
|
|
return {
|
|
|
|
type: ActionTypes.SHOW_EDIT_PROJECT_NAME
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function hideEditProjectName() {
|
|
|
|
return {
|
|
|
|
type: ActionTypes.HIDE_EDIT_PROJECT_NAME
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|