Skip to content

Commit 3979fc6

Browse files
committed
gave our usestatemodal the functionality to dynamically pass state into an html element's attribute.
Co-authored-by: Crys Lim [email protected]
1 parent 31f5600 commit 3979fc6

File tree

3 files changed

+52
-17
lines changed

3 files changed

+52
-17
lines changed

app/src/components/bottom/UseStateModal.tsx

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,47 @@ import Modal from '@material-ui/core/Modal';
33
import StateContext from '../../context/context';
44
import TableStateProps from '../right/TableStateProps';
55

6-
function UseStateModal() {
7-
6+
// TODO: typescript interface or type check
7+
function UseStateModal({ updateAttributeWithState, attributeToChange, childId }) {
88
const [open, setOpen] = useState(false);
99

10-
// choose state source
10+
// TODO: choose state source
11+
const componentProviderId = 1; // for now set to App
1112

12-
// state table
13+
// selectHandler function to pass into TableStateProps
14+
// get id of selected component
15+
// access the ChildElement
1316

17+
// return the selected state's ID back so the value of it can be updated in the customizationpanel. to the assign the value of selected state to attribute tied to useState button (currently just text)
18+
// attribute to change as parameter for UseStateModal
1419
const body = (
1520
<div className="useState-position">
1621
<div className="useState-header">
1722
<span>Choose State Source</span>
18-
<button style={{ padding: '1px', float: 'right' }} onClick={() => setOpen(false)}>X</button>
23+
<button
24+
style={{ padding: '1px', float: 'right' }}
25+
onClick={() => setOpen(false)}
26+
>
27+
X
28+
</button>
1929
</div>
2030
<div className="useState-window">
2131
<div className="useState-dropdown">
22-
<button >Choose Stateful Component</button>
32+
<button>Choose Stateful Component</button>
2333
<div>
2434
<a href="#">component 1</a>
2535
<a href="#">component 2</a>
2636
</div>
2737
</div>
2838
<div className="useState-stateDisplay">
29-
<TableStateProps selectHandler={() => console.log('selectHandler')} deleteHandler={() => console.log('deleteHandler')} isThemeLight={true}/>
39+
<TableStateProps
40+
selectHandler={(table) => {
41+
console.log('table.row.id',table.row.id);
42+
updateAttributeWithState(attributeToChange, componentProviderId, table.row.id)
43+
}}
44+
deleteHandler={() => func()}
45+
isThemeLight={true}
46+
/>
3047
</div>
3148
</div>
3249
</div>
@@ -35,13 +52,9 @@ function UseStateModal() {
3552
return (
3653
<div>
3754
<button onClick={() => setOpen(true)}>Use State</button>
38-
<Modal
39-
open={open}
40-
>
41-
{body}
42-
</Modal>
55+
<Modal open={open}>{body}</Modal>
4356
</div>
44-
)
57+
);
4558
}
4659

4760
export default UseStateModal;

app/src/containers/CustomizationPanel.tsx

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,22 @@ const CustomizationPanel = ({ isThemeLight }): JSX.Element => {
204204
return isLinked;
205205
};
206206

207+
// function to pass into UseStateModal to use state to update attribute
208+
const updateAttributeWithState = (attributeName, componentProviderId, statePropsId) => {
209+
210+
// get the stateProps of the componentProvider
211+
const currentComponent = state.components[componentProviderId - 1];
212+
const currentComponentProps = currentComponent.stateProps;
213+
const newInput = currentComponentProps[statePropsId - 1].value;
214+
215+
if (attributeName === 'compText') setCompText(newInput);
216+
if (attributeName === 'compLink') setCompLink(newInput);
217+
218+
// TODO: set something to signify that state was used
219+
// so it can be handled in generateCode
220+
221+
}
222+
207223
// dispatch to 'UPDATE CSS' called when save button is clicked,
208224
// passing in style object constructed from all changed input values
209225
const handleSave = (): Object => {
@@ -299,7 +315,6 @@ const CustomizationPanel = ({ isThemeLight }): JSX.Element => {
299315
key={'not delete'}
300316
button
301317
onClick={closeModal}
302-
ƒ
303318
style={{
304319
border: '1px solid #3f51b5',
305320
marginBottom: '2%',
@@ -518,7 +533,6 @@ const CustomizationPanel = ({ isThemeLight }): JSX.Element => {
518533
</div>
519534
</div>
520535
<div>
521-
522536
<div className={classes.configRow}>
523537
<div
524538
className={
@@ -547,7 +561,10 @@ const CustomizationPanel = ({ isThemeLight }): JSX.Element => {
547561
</FormControl>
548562
</div>
549563
<div>
550-
<UseStateModal />
564+
<UseStateModal
565+
updateAttributeWithState={updateAttributeWithState}
566+
attributeToChange="compText"
567+
childId={state.canvasFocus.childId}/>
551568
</div>
552569
</div>
553570
<div className={classes.configRow}>
@@ -577,6 +594,11 @@ const CustomizationPanel = ({ isThemeLight }): JSX.Element => {
577594
/>
578595
</FormControl>
579596
</div>
597+
<div>
598+
<UseStateModal
599+
updateAttributeWithState={updateAttributeWithState} attributeToChange="compLink"
600+
childId={state.canvasFocus.childId}/>
601+
</div>
580602
</div>
581603
<div className={classes.configRow}>
582604
<div

app/src/interfaces/Interfaces.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export interface Component {
3636
isPage: boolean;
3737
past: any[];
3838
future: any[];
39-
stateProps: StateProp[]; // state: [ { key: value, type }, {key: value, type}, {key: value, type} ]
39+
stateProps: StateProp[]; // state: [ { id, key, value, type }, ...]
4040
annotations?: string;
4141
useStateCodes: string[];
4242
}

0 commit comments

Comments
 (0)