This repository has been archived on 2024-10-29. You can view files and clone it, but cannot push or open issues or pull requests.
FunctionProject/website/components/FunctionAdmin/EditArticleFunction.jsx

50 lines
1.3 KiB
React
Raw Permalink Normal View History

2020-08-03 12:04:07 +02:00
import { useState } from 'react'
import dynamic from 'next/dynamic'
import { complex } from '../../utils/sunEditorConfig'
import api from '../../utils/api'
import FunctionArticle from '../FunctionPage/FunctionArticle'
2020-08-03 14:14:45 +02:00
const SunEditor = dynamic(() => import('suneditor-react'), { ssr: false })
2020-08-03 14:14:45 +02:00
const EditArticleFunction = props => {
2020-08-03 12:04:07 +02:00
const [htmlContent, setHtmlContent] = useState('')
2020-08-03 14:14:45 +02:00
const handleEditorChange = content => {
2020-08-03 12:04:07 +02:00
setHtmlContent(content)
}
2020-08-03 14:14:45 +02:00
const handleSave = async content => {
2020-08-03 12:04:07 +02:00
let Notyf
if (typeof window !== 'undefined') {
Notyf = require('notyf')
}
2020-08-03 12:04:07 +02:00
const notyf = new Notyf.Notyf({
duration: 5000
})
try {
2020-08-03 14:14:45 +02:00
await api.put(
`/admin/functions/article/${props.functionInfo.id}`,
{ article: content },
{ headers: { Authorization: props.user.token } }
)
2020-08-03 12:04:07 +02:00
notyf.success('Sauvegardé!')
} catch {
notyf.error('Erreur!')
}
2020-08-03 12:04:07 +02:00
}
2020-08-03 12:04:07 +02:00
return (
<div className='container-fluid'>
2020-08-03 14:14:45 +02:00
<SunEditor
setContents={props.functionInfo.article}
lang='fr'
onChange={handleEditorChange}
setOptions={{ buttonList: complex, callBackSave: handleSave }}
/>
2020-08-03 12:04:07 +02:00
<FunctionArticle article={htmlContent} />
</div>
)
}
2020-08-03 12:04:07 +02:00
export default EditArticleFunction