Skip to content
This repository was archived by the owner on Mar 16, 2021. It is now read-only.

Commit 2360ce3

Browse files
committed
refactored getDisplayURL
1 parent 23bb6b3 commit 2360ce3

File tree

5 files changed

+26
-25
lines changed

5 files changed

+26
-25
lines changed

internal/handlers/handlers.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,6 @@ func (h *Handler) setHandlers() error {
163163
h.engine.GET("/api/v1/info", h.handleInfo)
164164
h.engine.GET("/d/:id/:hash", h.handleDelete)
165165
h.engine.GET("/ok", h.handleHealthcheck)
166-
h.engine.GET("/displayurl", h.handleDisplayURL)
167166

168167
// Handling the shorted URLs, if no one exists, it checks
169168
// in the filesystem and sets headers for caching

web/src/Home/Home.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,22 @@ import './Home.css'
1111

1212
export default class HomeComponent extends Component {
1313
constructor(props) {
14-
super(props);
15-
this.urlParams = new URLSearchParams(window.location.search);
16-
this.state = {
17-
links: [],
18-
usedSettings: this.urlParams.get('customUrl') ? ['custom'] : [],
19-
customID: this.urlParams.get('customUrl') ? this.urlParams.get('customUrl') : '',
20-
showCustomIDError: false,
21-
expiration: null,
22-
displayURL: window.location.origin
23-
}
14+
super(props);
15+
this.urlParams = new URLSearchParams(window.location.search);
16+
this.state = {
17+
links: [],
18+
usedSettings: this.urlParams.get('customUrl') ? ['custom'] : [],
19+
customID: this.urlParams.get('customUrl') ? this.urlParams.get('customUrl') : '',
20+
showCustomIDError: false,
21+
expiration: null,
22+
displayURL: window.location.origin
23+
}
2424
}
2525
handleURLChange = (e, { value }) => this.url = value
2626
handlePasswordChange = (e, { value }) => this.password = value
2727
handleCustomExpirationChange = expire => this.setState({ expiration: expire })
2828
handleCustomIDChange = (e, { value }) => {
29-
this.setState({customID: value})
29+
this.setState({ customID: value })
3030
util.lookupEntry(value, () => this.setState({ showCustomIDError: true }), () => this.setState({ showCustomIDError: false }))
3131
}
3232
onSettingsChange = (e, { value }) => {
@@ -36,9 +36,8 @@ export default class HomeComponent extends Component {
3636

3737
componentDidMount() {
3838
this.urlInput.focus()
39-
fetch("/displayurl")
40-
.then(response => response.json())
41-
.then(data => this.setState({displayURL: data}));
39+
util.getDisplayURL()
40+
.then(displayURL => this.setState({ displayURL }));
4241
}
4342
handleURLSubmit = () => {
4443
if (!this.state.showCustomIDError) {
@@ -71,7 +70,7 @@ export default class HomeComponent extends Component {
7170
{this.urlParams.get("customUrl") ? (
7271
<Header size='medium'>I don't have a link named <em>"{this.urlParams.get("customUrl")}"</em> in my database, would
7372
you like to create one?</Header>
74-
) :
73+
) :
7574
<Header size='huge'>Simplify your links</Header>
7675
}
7776
<Form onSubmit={this.handleURLSubmit} autoComplete="off">
@@ -91,7 +90,7 @@ export default class HomeComponent extends Component {
9190
</MediaQuery>
9291
<Form.Group style={{ marginBottom: "1rem" }}>
9392
{usedSettings.includes("custom") && <Form.Field error={showCustomIDError} width={16}>
94-
<Input label={this.state.displayURL + "/"} onChange={this.handleCustomIDChange} placeholder='my-shortened-url' value={this.state.customID}/>
93+
<Input label={this.state.displayURL + "/"} onChange={this.handleCustomIDChange} placeholder='my-shortened-url' value={this.state.customID} />
9594
</Form.Field>}
9695
</Form.Group>
9796
<Form.Group widths="equal">

web/src/Lookup/Lookup.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ export default class LookupComponent extends Component {
1010
displayURL: window.location.origin
1111
}
1212
componentDidMount() {
13-
fetch("/displayurl")
14-
.then(response => response.json())
15-
.then(data => this.setState({displayURL: data}));
13+
util.getDisplayURL()
14+
.then(displayURL => this.setState({ displayURL }));
1615
}
1716
handleURLChange = (e, { value }) => this.url = value
1817
handleURLSubmit = () => {

web/src/Recent/Recent.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ export default class RecentComponent extends Component {
1414

1515
componentDidMount() {
1616
this.loadRecentURLs()
17-
fetch("/displayurl")
18-
.then(response => response.json())
19-
.then(data => this.setState({displayURL: data}));
17+
util.getDisplayURL()
18+
.then(displayURL => this.setState({ displayURL }));
2019
}
2120

2221
loadRecentURLs = () => {

web/src/util/util.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import toastr from 'toastr'
22

33
export default class UtilHelper {
44
static deleteEntry(url, cb) {
5-
fetch(url, {credentials: "include"})
5+
fetch(url, { credentials: "include" })
66
.then(res => res.ok ? res.json() : Promise.reject(res.json()))
77
.then(cb())
88
.catch(e => this._reportError(e, "delete entry"))
@@ -42,7 +42,7 @@ export default class UtilHelper {
4242
this._constructFetch("/api/v1/protected/visitors", { ID }, cbSucc)
4343
}
4444
static createEntry(entry, cbSucc) {
45-
this._constructFetch("/api/v1/protected/create",entry, cbSucc)
45+
this._constructFetch("/api/v1/protected/create", entry, cbSucc)
4646
}
4747
static getRecentURLs(cbSucc) {
4848
fetch('/api/v1/protected/recent', {
@@ -56,4 +56,9 @@ export default class UtilHelper {
5656
.then(res => cbSucc ? cbSucc(res) : null)
5757
.catch(e => this._reportError(e, "recent"))
5858
}
59+
static getDisplayURL() {
60+
return fetch("/api/v1/displayURL")
61+
.then(res => res.ok ? res.json() : Promise.reject(res.json()))
62+
.catch(e => this._reportError(e, "getDisplayURL"))
63+
}
5964
}

0 commit comments

Comments
 (0)