-
Notifications
You must be signed in to change notification settings - Fork 4
/
DashboardDay.tsx
177 lines (166 loc) · 7.15 KB
/
DashboardDay.tsx
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import * as React from "react"
import { ListGroup, ListGroupItem, ListGroupItemHeading } from "reactstrap"
import LectureService from "../api/services/lecture"
import {
AttendanceStatesContextProps,
WithAttendanceStatesContext
} from "../contexts/AttendanceStatesContext"
import ModalLectures from "../forms/ModalLectures"
import ModalLecturesWizard from "../forms/ModalLecturesWizard"
import { USER_CELEBRATION } from "../global/constants"
import {
isToday,
isUserCelebrating,
prettyDateWithLongDayYearIfDiff,
prettyTime,
toISODate
} from "../global/funcDateTime"
import { courseDuration } from "../global/utils"
import { LectureTypeWithDate } from "../types/models"
import { fEmptyVoid, TimeoutType } from "../types/types"
import Attendances from "./Attendances"
import Celebration from "./Celebration"
import CourseName from "./CourseName"
import "./DashboardDay.css"
import GroupName from "./GroupName"
import LectureNumber from "./LectureNumber"
import Loading from "./Loading"
import UncontrolledTooltipWrapper from "./UncontrolledTooltipWrapper"
type Props = AttendanceStatesContextProps & {
withoutWaiting?: boolean
date: string
shouldRefresh: boolean
setRefreshState: fEmptyVoid
}
type State = {
lectures: Array<LectureTypeWithDate>
isLoading: boolean
}
/** Komponenta zobrazující lekce pro jeden zadaný den. */
class DashboardDay extends React.Component<Props, State> {
state: State = {
lectures: [],
isLoading: true
}
timeoutId: TimeoutType
getDate = (): Date => new Date(this.props.date)
getLectures = (): void => {
this.setState({ isLoading: true }, () => {
LectureService.getAllFromDayOrdered(toISODate(this.getDate()), true).then(lectures =>
this.setState({
lectures,
isLoading: false
})
)
})
}
componentDidMount(): void {
this.getLectures()
}
componentWillUnmount(): void {
window.clearTimeout(this.timeoutId)
}
componentDidUpdate(prevProps: Props): void {
if (this.props.shouldRefresh && !prevProps.shouldRefresh) {
if (this.props.withoutWaiting) this.getLectures()
// zpozdeni pro usetreni requestu pri rychlem preklikavani tydnu v diari
else {
window.clearTimeout(this.timeoutId)
this.setState(
{ isLoading: true },
() => (this.timeoutId = window.setTimeout(this.getLectures, 700))
)
}
}
}
render(): React.ReactNode {
const { lectures, isLoading } = this.state
const title = prettyDateWithLongDayYearIfDiff(this.getDate())
const isUserCelebratingResult = isUserCelebrating(this.getDate())
return (
<ListGroup className="pageContent">
<ListGroupItem
color={isToday(this.getDate()) ? "primary" : ""}
className="text-center DashboardDay_date">
<h4
className={
"mb-0 text-nowrap d-inline-block " +
(isUserCelebratingResult === USER_CELEBRATION.NOTHING
? "celebration_none"
: "celebration")
}>
<Celebration isUserCelebratingResult={isUserCelebratingResult} /> {title}
</h4>
<ModalLecturesWizard
refresh={this.props.setRefreshState}
date={this.props.date}
className="float-right"
size="sm"
direction="up"
/>
</ListGroupItem>
{isLoading || !this.props.attendanceStatesContext.isLoaded ? (
<ListGroupItem className="lecture">
<Loading />
</ListGroupItem>
) : lectures.length > 0 ? (
lectures.map(lecture => {
let className = lecture.group ? "LectureGroup" : ""
if (lecture.canceled) className = "lecture-canceled"
return (
<ListGroupItem
key={lecture.id}
className={className + " lecture lecture_dashboardday"}>
<div
className="lecture_heading"
style={{ background: lecture.course.color }}>
<h4>
<span
id={"Card_CourseDuration_" + lecture.id}
className="font-weight-bold">
{prettyTime(new Date(lecture.start))}
</span>
<UncontrolledTooltipWrapper
target={"Card_CourseDuration_" + lecture.id}>
{courseDuration(lecture.duration)}
</UncontrolledTooltipWrapper>
</h4>
<CourseName course={lecture.course} />
<LectureNumber lecture={lecture} colorize />
<ModalLectures
object={
lecture.group
? lecture.group
: lecture.attendances[0].client
}
currentLecture={lecture}
refresh={this.props.setRefreshState}
/>
</div>
<div className="lecture_content">
{lecture.group && (
<h5>
<GroupName group={lecture.group} title link />
</h5>
)}
<Attendances
lecture={lecture}
funcRefresh={this.props.setRefreshState}
showClient
/>
</div>
</ListGroupItem>
)
})
) : (
<ListGroupItem className="lecture lecture_free">
<ListGroupItemHeading className="text-muted text-center">
Volno
</ListGroupItemHeading>
</ListGroupItem>
)}
</ListGroup>
)
}
}
export default WithAttendanceStatesContext(DashboardDay)