Skip to content

Commit d6bb891

Browse files
committed
integrated code preview functionality for adding context
Co-authored-by: Sal Saluga [email protected] Co-authored-by: Ken Bains [email protected] Co-authored-by: Bianca Picasso [email protected]
1 parent 387a1d7 commit d6bb891

File tree

11 files changed

+358
-180
lines changed

11 files changed

+358
-180
lines changed

app/src/components/App.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,16 @@ export const App = (): JSX.Element => {
4949
} else {
5050
console.log(
5151
'No user project found in localforage, setting initial state blank'
52-
);
53-
}
54-
});
55-
}
56-
}, []);
52+
);
53+
}
54+
});
55+
}
56+
}, []);
5757
useEffect(() => {
5858
// provide config properties to legacy projects so new edits can be auto saved
5959
if (state.config === undefined) {
60-
state.config = {saveFlag:true, saveTimer:false};
61-
};
60+
state.config = { saveFlag: true, saveTimer: false };
61+
}
6262
// New project save configuration to optimize server load and minimize Ajax requests
6363
if (state.config.saveFlag) {
6464
state.config.saveFlag = false;
@@ -82,7 +82,7 @@ export const App = (): JSX.Element => {
8282
state.config.saveFlag = true;
8383
}, 15000);
8484
}
85-
}, [state])
85+
}, [state]);
8686
return (
8787
<div className="app">
8888
<DndProvider backend={HTML5Backend}>

app/src/components/ContextAPIManager/AssignTab/AssignContainer.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const AssignContainer = () => {
2626
}, []);
2727

