/
ShortLinkForm.js
148 lines (126 loc) · 3.02 KB
/
ShortLinkForm.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
import React, { useState } from 'react'
import styled from 'styled-components'
import fetch from 'isomorphic-unfetch'
export default function ShortLinkForm() {
const [url, setURL] = useState('')
const [notification, setNotification] = useState(null)
const [isFetch, setIsFetch] = useState(false)
async function handleSubmit(event) {
event.preventDefault()
if (isFetch) {
return
}
if (!url || url.indexOf('//nazr.in') > -1) {
setNotification('Invalid URL provided')
return
}
clearNotification()
setIsFetch(true)
try {
const req = await fetch('/api/short_links', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({ url }),
})
setIsFetch(false)
const response = await req.json()
if (response.error) {
return setNotification(response.error)
}
setURL(response.shortURL)
} catch (err) {
setIsFetch(false)
setNotification('Unexpected API response: ' + err.message)
}
}
function clearNotification() {
setNotification(null)
}
return (
<Container>
<Form onSubmit={handleSubmit} onFocus={clearNotification}>
<InputContainer>
<InputIcon>web</InputIcon>
<Input
placeholder="URL"
value={url}
onChange={(event) => setURL(event.target.value)}
/>
</InputContainer>
<SubmitButton>
<SubmitIcon>{isFetch ? 'autorenew' : 'transform'}</SubmitIcon>
</SubmitButton>
</Form>
<Notification onClick={clearNotification}>{notification}</Notification>
</Container>
)
}
const Container = styled.div`
width: 100%;
`
const Form = styled.form`
margin: 30px 0;
display: flex;
flex-flow: row;
background-color: #efefef;
@media screen and (max-width: 600px) {
flex-flow: column;
}
`
const InputContainer = styled.div`
height: 90px;
width: 100%;
display: flex;
justify-self: stretch;
align-items: center;
`
const Input = styled.input`
width: 100%;
margin: 0 15px;
border: 0;
font-size: 20pt;
outline: none;
background-color: transparent;
`
const SubmitButton = styled.button.attrs({ type: 'submit' })`
height: 90px;
display: block;
padding: 10px 32px;
border: 0;
background-color: #f0b233;
cursor: pointer;
&:focus {
outline: 0;
}
&:hover {
background-color: #73706a;
}
`
const Notification = styled.div`
margin: 30px;
padding: 15px;
opacity: ${({ children }) => (children ? '1' : '0')};
transform: translateY(${({ children }) => (children ? '0' : '20px')});
background: #f54949;
color: #fff;
transition: all 0.2s ease-out;
cursor: pointer;
`
const Icon = styled.i.attrs({
className: 'material-icons',
})``
const InputIcon = styled(Icon)`
margin-left: 25px;
font-size: 40px;
color: #9c9c9c;
@media screen and (max-width: 400px) {
display: none;
}
`
const SubmitIcon = styled(Icon)`
font-size: 33px;
color: #fff;
`