2020-08-03 12:04:07 +02:00
|
|
|
|
import { useState } from 'react'
|
|
|
|
|
import Cookies from 'universal-cookie'
|
|
|
|
|
import SwipeableViews from 'react-swipeable-views'
|
|
|
|
|
import HeadTag from '../../components/HeadTag'
|
|
|
|
|
import AddEditFunction from '../../components/FunctionAdmin/AddEditFunction'
|
|
|
|
|
import EditArticleFunction from '../../components/FunctionAdmin/EditArticleFunction'
|
|
|
|
|
import EditFormFunction from '../../components/FunctionAdmin/EditFormFunction'
|
|
|
|
|
import redirect from '../../utils/redirect'
|
2020-08-03 14:37:05 +02:00
|
|
|
|
import api, { API_URL } from '../../utils/api'
|
2020-08-03 12:04:07 +02:00
|
|
|
|
import '../../components/FunctionPage/FunctionTabs.css'
|
|
|
|
|
import '../../public/css/pages/admin.css'
|
2020-04-11 21:07:13 +02:00
|
|
|
|
|
2020-08-03 14:37:05 +02:00
|
|
|
|
const AdminFunctionComponent = props => {
|
2020-08-03 12:04:07 +02:00
|
|
|
|
const [slideIndex, setSlideIndex] = useState(0)
|
2020-04-11 21:07:13 +02:00
|
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
|
const handleDeleteFunction = async () => {
|
2020-08-03 14:37:05 +02:00
|
|
|
|
await api.delete(`/admin/functions/${props.functionInfo.id}`, {
|
|
|
|
|
headers: { Authorization: props.user.token }
|
|
|
|
|
})
|
2020-08-03 12:04:07 +02:00
|
|
|
|
redirect({}, '/admin')
|
|
|
|
|
}
|
2020-04-11 21:07:13 +02:00
|
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
|
return (
|
|
|
|
|
<>
|
2020-08-03 14:37:05 +02:00
|
|
|
|
<HeadTag
|
|
|
|
|
title={props.functionInfo.title}
|
|
|
|
|
description={props.functionInfo.description}
|
|
|
|
|
image={API_URL + props.functionInfo.image}
|
|
|
|
|
/>
|
2020-05-06 09:55:27 +02:00
|
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
|
<div className='container-fluid'>
|
|
|
|
|
<div className='container'>
|
|
|
|
|
<div className='row justify-content-center'>
|
|
|
|
|
<ul className='FunctionTabs__nav'>
|
|
|
|
|
<li className='FunctionTabs__nav-item'>
|
2020-08-03 14:37:05 +02:00
|
|
|
|
<a
|
|
|
|
|
onClick={() => setSlideIndex(0)}
|
|
|
|
|
className={`FunctionTabs__nav-link ${slideIndex === 0 &&
|
|
|
|
|
'FunctionTabs__nav-link-active'}`}
|
|
|
|
|
>
|
|
|
|
|
✒️ Modifier
|
|
|
|
|
</a>
|
2020-08-03 12:04:07 +02:00
|
|
|
|
</li>
|
|
|
|
|
<li className='FunctionTabs__nav-item'>
|
2020-08-03 14:37:05 +02:00
|
|
|
|
<a
|
|
|
|
|
onClick={() => setSlideIndex(1)}
|
|
|
|
|
className={`FunctionTabs__nav-link ${slideIndex === 1 &&
|
|
|
|
|
'FunctionTabs__nav-link-active'}`}
|
|
|
|
|
>
|
|
|
|
|
📝 Article
|
|
|
|
|
</a>
|
2020-08-03 12:04:07 +02:00
|
|
|
|
</li>
|
|
|
|
|
<li className='FunctionTabs__nav-item'>
|
2020-08-03 14:37:05 +02:00
|
|
|
|
<a
|
|
|
|
|
onClick={() => setSlideIndex(2)}
|
|
|
|
|
className={`FunctionTabs__nav-link ${slideIndex === 2 &&
|
|
|
|
|
'FunctionTabs__nav-link-active'}`}
|
|
|
|
|
>
|
|
|
|
|
⚙️ Utilisation
|
|
|
|
|
</a>
|
2020-08-03 12:04:07 +02:00
|
|
|
|
</li>
|
|
|
|
|
</ul>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2020-04-15 22:50:40 +02:00
|
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
|
<div className='container-fluid'>
|
2020-08-03 14:37:05 +02:00
|
|
|
|
<SwipeableViews
|
|
|
|
|
onChangeIndex={index => setSlideIndex(index)}
|
|
|
|
|
index={slideIndex}
|
|
|
|
|
>
|
2020-08-03 12:04:07 +02:00
|
|
|
|
<div className='Admin__Function-slide'>
|
|
|
|
|
<AddEditFunction
|
|
|
|
|
defaultInputState={{ ...props.functionInfo }}
|
|
|
|
|
user={props.user}
|
|
|
|
|
isEditing
|
|
|
|
|
/>
|
|
|
|
|
<div style={{ marginBottom: '30px' }} className='text-center'>
|
2020-08-03 14:37:05 +02:00
|
|
|
|
<button onClick={handleDeleteFunction} className='btn btn-dark'>
|
|
|
|
|
Supprimer la fonction
|
|
|
|
|
</button>
|
2020-08-03 12:04:07 +02:00
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<div className='Admin__Function-slide'>
|
|
|
|
|
<EditArticleFunction {...props} />
|
|
|
|
|
</div>
|
|
|
|
|
<div className='Admin__Function-slide'>
|
|
|
|
|
<EditFormFunction {...props} />
|
2020-04-15 22:50:40 +02:00
|
|
|
|
</div>
|
2020-08-03 12:04:07 +02:00
|
|
|
|
</SwipeableViews>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</>
|
|
|
|
|
)
|
2020-04-11 21:07:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
|
export async function getServerSideProps (context) {
|
|
|
|
|
const cookies = new Cookies(context.req.headers.cookie)
|
|
|
|
|
const user = { ...cookies.get('user') }
|
|
|
|
|
const { slug } = context.params
|
|
|
|
|
if (!user.isAdmin) {
|
|
|
|
|
return redirect(context, '/404')
|
|
|
|
|
}
|
2020-08-03 14:37:05 +02:00
|
|
|
|
return api
|
|
|
|
|
.get(`/admin/functions/${slug}`, { headers: { Authorization: user.token } })
|
|
|
|
|
.then(response => {
|
2020-08-03 12:04:07 +02:00
|
|
|
|
return {
|
|
|
|
|
props: {
|
|
|
|
|
user,
|
|
|
|
|
functionInfo: response.data
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.catch(() => redirect(context, '/404'))
|
2020-04-11 21:07:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
|
export default AdminFunctionComponent
|