Skip to content

Commit 2c08ab0

Browse files
atvanekMatteoDiter
andcommitted
Can add multiple key values to current context
Co-authored-by: Matteo <[email protected]>
1 parent 22123ef commit 2c08ab0

File tree

4 files changed

+96
-67
lines changed

4 files changed

+96
-67
lines changed

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

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,29 @@
1-
import React, { useEffect, useState, useContext } from 'react';
2-
import { useStore } from 'react-redux';
1+
import React from 'react';
32
import Divider from '@mui/material/Divider';
43
import Grid from '@mui/material/Grid';
54
import DataTable from './components/DataTable';
65
import AddDataForm from './components/AddDataForm';
76
import AddContextForm from './components/AddContextForm';
87
// import * as actions from '../../../redux/actions/actions';
98
import { Typography } from '@mui/material';
10-
import { addContext, deleteContext, addContextValues } from '../../../redux/reducers/slice/contextReducer';
9+
import {
10+
addContext,
11+
deleteContext,
12+
addContextValues
13+
} from '../../../redux/reducers/slice/contextReducer';
1114
import { useSelector, useDispatch } from 'react-redux';
1215
import { deleteElement } from '../../../redux/reducers/slice/appStateSlice';
1316
import { RootState } from '../../../redux/store';
1417

