1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-07-19 01:29:05 +00:00
seahub/frontend/src/components/dialog/lib-settings/lib-old-files-auto-del-setting-panel.js

131 lines
3.7 KiB
JavaScript
Raw Normal View History

import React from 'react';
import PropTypes from 'prop-types';
import { Button, ModalBody, ModalFooter, Form, FormGroup, Label, Input, Alert } from 'reactstrap';
import { gettext } from '../../../utils/constants';
import { seafileAPI } from '../../../utils/seafile-api';
import { Utils } from '../../../utils/utils';
import toaster from '../../toast';
const propTypes = {
toggleDialog: PropTypes.func.isRequired,
repoID: PropTypes.string.isRequired,
};
class LibOldFilesAutoDelSetting extends React.Component {
constructor(props) {
super(props);
this.state = {
autoDelDays: 0,
isAutoDel: false,
errorInfo: '',
};
}
componentDidMount() {
seafileAPI.getRepoOldFilesAutoDelDays(this.props.repoID).then(res => {
this.setState({
autoDelDays: res.data.auto_delete_days,
isAutoDel: res.data.auto_delete_days > 0,
});
}).catch(error => {
let errMessage = Utils.getErrorMsg(error);
toaster.danger(errMessage);
});
}
submit = () => {
2021-10-09 07:21:37 +00:00
let daysNeedTobeSet;
2021-10-09 07:21:37 +00:00
if (this.state.isAutoDel) {
daysNeedTobeSet = this.state.autoDelDays;
2021-10-09 07:21:37 +00:00
let reg = /^-?\d+$/;
let isvalid_days = reg.test(daysNeedTobeSet);
if (!isvalid_days || daysNeedTobeSet <= 0) {
this.setState({
errorInfo: gettext('Please enter a positive integer'),
});
return;
}
} else {
2024-07-18 03:58:42 +00:00
daysNeedTobeSet = 0; // if no auto del, give 0 to server
}
2021-10-09 07:21:37 +00:00
let repoID = this.props.repoID;
seafileAPI.setRepoOldFilesAutoDelDays(repoID, daysNeedTobeSet).then(res => {
this.props.toggleDialog();
toaster.success(gettext('Successfully set it.'));
}).catch(error => {
let errMessage = Utils.getErrorMsg(error);
toaster.danger(errMessage);
});
};
handleKeyDown = (e) => {
if (e.key === 'Enter') {
this.submit();
e.preventDefault();
}
};
onChange = (e) => {
let days = e.target.value;
this.setState({
autoDelDays: days,
});
};
updateRadioCheck = (type) => {
if (type === 'noAutoDel') {
this.setState({
isAutoDel: false,
});
} else if (type === 'autoDel') {
this.setState({
isAutoDel: true,
});
}
};
render() {
return (
<>
<ModalBody>
<Form>
<FormGroup check>
2024-07-18 03:58:42 +00:00
<Input type="radio" name="radio1" checked={!this.state.isAutoDel} onChange={() => {this.updateRadioCheck('noAutoDel');}}/>{' '}
<Label>{gettext('Do not automatically delete files')}</Label>
</FormGroup>
<FormGroup check>
2024-07-18 03:58:42 +00:00
<Input type="radio" name="radio1" checked={this.state.isAutoDel} onChange={() => {this.updateRadioCheck('autoDel');}}/>{' '}
<Label>{gettext('Automatically delete files that are not modified within certain days:')}</Label>
<Input
type="text"
className="expire-input"
value={this.state.autoDelDays}
2021-10-09 07:21:37 +00:00
disabled={!this.state.isAutoDel}
onChange={this.onChange}
onKeyDown={this.handleKeyDown}
/>{' '}
<Label><span>{gettext('days')}</span></Label>
</FormGroup>
{this.state.errorInfo && <Alert color="danger">{this.state.errorInfo}</Alert>}
</Form>
</ModalBody>
<ModalFooter>
<Button color="secondary" onClick={this.props.toggleDialog}>{gettext('Cancel')}</Button>
<Button color="primary" onClick={this.submit}>{gettext('Submit')}</Button>
</ModalFooter>
</>
);
}
}
LibOldFilesAutoDelSetting.propTypes = propTypes;
export default LibOldFilesAutoDelSetting;