Commit 0964c6f6 by Ajmal.S

form combined

parent d4250635
{ {
"students": [ "students": [
{ {
"id": 1441, "text": "Arun S",
"mark": "1212", "mark": "98",
"text": "ghgh" "id": 1
}, },
{ {
"id": 4244, "text": "Partha P",
"mark": "56", "mark": "99",
"text": "rrr" "id": 2
}, },
{ {
"id": 1559, "text": "Ajmal aju",
"mark": "56", "mark": "2434",
"text": "ww" "id": 3
},
{
"id": 1749,
"mark": "56",
"text": "aru"
},
{
"id": 6950,
"mark": "67",
"text": "mam"
} }
] ]
} }
\ No newline at end of file
...@@ -9,7 +9,6 @@ ...@@ -9,7 +9,6 @@
"bootstrap": "^5.1.3", "bootstrap": "^5.1.3",
"json-server": "^0.17.0", "json-server": "^0.17.0",
"react": "^17.0.2", "react": "^17.0.2",
"react-bootstrap": "^2.1.2",
"react-dom": "^17.0.2", "react-dom": "^17.0.2",
"react-icons": "^4.3.1", "react-icons": "^4.3.1",
"react-scripts": "5.0.0", "react-scripts": "5.0.0",
......
...@@ -2,13 +2,14 @@ import { useState, useEffect } from 'react'; ...@@ -2,13 +2,14 @@ import { useState, useEffect } from 'react';
import Students from './components/Students'; import Students from './components/Students';
import Header from './components/Header' import Header from './components/Header'
import Form from './components/Form'; import Form from './components/Form';
import 'bootstrap/dist/css/bootstrap.min.css';
function App() { function App() {
const [studentsData, setStudentsData] = useState([]); const [studentsData, setStudentsData] = useState([]);
const [text, setText] = useState(); const [text, setText] = useState();
const [mark, setMark] = useState(); const [mark, setMark] = useState();
const [editId, setEdit] = useState();
const [editMode, setEditMode] = useState(false);
const fetchStudents = async () => { const fetchStudents = async () => {
const response = await fetch('http://localhost:5000/students'); const response = await fetch('http://localhost:5000/students');
...@@ -22,13 +23,13 @@ function App() { ...@@ -22,13 +23,13 @@ function App() {
setStudentsData(data); setStudentsData(data);
} }
setStudents(); setStudents();
}, []) }, [editMode])
const addStudent = async (data) => { const addStudent = async (data) => {
const response = await fetch('http://localhost:5000/students', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(data) }); const response = await fetch('http://localhost:5000/students', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(data) });
const datas = await response.json(); const datas = await response.json();
setStudentsData([...studentsData, datas]); setStudentsData([...studentsData, datas]);
}; };
const deleteStudent = async (id) => { const deleteStudent = async (id) => {
...@@ -36,7 +37,7 @@ function App() { ...@@ -36,7 +37,7 @@ function App() {
setStudentsData(studentsData.filter((data) => data.id !== id)) setStudentsData(studentsData.filter((data) => data.id !== id))
} }
const editStudent = async (id) => { const updateStudent = async (id) => {
let item = { text, mark } let item = { text, mark }
const response = await fetch(`http://localhost:5000/students/${id}`, { const response = await fetch(`http://localhost:5000/students/${id}`, {
method: 'PUT', method: 'PUT',
...@@ -47,18 +48,23 @@ function App() { ...@@ -47,18 +48,23 @@ function App() {
body: JSON.stringify(item) body: JSON.stringify(item)
}) })
const data = await response.json() const data = await response.json()
setStudentsData(data);
} }
const handleEditChange = (id, text, mark) => {
setEditMode(true);
setEdit(id);
setMark(mark);
setText(text);
};
return ( return (
<div className="c-container"> <div className="c-container">
<Header /> <Header />
<Form onAdd={addStudent} text={text} setText={setText} mark={mark} setMark={setMark}/> <Form editId={editId} setEdit={setEdit} text={text} setText={setText} mark={mark} setMark={setMark} onAdd={addStudent} onEdit={updateStudent} editMode={editMode}/>
{studentsData.map((data, index) => ( {studentsData.map((data, index) => (
<Students key={data.id} index={index} id={data.id} marks={data.mark} onAdd={addStudent} onDelete={deleteStudent}>{data.text}</Students> <Students key={data.id} index={index} id={data.id} marks={data.mark} onAdd={addStudent} onDelete={deleteStudent} handleEditChange={handleEditChange}>{data.text}</Students>
))} ))}
</div> </div>
); );
} }
......
const Form = ({ onAdd, editId, text, setText, mark, setMark, onEdit, editMode }) => {
const Form = ({ onAdd, text, setText, mark, setMark }) => {
const handleSubmit = (e) => { const handleSubmit = (e) => {
e.preventDefault() e.preventDefault()
...@@ -22,7 +21,7 @@ const Form = ({ onAdd, text, setText, mark, setMark }) => { ...@@ -22,7 +21,7 @@ const Form = ({ onAdd, text, setText, mark, setMark }) => {
} }
return ( return (
<form className='add-form' onSubmit={handleSubmit}> <form className='add-form' onSubmit={!editMode ? handleSubmit : () => onEdit(editId)}>
<div className='form-ctrl'> <div className='form-ctrl'>
<label>Student Name</label> <label>Student Name</label>
<input type='text' placeholder='Name' value={text} onChange={(e) => setText(e.target.value)} /> <input type='text' placeholder='Name' value={text} onChange={(e) => setText(e.target.value)} />
...@@ -31,8 +30,10 @@ const Form = ({ onAdd, text, setText, mark, setMark }) => { ...@@ -31,8 +30,10 @@ const Form = ({ onAdd, text, setText, mark, setMark }) => {
<label>Mark</label> <label>Mark</label>
<input type='number' placeholder='Mark' value={mark} onChange={(e) => setMark(e.target.value)} /> <input type='number' placeholder='Mark' value={mark} onChange={(e) => setMark(e.target.value)} />
</div> </div>
<input type='submit' value='Save' className='button button-block' /> {!editMode ?
</form> < input type='submit' value='Save' className='button button-block' ></input> :
< input type='submit' value='Update' className='button button-block' ></input>}
</form >
) )
} }
......
import { ImBin } from 'react-icons/im' import { ImBin } from 'react-icons/im'
import { MdOutlineModeEditOutline } from 'react-icons/md' import { MdOutlineModeEditOutline } from 'react-icons/md'
const Students = ({ children, marks, id, index, onDelete }) => { const Students = ({ children, marks, id, index, onDelete, handleEditChange }) => {
return ( return (
<div className='student'> <div className='student'>
...@@ -10,8 +10,8 @@ const Students = ({ children, marks, id, index, onDelete }) => { ...@@ -10,8 +10,8 @@ const Students = ({ children, marks, id, index, onDelete }) => {
<div>{marks}</div> <div>{marks}</div>
<div><small className={`${marks >= 35 ? 'pass' : 'fail'}`}>({marks >= 35 ? 'P' : 'F'})</small></div> <div><small className={`${marks >= 35 ? 'pass' : 'fail'}`}>({marks >= 35 ? 'P' : 'F'})</small></div>
<div style={{ display: 'flex' }}> <div style={{ display: 'flex' }}>
<div><MdOutlineModeEditOutline /></div> <div><MdOutlineModeEditOutline onClick={() => handleEditChange(id, children, marks)} /></div>
<div style={{ marginLeft: '10px' }}><ImBin onClick={() => onDelete(id)} /></div> <div style={{ marginLeft: '10px' }}><ImBin onClick={() => (onDelete(id))}/></div>
</div> </div>
</div> </div>
) )
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
box-sizing: border-box; box-sizing: border-box;
margin: 0; margin: 0;
padding: 0; padding: 0;
font-family: sans-serif;
} }
.c-container { .c-container {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment