Skip to content

Commit 55e23df

Browse files
authored
Merge pull request #8 from oslabs-beta/brian
User can now click to add
2 parents d96f85e + f77bbca commit 55e23df

File tree

1 file changed

+37
-5
lines changed

1 file changed

+37
-5
lines changed

app/src/components/left/HTMLItem.tsx

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import { useDrag } from 'react-dnd';
1212
import { IconProps } from '@mui/material';
1313
import CodeIcon from '@mui/icons-material/Code';
1414
import * as Icons from '@mui/icons-material';
15+
import { useDispatch, useSelector } from 'react-redux';
16+
import { addChild } from '../../redux/reducers/slice/appStateSlice';
17+
import { emitEvent } from '../../helperFunctions/socket';
1518

1619
const useStyles = makeStyles({
1720
HTMLPanelItem: {
@@ -41,6 +44,8 @@ const HTMLItem: React.FC<{
4144
//load mui icons base on string parameter
4245
const IconComponent = Icons[icon];
4346

47+
const roomCode = useSelector((store: RootState) => store.roomSlice.roomCode); // current roomCode
48+
4449
const classes = useStyles();
4550

4651
const [modal, setModal] = useState(null);
@@ -118,6 +123,26 @@ const HTMLItem: React.FC<{
118123
})
119124
);
120125
};
126+
127+
const dispatch = useDispatch();
128+
129+
const handleClick = () => {
130+
const childData = {
131+
type: 'HTML Element',
132+
typeId: id,
133+
childId: null,
134+
contextParam: {
135+
allContext: []
136+
}
137+
};
138+
139+
dispatch(addChild(childData));
140+
if (roomCode) {
141+
// Emit 'addChildAction' event to the server
142+
emitEvent('addChildAction', roomCode, childData);
143+
}
144+
};
145+
121146
// updated the id's to reflect the new element types input and label
122147
return (
123148
// HTML Elements
@@ -128,6 +153,9 @@ const HTMLItem: React.FC<{
128153
style={{ borderColor: '#C6C6C6' }}
129154
className={`${classes.HTMLPanelItem} ${classes.darkThemeFontColor}`}
130155
id="HTMLItem"
156+
onClick={() => {
157+
handleClick();
158+
}}
131159
>
132160
{typeof IconComponent !== 'undefined' && (
133161
<IconComponent fontSize="small" align-items="center" />
@@ -143,17 +171,21 @@ const HTMLItem: React.FC<{
143171
style={{ borderColor: '#C6C6C6' }}
144172
className={`${classes.HTMLPanelItem} ${classes.darkThemeFontColor}`}
145173
id="HTMLItem"
174+
onClick={() => {
175+
handleClick();
176+
}}
146177
>
147178
{typeof CodeIcon !== 'undefined' && (
148-
<CodeIcon fontSize="small" align-items="center" />)}
179+
<CodeIcon fontSize="small" align-items="center" />
180+
)}
149181
{name}
150-
<button
182+
<button
151183
id="newElement"
152184
style={{ color: '#C6C6C6' }}
153185
onClick={() => deleteAllInstances(id)}
154-
>
155-
X
156-
</button>
186+
>
187+
X
188+
</button>
157189
</div>
158190
)}
159191
{modal}

0 commit comments

Comments
 (0)