|
| 1 | +import { createComponent } from '../utils/reducer.util'; |
| 2 | +import { ComponentState } from '../types/types'; |
| 3 | +import * as types from '../types/actionTypes'; |
| 4 | +import { loadState } from '../localStorage.js'; |
| 5 | +import createFiles from '../utils/createFiles.util'; |
| 6 | +import createApplicationUtil from '../utils/createApplication.util'; |
| 7 | + |
| 8 | +// ** ACTION CREATORS ** \\ |
| 9 | + |
| 10 | +// ** openExpansionPanel just takes the id of the current component and toggles the expanded value to either true or false |
| 11 | +export const toggleExpansionPanel = (id: number) => ({ |
| 12 | + type: types.TOGGLE_EXPANSION_PANEL, |
| 13 | + payload: { id }, |
| 14 | +}); |
| 15 | + |
| 16 | +// ! Redux thunk action |
| 17 | +export const loadInitData = () => { |
| 18 | + return (dispatch: any) => { |
| 19 | + loadState().then((data: any) => dispatch({ |
| 20 | + type: types.LOAD_INIT_DATA, |
| 21 | + payload: { |
| 22 | + data: data ? data.application : {}, |
| 23 | + }, |
| 24 | + })); |
| 25 | + } |
| 26 | +}; |
| 27 | + |
| 28 | +// ! Redux thunk action |
| 29 | +// ** addComponent waits for createComponent to dispatch then createComponent returns a promise with the new component object created |
| 30 | +export const addComponent = (title: string) => { |
| 31 | + return (dispatch, getState) => { |
| 32 | + // ** grab state from our reducer to see how many components currently exist in our array |
| 33 | + const appComponentState = getState().application.components; |
| 34 | + // ** Conditionally assigning a variable componentId. If any components exist in the array find the last component's id and increment it else initialize at 1. |
| 35 | + const componentId = appComponentState.length ? appComponentState[appComponentState.length - 1].id + 1 : 1; |
| 36 | + dispatch(createComponent(componentId, title, appComponentState)) |
| 37 | + .then((component: ComponentState) => { |
| 38 | + // ** the dispatch to createComponent will return a promise. The promise will return either the new createdComponent or just the generic state object (which will have an id of 0 by default) |
| 39 | + if (component.id !== 0) { |
| 40 | + dispatch({ |
| 41 | + type: types.ADD_COMPONENT, |
| 42 | + payload: { component } |
| 43 | + }); |
| 44 | + } |
| 45 | + }) |
| 46 | + .catch((err: Error) => console.log(err)); |
| 47 | + }; |
| 48 | +} |
| 49 | + |
| 50 | +// ** updateComponent updates one of the values that can be updated from a component |
| 51 | +export const updateComponent = (id: number, update: {}) => ({ |
| 52 | + type: types.UPDATE_COMPONENT, |
| 53 | + payload: { |
| 54 | + id, |
| 55 | + ...update |
| 56 | + } |
| 57 | +}); |
| 58 | + |
| 59 | +// ** deleteComponent deletes a component from our global state component array |
| 60 | +export const deleteComponent = (id: number) => ({ |
| 61 | + type: types.DELETE_COMPONENT, |
| 62 | + payload: { id } |
| 63 | +}); |
| 64 | + |
| 65 | +export const addChild = ({ |
| 66 | + title, |
| 67 | + childType, |
| 68 | + HTMLInfo, |
| 69 | +}: { |
| 70 | +title: string; |
| 71 | +childType: string; |
| 72 | +HTMLInfo: object; |
| 73 | +}) => (dispatch: any) => { |
| 74 | + dispatch({ type: types.ADD_CHILD, payload: { title, childType, HTMLInfo } }); |
| 75 | +}; |
| 76 | + |
| 77 | +export const deleteChild = ({}) => (dispatch: any) => { |
| 78 | + // with no payload, it will delete focusd child |
| 79 | + dispatch({ type: types.DELETE_CHILD, payload: {} }); |
| 80 | +}; |
| 81 | + |
| 82 | +export const changeFocusComponent = ({ title }: { title: string }) => (dispatch: any) => { |
| 83 | + dispatch({ type: types.CHANGE_FOCUS_COMPONENT, payload: { title } }); |
| 84 | +}; |
| 85 | + |
| 86 | +// make sure childId is being sent in |
| 87 | +export const changeFocusChild = ({ childId }: { childId: number }) => (dispatch: any) => { |
| 88 | + dispatch({ type: types.CHANGE_FOCUS_CHILD, payload: { childId } }); |
| 89 | +}; |
| 90 | + |
| 91 | +export const changeComponentFocusChild = ({ |
| 92 | + componentId, |
| 93 | + childId, |
| 94 | +}: { |
| 95 | +componentId: number; |
| 96 | +childId: number; |
| 97 | +}) => (dispatch: any) => { |
| 98 | + dispatch({ |
| 99 | + type: types.CHANGE_COMPONENT_FOCUS_CHILD, |
| 100 | + payload: { componentId, childId }, |
| 101 | + }); |
| 102 | +}; |
| 103 | + |
| 104 | +export const exportFiles = ({ |
| 105 | + components, |
| 106 | + path, |
| 107 | + appName, |
| 108 | + exportAppBool, |
| 109 | +}: { |
| 110 | +components: ComponentState; |
| 111 | +path: string; |
| 112 | +appName: string; |
| 113 | +exportAppBool: boolean; |
| 114 | +}) => (dispatch: any) => { |
| 115 | + // this dispatch sets the global state property 'loading' to true until the createFiles call resolves below |
| 116 | + dispatch({ |
| 117 | + type: types.EXPORT_FILES, |
| 118 | + }); |
| 119 | + |
| 120 | + createFiles(components, path, appName, exportAppBool) |
| 121 | + .then(dir => dispatch({ |
| 122 | + type: types.EXPORT_FILES_SUCCESS, |
| 123 | + payload: { status: true, dir: dir[0] }, |
| 124 | + })) |
| 125 | + .catch(err => dispatch({ |
| 126 | + type: types.EXPORT_FILES_ERROR, |
| 127 | + payload: { status: true, err }, |
| 128 | + })); |
| 129 | +}; |
| 130 | + |
| 131 | +export const handleClose = () => ({ |
| 132 | + type: types.HANDLE_CLOSE, |
| 133 | + payload: false, |
| 134 | +}); |
| 135 | + |
| 136 | +export const handleTransform = ( |
| 137 | + componentId: number, |
| 138 | + childId: number, |
| 139 | + { |
| 140 | + x, y, width, height, |
| 141 | + }: { x: number; y: number; width: number; height: number }, |
| 142 | +) => ({ |
| 143 | + type: types.HANDLE_TRANSFORM, |
| 144 | + payload: { |
| 145 | + componentId, |
| 146 | + childId, |
| 147 | + x, |
| 148 | + y, |
| 149 | + width, |
| 150 | + height, |
| 151 | + }, |
| 152 | +}); |
| 153 | + |
| 154 | +export const createApplication = ({ |
| 155 | + path, |
| 156 | + components = [], |
| 157 | + genOption, |
| 158 | + appName = 'reactype_app', |
| 159 | + exportAppBool, |
| 160 | +}: { |
| 161 | +path: string; |
| 162 | +components: ComponentState; |
| 163 | +genOption: number; |
| 164 | +appName: string; |
| 165 | +exportAppBool: boolean; |
| 166 | +}) => (dispatch: any) => { |
| 167 | + if (genOption === 0) { |
| 168 | + exportAppBool = false; |
| 169 | + dispatch( |
| 170 | + exportFiles({ |
| 171 | + appName, |
| 172 | + path, |
| 173 | + components, |
| 174 | + exportAppBool, |
| 175 | + }), |
| 176 | + ); |
| 177 | + } else if (genOption) { |
| 178 | + exportAppBool = true; |
| 179 | + dispatch({ |
| 180 | + type: types.CREATE_APPLICATION, |
| 181 | + }); |
| 182 | + createApplicationUtil({ |
| 183 | + path, |
| 184 | + appName, |
| 185 | + genOption, |
| 186 | + // exportAppBool |
| 187 | + }) |
| 188 | + .then(() => { |
| 189 | + dispatch({ |
| 190 | + type: types.CREATE_APPLICATION_SUCCESS, |
| 191 | + }); |
| 192 | + dispatch( |
| 193 | + exportFiles({ |
| 194 | + appName, |
| 195 | + path, |
| 196 | + components, |
| 197 | + exportAppBool, |
| 198 | + }), |
| 199 | + ); |
| 200 | + }) |
| 201 | + .catch(err => dispatch({ |
| 202 | + type: types.CREATE_APPLICATION_ERROR, |
| 203 | + payload: { status: true, err }, |
| 204 | + })); |
| 205 | + } |
| 206 | +}; |
| 207 | + |
| 208 | +// export const openExpansionPanel = (component: ComponentInt) => ({ |
| 209 | +// type: OPEN_EXPANSION_PANEL, |
| 210 | +// payload: { component }, |
| 211 | +// }); |
| 212 | + |
| 213 | +export const deleteAllData = () => ({ |
| 214 | + type: types.DELETE_ALL_DATA, |
| 215 | +}); |
| 216 | + |
| 217 | +export const deleteProp = (propId: number) => (dispatch: any) => { |
| 218 | + dispatch({ type: types.DELETE_PROP, payload: propId }); |
| 219 | +}; |
| 220 | + |
| 221 | +export const addProp = (prop: PropInt) => ({ |
| 222 | + type: types.ADD_PROP, |
| 223 | + payload: { ...prop }, |
| 224 | +}); |
| 225 | + |
| 226 | +export const updateHtmlAttr = ({ attr, value }: { attr: string; value: string }) => ( |
| 227 | + dispatch: any, |
| 228 | +) => { |
| 229 | + dispatch({ |
| 230 | + type: types.UPDATE_HTML_ATTR, |
| 231 | + payload: { attr, value }, |
| 232 | + }); |
| 233 | +}; |
| 234 | + |
| 235 | +export const updateChildrenSort = ({ newSortValues }: { newSortValues: any }) => ( |
| 236 | + dispatch: any, |
| 237 | +) => { |
| 238 | + dispatch({ |
| 239 | + type: types.UPDATE_CHILDREN_SORT, |
| 240 | + payload: { newSortValues }, |
| 241 | + }); |
| 242 | +}; |
0 commit comments