All files / client/components/Admin/Share ShareList.tsx

0% Statements 0/27
0% Branches 0/14
0% Functions 0/9
0% Lines 0/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                                                                                                                                                                                               
import React from 'react'
import { withTranslation, WithTranslation } from 'react-i18next'
import moment from 'moment'
import { Table, Alert } from 'reactstrap'
import Pagination from 'components/Common/Pagination'
import Crowi from 'client/util/Crowi'
import { Share } from 'client/types/crowi'
 
interface Props extends WithTranslation {
  crowi: Crowi
}
 
interface State {
  shares: Share[]
  pagination: {
    current: number
    count: number
    limit: number
  }
  error: boolean
}
 
interface Record {
  index: number
  path: string
  username: string
  name: string
  date: string
  isActive: boolean
}
 
class ShareList extends React.Component<Props, State> {
  constructor(props: Props) {
    super(props)
 
    this.state = {
      shares: [],
      pagination: {
        current: 0,
        count: 0,
        limit: 20,
      },
      error: false,
    }
 
    this.getPage = this.getPage.bind(this)
    this.movePage = this.movePage.bind(this)
    this.renderTableBody = this.renderTableBody.bind(this)
  }
 
  async getPage(options = {}) {
    const limit = this.state.pagination.limit
    try {
      const { share } = await this.props.crowi.apiGet('/shares.list', { ...options, limit })
      const { docs: shares, page: current, pages: count } = share
      const pagination = { current, count, limit }
      this.setState({ shares, pagination })
    } catch (err) {
      this.setState({ error: true })
    }
  }
 
  movePage(i: number) {
    if (i !== this.state.pagination.current) {
      this.getPage({ page: i })
    }
  }
 
  componentDidMount() {
    this.getPage()
  }
 
  renderStatus(isActive: boolean) {
    const className = ['badge', isActive ? 'badge-success' : 'badge-danger'].join(' ')
    const text = isActive ? 'Active' : 'Inactive'
    return <span className={className}>{text}</span>
  }
 
  renderRecord({ index, path, username, name, date, isActive }: Record) {
    return (
      <tr key={index}>
        <td>{index}</td>
        <td>{path ? <a href={path}>{path}</a> : '(Deleted)'}</td>
        <td>
          <a href={`/user/${username}`}>{name}</a>
        </td>
        <td>{date}</td>
        <td>{this.renderStatus(isActive)}</td>
      </tr>
    )
  }
 
  renderTableBody() {
    const { current, limit } = this.state.pagination
    const start = (current - 1) * limit + 1
    return (
      <tbody>
        {this.state.shares.map(({ page, creator: { username, name }, status, createdAt }, i) => {
          const { path = '' } = page || {}
          return this.renderRecord({
            index: start + i,
            path,
            username,
            name,
            date: moment(createdAt).format('llll'),
            isActive: status === 'active',
          })
        })}
      </tbody>
    )
  }
 
  render() {
    const { t } = this.props
    const {
      pagination: { current, count },
      error,
    } = this.state
    return error ? (
      <Alert color="danger">
        <p>{t('share_list.error.message')}</p>
      </Alert>
    ) : (
      <div>
        <Table bordered hover size="sm">
          <thead>
            <tr>
              <th>#</th>
              <th>{t('Page name')}</th>
              <th>{t('Creator')}</th>
              <th>{t('Created')}</th>
              <th>{t('Status')}</th>
            </tr>
          </thead>
          {this.renderTableBody()}
        </Table>
        <Pagination current={current} count={count} onClick={this.movePage} />
      </div>
    )
  }
}
 
export default withTranslation()(ShareList)