/
SharePage.js
237 lines (209 loc) · 5.06 KB
/
SharePage.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import PropTypes from 'prop-types'
import React, { useState } from 'react'
import { useQuery, gql } from '@apollo/client'
import { Route, Switch } from 'react-router-dom'
import RouterProps from 'react-router-prop-types'
import { Form, Header, Icon, Input, Message } from 'semantic-ui-react'
import styled from 'styled-components'
import { getSharePassword, saveSharePassword } from '../../authentication'
import AlbumSharePage from './AlbumSharePage'
import MediaSharePage from './MediaSharePage'
export const SHARE_TOKEN_QUERY = gql`
query SharePageToken($token: String!, $password: String) {
shareToken(token: $token, password: $password) {
token
album {
...AlbumProps
subAlbums {
...AlbumProps
subAlbums {
...AlbumProps
}
}
}
media {
...MediaProps
}
}
}
fragment AlbumProps on Album {
id
title
thumbnail {
thumbnail {
url
}
}
media(filter: { order_by: "title", order_direction: DESC }) {
...MediaProps
}
}
fragment MediaProps on Media {
id
title
type
thumbnail {
url
width
height
}
downloads {
title
mediaUrl {
url
width
height
fileSize
}
}
highRes {
url
}
videoWeb {
url
}
exif {
camera
maker
lens
dateShot
exposure
aperture
iso
focalLength
flash
exposureProgram
}
}
`
export const VALIDATE_TOKEN_PASSWORD_QUERY = gql`
query ShareTokenValidatePassword($token: String!, $password: String) {
shareTokenValidatePassword(token: $token, password: $password)
}
`
const AuthorizedTokenRoute = ({ match }) => {
const token = match.params.token
const { loading, error, data } = useQuery(SHARE_TOKEN_QUERY, {
variables: {
token,
password: getSharePassword(token),
},
})
if (error) return error.message
if (loading) return 'Loading...'
if (data.shareToken.album) {
return <AlbumSharePage album={data.shareToken.album} match={match} />
}
if (data.shareToken.media) {
return <MediaSharePage media={data.shareToken.media} />
}
return <h1>Share not found</h1>
}
AuthorizedTokenRoute.propTypes = {
match: PropTypes.object.isRequired,
}
const MessageContainer = styled.div`
max-width: 400px;
margin: 100px auto 0;
`
const ProtectedTokenEnterPassword = ({
refetchWithPassword,
loading = false,
}) => {
const [passwordValue, setPasswordValue] = useState('')
const [invalidPassword, setInvalidPassword] = useState(false)
const onSubmit = () => {
refetchWithPassword(passwordValue)
setInvalidPassword(true)
}
let errorMessage = null
if (invalidPassword && !loading) {
errorMessage = (
<Message negative>
<Message.Content>Wrong password, please try again.</Message.Content>
</Message>
)
}
return (
<MessageContainer>
<Header as="h1" style={{ fontWeight: 400 }}>
Protected share
</Header>
<p>This share is protected with a password.</p>
<Form>
<Form.Field>
<label>Password</label>
<Input
loading={loading}
disabled={loading}
onKeyUp={event => event.key == 'Enter' && onSubmit()}
onChange={e => setPasswordValue(e.target.value)}
placeholder="Password"
type="password"
icon={<Icon onClick={onSubmit} link name="arrow right" />}
/>
</Form.Field>
{errorMessage}
</Form>
</MessageContainer>
)
}
ProtectedTokenEnterPassword.propTypes = {
refetchWithPassword: PropTypes.func.isRequired,
loading: PropTypes.bool,
}
const TokenRoute = ({ match }) => {
const token = match.params.token
const { loading, error, data, refetch } = useQuery(
VALIDATE_TOKEN_PASSWORD_QUERY,
{
notifyOnNetworkStatusChange: true,
variables: {
token: match.params.token,
password: getSharePassword(match.params.token),
},
}
)
if (error) {
if (error.message == 'GraphQL error: share not found') {
return (
<MessageContainer>
<h1>Share not found</h1>
<p>Maybe the share has expired or has been deleted.</p>
</MessageContainer>
)
}
return error.message
}
if (data && data.shareTokenValidatePassword == false) {
return (
<ProtectedTokenEnterPassword
match={match}
refetchWithPassword={password => {
saveSharePassword(token, password)
refetch({ token, password })
}}
loading={loading}
/>
)
}
if (loading) return 'Loading...'
return <AuthorizedTokenRoute match={match} />
}
TokenRoute.propTypes = {
match: PropTypes.object.isRequired,
}
const SharePage = ({ match }) => (
<Switch>
<Route path={`${match.url}/:token`}>
{({ match }) => {
return <TokenRoute match={match} />
}}
</Route>
<Route path="/">Route not found</Route>
</Switch>
)
SharePage.propTypes = {
...RouterProps,
}
export default SharePage