2020-08-03 12:04:07 +02:00
|
|
|
import { useState, useEffect, useContext } from 'react'
|
|
|
|
import Link from 'next/link'
|
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
|
|
|
|
import { faTrash, faCheck, faTimes } from '@fortawesome/free-solid-svg-icons'
|
|
|
|
import { UserContext } from '../../contexts/UserContext'
|
|
|
|
import redirect from '../../utils/redirect'
|
|
|
|
import FunctionPage from '../../components/FunctionPage/FunctionPage'
|
|
|
|
import FunctionTabs from '../../components/FunctionPage/FunctionTabs'
|
|
|
|
import FunctionArticle from '../../components/FunctionPage/FunctionArticle'
|
|
|
|
import FunctionComments from '../../components/FunctionPage/FunctionComments/FunctionComments'
|
|
|
|
import api from '../../utils/api'
|
2020-04-24 14:43:52 +02:00
|
|
|
|
|
|
|
const ManageToDo = () => {
|
2020-08-03 12:04:07 +02:00
|
|
|
const { isAuth, user } = useContext(UserContext)
|
|
|
|
const [inputState, setInputState] = useState({ task: '' })
|
|
|
|
const [tasks, setTasks] = useState([])
|
2020-04-24 14:43:52 +02:00
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
useEffect(() => {
|
|
|
|
const getTasks = async () => {
|
2020-08-03 14:37:05 +02:00
|
|
|
const { data } = await api.get('/tasks', {
|
|
|
|
headers: { Authorization: user.token }
|
|
|
|
})
|
2020-08-03 12:04:07 +02:00
|
|
|
setTasks(data)
|
2020-04-24 14:43:52 +02:00
|
|
|
}
|
2020-08-03 12:04:07 +02:00
|
|
|
if (isAuth && user.token != null) {
|
|
|
|
getTasks()
|
2020-04-24 14:43:52 +02:00
|
|
|
}
|
2020-08-03 12:04:07 +02:00
|
|
|
}, [isAuth])
|
2020-04-24 14:43:52 +02:00
|
|
|
|
2020-08-03 14:37:05 +02:00
|
|
|
const handleChange = event => {
|
2020-08-03 12:04:07 +02:00
|
|
|
const inputStateNew = { ...inputState }
|
|
|
|
inputStateNew[event.target.name] = event.target.value
|
|
|
|
setInputState(inputStateNew)
|
|
|
|
}
|
2020-04-24 14:43:52 +02:00
|
|
|
|
2020-08-03 14:37:05 +02:00
|
|
|
const handleSubmit = async event => {
|
2020-08-03 12:04:07 +02:00
|
|
|
event.preventDefault()
|
|
|
|
try {
|
2020-08-03 14:37:05 +02:00
|
|
|
const { data } = await api.post('/tasks', inputState, {
|
|
|
|
headers: { Authorization: user.token }
|
|
|
|
})
|
2020-08-03 12:04:07 +02:00
|
|
|
const newTasks = [...tasks]
|
|
|
|
newTasks.push(data)
|
|
|
|
setTasks(newTasks)
|
|
|
|
setInputState({ task: '' })
|
|
|
|
} catch {}
|
|
|
|
}
|
2020-04-24 14:43:52 +02:00
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
const handleRemoveTask = async (id, index) => {
|
|
|
|
const newTasks = [...tasks]
|
|
|
|
try {
|
2020-08-03 14:37:05 +02:00
|
|
|
await api.delete(`/tasks/${id}`, {
|
|
|
|
headers: { Authorization: user.token }
|
|
|
|
})
|
2020-08-03 12:04:07 +02:00
|
|
|
newTasks.splice(index, 1)
|
|
|
|
setTasks(newTasks)
|
|
|
|
} catch {}
|
|
|
|
}
|
2020-04-24 14:43:52 +02:00
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
const handleEditTask = async (id, index, isCompleted) => {
|
|
|
|
try {
|
2020-08-03 14:37:05 +02:00
|
|
|
await api.put(
|
|
|
|
`/tasks/${id}`,
|
|
|
|
{ isCompleted: !isCompleted },
|
|
|
|
{ headers: { Authorization: user.token } }
|
|
|
|
)
|
2020-08-03 12:04:07 +02:00
|
|
|
const newTasks = [...tasks]
|
|
|
|
const taskObject = newTasks[index]
|
|
|
|
taskObject.isCompleted = !isCompleted
|
|
|
|
setTasks(newTasks)
|
|
|
|
} catch {}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isAuth) {
|
2020-04-24 14:43:52 +02:00
|
|
|
return (
|
2020-08-03 12:04:07 +02:00
|
|
|
<p className='text-center'>
|
2020-08-03 14:37:05 +02:00
|
|
|
Vous devez être{' '}
|
|
|
|
<Link href='/users/login'>
|
|
|
|
<a>connecté</a>
|
|
|
|
</Link>{' '}
|
|
|
|
pour gérer des "tâches à faire".
|
2020-08-03 12:04:07 +02:00
|
|
|
</p>
|
|
|
|
)
|
|
|
|
}
|
2020-04-24 14:43:52 +02:00
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
return (
|
|
|
|
<div className='container-fluid'>
|
|
|
|
<div className='row justify-content-center'>
|
|
|
|
<div className='col-24'>
|
|
|
|
<form onSubmit={handleSubmit}>
|
|
|
|
<div className='text-center'>
|
2020-08-03 14:37:05 +02:00
|
|
|
<label htmlFor='task' className='form-label'>
|
|
|
|
Ajouter une tâche à faire :
|
|
|
|
</label>
|
|
|
|
<input
|
|
|
|
value={inputState.task}
|
|
|
|
onChange={handleChange}
|
|
|
|
name='task'
|
|
|
|
id='task'
|
|
|
|
type='text'
|
|
|
|
className='form-control'
|
|
|
|
placeholder='(e.g : Apprendre à coder)'
|
|
|
|
/>
|
2020-04-24 14:43:52 +02:00
|
|
|
</div>
|
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
<div className='form-group text-center'>
|
2020-08-03 14:37:05 +02:00
|
|
|
<button type='submit' className='btn btn-dark'>
|
|
|
|
Envoyer
|
|
|
|
</button>
|
2020-08-03 12:04:07 +02:00
|
|
|
</div>
|
|
|
|
</form>
|
2020-04-24 14:43:52 +02:00
|
|
|
</div>
|
2020-08-03 12:04:07 +02:00
|
|
|
</div>
|
|
|
|
|
2020-08-03 14:37:05 +02:00
|
|
|
{tasks.length > 0 && (
|
2020-08-03 12:04:07 +02:00
|
|
|
<div className='row justify-content-center'>
|
|
|
|
<div className='col-24 ManageToDo__container'>
|
|
|
|
<ul className='ManageToDo__list'>
|
|
|
|
{tasks.map((task, index) => {
|
|
|
|
return (
|
2020-08-03 14:37:05 +02:00
|
|
|
<li
|
|
|
|
key={task.id}
|
|
|
|
className={`ManageToDo__list-item ${
|
|
|
|
task.isCompleted ? 'isCompleted' : ''
|
|
|
|
}`}
|
|
|
|
>
|
|
|
|
<span className='ManageToDo__list-item-span'>
|
|
|
|
{task.task}
|
|
|
|
</span>
|
2020-08-03 12:04:07 +02:00
|
|
|
<div>
|
2020-08-03 14:37:05 +02:00
|
|
|
<button
|
|
|
|
className='ManageToDo__task-btn'
|
|
|
|
title='Supprimer de la liste'
|
|
|
|
onClick={() => handleRemoveTask(task.id, index)}
|
|
|
|
>
|
2020-08-03 12:04:07 +02:00
|
|
|
<FontAwesomeIcon icon={faTrash} />
|
|
|
|
</button>
|
2020-08-03 14:37:05 +02:00
|
|
|
<button
|
|
|
|
className='ManageToDo__task-btn'
|
|
|
|
onClick={() =>
|
|
|
|
handleEditTask(task.id, index, task.isCompleted)}
|
|
|
|
>
|
|
|
|
<FontAwesomeIcon
|
|
|
|
{...(task.isCompleted
|
|
|
|
? { icon: faTimes }
|
|
|
|
: { icon: faCheck })}
|
|
|
|
/>
|
2020-08-03 12:04:07 +02:00
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</li>
|
|
|
|
)
|
|
|
|
})}
|
|
|
|
</ul>
|
|
|
|
</div>
|
2020-08-03 14:37:05 +02:00
|
|
|
</div>
|
|
|
|
)}
|
2020-08-03 12:04:07 +02:00
|
|
|
</div>
|
|
|
|
)
|
2020-04-24 14:43:52 +02:00
|
|
|
}
|
|
|
|
|
2020-08-03 14:37:05 +02:00
|
|
|
const FunctionTabManager = props => {
|
2020-08-03 12:04:07 +02:00
|
|
|
return (
|
2020-08-03 14:37:05 +02:00
|
|
|
<FunctionTabs
|
|
|
|
setSlideIndex={props.setSlideIndex}
|
|
|
|
slideIndex={props.slideIndex}
|
|
|
|
>
|
2020-08-03 12:04:07 +02:00
|
|
|
<div className='FunctionComponent__slide'>
|
|
|
|
<ManageToDo />
|
|
|
|
</div>
|
|
|
|
<div className='FunctionComponent__slide'>
|
|
|
|
<FunctionArticle article={props.article} />
|
|
|
|
</div>
|
|
|
|
<div className='FunctionComponent__slide'>
|
|
|
|
<FunctionComments functionId={props.id} />
|
|
|
|
</div>
|
|
|
|
</FunctionTabs>
|
|
|
|
)
|
2020-04-24 14:43:52 +02:00
|
|
|
}
|
|
|
|
|
2020-08-03 14:37:05 +02:00
|
|
|
const toDoList = props => (
|
2020-08-03 12:04:07 +02:00
|
|
|
<FunctionPage
|
|
|
|
FunctionTabManager={FunctionTabManager}
|
|
|
|
{...props}
|
|
|
|
tabNames={['⚙️ Utilisation', '📝 Article', '📬 Commentaires']}
|
|
|
|
/>
|
|
|
|
)
|
2020-04-24 14:43:52 +02:00
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
export async function getServerSideProps (context) {
|
2020-08-03 14:37:05 +02:00
|
|
|
return api
|
|
|
|
.get('/functions/toDoList')
|
|
|
|
.then(response => ({ props: response.data }))
|
2020-08-03 12:04:07 +02:00
|
|
|
.catch(() => redirect(context, '/404'))
|
2020-04-24 14:43:52 +02:00
|
|
|
}
|
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
export default toDoList
|