2024-10-16 11:09:30 +08:00
|
|
|
import React, { useState, useCallback } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2024-11-06 20:12:39 +08:00
|
|
|
import { ModalBody, ModalFooter, Button } from 'reactstrap';
|
2024-10-16 15:12:23 +08:00
|
|
|
import classnames from 'classnames';
|
|
|
|
import Switch from '../../../../components/common/switch';
|
2024-10-16 11:09:30 +08:00
|
|
|
import { gettext } from '../../../../utils/constants';
|
|
|
|
import metadataAPI from '../../../api';
|
|
|
|
import toaster from '../../../../components/toast';
|
|
|
|
import { Utils } from '../../../../utils/utils';
|
|
|
|
|
2024-10-16 15:12:23 +08:00
|
|
|
import './index.css';
|
|
|
|
|
2024-11-06 20:12:39 +08:00
|
|
|
const MetadataFaceRecognitionDialog = ({ value: oldValue, repoID, toggleDialog: toggle, submit }) => {
|
2024-10-16 15:12:23 +08:00
|
|
|
const [value, setValue] = useState(oldValue);
|
2024-10-16 11:09:30 +08:00
|
|
|
const [submitting, setSubmitting] = useState(false);
|
|
|
|
|
|
|
|
const onToggle = useCallback(() => {
|
|
|
|
toggle();
|
|
|
|
}, [toggle]);
|
|
|
|
|
|
|
|
const onSubmit = useCallback(() => {
|
|
|
|
setSubmitting(true);
|
|
|
|
metadataAPI.openFaceRecognition(repoID).then(res => {
|
|
|
|
submit(true);
|
|
|
|
toggle();
|
|
|
|
}).catch(error => {
|
|
|
|
const errorMsg = Utils.getErrorMsg(error);
|
|
|
|
toaster.danger(errorMsg);
|
|
|
|
setSubmitting(false);
|
|
|
|
});
|
|
|
|
}, [repoID, submit, toggle]);
|
|
|
|
|
2024-10-16 15:12:23 +08:00
|
|
|
const onValueChange = useCallback(() => {
|
|
|
|
const nextValue = !value;
|
|
|
|
setValue(nextValue);
|
|
|
|
}, [value]);
|
|
|
|
|
2024-10-16 11:09:30 +08:00
|
|
|
return (
|
2024-11-06 20:12:39 +08:00
|
|
|
<>
|
|
|
|
<ModalBody className="metadata-face-recognition-dialog">
|
2024-10-16 15:12:23 +08:00
|
|
|
<Switch
|
|
|
|
checked={value}
|
|
|
|
disabled={submitting || oldValue}
|
|
|
|
size="large"
|
|
|
|
textPosition="right"
|
|
|
|
className={classnames('change-face-recognition-status-management w-100', { 'disabled': submitting || oldValue })}
|
|
|
|
onChange={onValueChange}
|
|
|
|
placeholder={gettext('Face recognition')}
|
|
|
|
/>
|
2024-11-06 20:12:39 +08:00
|
|
|
<p className="tip m-0">
|
2024-10-16 15:12:23 +08:00
|
|
|
{gettext('Enable face recognition to identify people in your photos.')}
|
2024-11-06 20:12:39 +08:00
|
|
|
</p>
|
2024-10-16 11:09:30 +08:00
|
|
|
</ModalBody>
|
2024-10-16 15:12:23 +08:00
|
|
|
{!oldValue && (
|
2024-10-16 11:09:30 +08:00
|
|
|
<ModalFooter>
|
|
|
|
<Button color="secondary" onClick={onToggle}>{gettext('Cancel')}</Button>
|
2024-10-16 15:12:23 +08:00
|
|
|
<Button color="primary" disabled={oldValue === value || submitting} onClick={onSubmit}>{gettext('Submit')}</Button>
|
2024-10-16 11:09:30 +08:00
|
|
|
</ModalFooter>
|
|
|
|
)}
|
2024-11-06 20:12:39 +08:00
|
|
|
</>
|
2024-10-16 11:09:30 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
MetadataFaceRecognitionDialog.propTypes = {
|
|
|
|
value: PropTypes.bool.isRequired,
|
|
|
|
repoID: PropTypes.string.isRequired,
|
2024-11-06 20:12:39 +08:00
|
|
|
toggleDialog: PropTypes.func.isRequired,
|
2024-10-16 11:09:30 +08:00
|
|
|
submit: PropTypes.func.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default MetadataFaceRecognitionDialog;
|