-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
90 lines (77 loc) · 2.09 KB
/
index.js
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
import React from 'react';
import moment from 'moment';
import { styled, View } from 'bappo-components';
import TimesheetHeader from './TimesheetHeader';
import RowHeader from './RowHeader';
import JobRows from './JobRows';
import { getMonday } from './utils';
class TableView extends React.Component {
state = {
timesheet: null,
}
async componentDidMount() {
const { $navigation, $models } = this.props;
const { recordId } = $navigation.state.params;
const timesheet = (await $models.Timesheet.findAll({
where: {
id: recordId,
},
include: [
{ as: 'consultant' }
]
}))[0];
// Change day of a week to Monday if needed
timesheet.week = getMonday(timesheet.week);
this.setState({ timesheet });
}
switchWeek = async (isNext) => {
const { $navigation, $models } = this.props;
const { timesheet } = this.state;
const weekGap = isNext ? 1 : -1;
const targetWeek = moment(getMonday(timesheet.week)).add(weekGap, 'week').format('YYYY-MM-DD');
let targetTimesheet;
let templateTimesheetId;
targetTimesheet = await $models.Timesheet.findOne({
where: {
week: targetWeek,
consultant_id: timesheet.consultant_id,
},
});
if (!targetTimesheet) {
// create new time sheet
targetTimesheet = await $models.Timesheet.create({
week: targetWeek,
consultant_id: timesheet.consultant_id,
});
templateTimesheetId = timesheet.id;
}
// navigate to target week
$navigation.navigate(
'TimesheetDetailsPage',
{
recordId: targetTimesheet.id,
templateTimesheetId,
}
);
}
render() {
const { timesheet } = this.state;
if (!timesheet) return null;
return (
<Container>
<TimesheetHeader
timesheet={timesheet}
switchWeek={this.switchWeek}
/>
<RowHeader startDate={timesheet.week} />
<JobRows
timesheet={timesheet}
{...this.props}
/>
</Container>
);
}
}
export default TableView;
const Container = styled(View)`
`;