-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathindex.js
More file actions
117 lines (101 loc) · 3.12 KB
/
Copy pathindex.js
File metadata and controls
117 lines (101 loc) · 3.12 KB
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
import PropTypes from 'prop-types'
import React, { useEffect, useState } from 'react'
import { connect } from 'react-redux'
import { withRouter } from 'react-router-dom'
import _ from 'lodash'
import Loader from '../../components/Loader'
import { loadOnlyProjectInfo } from '../../actions/projects'
import { checkIsUserInvitedToProject } from '../../util/tc'
/**
* Resolves the correct project landing route for `/projects/:projectId`.
*
* It loads lightweight project details first so invited users can be sent to
* the invitation modal before challenge-specific requests run.
*/
const ProjectEntry = ({
hasProjectAccess,
history,
isProjectLoading,
loadOnlyProjectInfo,
match,
projectDetail,
token
}) => {
const projectId = _.get(match, 'params.projectId')
const [resolvedProjectId, setResolvedProjectId] = useState(null)
const [accessDenied, setAccessDenied] = useState(false)
useEffect(() => {
let isActive = true
if (!projectId) {
history.replace('/projects')
return undefined
}
setResolvedProjectId(null)
setAccessDenied(false)
loadOnlyProjectInfo(projectId)
.then(() => {
if (isActive) {
setResolvedProjectId(projectId)
}
})
.catch((error) => {
if (isActive) {
const status = _.get(error, 'payload.response.status', _.get(error, 'response.status'))
if (status === 403) {
setAccessDenied(true)
setResolvedProjectId(projectId)
} else {
history.replace('/projects')
}
}
})
return () => {
isActive = false
}
}, [history, loadOnlyProjectInfo, projectId])
useEffect(() => {
if (!resolvedProjectId || isProjectLoading) {
return
}
// Handle 403 access denied - redirect to challenges page which will show the error
if (accessDenied || !hasProjectAccess) {
history.replace(`/projects/${resolvedProjectId}/challenges`)
return
}
if (`${_.get(projectDetail, 'id', '')}` !== `${resolvedProjectId}`) {
return
}
const destination = checkIsUserInvitedToProject(token, projectDetail)
? `/projects/${resolvedProjectId}/invitation`
: `/projects/${resolvedProjectId}/challenges`
history.replace(destination)
}, [accessDenied, hasProjectAccess, history, isProjectLoading, projectDetail, resolvedProjectId, token])
return <Loader />
}
ProjectEntry.propTypes = {
hasProjectAccess: PropTypes.bool,
history: PropTypes.shape({
replace: PropTypes.func.isRequired
}).isRequired,
isProjectLoading: PropTypes.bool,
loadOnlyProjectInfo: PropTypes.func.isRequired,
match: PropTypes.shape({
params: PropTypes.shape({
projectId: PropTypes.string
})
}).isRequired,
projectDetail: PropTypes.object,
token: PropTypes.string
}
const mapStateToProps = ({ auth, projects }) => ({
hasProjectAccess: projects.hasProjectAccess,
isProjectLoading: projects.isLoading,
projectDetail: projects.projectDetail,
token: auth.token
})
const mapDispatchToProps = {
loadOnlyProjectInfo
}
export default withRouter(
connect(mapStateToProps, mapDispatchToProps)(ProjectEntry)
)