All files / client/components/ExternalShare/SecretKeywordForm SecretKeywordFormContainer.tsx

0% Statements 0/31
0% Branches 0/9
0% Functions 0/10
0% Lines 0/31

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                                                                                                                                                                                                                                                                           
// FIXME: This file is not used from any components ...
import React from 'react'
import { withTranslation, WithTranslation } from 'react-i18next'
import { Button, InputGroup, InputGroupAddon, InputGroupText, Col, Input, FormGroup, FormFeedback } from 'reactstrap'
 
import Icon from 'components/Common/Icon'
import Crowi from 'client/util/Crowi'
 
interface Props extends WithTranslation {
  crowi: Crowi
}
 
interface State {
  secretKeyword: string
  error: {
    status: boolean
    message: string
  }
  usingIME: boolean
}
 
class SecretKeywordFormContainer extends React.Component<Props, State> {
  constructor(props: Props) {
    super(props)
 
    this.state = {
      secretKeyword: '',
      error: {
        status: false,
        message: '',
      },
      usingIME: true,
    }
 
    this.setSecretKeyword = this.setSecretKeyword.bind(this)
    this.canSubmit = this.canSubmit.bind(this)
    this.handleError = this.handleError.bind(this)
    this.checkSecretKeyword = this.checkSecretKeyword.bind(this)
    this.handleKeyPress = this.handleKeyPress.bind(this)
    this.handleSubmit = this.handleSubmit.bind(this)
    this.handleKeyUp = this.handleKeyUp.bind(this)
  }
 
  setSecretKeyword(e: React.ChangeEvent<HTMLInputElement>) {
    this.setState({ error: { status: false, message: '' }, secretKeyword: e.target.value })
  }
 
  canSubmit() {
    return this.state.secretKeyword.length > 0
  }
 
  handleError() {
    this.setState({ error: { status: true, message: this.props.t('Incorrect secret keyword') } })
  }
 
  async checkSecretKeyword() {
    const { secretKeyword } = this.state
    const shareId = $('#secret-keyword-form-container').data('share-id')
    const _csrf = $('#secret-keyword-form-container').data('csrftoken')
    try {
      const { hasAccessAuthority = false } = await this.props.crowi.apiPost('/shares/secretKeyword.check', {
        _csrf,
        share_id: shareId,
        secret_keyword: secretKeyword,
      })
      if (hasAccessAuthority) {
        this.setState({ error: { status: false, message: '' } })
        top.location.href = '/_share/' + shareId
      } else {
        this.handleError()
      }
    } catch (err) {
      console.error(err)
    }
  }
 
  isEnterAndNotUsingIME(e: React.KeyboardEvent<HTMLInputElement>) {
    return e.keyCode == 13 && !this.state.usingIME
  }
 
  handleKeyPress(e: React.KeyboardEvent<HTMLInputElement>) {
    // `onKeyPress` event is not triggered if using IME.
    this.setState({ usingIME: false })
  }
 
  async handleKeyUp(e: React.KeyboardEvent<HTMLInputElement>) {
    if (this.isEnterAndNotUsingIME(e)) {
      await this.checkSecretKeyword()
    }
    this.setState({ usingIME: true })
  }
 
  handleSubmit() {
    this.checkSecretKeyword()
  }
 
  render() {
    const { t } = this.props
    const { error } = this.state
    return (
      <Col lg={4} md={6} sm={8} xs={10}>
        <h4 className="page-header">{t('Required secret keyword')}</h4>
        <div>
          <FormGroup>
            <InputGroup>
              <InputGroupAddon addonType="prepend">
                <InputGroupText>
                  <Icon name="key" />
                </InputGroupText>
              </InputGroupAddon>
              <Input
                placeholder="Secret Keyword"
                onChange={this.setSecretKeyword}
                onKeyPress={this.handleKeyPress}
                onKeyUp={this.handleKeyUp}
                invalid={error.status}
              />
              {error.status && <FormFeedback>{error.message}</FormFeedback>}
            </InputGroup>
          </FormGroup>
 
          <FormGroup>
            <Button className="d-block ml-auto" onClick={this.handleSubmit} disabled={!this.canSubmit()}>
              Submit
            </Button>
          </FormGroup>
        </div>
      </Col>
    )
  }
}
 
export default withTranslation()(SecretKeywordFormContainer)