import { Helmet } from 'react-helmet';
import { withTranslation } from 'react-i18next';
import { useParams } from 'react-router-dom';
+import toastr from 'toastr';
import CanonicalLinks from '../common/CanonicalLinks';
import ErrorBoundary from '../common/ErrorBoundary';
import Loading from '../common/Loading';
import NotFound from '../pages/NotFound';
import Detail from '../techniques/Detail';
+import Dialog from '../techniques/Dialog';
+import {
+ mayEditContent,
+} from '../../helpers/permissions';
import { getLanguages, getMatchedLocale, getTranslation } from '../../helpers/Technique';
+import { useUser } from '../../helpers/UserContext';
import i18n from '../../i18n';
const Technique = ({ type }) => {
const params = useParams();
const { name } = params;
+ const user = useUser();
const [error, setError] = useState(null);
const [loading, setLoading] = useState(true);
const [technique, setTechnique] = useState(null);
+ const [editContent, setEditContent] = useState(null);
+ const [showContentDialog, setShowContentDialog] = useState(false);
+
+ const actions = React.useMemo(() => ({
+ editContent: mayEditContent(user) ? content => {
+ setEditContent(content);
+ setShowContentDialog(true);
+ } : null,
+ }), [user]);
+
+ const saveContent = React.useCallback(async values => {
+ try {
+ const response = await axios.put(`/api/content/${values.id}`, {
+ parent_id: technique.id,
+ ...values,
+ });
+ toastr.success(i18n.t('content.saveSuccess'));
+ setTechnique(response.data);
+ setShowContentDialog(false);
+ } catch (e) {
+ toastr.error(i18n.t('content.saveError'));
+ }
+ }, [technique && technique.id]);
+
useEffect(() => {
const ctrl = new AbortController();
setLoading(true);
lang={getMatchedLocale(technique, i18n.language)}
langs={getLanguages(technique)}
/>
- <Detail technique={technique} />
+ <Detail actions={actions} technique={technique} />
+ <Dialog
+ content={editContent}
+ language={i18n.language}
+ onHide={() => { setShowContentDialog(false); }}
+ onSubmit={saveContent}
+ show={showContentDialog}
+ />
</ErrorBoundary>;
};