Commit f49e25cb by Ajmal.S

correctins

parent 5ccaa161
{ {
"students": [ "students": [
{ {
"text": "aruny", "mark": "45",
"mark": "100", "text": "1",
"id": 1 "id": 4
}, },
{ {
"text": "partha", "mark": "90",
"mark": "997", "text": "2",
"id": 2 "id": 5
},
{
"mark": "95",
"text": "3",
"id": 6
} }
] ]
} }
\ No newline at end of file
...@@ -8,10 +8,6 @@ export const valueContext = React.createContext(); ...@@ -8,10 +8,6 @@ export const valueContext = React.createContext();
function App() { function App() {
const [studentsData, setStudentsData] = useState([]); const [studentsData, setStudentsData] = useState([]);
const [text, setText] = 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');
...@@ -25,7 +21,7 @@ function App() { ...@@ -25,7 +21,7 @@ function App() {
setStudentsData(data); setStudentsData(data);
} }
setStudents(); setStudents();
}, [editMode]) }, [])
const addStudent = async (data) => { const addStudent = async (data) => {
...@@ -41,35 +37,14 @@ function App() { ...@@ -41,35 +37,14 @@ function App() {
} }
} }
const updateStudent = async (id) => {
let item = { text, mark }
const response = await fetch(`http://localhost:5000/students/${id}`, {
method: 'PUT',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(item)
})
const data = await response.json();
}
const handleEditChange = (id, text, mark) => {
setEditMode(true);
setEdit(id);
setMark(mark);
setText(text);
};
return ( return (
<div className="c-container"> <div className="c-container">
<valueContext.Provider value="Students Mark List <valueContext.Provider value="Students Mark List">
">
<Header /> <Header />
</valueContext.Provider> </valueContext.Provider>
<Form editId={editId} setEdit={setEdit} text={text} setText={setText} mark={mark} setMark={setMark} onAdd={addStudent} onEdit={updateStudent} editMode={editMode} /> <Form onAdd={addStudent}/>
{studentsData.map((data, index) => ( {studentsData.map((data, index) => (
<Students key={data.id} index={index} id={data.id} marks={data.mark} onAdd={addStudent} onDelete={deleteStudent} handleEditChange={handleEditChange}>{data.text}</Students> <Students key={data.id} index={index} id={data.id} marks={data.mark} onAdd={addStudent} onDelete={deleteStudent}>{data.text}</Students>
))} ))}
</div> </div>
); );
......
import React from 'react'; import React, { useState } from 'react';
import PropTypes from 'prop-types';
import '../../src/index.css'
export const Form = ({ onAdd, editId, text, setText, mark, setMark, onEdit, editMode }) => { const Form = ({ onAdd }) => {
const [text, setText] = useState();
const [mark, setMark] = useState();
const [editId, setEdit] = useState();
const [editMode, setEditMode] = useState(false);
const handleSubmit = (e) => { const handleSubmit = (e) => {
e.preventDefault() e.preventDefault()
...@@ -24,8 +27,28 @@ export const Form = ({ onAdd, editId, text, setText, mark, setMark, onEdit, edit ...@@ -24,8 +27,28 @@ export const Form = ({ onAdd, editId, text, setText, mark, setMark, onEdit, edit
setMark(''); setMark('');
} }
const handleEditChange = (id, children, marks) => {
setEditMode(true);
setEdit(id);
setMark(marks);
setText(children);
};
const updateStudent = async (id) => {
let item = { text, mark }
const response = await fetch(`http://localhost:5000/students/${id}`, {
method: 'PUT',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(item)
})
const data = await response.json();
}
return ( return (
<form className='add-form' onSubmit={!editMode ? handleSubmit : () => onEdit(editId)}> <form className='add-form' onSubmit={!editMode ? handleSubmit : () => updateStudent(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)} />
...@@ -35,22 +58,10 @@ export const Form = ({ onAdd, editId, text, setText, mark, setMark, onEdit, edit ...@@ -35,22 +58,10 @@ export const Form = ({ onAdd, editId, text, setText, mark, setMark, onEdit, edit
<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>
{!editMode ? {!editMode ?
< input type='submit' value='Save' className='button button-block' ></input> : <input type='submit' value='Save' className='button button-block'></input> :
< input type='submit' value='Update' className='button button-block' ></input>} <input type='submit' value='Update' className='button button-block'></input>}
</form > </form>
) )
} }
Form.propTypes = {
editMode: PropTypes.bool,
handleSubmit: PropTypes.func,
onEdit: PropTypes.func,
editId: PropTypes.string,
text: PropTypes.string,
mark: PropTypes.string,
// setText: PropTypes.func,
// setMark: PropTypes.func,
onAdd: PropTypes.func
};
export default Form export default Form
...@@ -11,7 +11,7 @@ const Students = ({ children, marks, id, index, onDelete, handleEditChange }) => ...@@ -11,7 +11,7 @@ const Students = ({ children, marks, id, index, onDelete, handleEditChange }) =>
<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 onClick={() => handleEditChange(id, children, marks)} /></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>
) )
......
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