Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Layout : Added Edit User Details Dialog & Confirm Delete Dialog #10

Merged
merged 2 commits into from
Feb 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"@iconify/react": "^3.1.3",
"@material-ui/core": "^4.11.3",
"@material-ui/icons": "^4.11.2",
"@material-ui/lab": "^4.0.0-alpha.56",
"@testing-library/jest-dom": "^5.12.0",
"@testing-library/react": "^11.2.6",
"@testing-library/user-event": "^12.8.3",
Expand Down
1 change: 0 additions & 1 deletion frontend/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ function App() {
<Route exact path="/" component={Map}/>
<Route exact path="/all" component={AllUsers} />
<Route exact path="/add" component={AddUser} />
<Route exact path="/edit/:id" component={EditUser} />
<Route component={NotFound} />
</Switch>
</BrowserRouter>
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/Component/AllUsers.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ const AllUsers = () => {
id={item._id}
name={item.name}
lat={item.lat}
long={item.lng}
lng={item.lng}
phone={item.phone}
setUsers={setUsers}
category={item.category}
/>
))}
</div>
Expand Down
159 changes: 159 additions & 0 deletions frontend/src/Component/EditDetailModal.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
import React, { useState, useEffect } from 'react';
import { useHistory, useParams } from 'react-router-dom';
import { getUsers, editUser } from '../Service/api';
import {
FormGroup,
FormControl,
InputLabel,
Input,
Button,
makeStyles,
Typography,
Modal,
Box,
Dialog,
DialogContent,
DialogContentText,
DialogActions,
DialogTitle,
TextField,
} from '@material-ui/core';
import { Alert } from '@material-ui/lab';

const useStyles = makeStyles({
heading: {
marginTop: '1rem',
fontFamily: 'Sora',
fontWeight: '600',
color: 'black',
textAlign: 'center',
},
});

function EditDetailModal({ user, show, setShow, setUsers, userid }) {
const classes = useStyles();
const [initData, setInitData] = useState(
user ? user : { name: '', lat: '', lng: '', phone: '', category: '' }
);
const [alert, setAlert] = useState({ show: false, text: '' });
const [loading, setloading] = useState(false);
useEffect(() => {
setInitData(user);
}, [user]);

const onValueChange = (k, v) => {
let curr = { ...initData };
curr[k] = v;
setInitData(curr);
};

const submitHandler = async () => {
if (
initData.name === '' ||
initData.lat === '' ||
initData.lng === '' ||
initData.phone === '' ||
initData.category === ''
) {
setAlert({ show: true, text: 'Input fields can not be empty' });
return;
}
setAlert({ show: false, text: '' });
try {
setloading(true);
const res = await editUser(userid, initData);
let response = await getUsers();
setUsers(response.data);
setShow(false);
setloading(false);
} catch (error) {
console.log(error);
setloading(false);
}
};

return (
<Dialog
fullWidth
maxWidth="sm"
open={show}
onClose={() => setShow(false)}
aria-labelledby="alert-dialog-title"
aria-describedby="alert-dialog-description"
>
<Typography className={classes.heading} align="center" variant="h5">
Edit Details
</Typography>
<DialogContent>
{alert.show ? <Alert severity="error">{alert.text}</Alert> : null}
<FormControl fullWidth>
<TextField
variant="outlined"
margin="normal"
label="Name"
onChange={(e) => onValueChange(e.target.name, e.target.value)}
name="name"
value={initData.name}
id="my-input"
/>
<TextField
variant="outlined"
label="Lat"
margin="normal"
onChange={(e) => onValueChange(e.target.name, e.target.value)}
name="lat"
value={initData.lat}
id="my-input"
/>
<TextField
variant="outlined"
margin="normal"
label="Long"
onChange={(e) => onValueChange(e.target.name, e.target.value)}
name="lng"
value={initData.lng}
id="my-input"
/>
<TextField
variant="outlined"
margin="normal"
label="Phone"
onChange={(e) => onValueChange(e.target.name, e.target.value)}
name="phone"
value={initData.phone}
id="my-input"
/>
<TextField
variant="outlined"
margin="normal"
label="Category"
onChange={(e) => onValueChange(e.target.name, e.target.value)}
name="category"
value={initData.category}
id="my-input"
/>
</FormControl>
</DialogContent>
<DialogActions>
<Button
disabled={loading}
variant="contained"
onClick={() => submitHandler()}
color="primary"
>
Submit
</Button>
<Button
disabled={loading}
onClick={() => setShow(false)}
color="secondary"
variant="contained"
>
Cancel
</Button>
</DialogActions>
</Dialog>
);
}

