I am trying to integrate React Scheduler into my app that uses JSON data base. how can I pass the shifts and functions defined in App.js that update the data to DataSheet.js and use them instead of the defaults? Is there a better way to enable the database update from the DataSheet?
App.js:
JavaScript
x
66
66
1
import React from 'react';
2
import Header from "./components/Header";
3
import { useState, useEffect } from "react"
4
import AddShift from './components/AddShift';
5
import Shifts from './components/Shifts';
6
import Week from './components/Week'
7
import DataSheet from './components/DataSheet';
8
9
const containerStyle= {
10
width: '100vw',
11
height: '100vh',
12
}
13
14
const App = () => {
15
const [showAddShfit, setShowAddShift] = useState(false)
16
const [shifts, setShifts] = useState([])
17
18
useEffect(() => {
19
const getShifts = async () => {
20
const shiftsFromServer = await fetchShifts()
21
setShifts(shiftsFromServer)
22
}
23
getShifts()
24
}, [])
25
26
const fetchShifts = async () => {
27
const res = await fetch(`http://localhost:5000/shifts/`)
28
const data = await res.json()
29
return data
30
}
31
32
//Add Shift
33
const addShift = async (shift) => {
34
const res = await fetch(`http://localhost:5000/shifts/`,{
35
method: 'POST',
36
headers: {
37
'Content-type': 'application/json'
38
},
39
body: JSON.stringify(shift)
40
})
41
42
const data = await res.json()
43
44
setShifts([shifts, data])
45
}
46
47
const deleteShift = async (id) => {
48
await fetch(`http://localhost:5000/shifts/${id}`, {
49
method: 'DELETE',
50
})
51
52
setShifts(shifts.filter((shift) => shift.id !== id))
53
}
54
55
return (
56
<div className="container"
57
style={containerStyle} >
58
<div className='secondary_container'>
59
<Header />
60
<DataSheet shifts={shifts} addShift={addShift}/>
61
</div>
62
</div>
63
);
64
}
65
export default App;
66
DataSheet.js:
JavaScript
1
109
109
1
import * as React from 'react';
2
import Paper from '@material-ui/core/Paper';
3
import { ViewState, EditingState } from '@devexpress/dx-react-scheduler';
4
import {
5
Scheduler,
6
Appointments,
7
AppointmentForm,
8
AppointmentTooltip,
9
WeekView,
10
ConfirmationDialog,
11
} from '@devexpress/dx-react-scheduler-material-ui';
12
13
const appointments = [
14
{
15
title: "Website Re-Design Plan",
16
startDate: new Date(2018, 5, 25, 9, 35),
17
endDate: new Date(2018, 5, 25, 11, 30),
18
id: 0,
19
location: "Room 1"
20
},
21
];
22
23
export default class DataSheet extends React.PureComponent {
24
constructor(props) {
25
super(props);
26
this.state = {
27
data: appointments,
28
currentDate: '2018-06-27',
29
addedAppointment: {},
30
appointmentChanges: {},
31
editingAppointment: undefined,
32
};
33
34
this.commitChanges = this.commitChanges.bind(this);
35
this.changeAddedAppointment = this.changeAddedAppointment.bind(this);
36
this.changeAppointmentChanges = this.changeAppointmentChanges.bind(this);
37
this.changeEditingAppointment = this.changeEditingAppointment.bind(this);
38
}
39
40
changeAddedAppointment(addedAppointment) {
41
this.setState({ addedAppointment });
42
}
43
44
changeAppointmentChanges(appointmentChanges) {
45
this.setState({ appointmentChanges });
46
}
47
48
changeEditingAppointment(editingAppointment) {
49
this.setState({ editingAppointment });
50
}
51
52
commitChanges({ added, changed, deleted }) {
53
this.setState((state) => {
54
let { data } = state;
55
if (added) {
56
const startingAddedId = data.length > 0 ? data[data.length - 1].id + 1 : 0;
57
data = [data, { id: startingAddedId, added }];
58
}
59
if (changed) {
60
data = data.map(appointment => (
61
changed[appointment.id] ? { appointment, changed[appointment.id] } : appointment));
62
}
63
if (deleted !== undefined) {
64
data = data.filter(appointment => appointment.id !== deleted);
65
}
66
return { data };
67
});
68
}
69
70
render() {
71
const {
72
currentDate, data, addedAppointment, appointmentChanges, editingAppointment,
73
} = this.state;
74
75
return (
76
<Paper>
77
<Scheduler
78
data={data}
79
height={660}
80
>
81
<ViewState
82
currentDate={currentDate}
83
/>
84
<EditingState
85
onCommitChanges={this.commitChanges}
86
addedAppointment={addedAppointment}
87
onAddedAppointmentChange={this.changeAddedAppointment}
88
appointmentChanges={appointmentChanges}
89
onAppointmentChangesChange={this.changeAppointmentChanges}
90
editingAppointment={editingAppointment}
91
onEditingAppointmentChange={this.changeEditingAppointment}
92
/>
93
<WeekView
94
startDayHour={9}
95
endDayHour={17}
96
/>
97
<ConfirmationDialog />
98
<Appointments />
99
<AppointmentTooltip
100
showOpenButton
101
showDeleteButton
102
/>
103
<AppointmentForm />
104
</Scheduler>
105
</Paper>
106
);
107
}
108
}
109
Advertisement
Answer
Pass the data as props from parent component App
and capture it from the child component DataSheet
.
App
JavaScript
1
6
1
2
3
<DataSheet shifts={shifts} addShift={addShift}/>
4
5
6
Inside Datasheet
component you can use them as this.props.shifts
and this.props.addShift
.