|
| 1 | +import { |
| 2 | + ComponentInt, |
| 3 | + ChildInt, |
| 4 | + ApplicationStateInt, |
| 5 | + ComponentsInt, |
| 6 | + PropInt |
| 7 | +} from "../utils/interfaces"; |
| 8 | + |
| 9 | +import { |
| 10 | + LOAD_INIT_DATA, |
| 11 | + ADD_COMPONENT, |
| 12 | + ADD_CHILD, |
| 13 | + DELETE_CHILD, |
| 14 | + DELETE_COMPONENT, |
| 15 | + CHANGE_FOCUS_COMPONENT, |
| 16 | + CHANGE_FOCUS_CHILD, |
| 17 | + CHANGE_COMPONENT_FOCUS_CHILD, |
| 18 | + EXPORT_FILES, |
| 19 | + EXPORT_FILES_SUCCESS, |
| 20 | + EXPORT_FILES_ERROR, |
| 21 | + HANDLE_CLOSE, |
| 22 | + HANDLE_TRANSFORM, |
| 23 | + CREATE_APPLICATION, |
| 24 | + CREATE_APPLICATION_SUCCESS, |
| 25 | + CREATE_APPLICATION_ERROR, |
| 26 | + OPEN_EXPANSION_PANEL, |
| 27 | + DELETE_PROP, |
| 28 | + ADD_PROP, |
| 29 | + DELETE_ALL_DATA, |
| 30 | + UPDATE_HTML_ATTR, |
| 31 | + UPDATE_CHILDREN_SORT |
| 32 | +} from "../actionTypes/index"; |
| 33 | + |
| 34 | +import { loadState } from "../localStorage"; |
| 35 | +import createFiles from "../utils/createFiles.util"; |
| 36 | +import createApplicationUtil from "../utils/createApplication.util"; |
| 37 | + |
| 38 | +export const loadInitData = () => (dispatch: any) => { |
| 39 | + loadState().then((data: any) => |
| 40 | + dispatch({ |
| 41 | + type: LOAD_INIT_DATA, |
| 42 | + payload: { |
| 43 | + data: data ? data.workspace : {} |
| 44 | + } |
| 45 | + }) |
| 46 | + ); |
| 47 | +}; |
| 48 | + |
| 49 | +export const addComponent = ({ title }: { title: string }) => ( |
| 50 | + dispatch: any |
| 51 | +) => { |
| 52 | + dispatch({ type: ADD_COMPONENT, payload: { title } }); |
| 53 | +}; |
| 54 | + |
| 55 | +export const addChild = ({ |
| 56 | + title, |
| 57 | + childType, |
| 58 | + HTMLInfo |
| 59 | +}: { |
| 60 | + title: string; |
| 61 | + childType: string; |
| 62 | + HTMLInfo: object; |
| 63 | +}) => (dispatch: any) => { |
| 64 | + dispatch({ type: ADD_CHILD, payload: { title, childType, HTMLInfo } }); |
| 65 | +}; |
| 66 | + |
| 67 | +export const deleteChild = ({}) => (dispatch: any) => { |
| 68 | + // with no payload, it will delete focusd child |
| 69 | + dispatch({ type: DELETE_CHILD, payload: {} }); |
| 70 | +}; |
| 71 | + |
| 72 | +export const deleteComponent = ({ |
| 73 | + componentId, |
| 74 | + stateComponents |
| 75 | +}: { |
| 76 | + componentId: number; |
| 77 | + stateComponents: ComponentsInt; |
| 78 | +}) => (dispatch: any) => { |
| 79 | + // find all places where the "to be delted" is a child and do what u gotta do |
| 80 | + stateComponents.forEach(parent => { |
| 81 | + parent.childrenArray |
| 82 | + .filter(child => child.childComponentId === componentId) |
| 83 | + .forEach(child => { |
| 84 | + dispatch({ |
| 85 | + type: DELETE_CHILD, |
| 86 | + payload: { |
| 87 | + parentId: parent.id, |
| 88 | + childId: child.childId, |
| 89 | + calledFromDeleteComponent: true |
| 90 | + } |
| 91 | + }); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + // change focus to APp |
| 96 | + dispatch({ type: CHANGE_FOCUS_COMPONENT, payload: { title: "App" } }); |
| 97 | + // after taking care of the children delete the component |
| 98 | + dispatch({ type: DELETE_COMPONENT, payload: { componentId } }); |
| 99 | +}; |
| 100 | + |
| 101 | +export const changeFocusComponent = ({ title }: { title: string }) => ( |
| 102 | + dispatch: any |
| 103 | +) => { |
| 104 | + dispatch({ type: CHANGE_FOCUS_COMPONENT, payload: { title } }); |
| 105 | +}; |
| 106 | + |
| 107 | +// make sure childId is being sent in |
| 108 | +export const changeFocusChild = ({ childId }: { childId: number }) => ( |
| 109 | + dispatch: any |
| 110 | +) => { |
| 111 | + dispatch({ type: CHANGE_FOCUS_CHILD, payload: { childId } }); |
| 112 | +}; |
| 113 | + |
| 114 | +export const changeComponentFocusChild = ({ |
| 115 | + componentId, |
| 116 | + childId |
| 117 | +}: { |
| 118 | + componentId: number; |
| 119 | + childId: number; |
| 120 | +}) => (dispatch: any) => { |
| 121 | + dispatch({ |
| 122 | + type: CHANGE_COMPONENT_FOCUS_CHILD, |
| 123 | + payload: { componentId, childId } |
| 124 | + }); |
| 125 | +}; |
| 126 | + |
| 127 | +export const exportFiles = ({ |
| 128 | + components, |
| 129 | + path, |
| 130 | + appName, |
| 131 | + exportAppBool |
| 132 | +}: { |
| 133 | + components: ComponentsInt; |
| 134 | + path: string; |
| 135 | + appName: string; |
| 136 | + exportAppBool: boolean; |
| 137 | +}) => (dispatch: any) => { |
| 138 | + // this dispatch sets the global state property 'loading' to true until the createFiles call resolves below |
| 139 | + dispatch({ |
| 140 | + type: EXPORT_FILES |
| 141 | + }); |
| 142 | + |
| 143 | + createFiles(components, path, appName, exportAppBool) |
| 144 | + .then(dir => |
| 145 | + dispatch({ |
| 146 | + type: EXPORT_FILES_SUCCESS, |
| 147 | + payload: { status: true, dir: dir[0] } |
| 148 | + }) |
| 149 | + ) |
| 150 | + .catch(err => |
| 151 | + dispatch({ |
| 152 | + type: EXPORT_FILES_ERROR, |
| 153 | + payload: { status: true, err } |
| 154 | + }) |
| 155 | + ); |
| 156 | +}; |
| 157 | + |
| 158 | +export const handleClose = () => ({ |
| 159 | + type: HANDLE_CLOSE, |
| 160 | + payload: false |
| 161 | +}); |
| 162 | + |
| 163 | +export const handleTransform = ( |
| 164 | + componentId: number, |
| 165 | + childId: number, |
| 166 | + { |
| 167 | + x, |
| 168 | + y, |
| 169 | + width, |
| 170 | + height |
| 171 | + }: { x: number; y: number; width: number; height: number } |
| 172 | +) => ({ |
| 173 | + type: HANDLE_TRANSFORM, |
| 174 | + payload: { |
| 175 | + componentId, |
| 176 | + childId, |
| 177 | + x, |
| 178 | + y, |
| 179 | + width, |
| 180 | + height |
| 181 | + } |
| 182 | +}); |
| 183 | + |
| 184 | +export const createApplication = ({ |
| 185 | + path, |
| 186 | + components = [], |
| 187 | + genOption, |
| 188 | + appName = "reactype_app", |
| 189 | + exportAppBool |
| 190 | +}: { |
| 191 | + path: string; |
| 192 | + components: ComponentsInt; |
| 193 | + genOption: number; |
| 194 | + appName: string; |
| 195 | + exportAppBool: boolean; |
| 196 | +}) => (dispatch: any) => { |
| 197 | + if (genOption === 0) { |
| 198 | + exportAppBool = false; |
| 199 | + dispatch( |
| 200 | + exportFiles({ |
| 201 | + appName, |
| 202 | + path, |
| 203 | + components, |
| 204 | + exportAppBool |
| 205 | + }) |
| 206 | + ); |
| 207 | + } else if (genOption) { |
| 208 | + exportAppBool = true; |
| 209 | + dispatch({ |
| 210 | + type: CREATE_APPLICATION |
| 211 | + }); |
| 212 | + createApplicationUtil({ |
| 213 | + path, |
| 214 | + appName, |
| 215 | + genOption |
| 216 | + // exportAppBool |
| 217 | + }) |
| 218 | + .then(() => { |
| 219 | + dispatch({ |
| 220 | + type: CREATE_APPLICATION_SUCCESS |
| 221 | + }); |
| 222 | + dispatch( |
| 223 | + exportFiles({ |
| 224 | + appName, |
| 225 | + path, |
| 226 | + components, |
| 227 | + exportAppBool |
| 228 | + }) |
| 229 | + ); |
| 230 | + }) |
| 231 | + .catch(err => |
| 232 | + dispatch({ |
| 233 | + type: CREATE_APPLICATION_ERROR, |
| 234 | + payload: { status: true, err } |
| 235 | + }) |
| 236 | + ); |
| 237 | + } |
| 238 | +}; |
| 239 | + |
| 240 | +export const openExpansionPanel = (component: ComponentInt) => ({ |
| 241 | + type: OPEN_EXPANSION_PANEL, |
| 242 | + payload: { component } |
| 243 | +}); |
| 244 | + |
| 245 | +export const deleteAllData = () => ({ |
| 246 | + type: DELETE_ALL_DATA |
| 247 | +}); |
| 248 | + |
| 249 | +export const deleteProp = (propId: number) => (dispatch: any) => { |
| 250 | + dispatch({ type: DELETE_PROP, payload: propId }); |
| 251 | +}; |
| 252 | + |
| 253 | +export const addProp = (prop: PropInt) => ({ |
| 254 | + type: ADD_PROP, |
| 255 | + payload: { ...prop } |
| 256 | +}); |
| 257 | + |
| 258 | +export const updateHtmlAttr = ({ |
| 259 | + attr, |
| 260 | + value |
| 261 | +}: { |
| 262 | + attr: string; |
| 263 | + value: string; |
| 264 | +}) => (dispatch: any) => { |
| 265 | + dispatch({ |
| 266 | + type: UPDATE_HTML_ATTR, |
| 267 | + payload: { attr, value } |
| 268 | + }); |
| 269 | +}; |
| 270 | + |
| 271 | +export const updateChildrenSort = ({ |
| 272 | + newSortValues |
| 273 | +}: { |
| 274 | + newSortValues: any; |
| 275 | +}) => (dispatch: any) => { |
| 276 | + dispatch({ |
| 277 | + type: UPDATE_CHILDREN_SORT, |
| 278 | + payload: { newSortValues } |
| 279 | + }); |
| 280 | +}; |
0 commit comments