export default EditDetailModal;
174 changes: 109 additions & 65 deletions frontend/src/Component/EditUser.jsx
Original file line number Diff line number Diff line change
@@ -1,80 +1,124 @@
import { useState, useEffect } from 'react';
import { FormGroup, FormControl, InputLabel, Input, Button, makeStyles, Typography } from '@material-ui/core';
import {
FormGroup,
FormControl,
InputLabel,
Input,
Button,
makeStyles,
Typography,
} from '@material-ui/core';
import { useHistory, useParams } from 'react-router-dom';
import { getUsers, editUser } from '../Service/api';

const initialValue = {
name: '',
lat: '',
lng:'',
phone: '',
category:''
}
name: '',
lat: '',
lng: '',
phone: '',
category: '',
};

const useStyles = makeStyles({
container: {
width: '50%',
margin: '5% 0 0 25%',
'& > *': {
marginTop: 20
}
}
})
container: {
width: '50%',
margin: '5% 0 0 25%',
'& > *': {
marginTop: 20,
},
},
});

const EditUser = () => {
const [user, setUser] = useState(initialValue);
const { name, lat, lng, phone , category} = user;
const { id } = useParams();
const classes = useStyles();
let history = useHistory();
const [user, setUser] = useState(initialValue);
const { name, lat, lng, phone, category } = user;
const { id } = useParams();
const classes = useStyles();
let history = useHistory();

useEffect(() => {
loadUserDetails();
}, []);
useEffect(() => {
loadUserDetails();
}, []);

const loadUserDetails = async() => {
const response = await getUsers(id);
setUser(response.data);
}
const loadUserDetails = async () => {
const response = await getUsers(id);
setUser(response.data);
};

const editUserDetails = async() => {
const response = await editUser(id, user);
history.push('/all');
}
const editUserDetails = async () => {
const response = await editUser(id, user);
history.push('/all');
};

const onValueChange = (e) => {
console.log(e.target.value);
setUser({...user, [e.target.name]: e.target.value})
}
const onValueChange = (e) => {
console.log(e.target.value);
setUser({ ...user, [e.target.name]: e.target.value });
};

return (
<FormGroup className={classes.container}>
<Typography variant="h4">Edit Information</Typography>
<FormControl>
<InputLabel htmlFor="my-input">Name</InputLabel>
<Input onChange={(e) => onValueChange(e)} name='name' value={name} id="my-input" aria-describedby="my-helper-text" />
</FormControl>
<FormControl>
<InputLabel htmlFor="my-input">Username</InputLabel>
<Input onChange={(e) => onValueChange(e)} name='lat' value={lat} id="my-input" aria-describedby="my-helper-text" />
</FormControl>
<FormControl>
<InputLabel htmlFor="my-input">Email</InputLabel>
<Input onChange={(e) => onValueChange(e)} name='lng' value={lng} id="my-input" aria-describedby="my-helper-text" />
</FormControl>
<FormControl>
<InputLabel htmlFor="my-input">Phone</InputLabel>
<Input onChange={(e) => onValueChange(e)} name='phone' value={phone} id="my-input" aria-describedby="my-helper-text" />
</FormControl>
<FormControl>
<InputLabel htmlFor="my-input">Phone</InputLabel>
<Input onChange={(e) => onValueChange(e)} name='category' value={category} id="my-input" aria-describedby="my-helper-text" />
</FormControl>
<FormControl>
<Button variant="contained" color="primary" onClick={() => editUserDetails()}>Edit User</Button>
</FormControl>
</FormGroup>
)
}
return (
<FormGroup className={classes.container}>
<Typography variant="h4">Edit Information</Typography>
<FormControl>
<InputLabel htmlFor="my-input">Name</InputLabel>
<Input
onChange={(e) => onValueChange(e)}
name="name"
value={name}
id="my-input"
aria-describedby="my-helper-text"
/>
</FormControl>
<FormControl>
<InputLabel htmlFor="my-input">Username</InputLabel>
<Input
onChange={(e) => onValueChange(e)}
name="lat"
value={lat}
id="my-input"
aria-describedby="my-helper-text"
/>
</FormControl>
<FormControl>
<InputLabel htmlFor="my-input">Email</InputLabel>
<Input
onChange={(e) => onValueChange(e)}
name="lng"
value={lng}
id="my-input"
aria-describedby="my-helper-text"
/>
</FormControl>
<FormControl>
<InputLabel htmlFor="my-input">Phone</InputLabel>
<Input
onChange={(e) => onValueChange(e)}
name="phone"
value={phone}
id="my-input"
aria-describedby="my-helper-text"
/>
</FormControl>
<FormControl>
<InputLabel htmlFor="my-input">Phone</InputLabel>
<Input
onChange={(e) => onValueChange(e)}
name="category"
value={category}
id="my-input"
aria-describedby="my-helper-text"
/>
</FormControl>
<FormControl>
<Button
variant="contained"
color="primary"
onClick={() => editUserDetails()}
>
Edit User
</Button>
</FormControl>
</FormGroup>
);
};

export default EditUser;
export default EditUser;
Loading