import JSZip from 'jszip' ; import FileSaver from 'file-saver' ; import { ComponentInt , ComponentsInt , PropInt , ChildInt , InterfacesInterface , ReducersInterface , StoreConfigInterface , ComponentStateInterface , } from '../utils/InterfaceDefinitions' ; import { LOAD_INIT_DATA , ADD_COMPONENT , ADD_CHILD , DELETE_CHILD , DELETE_COMPONENT , CHANGE_FOCUS_COMPONENT , CHANGE_FOCUS_CHILD , CHANGE_COMPONENT_FOCUS_CHILD , EXPORT_FILES , EXPORT_FILES_SUCCESS , EXPORT_FILES_ERROR , HANDLE_CLOSE , HANDLE_TRANSFORM , CREATE_APPLICATION , CREATE_APPLICATION_SUCCESS , CREATE_APPLICATION_ERROR , OPEN_EXPANSION_PANEL , DELETE_PROP , ADD_PROP , DELETE_ALL_DATA , UPDATE_HTML_ATTR , UPDATE_CHILDREN_SORT , ADD_SELECTOR , DELETE_SELECTOR , ADD_ACTION_TO_COMPONENT , DELETE_ACTION_FROM_COMPONENT , SET_REDUCER , DELETE_REDUCER , SET_INTERFACE , DELETE_INTERFACE , SET_STATE , DELETE_STATE , } from '../actionTypes/index.js' ; import { loadState } from '../localStorage' ; import createComponentFiles from '../utils/createComponentFiles.util' ; import createApplicationUtil from '../utils/createApplication.util' ; const zip = new JSZip ( ) ; export const loadInitData = ( ) => ( dispatch ) => { loadState ( ) . then ( ( data ) => dispatch ( { type : LOAD_INIT_DATA , payload : { data : data ? data . workspace : { } , } , } ) ) ; } ; export const addComponent = ( { title } ) => ( dispatch ) => { dispatch ( { type : ADD_COMPONENT , payload : { title } } ) ; } ; export const addChild = ( { title , childType , HTMLInfo , } ) => ( dispatch ) => { dispatch ( { type : ADD_CHILD , payload : { title , childType , HTMLInfo } } ) ; } ; export const deleteChild = ( childToDeleteId ? ) => ( dispatch ) => { dispatch ( { type : DELETE_CHILD , payload : childToDeleteId } ) ; } ; export const deleteComponent = ( { componentId , stateComponents , } ) => ( dispatch ) => { stateComponents . forEach ( ( parent ) => { parent . childrenArray . filter ( ( child ) => child . childComponentId === componentId ) . forEach ( ( child ) => { dispatch ( { type : DELETE_CHILD , payload : { parentId : parent . id , childId : child . childId , calledFromDeleteComponent : true , } , } ) ; } ) ; } ) ; dispatch ( { type : CHANGE_FOCUS_COMPONENT , payload : { title : 'App' } } ) ; dispatch ( { type : DELETE_COMPONENT , payload : { componentId } } ) ; } ; export const changeFocusComponent = ( { title } ) => ( dispatch ) => { dispatch ( { type : CHANGE_FOCUS_COMPONENT , payload : { title } } ) ; } ; export const changeFocusChild = ( { childId } ) => ( dispatch ) => { dispatch ( { type : CHANGE_FOCUS_CHILD , payload : { childId } } ) ; } ; export const changeComponentFocusChild = ( { componentId , childId , } ) => ( dispatch ) => { dispatch ( { type : CHANGE_COMPONENT_FOCUS_CHILD , payload : { componentId , childId } , } ) ; } ; export const exportFiles = ( { components , path , appName , exportAppBool , } ) => ( dispatch ) => { const dir = createComponentFiles ( components , path , appName , exportAppBool , zip ) ; dispatch ( { type : EXPORT_FILES_SUCCESS , payload : { status : true , dir : dir [ 0 ] } , } ) ; zip . generateAsync ( { type : "blob" } ) . then ( blob => { FileSaver . saveAs ( blob , "preducksApp.zip" ) ; } , function ( err ) { console . log ( err ) ; } ) ; } ; export const handleClose = ( ) => ( { type : HANDLE_CLOSE , payload : false , } ) ; export const handleTransform = ( componentId , childId , { x , y , width , height , } ) => ( { type : HANDLE_TRANSFORM , payload : { componentId , childId , x , y , width , height , } , } ) ; export const createApplication = ( { path , components = [ ] , genOption , appName = 'dope_exported_preducks_app' , exportAppBool , storeConfig , } ) => ( dispatch ) => { if ( genOption ) { exportAppBool = true ; dispatch ( { type : CREATE_APPLICATION , } ) ; createApplicationUtil ( { path , appName , genOption , storeConfig , zip } ) . then ( ( ) => { dispatch ( { type : CREATE_APPLICATION_SUCCESS , } ) ; dispatch ( exportFiles ( { appName , path , components , exportAppBool , } ) ) ; } ) . catch ( err => dispatch ( { type : CREATE_APPLICATION_ERROR , payload : { status : true , err } , } ) ) ; } } ; export const openExpansionPanel = ( component ) => ( { type : OPEN_EXPANSION_PANEL , payload : { component } , } ) ; export const deleteAllData = ( ) => ( { type : DELETE_ALL_DATA , } ) ; export const deleteProp = ( propId ) => ( dispatch ) => { dispatch ( { type : DELETE_PROP , payload : propId } ) ; } ; export const addProp = ( prop ) => ( { type : ADD_PROP , payload : { ... prop } , } ) ; export const updateHtmlAttr = ( { attr , value } ) => ( dispatch ) => { dispatch ( { type : UPDATE_HTML_ATTR , payload : { attr , value } , } ) ; } ; export const updateChildrenSort = ( { newSortValues } ) => ( dispatch ) => { dispatch ( { type : UPDATE_CHILDREN_SORT , payload : { newSortValues } , } ) ; } ; export const addSelector = ( name ) => ( { type : ADD_SELECTOR , payload : name , } ) ; export const deleteSelector = ( name ) => ( { type : DELETE_SELECTOR , payload : name , } ) ; export const addActionToComponent = ( name ) => ( { type : ADD_ACTION_TO_COMPONENT , payload : name , } ) ; export const deleteActionFromComponent = ( name ) => ( { type : DELETE_ACTION_FROM_COMPONENT , payload : name , } ) ; export const setReducer = ( reducer ) => ( { type : SET_REDUCER , payload : reducer , } ) ; export const deleteReducer = ( name ) => ( { type : DELETE_REDUCER , payload : name , } ) ; export const setInterface = ( userInterface ) => ( { type : SET_INTERFACE , payload : userInterface , } ) ; export const deleteInterface = ( name ) => ( { type : DELETE_INTERFACE , payload : name , } ) ; export const setState = ( state ) => ( { type : SET_STATE , payload : state , } ) ; export const deleteState = ( name ) => ( { type : DELETE_STATE , payload : name , } ) ;