Skip to content

Commit b5d0cef

Browse files
Merge pull request #4 from oslabs-beta/deleteContext
Delete context
2 parents 34a6031 + ccf3ca4 commit b5d0cef

File tree

9 files changed

+94
-35
lines changed

9 files changed

+94
-35
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ const ComponentDropDown = ({
8484
sx={{ width: 425 }}
8585
freeSolo
8686
renderInput={params => (
87-
<TextField {...params} label="Select Component" />
87+
<TextField {...params} label="Select Component" helperText='Select a component for your selected context to consume' />
8888
)}
8989
/>
9090
</Box>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ const ContextDropDown = ({
8484
sx={{ width: 425 }}
8585
freeSolo
8686
renderInput={params => (
87-
<TextField {...params} label="Select Context" />
87+
<TextField {...params} label="Select Context" helperText='Select a context you would like to assign'/>
8888
)}
8989
/>
9090
</Box>

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import DataTable from './components/DataTable';
77
import AddDataForm from './components/AddDataForm';
88
import AddContextForm from './components/AddContextForm';
99
import * as actions from '../../../redux/actions/actions';
10+
import { Typography } from '@mui/material';
1011

1112
const CreateContainer = () => {
1213
const defaultTableData = [{ key: 'Enter Key', value: 'Enter value' }];
@@ -21,9 +22,16 @@ const CreateContainer = () => {
2122

2223
const dispatch = useDispatch();
2324

24-
const handleClickSelectContext = contextInput => {
25+
const handleClickSelectContext = () => {
26+
//prevent user from adding duplicate context
27+
for (let i = 0; i < state.allContext.length; i += 1) {
28+
if (state.allContext[i].name === contextInput.name) {
29+
return;
30+
}
31+
}
2532
dispatch(actions.addContextActionCreator(contextInput));
2633
setState(store.getState().contextSlice);
34+
2735
};
2836

2937
const handleClickInputData = ({ name }, { inputKey, inputValue }) => {
@@ -33,6 +41,12 @@ const CreateContainer = () => {
3341
setState(store.getState().contextSlice);
3442
};
3543

44+
const handleDeleteContextClick = () => {
45+
dispatch(actions.deleteContext(contextInput));
46+
setContextInput('');
47+
setState(store.getState().contextSlice);
48+
};
49+
3650
const renderTable = targetContext => {
3751
if (!targetContext.values) {
3852
setTableState(defaultTableData);
@@ -56,6 +70,7 @@ const CreateContainer = () => {
5670
<AddContextForm
5771
contextStore={state}
5872
handleClickSelectContext={handleClickSelectContext}
73+
handleDeleteContextClick={handleDeleteContextClick}
5974
renderTable={renderTable}
6075
contextInput={contextInput}
6176
setContextInput={setContextInput}
@@ -73,7 +88,14 @@ const CreateContainer = () => {
7388
</Grid>
7489
<Divider orientation="vertical" variant="middle" flexItem />
7590
<Grid item>
76-
<DataTable target={tableState} />
91+
<Typography
92+
style={{ color: 'black' }}
93+
variant="h6"
94+
gutterBottom={true}
95+
>
96+
Context Data Table
97+
</Typography>
98+
<DataTable target={tableState} contextInput={contextInput} />
7799
</Grid>
78100
</Grid>
79101
</>

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ const filter = createFilterOptions();
1010
const AddContextForm = ({
1111
contextStore,
1212
handleClickSelectContext,
13+
handleDeleteContextClick,
1314
renderTable,
1415
contextInput,
1516
setContextInput
1617
}) => {
1718
const { allContext } = contextStore;
1819
const [btnDisabled, setBtnDisabled] = useState(false);
20+
1921
const handleClick = () => {
2022
if (contextInput === '' || contextInput === null) return;
21-
const temp = contextInput;
22-
setContextInput('');
23-
handleClickSelectContext(temp);
23+
handleClickSelectContext();
2424
};
2525

2626
const onChange = (event, newValue) => {
@@ -104,10 +104,17 @@ const AddContextForm = ({
104104
<Button
105105
variant="contained"
106106
onClick={handleClick}
107-
disabled={btnDisabled ? true : false}
107+
disabled={btnDisabled}
108108
>
109109
Create
110110
</Button>
111+
<Button
112+
color="error"
113+
variant="contained"
114+
onClick={handleDeleteContextClick}
115+
>
116+
Delete
117+
</Button>
111118
{/* <Button variant="contained">Delete</Button> */}
112119
</Box>
113120
</Fragment>

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ import { Typography } from '@mui/material';
66

77
const AddDataForm = ({ handleClickInputData, contextInput }) => {
88
//const [contextInput, setContextInput] = React.useState(null);
9-
const [dataContext, setDataContext] = React.useState({
10-
inputKey: '',
11-
inputValue: ''
12-
});
9+
const defaultInputData = {inputKey: '', inputValue: ''};
10+
const [dataContext, setDataContext] = React.useState(defaultInputData);
11+
12+
const saveData = () => {
13+
setDataContext(defaultInputData);
14+
handleClickInputData(contextInput, dataContext)
15+
}
1316

1417
const handleChange = e => {
1518
setDataContext(prevDataContext => {
@@ -23,7 +26,7 @@ const AddDataForm = ({ handleClickInputData, contextInput }) => {
2326
return (
2427
<Fragment>
2528
<Typography style={{ color: 'black' }} variant="h6" gutterBottom={true}>
26-
Add context data
29+
Add context data
2730
</Typography>
2831
<Box sx={{ display: 'flex', gap: 2 }}>
2932
<TextField
@@ -44,7 +47,7 @@ const AddDataForm = ({ handleClickInputData, contextInput }) => {
4447
/>
4548
<Button
4649
variant="contained"
47-
onClick={() => handleClickInputData(contextInput, dataContext)}
50+
onClick={saveData}
4851
>
4952
Save
5053
</Button>

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

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import TableRow from '@mui/material/TableRow';
77
import Paper from '@mui/material/Paper';
88
import { styled } from '@mui/material/styles';
99
import TableCell, { tableCellClasses } from '@mui/material/TableCell';
10+
import TableFooter from '@mui/material/TableFooter';
1011

1112
const StyledTableCell = styled(TableCell)(({ theme }) => ({
1213
[`&.${tableCellClasses.head}`]: {
@@ -28,27 +29,40 @@ const StyledTableRow = styled(TableRow)(({ theme }) => ({
2829
}
2930
}));
3031

31-
export default function DataTable({target}) {
32+
export default function DataTable({ target, contextInput }) {
3233
return (
33-
<TableContainer component={Paper}>
34-
<Table sx={{ width: '510px' }} aria-label="customized table">
35-
<TableHead>
36-
<TableRow>
37-
<StyledTableCell>Key</StyledTableCell>
38-
<StyledTableCell align="right">Value</StyledTableCell>
39-
</TableRow>
40-
</TableHead>
41-
<TableBody>
42-
{target.map((data, index) => (
43-
<StyledTableRow key={index}>
44-
<StyledTableCell component="th" scope="row">
45-
{data.key}
34+
<>
35+
<TableContainer component={Paper} sx={{ maxHeight: '350px' }}>
36+
<Table
37+
sx={{ width: '510px' }}
38+
aria-label="customized table"
39+
stickyHeader
40+
>
41+
<TableHead>
42+
<TableRow>
43+
{/* <StyledTableCell>Key</StyledTableCell> */}
44+
<StyledTableCell align="center" colSpan={3}>
45+
{contextInput ? contextInput.name : 'Context Name'}
4646
</StyledTableCell>
47-
<StyledTableCell align="right">{data.value}</StyledTableCell>
48-
</StyledTableRow>
49-
))}
50-
</TableBody>
51-
</Table>
52-
</TableContainer>
47+
</TableRow>
48+
</TableHead>
49+
<TableBody>
50+
{target.map((data, index) => (
51+
<StyledTableRow key={index}>
52+
<StyledTableCell component="th" scope="row">
53+
{data.key}
54+
</StyledTableCell>
55+
<StyledTableCell align="right">{data.value}</StyledTableCell>
56+
</StyledTableRow>
57+
))}
58+
</TableBody>
59+
{/* <TableFooter>
60+
<StyledTableCell align="center" colSpan={3}>
61+
{contextInput ? contextInput.name : 'Context Name'}
62+
</StyledTableCell>
63+
</TableFooter> */}
64+
</Table>
65+
</TableContainer>
66+
</>
5367
);
54-
}
68+
}

app/src/redux/actions/actions.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,8 @@ export const addComponentToContext = newEntry => ({
1818
type: types.ADD_COMPONENT_TO_CONTEXT,
1919
payload: newEntry
2020
});
21+
22+
export const deleteContext = (contextInput) => ({
23+
type: types.DELETE_CONTEXT,
24+
payload: contextInput
25+
})

app/src/redux/constants/actionTypes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export const CODE_PREVIEW_INPUT = 'CODE_PREVIEW_INPUT';
55
export const ADD_CONTEXT = 'ADD_CONTEXT';
66
export const ADD_CONTEXT_VALUES = 'ADD_CONTEXT_VALUES';
77
export const ADD_COMPONENT_TO_CONTEXT = 'ADD_COMPONENT_TO_CONTEXT';
8+
export const DELETE_CONTEXT = 'DELETE_CONTEXT;'

app/src/redux/reducers/slice/contextReducer.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ const contextReducer = (state = initialState, action) => {
5151
...state,
5252
allContext: newAllContext
5353
};
54+
case types.DELETE_CONTEXT:
55+
const remains = state.allContext.filter((el) => el.name !== action.payload.name)
56+
return {
57+
...state,
58+
allContext : remains
59+
}
60+
5461
case types.ADD_COMPONENT_TO_CONTEXT:
5562
const newTempState = [...state.allContext];
5663

0 commit comments

Comments
 (0)