From a7eaaac447eedf850f966b9fa83fe59ac83a2086 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20FAMIBELLE-PRONZOLA?= Date: Sun, 23 Jun 2024 19:18:25 +0400 Subject: [PATCH] Add 'articles' collection to create operation --- app/page.js | 2 +- components/konstitisyon/create/create-form.js | 60 +++++++++++++++---- .../konstitisyon/create/handle-create.js | 25 +++++++- components/konstitisyon/create/index.js | 6 +- 4 files changed, 76 insertions(+), 17 deletions(-) diff --git a/app/page.js b/app/page.js index 7ea70dc..ae00d64 100644 --- a/app/page.js +++ b/app/page.js @@ -59,7 +59,7 @@ export default async function Page() { {appTitle.toUpperCase()} {session && ( - + )} diff --git a/components/konstitisyon/create/create-form.js b/components/konstitisyon/create/create-form.js index 207769d..446917d 100644 --- a/components/konstitisyon/create/create-form.js +++ b/components/konstitisyon/create/create-form.js @@ -1,6 +1,6 @@ 'use client' -import {useRef} from 'react' +import {useRef, useState, useEffect} from 'react' import PropTypes from 'prop-types' import Button from '@mui/material/Button' import TextField from '@mui/material/TextField' @@ -10,6 +10,7 @@ import DialogContent from '@mui/material/DialogContent' import DialogContentText from '@mui/material/DialogContentText' import DialogTitle from '@mui/material/DialogTitle' import LogoutCountdown from '../../session/logout-countdown.js' +import ListItems from './list-items.js' import {handleSubmit} from '@/lib/directus.js' import {formatFormContent} from '@/lib/format.js' @@ -26,9 +27,17 @@ export default function CreateForm({ collection, title, label, - hasMultiline = true + hasMultiline = true, + listItems }) { const countdownRef = useRef() + const [selectValue, setSelectValue] = useState('') + + useEffect(() => { + if (listItems && listItems.length > 0) { + setSelectValue(listItems[0].id) + } + }, [listItems]) const handleClose = () => { setIsOpen(false) @@ -40,17 +49,36 @@ export default function CreateForm({ const formattedContent = formatFormContent(e.currentTarget) - if (collection === 'commentaires') { - requestObject = { - text: formattedContent, - titre: selectedTitre, - status: 'draft', + switch (collection) { + case 'commentaires': { + requestObject = { + text: formattedContent, + titre: selectedTitre, + status: 'draft', + } + + break } - } else if (collection === 'titres') { - requestObject = { - contenu: formattedContent, - status: 'draft', + + case 'titres': { + requestObject = { + contenu: formattedContent, + status: 'draft', + } + + break } + + case 'articles': { + requestObject = { + contenu: formattedContent, + titre: selectValue, + status: 'draft', + } + + break + } + // No default } await handleSubmit({ @@ -83,6 +111,13 @@ export default function CreateForm({ {dialogText} + {listItems && listItems.length > 0 && ( + + )} ) } + + if (collection === 'articles') { + return ( + + ) + } } HandleCreate.propTypes = { @@ -60,5 +80,6 @@ HandleCreate.propTypes = { setSuccess: PropTypes.func.isRequired, setIsErrorAlertOpen: PropTypes.func.isRequired, setIsSuccessAlertOpen: PropTypes.func.isRequired, - collection: PropTypes.oneOf(['titres', 'articles']).isRequired + collection: PropTypes.oneOf(['titres', 'articles']).isRequired, + listItems: PropTypes.array } diff --git a/components/konstitisyon/create/index.js b/components/konstitisyon/create/index.js index e29bbe5..959b1ea 100644 --- a/components/konstitisyon/create/index.js +++ b/components/konstitisyon/create/index.js @@ -16,7 +16,7 @@ const actions = [ {id: 'articles', icon: , name: 'Créer un article'} ] -export default function Create({session}) { +export default function Create({session, titres}) { const [collection, setCollection] = useState(null) const [isDialogOpen, setIsDialogOpen] = useState(false) const [isErrorAlertOpen, setIsErrorAlertOpen] = useState(false) @@ -73,6 +73,7 @@ export default function Create({session}) { setIsErrorAlertOpen={setIsErrorAlertOpen} setIsSuccessAlertOpen={setIsSuccessAlertOpen} collection={collection} + listItems={titres} /> )} @@ -80,5 +81,6 @@ export default function Create({session}) { } Create.propTypes = { - session: PropTypes.object + session: PropTypes.object, + titres: PropTypes.array }