All files / client/components/ExternalShare SettingModal.tsx

0% Statements 0/40
0% Branches 0/20
0% Functions 0/13
0% Lines 0/39

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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196                                                                                                                                                                                                                                                                                                                                                                                                       
import React from 'react'
import { withTranslation, WithTranslation } from 'react-i18next'
import Icon from 'components/Common/Icon'
import DeleteConfirmModal from './DeleteConfirmModal'
import { Button, Container, Row, Col, Label, Input, CustomInput, Modal, ModalHeader, ModalBody, ModalFooter } from 'reactstrap'
import Crowi from 'client/util/Crowi'
import { Share } from 'client/types/crowi'
 
interface Props extends WithTranslation {
  share: Share | null
  show: boolean
  onHide: () => void
  isChanging: boolean
  handleDelete: Function
  crowi: Crowi
}
 
interface State {
  shareId: string | null
  secretKeyword: string | null
  restricted: boolean
  showConfirmModal: boolean
  result: {
    show: boolean
    error: boolean
    message: string
  }
}
 
class SettingModal extends React.Component<Props, State> {
  static defaultProps = { show: false }
 
  constructor(props: Props) {
    super(props)
 
    this.state = {
      shareId: null,
      secretKeyword: null,
      restricted: false,
      showConfirmModal: false,
      result: {
        show: false,
        error: false,
        message: '',
      },
    }
 
    this.setRestricted = this.setRestricted.bind(this)
    this.setSecretKeyword = this.setSecretKeyword.bind(this)
    this.handleSubmit = this.handleSubmit.bind(this)
    this.handleOpen = this.handleOpen.bind(this)
    this.handleClose = this.handleClose.bind(this)
    this.handleCloseAll = this.handleCloseAll.bind(this)
    this.renderResult = this.renderResult.bind(this)
  }
 
  componentDidUpdate() {
    const { share } = this.props
    if (!share) {
      return
    }
 
    const { uuid: shareId, secretKeyword } = share
 
    if (shareId !== this.state.shareId) {
      this.setState({
        shareId,
        secretKeyword,
        restricted: !!secretKeyword,
      })
    }
  }
 
  setRestricted(value: boolean) {
    return () => {
      this.setState({ restricted: value })
    }
  }
 
  setSecretKeyword(e: React.ChangeEvent<HTMLInputElement>) {
    this.setState({ secretKeyword: e.target.value })
  }
 
  canSubmit() {
    const { restricted, secretKeyword } = this.state
    if (!restricted) {
      return true
    }
    return !!secretKeyword && secretKeyword.length > 0
  }
 
  async handleSubmit() {
    const { shareId, secretKeyword, restricted } = this.state
    try {
      await this.props.crowi.apiPost('/shares/secretKeyword.set', {
        share_id: shareId,
        secret_keyword: restricted ? secretKeyword : null,
      })
      this.setState({ result: { show: true, error: false, message: this.props.t('share.setting.saved') } })
      setTimeout(() => this.setState({ result: { show: false, error: false, message: '' } }), 1000)
    } catch (err) {
      this.setState({ result: { show: true, error: true, message: this.props.t('share.setting.error.message') } })
    }
  }
 
  handleOpen() {
    this.setState({ showConfirmModal: true })
  }
 
  handleClose() {
    this.setState({ showConfirmModal: false })
  }
 
  handleCloseAll() {
    const { onHide } = this.props
    onHide()
    this.handleClose()
  }
 
  renderResult() {
    const {
      result: { show, error, message },
    } = this.state
    return (
      show && (
        <div
          style={{
            display: 'inline-block',
            marginRight: 20,
          }}
        >
          <span className={error ? 'text-danger' : 'text-success'}>{message}</span>
        </div>
      )
    )
  }
 
  render() {
    const { t, show, onHide, isChanging, handleDelete } = this.props
    const { restricted, secretKeyword, showConfirmModal } = this.state
    return (
      <Modal className="share-setting-modal" isOpen={show} toggle={onHide}>
        <ModalHeader toggle={onHide}>{t('share.link_settings')}</ModalHeader>
        <ModalBody>
          <Container>
            <Row>
              <Col sm={2}>
                <Label>{t('share.setting.restrict_access')}</Label>
              </Col>
              <Col sm={10}>
                <CustomInput
                  type="radio"
                  id="not_restricted"
                  name="restricted"
                  onClick={this.setRestricted(false)}
                  defaultChecked={!restricted}
                  label={t('share.setting.people_who_know_this_link')}
                />
                {}
                <CustomInput
                  type="radio"
                  id="restricted"
                  name="restricted"
                  onClick={this.setRestricted(true)}
                  defaultChecked={restricted}
                  label={t('share.setting.people_who_know_this_secret_keyword')}
                />
                {restricted && (
                  <Input
                    className="secret-keyword"
                    placeholder={t('share.setting.secret_keyword')}
                    onChange={this.setSecretKeyword}
                    defaultValue={secretKeyword || ''}
                  />
                )}
              </Col>
            </Row>
          </Container>
          <DeleteConfirmModal show={showConfirmModal} onHide={this.handleClose} handleClose={this.handleCloseAll} handleDelete={handleDelete} />
        </ModalBody>
        <ModalFooter>
          <Button className="mr-auto" onClick={this.handleOpen} color="danger" disabled={isChanging}>
            <Icon name={isChanging ? 'loading' : 'linkOff'} spin={isChanging} /> {t('share.delete_link')}
          </Button>
          {this.renderResult()}
          <Button onClick={this.handleSubmit} color="primary" disabled={!this.canSubmit()}>
            {t('share.setting.save_settings')}
          </Button>
        </ModalFooter>
      </Modal>
    )
  }
}
 
export default withTranslation()(SettingModal)