forked from rage/java-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
113 lines (104 loc) · 2.77 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import React, { Component } from "react"
import withSimpleErrorBoundary from "../../util/withSimpleErrorBoundary"
import LoginStateContext from "../../contexes/LoginStateContext"
import { withTranslation } from "react-i18next"
import { Card } from "@material-ui/core"
import styled from "styled-components"
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"
import { faExclamationTriangle as icon } from "@fortawesome/free-solid-svg-icons"
import Loading from "../../components/Loading"
import { fetchAbGroup } from "../../services/abstudio"
import AbGroupContext from "../../contexes/AbGroupContext"
const StyledIcon = styled(FontAwesomeIcon)`
margin-right: 0.25em;
font-size: 3.3em !important;
`
const Wrapper = styled(Card)`
margin-bottom: 2rem;
padding: 1rem;
`
const MessageWrapper = styled.div`
display: flex;
align-items: center;
`
const P = styled.p`
margin-bottom: 1rem !important;
`
class AbStudy extends Component {
static contextType = LoginStateContext
state = {
error: undefined,
render: false,
group: undefined,
}
async componentDidMount() {
this.setState({ render: true })
if (!this.props.id || !this.context.loggedIn) {
return
}
try {
const { group } = await fetchAbGroup(this.props.id)
if (!group) {
this.setState({ error: "Group is null" })
}
this.setState({ group })
} catch (e) {
this.setState({ error: e.toString() })
}
}
render() {
if (!this.state.render) {
return (
<Wrapper>
{this.props.t("loading")}
<Loading heightHint="200px" />
</Wrapper>
)
}
if (!this.context.loggedIn) {
return (
<Wrapper>
<MessageWrapper>
<StyledIcon icon={icon} />
<div>
<P>{this.props.t("loginToSee")}</P>
</div>
</MessageWrapper>
</Wrapper>
)
}
if (this.state.error) {
return (
<Wrapper>
<MessageWrapper>
<StyledIcon icon={icon} />
<div>
<P>
{this.props.t("errorInMaterial")} <pre>{this.state.error}</pre>
</P>
</div>
</MessageWrapper>
</Wrapper>
)
}
if (!this.props.id) {
return <Wrapper>{this.props.t("incorrectMaterial")}</Wrapper>
}
if (!this.state.group) {
return (
<Wrapper>
{this.props.t("loading")}
<Loading heightHint="200px">{this.props.children}</Loading>
</Wrapper>
)
}
return (
<div>
<AbGroupContext.Provider value={this.state.group}>
{this.props.children}
</AbGroupContext.Provider>
</div>
)
}
}
export default withTranslation("common")(withSimpleErrorBoundary(AbStudy))