X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=resources%2Fjs%2Fcomponents%2Fpages%2FTechnique.js;h=30eed5b03a4104634ac5f806f3c7a563693b76cb;hb=5a0be9cded90d70dd16a56d30b4804dbd830aa0f;hp=4004707895ad23dc18607e1af5a1758c2b01e3fa;hpb=9c8e6581de9599f34d231a141134af015cb28be5;p=alttp.git diff --git a/resources/js/components/pages/Technique.js b/resources/js/components/pages/Technique.js index 4004707..30eed5b 100644 --- a/resources/js/components/pages/Technique.js +++ b/resources/js/components/pages/Technique.js @@ -4,6 +4,7 @@ import React, { useEffect, useState } from 'react'; 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'; @@ -11,17 +12,47 @@ import ErrorMessage from '../common/ErrorMessage'; 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 Technique = ({ basepath, 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); @@ -60,15 +91,23 @@ const Technique = ({ type }) => { - + + { setShowContentDialog(false); }} + onSubmit={saveContent} + show={showContentDialog} + /> ; }; Technique.propTypes = { + basepath: PropTypes.string, type: PropTypes.string, };