-
Notifications
You must be signed in to change notification settings - Fork 8
/
ImportResourceTemplate.jsx
179 lines (155 loc) · 5.36 KB
/
ImportResourceTemplate.jsx
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
178
179
// Copyright 2018 Stanford University see LICENSE for license
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import Header from './Header'
import ImportFileZone from './ImportFileZone'
import SinopiaResourceTemplates from './SinopiaResourceTemplates'
import UpdateResourceModal from './UpdateResourceModal'
import { createResourceTemplate, updateResourceTemplate } from '../../sinopiaServer'
import { connect } from 'react-redux'
import { getAuthenticationState } from '../../selectors';
class ImportResourceTemplate extends Component {
constructor(props) {
super(props)
this.state = {
flashMessages: [],
modalMessages: [],
updateKey: 0,
modalShow: false
}
}
componentDidMount() {
const incrementedKey = this.state.updateKey + 1
// This causes the `SinopiaResourceTemplates` component to do the initial load of RTs
this.setState({ updateKey: incrementedKey })
}
modalClose = () => {
this.setState({ modalShow: false })
}
// Resource templates are set via ImportFileZone and passed to ResourceTemplate via redirect to Editor
setResourceTemplates = async (content, group) => {
this.resetMessages()
const responses = []
// Prefer for ... of to forEach when loop body uses async/await
for (const rt of content.Profile.resourceTemplates) {
const response = await this.createResource(rt, group)
responses.push(response)
}
this.updateStateFromServerResponses(responses)
}
createResource = async (content, group) => {
try {
const response = await createResourceTemplate(content, group, this.props.authenticationState)
return response.response
} catch(error) {
this.setState({
modalMessages: [...this.state.modalMessages, error.response]
})
return error.response
}
}
updateResource = async (content, group) => {
try {
const response = await updateResourceTemplate(content, group, this.props.authenticationState)
return response.response
} catch(error) {
return error.response
}
}
resetMessages = () => {
this.setState({
flashMessages: [],
modalMessages: []
})
}
updateStateFromServerResponses = (responses) => {
const newFlashMessages = []
const newState = {
updateKey: this.state.updateKey + 1
}
let showModal = false
responses.forEach(response => {
// If any responses are HTTP 409s, flip `showModal` to true
showModal = showModal || response.status === 409
newFlashMessages.push(`${this.humanReadableStatus(response.status)} ${this.humanReadableLocation(response)}`)
})
if (newFlashMessages.length > 0)
newState.flashMessages = [...this.state.flashMessages, ...newFlashMessages]
if (showModal)
newState.modalShow = true
this.setState(newState)
}
// Returns a URL or an empty string
humanReadableLocation = response => {
if (response?.headers?.location)
return response.headers.location
if (response?.req?._data?.id) {
// If RT has special characters—e.g., colons—in it, decode the URI to compare against ID
const decodedURI = decodeURIComponent(response.req.url)
// If the request URL already contains the ID, don't bother appending
if (decodedURI.endsWith(response.req._data.id))
return decodedURI
return `${decodedURI}/${response.req._data.id}`
}
// Welp, we tried anyway
return ''
}
humanReadableStatus = status => {
switch(status) {
case 201:
return 'Created'
case 204:
return 'Updated'
case 401:
return 'You are not authorized to do this. Try logging in again!'
case 409:
return 'Prompting user about updating'
default:
return `Unexpected response (${status})!`
}
}
handleUpdateResource = async (rts, group) => {
const responses = []
// Prefer for ... of to forEach when loop body uses async/await
for (const rt of rts) {
const response = await this.updateResource(rt, group)
responses.push(response)
}
this.updateStateFromServerResponses(responses)
this.modalClose()
}
render() {
return(
<div id="importResourceTemplate">
<div>
{ (this.state.modalShow) ? (
<UpdateResourceModal show={this.state.modalShow}
close={this.modalClose}
messages={this.state.modalMessages}
update={this.handleUpdateResource} /> ) :
( <div/> ) }
</div>
<Header triggerEditorMenu={this.props.triggerHandleOffsetMenu}/>
<ImportFileZone setResourceTemplateCallback={this.setResourceTemplates} />
<SinopiaResourceTemplates updateKey={this.state.updateKey}
messages={this.state.flashMessages} />
</div>
)
}
}
ImportResourceTemplate.propTypes = {
children: PropTypes.array,
triggerHandleOffsetMenu: PropTypes.func,
authenticationState: PropTypes.object
}
const mapStateToProps = (state) => {
return {
authenticationState: getAuthenticationState(state)
}
}
//TODO: likely to end up wiring up auth error reporting via redux dispatch
// const mapDispatchToProps = dispatch => {
// return { }
// }
export default connect(mapStateToProps)(ImportResourceTemplate)
// export default connect(mapStateToProps, mapDispatchToProps)(ImportResourceTemplate)