fix adding files on front end
This commit is contained in:
parent
d08c7c6b94
commit
74e207553f
1 changed files with 7 additions and 9 deletions
|
@ -3,6 +3,7 @@ import axios from 'axios';
|
||||||
import blobUtil from 'blob-util';
|
import blobUtil from 'blob-util';
|
||||||
import xhr from 'xhr';
|
import xhr from 'xhr';
|
||||||
import fileType from 'file-type';
|
import fileType from 'file-type';
|
||||||
|
import objectID from 'bson-objectid';
|
||||||
|
|
||||||
const ROOT_URL = location.href.indexOf('localhost') > 0 ? 'http://localhost:8000/api' : '/api';
|
const ROOT_URL = location.href.indexOf('localhost') > 0 ? 'http://localhost:8000/api' : '/api';
|
||||||
|
|
||||||
|
@ -68,13 +69,14 @@ export function getBlobUrl(file) {
|
||||||
export function createFile(formProps) {
|
export function createFile(formProps) {
|
||||||
return (dispatch, getState) => {
|
return (dispatch, getState) => {
|
||||||
const state = getState();
|
const state = getState();
|
||||||
|
const rootFile = state.files.filter(file => file.name === 'root')[0];
|
||||||
if (state.project.id) {
|
if (state.project.id) {
|
||||||
const postParams = {
|
const postParams = {
|
||||||
name: createUniqueName(formProps.name, state.files),
|
name: createUniqueName(formProps.name, state.files),
|
||||||
url: formProps.url,
|
url: formProps.url,
|
||||||
content: formProps.content || ''
|
content: formProps.content || ''
|
||||||
// TODO pass parent id to API, once there are folders
|
// TODO pass parent id to API, once there are folders
|
||||||
parentId: '0'
|
parentId: rootFile.id
|
||||||
};
|
};
|
||||||
axios.post(`${ROOT_URL}/projects/${state.project.id}/files`, postParams, { withCredentials: true })
|
axios.post(`${ROOT_URL}/projects/${state.project.id}/files`, postParams, { withCredentials: true })
|
||||||
.then(response => {
|
.then(response => {
|
||||||
|
@ -94,23 +96,19 @@ export function createFile(formProps) {
|
||||||
error: response.data
|
error: response.data
|
||||||
}));
|
}));
|
||||||
} else {
|
} else {
|
||||||
let maxFileId = 0;
|
|
||||||
state.files.forEach(file => {
|
|
||||||
if (parseInt(file.id, 10) > maxFileId) {
|
|
||||||
maxFileId = parseInt(file.id, 10);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
if (formProps.url) {
|
if (formProps.url) {
|
||||||
getBlobUrl(formProps)(dispatch);
|
getBlobUrl(formProps)(dispatch);
|
||||||
}
|
}
|
||||||
|
const id = objectID().toHexString();
|
||||||
dispatch({
|
dispatch({
|
||||||
type: ActionTypes.CREATE_FILE,
|
type: ActionTypes.CREATE_FILE,
|
||||||
name: createUniqueName(formProps.name, state.files),
|
name: createUniqueName(formProps.name, state.files),
|
||||||
id: `${maxFileId + 1}`,
|
id,
|
||||||
|
_id: id,
|
||||||
url: formProps.url,
|
url: formProps.url,
|
||||||
content: formProps.content || ''
|
content: formProps.content || ''
|
||||||
// TODO pass parent id from File Tree
|
// TODO pass parent id from File Tree
|
||||||
parentId: '0'
|
parentId: rootFile.id
|
||||||
});
|
});
|
||||||
dispatch({
|
dispatch({
|
||||||
type: ActionTypes.HIDE_MODAL
|
type: ActionTypes.HIDE_MODAL
|
||||||
|
|
Loading…
Reference in a new issue