forked from irinazheltisheva/fastsite-console
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntities.js
More file actions
116 lines (104 loc) · 3.25 KB
/
Entities.js
File metadata and controls
116 lines (104 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import React, { useState, useEffect, Fragment } from 'react'
import AddEntityForm from './forms/AddEntityForm'
import EditEntityForm from './forms/EditEntityForm'
import EntityTable from './tables/EntityTable'
import axios from './myAxios'
const Entities = props => {
const initialFormState = { id: '', title: '', subtitle: '', image: '', description: '', category: '' }
// Setting state
const [ entities, setEntities ] = useState([])
const [ currentEntity, setCurrentEntity ] = useState(initialFormState)
const [ editing, setEditing ] = useState(false)
const [ loading, setLoading ] = useState(false)
useEffect(() => {
fetchData()
}, []) // eslint-disable-line react-hooks/exhaustive-deps
// CRUD operations
const fetchData = async () => {
setLoading(true)
try {
const results = await axios.get('/.netlify/functions/list?site=' + props.currentSite.id)
console.log(results.data)
setEntities(results.data)
setLoading(false)
}catch (e) {
if (e) {
console.error(e)
setLoading(false)
}
}
}
const addEntity = entity => {
setLoading(true)
axios.post('/.netlify/functions/add?site=' + props.currentSite.id, entity)
.then((response) => {
console.log(response)
setEntities([ ...entities, entity ])
setLoading(false)
})
.catch((err) => {
console.error(err)
setLoading(false)
})
}
const deleteEntity = id => {
setLoading(true)
axios.post('/.netlify/functions/delete?site=' + props.currentSite.id, {id : id})
.then((response) => {
setEntities(entities.filter(entity => entity.id !== id))
setLoading(false)
})
.catch((err) => {
console.error(err)
setLoading(false)
})
}
const updateEntity = (id, updatedEntity) => {
setLoading(true)
axios.post('/.netlify/functions/add?site=' + props.currentSite.id, updatedEntity)
.then((response) => {
setEditing(false)
setEntities(entities.map(entity => (entity.id === id ? updatedEntity : entity)))
setLoading(false)
})
.catch((err) => {
console.error(err)
setLoading(false)
})
}
const editRow = entity => {
setEditing(true)
setCurrentEntity(entity)
}
return (
<div className="flex-row">
<div className={loading ? "loading" : ""}></div>
<div className="flex-large">
<button onClick={() => props.setViewing(false)} className="button muted-button">
Back
</button>
<h2>Content for site: {props.currentSite.name} </h2>
<EntityTable entities={entities} editRow={editRow} deleteEntity={deleteEntity} />
</div>
<div className="flex-large">
{editing ? (
<Fragment>
<h2>Edit entity</h2>
<EditEntityForm
editing={editing}
setEditing={setEditing}
currentEntity={currentEntity}
updateEntity={updateEntity}
/>
</Fragment>
) : (
<Fragment>
<h2>Add entity</h2>
<AddEntityForm addEntity={addEntity} />
</Fragment>
)}
</div>
</div>
)
}
export default Entities