|
|
|
@ -1,5 +1,5 @@ |
|
|
|
import React, { Component } from 'react' |
|
|
|
import { Container, Table } from 'semantic-ui-react' |
|
|
|
import { Container, Table, Button, Icon } from 'semantic-ui-react' |
|
|
|
import toastr from 'toastr' |
|
|
|
import Moment from 'react-moment'; |
|
|
|
|
|
|
|
@ -9,6 +9,10 @@ export default class RecentComponent extends Component { |
|
|
|
} |
|
|
|
|
|
|
|
componentDidMount() { |
|
|
|
this.loadRecentURLs() |
|
|
|
} |
|
|
|
|
|
|
|
loadRecentURLs() { |
|
|
|
fetch('/api/v1/protected/recent', { |
|
|
|
method: 'POST', |
|
|
|
headers: { |
|
|
|
@ -17,13 +21,20 @@ export default class RecentComponent extends Component { |
|
|
|
}) |
|
|
|
.then(res => res.ok ? res.json() : Promise.reject(res.json())) |
|
|
|
.then(recent => this.setState({ recent: recent })) |
|
|
|
.catch(e => e.done(res => toastr.error(`Could not fetch recent shortened URLs: ${res}`))) |
|
|
|
.catch(e => e instanceof Promise ? e.then(error => toastr.error(`Could load recent URLs: ${error.error}`)) : null) |
|
|
|
} |
|
|
|
|
|
|
|
onRowClick(id) { |
|
|
|
this.props.history.push(`/visitors/${id}`) |
|
|
|
} |
|
|
|
|
|
|
|
onEntryDeletion(visit) { |
|
|
|
fetch(visit.DeletionURL) |
|
|
|
.then(res => res.ok ? res.json() : Promise.reject(res.json())) |
|
|
|
.then(() => this.loadRecentURLs()) |
|
|
|
.catch(e => e instanceof Promise ? e.then(error => toastr.error(`Could not delete: ${error.error}`)) : null) |
|
|
|
} |
|
|
|
|
|
|
|
render() { |
|
|
|
const { recent } = this.state |
|
|
|
return ( |
|
|
|
@ -35,14 +46,21 @@ export default class RecentComponent extends Component { |
|
|
|
<Table.HeaderCell>Created</Table.HeaderCell> |
|
|
|
<Table.HeaderCell>Short URL</Table.HeaderCell> |
|
|
|
<Table.HeaderCell>All Clicks</Table.HeaderCell> |
|
|
|
<Table.HeaderCell>Delete</Table.HeaderCell> |
|
|
|
</Table.Row> |
|
|
|
</Table.Header> |
|
|
|
<Table.Body> |
|
|
|
{recent && Object.keys(recent).map(key => <Table.Row key={key} title="Click to view visitor statistics" onClick={this.onRowClick.bind(this, key)}> |
|
|
|
<Table.Cell>{recent[key].Public.URL}</Table.Cell> |
|
|
|
<Table.Cell><Moment>{recent[key].Public.CreatedOn}</Moment></Table.Cell> |
|
|
|
<Table.Cell>{`${window.location.origin}/${key}`}</Table.Cell> |
|
|
|
<Table.Cell>{recent[key].Public.VisitCount}</Table.Cell> |
|
|
|
{recent && Object.keys(recent).map(key => <Table.Row key={key} title="Click to view visitor statistics"> |
|
|
|
<Table.Cell onClick={this.onRowClick.bind(this, key)}>{recent[key].Public.URL}</Table.Cell> |
|
|
|
<Table.Cell onClick={this.onRowClick.bind(this, key)}><Moment>{recent[key].Public.CreatedOn}</Moment></Table.Cell> |
|
|
|
<Table.Cell onClick={this.onRowClick.bind(this, key)}>{`${window.location.origin}/${key}`}</Table.Cell> |
|
|
|
<Table.Cell onClick={this.onRowClick.bind(this, key)}>{recent[key].Public.VisitCount}</Table.Cell> |
|
|
|
<Table.Cell><Button animated='vertical' onClick={this.onEntryDeletion.bind(this, recent[key])}> |
|
|
|
<Button.Content hidden>Delete</Button.Content> |
|
|
|
<Button.Content visible> |
|
|
|
<Icon name='trash' /> |
|
|
|
</Button.Content> |
|
|
|
</Button></Table.Cell> |
|
|
|
</Table.Row>)} |
|
|
|
</Table.Body> |
|
|
|
</Table> |
|
|
|
|