1518
const CreateContainer = () => {
16-
const defaultTableData = [{ key: 'Enter Key', value: 'Enter value' }];
17-
const state = useSelector((store:RootState) => store.contextSlice);
18-
19-
// const store = useStore();
20-
// const [state, setState] = useState([]);
21-
const [tableState, setTableState] = React.useState(defaultTableData);
19+
const state = useSelector((store: RootState) => store.contextSlice);
2220
const [contextInput, setContextInput] = React.useState(null);
23-
// const [stateContext, dispatchContext] = useContext(StateContext);
21+
const [currentContext, setCurrentContext] = React.useState(null);
22+
const currentKeyValues = state.allContext.find(
23+
(obj) => obj.name === currentContext
24+
)?.values || [{ key: 'Enter Key', value: 'Enter value' }];
2425
const dispatch = useDispatch();
2526

26-
//pulling data from redux store
27-
// useEffect(() => {
28-
29-
// setState(allContext)
30-
// // setState(store.getState().contextSlice);
31-
32-
// }, [allContext]);
33-
34-
35-
3627
//update data store when user adds a new context
3728
const handleClickSelectContext = () => {
3829
//prevent user from adding duplicate context
@@ -48,10 +39,8 @@ const CreateContainer = () => {
4839
};
4940

5041
//update data store when user add new key-value pair to context
51-
const handleClickInputData = ({ name }, { inputKey, inputValue }) => {
52-
dispatch(
53-
addContextValues({ name, inputKey, inputValue })
54-
);
42+
const handleClickInputData = (name, { inputKey, inputValue }) => {
43+
dispatch(addContextValues({ name, inputKey, inputValue }));
5544
// setState(allContext);
5645
};
5746

@@ -62,15 +51,15 @@ const CreateContainer = () => {
6251
// setState(allContext);
6352
setTableState(defaultTableData);
6453

65-
dispatch(deleteElement({id:'FAKE_ID', contextParam: state}))
54+
dispatch(deleteElement({ id: 'FAKE_ID', contextParam: state }));
6655
// dispatchContext({
6756
// type: 'DELETE ELEMENT',
6857
// payload: 'FAKE_ID'
6958
// });
7059
};
7160

7261
//re-render data table when there's new changes
73-
const renderTable = targetContext => {
62+
const renderTable = (targetContext) => {
7463
if (
7564
targetContext === null ||
7665
targetContext === undefined ||
@@ -82,6 +71,7 @@ const CreateContainer = () => {
8271
setTableState(targetContext.values);
8372
}
8473
};
74+
console.log('state.allContext', state.allContext);
8575
return (
8676
<>
8777
<Grid container display="flex" justifyContent="space-evenly">
@@ -102,6 +92,8 @@ const CreateContainer = () => {
10292
renderTable={renderTable}
10393
contextInput={contextInput}
10494
setContextInput={setContextInput}
95+
currentContext={currentContext}
96+
setCurrentContext={setCurrentContext}
10597
/>
10698
</Grid>
10799

@@ -110,6 +102,7 @@ const CreateContainer = () => {
110102
<AddDataForm
111103
handleClickInputData={handleClickInputData}
112104
contextInput={contextInput}
105+
currentContext={currentContext}
113106
/>
114107
</Grid>
115108
</Grid>
@@ -123,11 +116,14 @@ const CreateContainer = () => {
123116
>
124117
Context Data Table
125118
</Typography>
126-
<DataTable target={tableState} contextInput={contextInput} />
119+
<DataTable
120+
target={currentKeyValues}
121+
currentContext={currentContext}
122+
/>
127123
</Grid>
128124
</Grid>
129125
</>
130126
);
131127
};
132128

133-
export default CreateContainer;
129+
export default CreateContainer;

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

Lines changed: 64 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ import React, { Fragment, useState } from 'react';
22
import TextField from '@mui/material/TextField';
33
import Select from '@mui/material/Select';
44
import FormControl from '@mui/material/FormControl';
5-
import Autocomplete, { createFilterOptions } from '@mui/material/Autocomplete';
5+
import { createFilterOptions } from '@mui/material/Autocomplete';
66
import Button from '@mui/material/Button';
77
import Box from '@mui/material/Box';
88
import { MenuItem, Typography } from '@mui/material';
99
import { useSelector } from 'react-redux';
1010
import { RootState } from '../../../../redux/store';
11+
import { error } from 'console';
1112

1213
const filter = createFilterOptions();
1314

@@ -17,7 +18,9 @@ const AddContextForm = ({
1718
handleDeleteContextClick,
1819
renderTable,
1920
contextInput,
20-
setContextInput
21+
setContextInput,
22+
currentContext,
23+
setCurrentContext
2124
}) => {
2225
const { allContext } = contextStore;
2326
console.log('all contexts', allContext);
@@ -27,6 +30,7 @@ const AddContextForm = ({
2730
state: store.appState
2831
}));
2932
const color = isDarkMode ? 'white' : 'black';
33+
const [selection, setSelection] = React.useState('');
3034

3135
const handleClick = () => {
3236
if (contextInput === '' || contextInput === null) {
@@ -36,22 +40,26 @@ const AddContextForm = ({
3640
handleClickSelectContext();
3741
};
3842

39-
const onChange = (event, newValue) => {
40-
if (typeof newValue === 'string') {
41-
setContextInput({
42-
name: newValue
43-
});
44-
} else if (newValue && newValue.inputValue) {
45-
// Create a new contextInput from the user input
46-
setContextInput({
47-
name: newValue.inputValue,
48-
values: []
49-
});
50-
renderTable(newValue);
51-
} else {
52-
setContextInput(newValue);
53-
renderTable(newValue);
54-
}
43+
// const onChange = (event, newValue) => {
44+
// if (typeof newValue === 'string') {
45+
// setContextInput({
46+
// name: newValue
47+
// });
48+
// } else if (newValue && newValue.inputValue) {
49+
// // Create a new contextInput from the user input
50+
// setContextInput({
51+
// name: newValue.inputValue,
52+
// values: []
53+
// });
54+
// renderTable(newValue);
55+
// } else {)
56+
// setContextInput(newValue);
57+
// renderTable(newValue);
58+
// }
59+
// };
60+
61+
const handleChange = (e) => {
62+
setContextInput({ name: e.target.value });
5563
};
5664

5765
const filterOptions = (options, params) => {
@@ -92,7 +100,11 @@ const AddContextForm = ({
92100
);
93101

94102
const contexts = allContext.map((context) => {
95-
return <MenuItem style={{ color: 'black' }}>{context.name}</MenuItem>;
103+
return (
104+
<MenuItem style={{ color: 'black' }} value={context.name}>
105+
{context.name}
106+
</MenuItem>
107+
);
96108
});
97109

98110
return (
@@ -101,7 +113,7 @@ const AddContextForm = ({
101113
Create Context
102114
</Typography>
103115
<Box sx={{ display: 'flex', gap: 2, mb: 4 }}>
104-
<Autocomplete
116+
{/* <Autocomplete
105117
id="autoCompleteContextField"
106118
value={contextInput}
107119
onChange={onChange}
@@ -125,6 +137,24 @@ const AddContextForm = ({
125137
label="Create/Select Context"
126138
/>
127139
)}
140+
/> */}
141+
<TextField
142+
InputProps={{
143+
style: { color: 'black' }
144+
}}
145+
// {...params}
146+
// InputProps={{
147+
// ...params.InputProps,
148+
// style: { color: color }
149+
// }}
150+
onChange={handleChange}
151+
sx={{
152+
width: 425,
153+
border: '1px solid black',
154+
color: 'black !important'
155+
}}
156+
variant="filled"
157+
label="Create Context"
128158
/>
129159
<Button
130160
variant="contained"
@@ -144,23 +174,22 @@ const AddContextForm = ({
144174
<Typography style={{ color: color }} variant="h6" gutterBottom={true}>
145175
Select Context
146176
</Typography>
147-
<FormControl variant="filled">
148-
<Select
149-
style={{ border: '1px solid #0099e6', color: 'black' }}
150-
value={''}
151-
name={'test'}
152-
displayEmpty
153-
renderValue={() => 'no context selected'}
154-
MenuProps={{ disablePortal: true }}
155177

156-
// value={props.selectValue}
157-
// onChange={props.handleChange}
178+
<Select
179+
style={{ border: '1px solid #0099e6', color: 'black' }}
180+
value={currentContext}
181+
label="Select Context"
182+
MenuProps={{ disablePortal: true }}
183+
onChange={(e) => {
184+
console.log(e);
185+
setCurrentContext(e.target.value);
186+
}}
158187

159-
// inputProps={{ className: props.isThemeLight ? `${props.classes.selectInput} ${props.classes.lightThemeFontColor}` : `${props.classes.selectInput} ${props.classes.darkThemeFontColor}` }}
160-
>
161-
{contexts}
162-
</Select>
163-
</FormControl>
188+
// value={props.selectValue}
189+
// onChange={props.handleChange}
190+
>
191+
{contexts}
192+
</Select>
164193
</Fragment>
165194
);
166195
};

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ import { Typography } from '@mui/material';
66
import { useSelector } from 'react-redux';
77
import { RootState } from '../../../../redux/store';
88

9-
const AddDataForm = ({ handleClickInputData, contextInput }) => {
9+
const AddDataForm = ({
10+
handleClickInputData,
11+
contextInput,
12+
currentContext
13+
}) => {
1014
//const [contextInput, setContextInput] = React.useState(null);
1115
const defaultInputData = { inputKey: '', inputValue: '' };
1216
const [dataContext, setDataContext] = React.useState(defaultInputData);
@@ -17,7 +21,7 @@ const AddDataForm = ({ handleClickInputData, contextInput }) => {
1721
window.alert('empty key or value');
1822
return;
1923
}
20-
handleClickInputData(contextInput, dataContext);
24+
handleClickInputData(currentContext, dataContext);
2125
};
2226
const color = isDarkMode ? 'white' : 'black';
2327

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const StyledTableRow = styled(TableRow)(({ theme }) => ({
2828
}
2929
}));
3030

31-
export default function DataTable({ target, contextInput }) {
31+
export default function DataTable({ target, currentContext }) {
3232
return (
3333
<>
3434
<TableContainer component={Paper} sx={{ maxHeight: '350px' }}>
@@ -41,7 +41,7 @@ export default function DataTable({ target, contextInput }) {
4141
<TableRow>
4242
{/* <StyledTableCell>Key</StyledTableCell> */}
4343
<StyledTableCell align="center" colSpan={3}>
44-
{contextInput ? contextInput.name : 'Context Name'}
44+
{currentContext ? currentContext : 'Context Name'}
4545
</StyledTableCell>
4646
</TableRow>
4747
</TableHead>

0 commit comments

Comments
 (0)