2828
const renderTable = targetContext => {
29-
if (!targetContext.values) {
29+
if (targetContext === null || !targetContext.values) {
3030
setTableState(defaultTableData);
3131
} else {
3232
setTableState(targetContext.values);
@@ -37,12 +37,17 @@ const AssignContainer = () => {
3737
//target Component is main
3838

3939
const listOfContexts = [];
40-
if (!Array.isArray(state)) {
40+
if (
41+
!Array.isArray(state) &&
42+
targetComponent !== null &&
43+
targetComponent.name
44+
) {
4145
state.allContext.forEach(context => {
4246
if (context.components.includes(targetComponent.name)) {
4347
listOfContexts.push(context.name);
4448
}
4549
});
50+
console.log('setting component table with ', listOfContexts);
4651
setComponentTable(listOfContexts);
4752
}
4853
};

app/src/components/ContextAPIManager/CreateTab/CreateContainer.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,10 @@ const CreateContainer = () => {
2727
for (let i = 0; i < state.allContext.length; i += 1) {
2828
if (state.allContext[i].name === contextInput.name) {
2929
return;
30-
}
30+
}
3131
}
3232
dispatch(actions.addContextActionCreator(contextInput));
3333
setState(store.getState().contextSlice);
34-
3534
};
3635

3736
const handleClickInputData = ({ name }, { inputKey, inputValue }) => {
@@ -48,7 +47,12 @@ const CreateContainer = () => {
4847
};
4948

5049
const renderTable = targetContext => {
51-
if (!targetContext.values) {
50+
if (
51+
targetContext === null ||
52+
targetContext === undefined ||
53+
!targetContext.values
54+
) {
55+
// if (targetContext === null || targetContext === undefined) {
5256
setTableState(defaultTableData);
5357
} else {
5458
setTableState(targetContext.values);

app/src/components/ContextAPIManager/CreateTab/components/AddContextForm.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import React, { Fragment, useState, useEffect } from 'react';
1+
import React, { Fragment, useState, useEffect, useContext } from 'react';
22
import TextField from '@mui/material/TextField';
33
import Autocomplete, { createFilterOptions } from '@mui/material/Autocomplete';
44
import Button from '@mui/material/Button';
55
import Box from '@mui/material/Box';
66
import { Typography } from '@mui/material';
7+
import StateContext from '../../../../context/context';
78

89
const filter = createFilterOptions();
910

@@ -17,10 +18,17 @@ const AddContextForm = ({
1718
}) => {
1819
const { allContext } = contextStore;
1920
const [btnDisabled, setBtnDisabled] = useState(false);
21+
const [state, dispatch] = useContext(StateContext);
2022

2123
const handleClick = () => {
2224
if (contextInput === '' || contextInput === null) return;
2325
handleClickSelectContext();
26+
27+
//need to trigger the generate code functionality to update the code preview tab. Sending dummy data to trigger with a DELELTE ELEMENT dispatch method
28+
dispatch({
29+
type: 'DELETE ELEMENT',
30+
payload: 'FAKE_ID'
31+
});
2432
};
2533

2634
const onChange = (event, newValue) => {

app/src/components/bottom/CodePreview.tsx

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,24 @@ import store from '../../redux/store';
1919
const CodePreview: React.FC<{
2020
theme: string | null;
2121
setTheme: any | null;
22-
}> = ({ theme, setTheme }) => {
23-
24-
22+
}> = ({ theme, setTheme }) => {
2523
const ref = useRef<any>();
26-
24+
2725
/**
28-
* Starts the Web Assembly service.
29-
*/
26+
* Starts the Web Assembly service.
27+
*/
3028
const startService = async () => {
3129
ref.current = await esbuild.startService({
3230
worker: true,
33-
wasmURL: 'https://unpkg.com/[email protected]/esbuild.wasm',
34-
})
35-
}
31+
wasmURL: 'https://unpkg.com/[email protected]/esbuild.wasm'
32+
});
33+
};
3634

3735
const wrapper = useRef();
3836
const dimensions = useResizeObserver(wrapper);
39-
const {height } =
40-
dimensions || 0;
37+
const { height } = dimensions || 0;
4138

42-
const [state,] = useContext(StateContext);
39+
const [state] = useContext(StateContext);
4340
const [, setDivHeight] = useState(0);
4441
let currentComponent = state.components.find(
4542
(elem: Component) => elem.id === state.canvasFocus.componentId
@@ -53,49 +50,51 @@ const CodePreview: React.FC<{
5350

5451
useEffect(() => {
5552
setDivHeight(height);
56-
}, [height])
53+
}, [height]);
5754

5855
useEffect(() => {
59-
60-
setInput(currentComponent.code);
61-
store.dispatch({type: "CODE_PREVIEW_INPUT", payload: currentComponent.code});
62-
}, [state.components])
56+
setInput(currentComponent.code);
57+
store.dispatch({
58+
type: 'CODE_PREVIEW_INPUT',
59+
payload: currentComponent.code
60+
});
61+
}, [state.components]);
6362

6463
/**
65-
* Handler thats listens to changes in code editor
66-
* @param {string} data - Code entered by the user
67-
*/
68-
const handleChange = async (data) => {
64+
* Handler thats listens to changes in code editor
65+
* @param {string} data - Code entered by the user
66+
*/
67+
const handleChange = async data => {
6968
setInput(data);
70-
store.dispatch({type: "CODE_PREVIEW_INPUT", payload: data});
71-
if(!ref.current) {
69+
store.dispatch({ type: 'CODE_PREVIEW_INPUT', payload: data });
70+
if (!ref.current) {
7271
return;
7372
}
7473
let result = await ref.current.build({
7574
entryPoints: ['index.js'],
7675
bundle: true,
7776
write: false,
78-
incremental:true,
77+
incremental: true,
7978
minify: true,
80-
plugins: [
81-
unpkgPathPlugin(),
82-
fetchPlugin(data)
83-
],
79+
plugins: [unpkgPathPlugin(), fetchPlugin(data)],
8480
define: {
8581
'process.env.NODE_ENV': '"production"',
8682
global: 'window'
8783
}
88-
})
89-
store.dispatch({type: "CODE_PREVIEW_SAVE", payload: result.outputFiles[0].text});
90-
}
84+
});
85+
store.dispatch({
86+
type: 'CODE_PREVIEW_SAVE',
87+
payload: result.outputFiles[0].text
88+
});
89+
};
9190

9291
return (
9392
<div
94-
ref={wrapper}
93+
ref={wrapper}
9594
style={{
9695
height: '100%',
9796
maxWidth: '100%',
98-
justifyContent: 'center',
97+
justifyContent: 'center'
9998
}}
10099
>
101100
<AceEditor
@@ -110,13 +109,8 @@ const CodePreview: React.FC<{
110109
fontSize={18}
111110
tabSize={2}
112111
/>
113-
114112
</div>
115113
);
116114
};
117115

118116
export default CodePreview;
119-
120-
121-
122-

app/src/components/right/ExportButton.tsx

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import React, { useState, useContext, useCallback, useEffect } from 'react';
32
import StateContext from '../../context/context';
43
import List from '@material-ui/core/List';
@@ -11,7 +10,7 @@ import createModal from './createModal';
1110
import { styleContext } from '../../containers/AppContainer';
1211
export default function ExportButton() {
1312
const [modal, setModal] = useState(null);
14-
const [state,] = useContext(StateContext);
13+
const [state] = useContext(StateContext);
1514

1615
const genOptions: string[] = [
1716
'Export components',
@@ -22,7 +21,6 @@ export default function ExportButton() {
2221
// Closes out the open modal
2322
const closeModal = () => setModal('');
2423

25-
2624
const showGenerateAppModal = () => {
2725
const children = (
2826
<List className="export-preference">
@@ -92,29 +90,31 @@ export default function ExportButton() {
9290
);
9391
};
9492

95-
const exportKeyBind = useCallback((e) => {
93+
const exportKeyBind = useCallback(e => {
9694
//Export Project
97-
(e.key === 'e' && e.metaKey || e.key === 'e' && e.ctrlKey ) ? showGenerateAppModal() : '';
95+
(e.key === 'e' && e.metaKey) || (e.key === 'e' && e.ctrlKey)
96+
? showGenerateAppModal()
97+
: '';
9898
}, []);
9999

100100
useEffect(() => {
101101
document.addEventListener('keydown', exportKeyBind);
102102
return () => {
103-
document.removeEventListener('keydown', exportKeyBind)
104-
}
103+
document.removeEventListener('keydown', exportKeyBind);
104+
};
105105
}, []);
106106
return (
107107
<div>
108-
<Button
109-
variant="contained"
110-
color="secondary"
111-
onClick={showGenerateAppModal}
112-
id="navbarButton"
113-
endIcon={<GetAppIcon/>}
114-
>
115-
EXPORT
116-
</Button>
117-
{modal}
108+
<Button
109+
variant="contained"
110+
color="secondary"
111+
onClick={showGenerateAppModal}
112+
id="navbarButton"
113+
endIcon={<GetAppIcon />}
114+
>
115+
EXPORT
116+
</Button>
117+
{modal}
118118
</div>
119119
);
120-
};
120+
}

0 commit comments

Comments
 (0)