2020-04-15 22:50:40 +02:00
|
|
|
|
import { Fragment, useState } from 'react';
|
2020-04-11 21:07:13 +02:00
|
|
|
|
import Cookies from "universal-cookie";
|
2020-04-15 22:50:40 +02:00
|
|
|
|
import SwipeableViews from 'react-swipeable-views';
|
2020-04-11 21:07:13 +02:00
|
|
|
|
import HeadTag from '../../components/HeadTag';
|
2020-04-15 22:50:40 +02:00
|
|
|
|
import AddEditFunction from '../../components/AddEditFunction';
|
|
|
|
|
import EditArticleFunction from '../../components/EditArticleFunction';
|
2020-04-16 19:44:19 +02:00
|
|
|
|
import EditFormFunction from '../../components/EditFormFunction';
|
2020-04-11 21:07:13 +02:00
|
|
|
|
import redirect from '../../utils/redirect';
|
2020-04-15 22:50:40 +02:00
|
|
|
|
import api from '../../utils/api';
|
|
|
|
|
import { API_URL } from '../../utils/config';
|
|
|
|
|
import '../../components/FunctionTabs/FunctionTabs.css';
|
|
|
|
|
import '../../public/css/pages/admin.css';
|
2020-04-11 21:07:13 +02:00
|
|
|
|
|
|
|
|
|
const AdminFunctionComponent = (props) => {
|
|
|
|
|
|
2020-04-20 17:53:44 +02:00
|
|
|
|
const [slideIndex, setSlideIndex] = useState(0);
|
2020-04-11 21:07:13 +02:00
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Fragment>
|
2020-04-15 22:50:40 +02:00
|
|
|
|
<HeadTag title={props.functionInfo.title} description={props.functionInfo.description} image={API_URL + props.functionInfo.image} />
|
|
|
|
|
|
|
|
|
|
<div className="container-fluid">
|
|
|
|
|
<div className="container">
|
|
|
|
|
<div className="row justify-content-center">
|
|
|
|
|
<ul className="FunctionTabs__nav">
|
|
|
|
|
<li className="FunctionTabs__nav-item">
|
|
|
|
|
<a onClick={() => setSlideIndex(0)} className={`FunctionTabs__nav-link ${(slideIndex === 0) && "FunctionTabs__nav-link-active"}`}>✒️ Modifier</a>
|
|
|
|
|
</li>
|
|
|
|
|
<li className="FunctionTabs__nav-item">
|
|
|
|
|
<a onClick={() => setSlideIndex(1)} className={`FunctionTabs__nav-link ${(slideIndex === 1) && "FunctionTabs__nav-link-active"}`}>📝 Article</a>
|
|
|
|
|
</li>
|
|
|
|
|
<li className="FunctionTabs__nav-item">
|
|
|
|
|
<a onClick={() => setSlideIndex(2)} className={`FunctionTabs__nav-link ${(slideIndex === 2) && "FunctionTabs__nav-link-active"}`}>⚙️ Utilisation</a>
|
|
|
|
|
</li>
|
|
|
|
|
</ul>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className="container-fluid">
|
|
|
|
|
<SwipeableViews onChangeIndex={(index) => setSlideIndex(index)} index={slideIndex}>
|
|
|
|
|
<div className="Admin__Function-slide">
|
|
|
|
|
<AddEditFunction
|
|
|
|
|
defaultInputState={{ ...props.functionInfo }}
|
|
|
|
|
user={props.user}
|
|
|
|
|
isEditing
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="Admin__Function-slide">
|
|
|
|
|
<EditArticleFunction { ...props } />
|
|
|
|
|
</div>
|
2020-04-16 19:44:19 +02:00
|
|
|
|
<div className="Admin__Function-slide">
|
|
|
|
|
<EditFormFunction { ...props } />
|
|
|
|
|
</div>
|
2020-04-15 22:50:40 +02:00
|
|
|
|
</SwipeableViews>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2020-04-11 21:07:13 +02:00
|
|
|
|
</Fragment>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 22:50:40 +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');
|
|
|
|
|
}
|
|
|
|
|
return api.get(`/admin/functions/${slug}`, { headers: { 'Authorization': user.token } })
|
|
|
|
|
.then((response) => {
|
|
|
|
|
return {
|
|
|
|
|
props: {
|
|
|
|
|
user,
|
|
|
|
|
functionInfo: response.data
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
})
|
|
|
|
|
.catch(() => redirect(context, '/404'));
|
2020-04-11 21:07:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default AdminFunctionComponent;
|