I got this form where you can update user data (code, article, price, vat, status, companyid).
I would like to populate the Update/Edit form with user data so i can easly modify them (once this form shows up, i would like to get data from API in each input and show them as input value).
JavaScript
x
99
99
1
import { getUsers, editUser } from '../Service/api'
2
import { useEffect } from 'react'
3
4
const DialogEditUser = (data) => {
5
const [RowData, SetRowData] = useState([])
6
const [ViewEdit, setEditShow] = useState(false)
7
const [id, setId] = useState('')
8
const [code, setCode] = useState('')
9
const [article, setArticle] = useState('')
10
const user = { id, code, article }
11
12
const normalize = (v) => ({
13
code: String(v.code),
14
article: String(v.article),
15
})
16
17
const handleEditShow = () => {
18
setEditShow(true)
19
}
20
const handleEditClose = () => {
21
setEditShow(false)
22
}
23
24
const handleEdit = () => {
25
editUser(data.props, normalize(user))
26
}
27
28
useEffect(() => {
29
getUsers()
30
}, [])
31
32
return (
33
<>
34
<CButton
35
style={{
36
marginRight: '10px',
37
color: 'info',
38
border: 'none',
39
boxShadow: `2px 2px 9px -3px rgba(0,0,0,0.6)`,
40
}}
41
onClick={() => handleEditShow(SetRowData(data), setId(data.id))}
42
>
43
Edit
44
</CButton>
45
<CModal
46
visible={ViewEdit}
47
onClose={handleEditClose}
48
backdrop={'static'}
49
keyboard={false}
50
portal={false}
51
>
52
<CModalHeader>
53
<CModalTitle>Edit User:</CModalTitle>
54
</CModalHeader>
55
<CModalBody>
56
<CForm>
57
<CFormInput
58
type="text"
59
id="exampleFormControlInput1"
60
label="Code :"
61
placeholder="Enter Code"
62
text=" "
63
aria-describedby="exampleFormControlInputHelpInline"
64
name="code"
65
onChange={(e) => setCode(e.target.value)}
66
defaultValue={RowData.code}
67
/>
68
<CFormInput
69
type="text"
70
id="exampleFormControlInput2"
71
label="Article :"
72
placeholder="Enter Article"
73
text=" "
74
aria-describedby="exampleFormControlInputHelpInline"
75
name="article"
76
onChange={(e) => setArticle(e.target.value)}
77
defaultValue={RowData.article}
78
/>
79
<CModalFooter>
80
<CButton color="secondary" onClick={() => setEditShow(false)}>
81
Cancel
82
</CButton>
83
<CButton
84
color="primary"
85
onClick={() => {
86
handleEdit()
87
setEditShow(false)
88
}}
89
>
90
Update
91
</CButton>
92
</CModalFooter>
93
</CModal>
94
</>
95
)
96
}
97
98
export default DialogEditUser
99
src/Service/api.js:
JavaScript
1
11
11
1
//...
2
export const getUsers = async (id) => {
3
id = id || ''
4
try {
5
return await axios.get(`${baseURL}`, config)
6
} catch (error) {
7
console.log('Error while calling getUsers api ', error)
8
}
9
}
10
//...
11
Advertisement
Answer
Create a new function
that handle loading of data then created a useEffect
function and put inside the handled function so the data will be loaded in inputs
(populated):
JavaScript
1
10
10
1
useEffect(() => {
2
loadArticleDetails()
3
}, [])
4
5
const loadArticleDetails = async () => {
6
const response = await getUsers(id)
7
console.log('Loading article details ', response)
8
setArticleData(response.data.find((x) => x.id == data.props))
9
}
10
And in inputs, change each defaultValue to:
JavaScript
1
4
1
//...
2
defaultValue={articleData.code} //Change code to its corresponding data: price, vat..etc
3
//...
4