Skip to content

Commit 6c2975b

Browse files
authored
Merge pull request #26 from oslabs-beta/context-ui
Context UI
2 parents fd85465 + 2c08ab0 commit 6c2975b

File tree

4 files changed

+109
-53
lines changed

4 files changed

+109
-53
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: 77 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import React, { Fragment, useState } from 'react';
22
import TextField from '@mui/material/TextField';
3-
import Autocomplete, { createFilterOptions } from '@mui/material/Autocomplete';
3+
import Select from '@mui/material/Select';
4+
import FormControl from '@mui/material/FormControl';
5+
import { createFilterOptions } from '@mui/material/Autocomplete';
46
import Button from '@mui/material/Button';
57
import Box from '@mui/material/Box';
6-
import { Typography } from '@mui/material';
8+
import { MenuItem, Typography } from '@mui/material';
79
import { useSelector } from 'react-redux';
810
import { RootState } from '../../../../redux/store';
11+
import { error } from 'console';
912

1013
const filter = createFilterOptions();
1114

@@ -15,15 +18,19 @@ const AddContextForm = ({
1518
handleDeleteContextClick,
1619
renderTable,
1720
contextInput,
18-
setContextInput
21+
setContextInput,
22+
currentContext,
23+
setCurrentContext
1924
}) => {
2025
const { allContext } = contextStore;
26+
console.log('all contexts', allContext);
2127
const [btnDisabled, setBtnDisabled] = useState(false);
2228
const { state, isDarkMode } = useSelector((store: RootState) => ({
2329
isDarkMode: store.darkMode.isDarkMode,
2430
state: store.appState
2531
}));
2632
const color = isDarkMode ? 'white' : 'black';
33+
const [selection, setSelection] = React.useState('');
2734

2835
const handleClick = () => {
2936
if (contextInput === '' || contextInput === null) {
@@ -33,22 +40,26 @@ const AddContextForm = ({
3340
handleClickSelectContext();
3441
};
3542

36-
const onChange = (event, newValue) => {
37-
if (typeof newValue === 'string') {
38-
setContextInput({
39-
name: newValue
40-
});
41-
} else if (newValue && newValue.inputValue) {
42-
// Create a new contextInput from the user input
43-
setContextInput({
44-
name: newValue.inputValue,
45-
values: []
46-
});
47-
renderTable(newValue);
48-
} else {
49-
setContextInput(newValue);
50-
renderTable(newValue);
51-
}
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 });
5263
};
5364

5465
const filterOptions = (options, params) => {
@@ -88,13 +99,21 @@ const AddContextForm = ({
8899
</li>
89100
);
90101

102+
const contexts = allContext.map((context) => {
103+
return (
104+
<MenuItem style={{ color: 'black' }} value={context.name}>
105+
{context.name}
106+
</MenuItem>
107+
);
108+
});
109+
91110
return (
92111
<Fragment>
93112
<Typography style={{ color: color }} variant="h6" gutterBottom={true}>
94-
Context Input
113+
Create Context
95114
</Typography>
96115
<Box sx={{ display: 'flex', gap: 2, mb: 4 }}>
97-
<Autocomplete
116+
{/* <Autocomplete
98117
id="autoCompleteContextField"
99118
value={contextInput}
100119
onChange={onChange}
@@ -118,6 +137,24 @@ const AddContextForm = ({
118137
label="Create/Select Context"
119138
/>
120139
)}
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"
121158
/>
122159
<Button
123160
variant="contained"
@@ -134,6 +171,25 @@ const AddContextForm = ({
134171
Delete
135172
</Button>
136173
</Box>
174+
<Typography style={{ color: color }} variant="h6" gutterBottom={true}>
175+
Select Context
176+
</Typography>
177+
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+
}}
187+
188+
// value={props.selectValue}
189+
// onChange={props.handleChange}
190+
>
191+
{contexts}
192+
</Select>
137193
</Fragment>
138194
);
139195
};

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)