|
1 | 1 | import objectID from 'bson-objectid';
|
2 | 2 | import blobUtil from 'blob-util';
|
3 |
| -import { reset } from 'redux-form'; |
4 | 3 | import apiClient from '../../../utils/apiClient';
|
5 | 4 | import * as ActionTypes from '../../../constants';
|
6 | 5 | import { setUnsavedChanges, closeNewFolderModal, closeNewFileModal } from './ide';
|
7 | 6 | import { setProjectSavedTime } from './project';
|
| 7 | +import { createError } from './ide'; |
8 | 8 |
|
9 | 9 |
|
10 | 10 | function appendToFilename(filename, string) {
|
@@ -36,106 +36,117 @@ export function updateFileContent(id, content) {
|
36 | 36 | };
|
37 | 37 | }
|
38 | 38 |
|
39 |
| -export function createFile(formProps) { |
| 39 | +export function createFile(file, parentId) { |
| 40 | + return { |
| 41 | + type: ActionTypes.CREATE_FILE, |
| 42 | + ...file, |
| 43 | + parentId |
| 44 | + }; |
| 45 | +} |
| 46 | + |
| 47 | +export function submitFile(formProps, files, parentId, projectId) { |
| 48 | + if (projectId) { |
| 49 | + const postParams = { |
| 50 | + name: createUniqueName(formProps.name, parentId, files), |
| 51 | + url: formProps.url, |
| 52 | + content: formProps.content || '', |
| 53 | + parentId, |
| 54 | + children: [] |
| 55 | + }; |
| 56 | + return apiClient.post(`/projects/${projectId}/files`, postParams) |
| 57 | + .then(response => ({ |
| 58 | + file: response.data.updatedFile, |
| 59 | + updatedAt: response.data.project.updatedAt |
| 60 | + })); |
| 61 | + } |
| 62 | + const id = objectID().toHexString(); |
| 63 | + const file = { |
| 64 | + name: createUniqueName(formProps.name, parentId, files), |
| 65 | + id, |
| 66 | + _id: id, |
| 67 | + url: formProps.url, |
| 68 | + content: formProps.content || '', |
| 69 | + children: [] |
| 70 | + }; |
| 71 | + return Promise.resolve({ |
| 72 | + file |
| 73 | + }); |
| 74 | +} |
| 75 | + |
| 76 | +export function handleCreateFile(formProps) { |
40 | 77 | return (dispatch, getState) => {
|
41 | 78 | const state = getState();
|
| 79 | + const { files } = state; |
42 | 80 | const { parentId } = state.ide;
|
43 |
| - if (state.project.id) { |
44 |
| - const postParams = { |
45 |
| - name: createUniqueName(formProps.name, parentId, state.files), |
46 |
| - url: formProps.url, |
47 |
| - content: formProps.content || '', |
48 |
| - parentId, |
49 |
| - children: [] |
50 |
| - }; |
51 |
| - apiClient.post(`/projects/${state.project.id}/files`, postParams) |
52 |
| - .then((response) => { |
53 |
| - dispatch({ |
54 |
| - type: ActionTypes.CREATE_FILE, |
55 |
| - ...response.data.updatedFile, |
56 |
| - parentId |
57 |
| - }); |
58 |
| - dispatch(setProjectSavedTime(response.data.project.updatedAt)); |
59 |
| - dispatch(closeNewFileModal()); |
60 |
| - dispatch(reset('new-file')); |
61 |
| - // dispatch({ |
62 |
| - // type: ActionTypes.HIDE_MODAL |
63 |
| - // }); |
64 |
| - dispatch(setUnsavedChanges(true)); |
65 |
| - }) |
66 |
| - .catch((error) => { |
67 |
| - const { response } = error; |
68 |
| - dispatch({ |
69 |
| - type: ActionTypes.ERROR, |
70 |
| - error: response.data |
71 |
| - }); |
72 |
| - }); |
73 |
| - } else { |
74 |
| - const id = objectID().toHexString(); |
75 |
| - dispatch({ |
76 |
| - type: ActionTypes.CREATE_FILE, |
77 |
| - name: createUniqueName(formProps.name, parentId, state.files), |
78 |
| - id, |
79 |
| - _id: id, |
80 |
| - url: formProps.url, |
81 |
| - content: formProps.content || '', |
82 |
| - parentId, |
83 |
| - children: [] |
| 81 | + const projectId = state.project.id; |
| 82 | + return new Promise((resolve) => { |
| 83 | + submitFile(formProps, files, parentId, projectId).then((response) => { |
| 84 | + const { file, updatedAt } = response; |
| 85 | + dispatch(createFile(file, parentId)); |
| 86 | + if (updatedAt) dispatch(setProjectSavedTime(updatedAt)); |
| 87 | + dispatch(closeNewFileModal()); |
| 88 | + dispatch(setUnsavedChanges(true)); |
| 89 | + resolve(); |
| 90 | + }).catch((error) => { |
| 91 | + const { response } = error; |
| 92 | + dispatch(createError(response.data)); |
| 93 | + resolve({ error }); |
84 | 94 | });
|
85 |
| - dispatch(reset('new-file')); |
86 |
| - // dispatch({ |
87 |
| - // type: ActionTypes.HIDE_MODAL |
88 |
| - // }); |
89 |
| - dispatch(setUnsavedChanges(true)); |
90 |
| - dispatch(closeNewFileModal()); |
91 |
| - } |
| 95 | + }); |
| 96 | + }; |
| 97 | +} |
| 98 | + |
| 99 | +export function submitFolder(formProps, files, parentId, projectId) { |
| 100 | + if (projectId) { |
| 101 | + const postParams = { |
| 102 | + name: createUniqueName(formProps.name, parentId, files), |
| 103 | + content: '', |
| 104 | + children: [], |
| 105 | + parentId, |
| 106 | + fileType: 'folder' |
| 107 | + }; |
| 108 | + return apiClient.post(`/projects/${projectId}/files`, postParams) |
| 109 | + .then(response => ({ |
| 110 | + file: response.data.updatedFile, |
| 111 | + updatedAt: response.data.project.updatedAt |
| 112 | + })); |
| 113 | + } |
| 114 | + const id = objectID().toHexString(); |
| 115 | + const file = { |
| 116 | + type: ActionTypes.CREATE_FILE, |
| 117 | + name: createUniqueName(formProps.name, parentId, files), |
| 118 | + id, |
| 119 | + _id: id, |
| 120 | + content: '', |
| 121 | + // TODO pass parent id from File Tree |
| 122 | + fileType: 'folder', |
| 123 | + children: [] |
92 | 124 | };
|
| 125 | + return Promise.resolve({ |
| 126 | + file |
| 127 | + }); |
93 | 128 | }
|
94 | 129 |
|
95 |
| -export function createFolder(formProps) { |
| 130 | +export function handleCreateFolder(formProps) { |
96 | 131 | return (dispatch, getState) => {
|
97 | 132 | const state = getState();
|
| 133 | + const { files } = state; |
98 | 134 | const { parentId } = state.ide;
|
99 |
| - if (state.project.id) { |
100 |
| - const postParams = { |
101 |
| - name: createUniqueName(formProps.name, parentId, state.files), |
102 |
| - content: '', |
103 |
| - children: [], |
104 |
| - parentId, |
105 |
| - fileType: 'folder' |
106 |
| - }; |
107 |
| - apiClient.post(`/projects/${state.project.id}/files`, postParams) |
108 |
| - .then((response) => { |
109 |
| - dispatch({ |
110 |
| - type: ActionTypes.CREATE_FILE, |
111 |
| - ...response.data.updatedFile, |
112 |
| - parentId |
113 |
| - }); |
114 |
| - dispatch(setProjectSavedTime(response.data.project.updatedAt)); |
115 |
| - dispatch(closeNewFolderModal()); |
116 |
| - }) |
117 |
| - .catch((error) => { |
118 |
| - const { response } = error; |
119 |
| - dispatch({ |
120 |
| - type: ActionTypes.ERROR, |
121 |
| - error: response.data |
122 |
| - }); |
123 |
| - }); |
124 |
| - } else { |
125 |
| - const id = objectID().toHexString(); |
126 |
| - dispatch({ |
127 |
| - type: ActionTypes.CREATE_FILE, |
128 |
| - name: createUniqueName(formProps.name, parentId, state.files), |
129 |
| - id, |
130 |
| - _id: id, |
131 |
| - content: '', |
132 |
| - // TODO pass parent id from File Tree |
133 |
| - parentId, |
134 |
| - fileType: 'folder', |
135 |
| - children: [] |
| 135 | + const projectId = state.project.id; |
| 136 | + return new Promise((resolve) => { |
| 137 | + submitFolder(formProps, files, parentId, projectId).then((response) => { |
| 138 | + const { file, updatedAt } = response; |
| 139 | + dispatch(createFile(file, parentId)); |
| 140 | + if (updatedAt) dispatch(setProjectSavedTime(updatedAt)); |
| 141 | + dispatch(closeNewFolderModal()); |
| 142 | + dispatch(setUnsavedChanges(true)); |
| 143 | + resolve(); |
| 144 | + }).catch((error) => { |
| 145 | + const { response } = error; |
| 146 | + dispatch(createError(response.data)); |
| 147 | + resolve({ error }); |
136 | 148 | });
|
137 |
| - dispatch(closeNewFolderModal()); |
138 |
| - } |
| 149 | + }); |
139 | 150 | };
|
140 | 151 | }
|
141 | 152 |
|
|
0 commit comments