2018-12-07 02:46:47 +00:00
import React , { Fragment } from 'react' ;
import PropTypes from 'prop-types' ;
import { gettext } from '../../utils/constants' ;
2018-12-10 04:05:54 +00:00
import toaster from '../toast' ;
2018-12-07 02:46:47 +00:00
import { Button , Modal , ModalHeader , ModalBody , Form , FormGroup , Label , Input } from 'reactstrap' ;
import { seafileAPI } from '../../utils/seafile-api.js' ;
const propTypes = {
itemName : PropTypes . string . isRequired ,
toggleDialog : PropTypes . func . isRequired ,
repoID : PropTypes . string . isRequired ,
} ;
class LibHistorySetting extends React . Component {
constructor ( props ) {
super ( props ) ;
this . state = {
keepDays : - 1 ,
expireDays : 30 ,
disabled : true ,
allHistory : true ,
noHistory : false ,
autoHistory : false ,
errorInfo : ''
} ;
}
componentDidMount ( ) {
2018-12-08 10:29:13 +00:00
seafileAPI . getRepoHistoryLimit ( this . props . repoID ) . then ( res => {
2018-12-07 02:46:47 +00:00
this . setState ( {
keepDays : res . data . keep _days ,
allHistory : res . data . keep _days < 0 ? true : false ,
noHistory : res . data . keep _days === 0 ? true : false ,
autoHistory : res . data . keep _days > 0 ? true : false ,
disabled : res . data . keep _days > 0 ? false : true ,
expireDays : res . data . keep _days > 0 ? res . data . keep _days : 30 ,
} ) ;
} ) ;
}
submit = ( ) => {
let days = this . state . keepDays ;
let repoID = this . props . repoID ;
let reg = /^-?\d+$/ ;
let flag = reg . test ( days ) ;
if ( flag ) {
2018-12-10 04:05:54 +00:00
let message = gettext ( 'Successfully set library history.' ) ;
2018-12-08 10:29:13 +00:00
seafileAPI . setRepoHistoryLimit ( repoID , days ) . then ( res => {
2018-12-10 04:05:54 +00:00
toaster . success ( message ) ;
2018-12-07 02:46:47 +00:00
this . setState ( {
keepDays : res . data . keep _days
} ) ;
} ) ;
this . props . toggleDialog ( ) ;
} else {
this . setState ( {
errorInfo : gettext ( 'Please enter a non-negative integer' ) ,
} )
}
}
onChange = ( e ) => {
let num = e . target . value ;
this . setState ( {
keepDays : num ,
expireDays : num ,
} ) ;
}
setLimitDays = ( type ) => {
if ( type === 'allHistory' ) {
this . setState ( {
keepDays : - 1 ,
} ) ;
} else if ( type === 'noHistory' ) {
this . setState ( {
keepDays : 0 ,
} ) ;
} else {
this . setState ( {
disabled : false
} ) ;
}
this . setState ( {
allHistory : type === 'allHistory' ? true : false ,
noHistory : type === 'noHistory' ? true : false ,
autoHistory : type === 'autoHistory' ? true : false ,
} ) ;
}
render ( ) {
const itemName = this . props . itemName ;
return (
< Modal isOpen = { true } centered = { true } >
< ModalHeader toggle = { this . props . toggleDialog } >
< span className = "sf-font" title = { itemName } > { itemName } < / s p a n > { ' ' } { g e t t e x t ( ' H i s t o r y S e t t i n g ' ) }
< / M o d a l H e a d e r >
< ModalBody >
< Form >
< FormGroup check >
< Label check >
< Input type = "radio" name = "radio1" checked = { this . state . allHistory } onChange = { ( ) => { this . setLimitDays ( 'allHistory' ) } } / > { ' ' } { gettext ( 'Keep full history' ) }
< / L a b e l >
< / F o r m G r o u p >
< FormGroup check >
< Label check >
< Input type = "radio" name = "radio1" checked = { this . state . noHistory } onChange = { ( ) => { this . setLimitDays ( 'noHistory' ) } } / > { ' ' } { gettext ( 'Don\'t keep history' ) }
< / L a b e l >
< / F o r m G r o u p >
< FormGroup check >
< Label check >
< Input type = "radio" name = "radio1" checked = { this . state . autoHistory } onChange = { ( ) => { this . setLimitDays ( 'autoHistory' ) } } / > { ' ' } { gettext ( 'Only keep a period of history:' ) }
< Input className = "expire-input" type = "text"
value = { this . state . expireDays }
onChange = { this . onChange }
disabled = { this . state . disabled }
/ >
< span > { gettext ( 'days' ) } < / s p a n >
< / L a b e l >
< / F o r m G r o u p >
< Label className = "err-message" > { this . state . errorInfo } < / L a b e l >
< br / >
< Button onClick = { this . submit } > { gettext ( 'Submit' ) } < / B u t t o n >
< / F o r m >
< / M o d a l B o d y >
< / M o d a l >
) ;
}
}
LibHistorySetting . propTypes = propTypes ;
export default LibHistorySetting ;