From 4236428891b8d4881a1df4b51b51821604703245 Mon Sep 17 00:00:00 2001 From: amritkv Date: Mon, 11 Dec 2023 10:50:38 +0530 Subject: [PATCH] fix(delete project): Removed project details list at delete project dialog --- .../components/DeleteProjectDialog.tsx | 180 ++++++++++++++++++ 1 file changed, 180 insertions(+) create mode 100644 src/app/[locale]/projects/components/DeleteProjectDialog.tsx diff --git a/src/app/[locale]/projects/components/DeleteProjectDialog.tsx b/src/app/[locale]/projects/components/DeleteProjectDialog.tsx new file mode 100644 index 000000000..801590065 --- /dev/null +++ b/src/app/[locale]/projects/components/DeleteProjectDialog.tsx @@ -0,0 +1,180 @@ +// Copyright (C) Siemens AG, 2023. Part of the SW360 Frontend Project. + +// This program and the accompanying materials are made +// available under the terms of the Eclipse Public License 2.0 +// which is available at https://www.eclipse.org/legal/epl-2.0/ + +// SPDX-License-Identifier: EPL-2.0 +// License-Filename: LICENSE + +'use client' + +import { HttpStatus, Project } from '@/object-types' +import { ApiUtils } from '@/utils' +import { signOut, useSession } from 'next-auth/react' +import { useTranslations } from 'next-intl' +import { useRouter } from 'next/navigation' +import { useCallback, useEffect, useState } from 'react' +import { Alert, Button, Form, Modal } from 'react-bootstrap' +import { AiOutlineQuestionCircle } from 'react-icons/ai' + +const DEFAULT_PROJECT_DATA: Project = { + name: '', + _embedded: { + 'sw360:releases': [], + 'sw360:projects': [], + 'sw360:attachments': [], + }, +} + +interface Props { + projectId?: string + show?: boolean + setShow?: React.Dispatch> +} + +const DeleteProjectDialog = ({ projectId, show, setShow }: Props) => { + const { data: session } = useSession() + const t = useTranslations('default') + const router = useRouter() + const [project, setProject] = useState(DEFAULT_PROJECT_DATA) + const [variant, setVariant] = useState('success') + const [message, setMessage] = useState('') + const [showMessage, setShowMessage] = useState(false) + const [reloadPage, setReloadPage] = useState(false) + const [comment, setComment] = useState('') + + const displayMessage = (variant: string, message: string) => { + setVariant(variant) + setMessage(message) + setShowMessage(true) + } + + const handleError = useCallback(() => { + displayMessage('danger', t('Error when processing')) + setReloadPage(true) + }, [t]) + + const deleteProject = async () => { + const response = await ApiUtils.DELETE(`projects/${projectId}`, session.user.access_token) + try { + if (response.status == HttpStatus.OK) { + displayMessage('success', t('Delete project successful!')) + router.push('/projects') + setReloadPage(true) + } else if (response.status == HttpStatus.ACCEPTED) { + displayMessage('info', t('Moderation request is created!')) + } else if (response.status == HttpStatus.CONFLICT) { + displayMessage('danger', t('The project cannot be deleted, since it is used by another project!')) + } else if (response.status == HttpStatus.UNAUTHORIZED) { + await signOut() + } else { + displayMessage('danger', t('Error when processing')) + } + } catch (err) { + handleError() + } + } + + const fetchData = useCallback( + async (signal: AbortSignal) => { + if (session) { + const projectsResponse = await ApiUtils.GET(`projects/${projectId}`, session.user.access_token, signal) + if (projectsResponse.status == HttpStatus.OK) { + const project = (await projectsResponse.json()) as Project + setProject(project) + } else if (projectsResponse.status == HttpStatus.UNAUTHORIZED) { + await signOut() + } else { + setProject(DEFAULT_PROJECT_DATA) + handleError() + } + } + }, + [projectId, handleError, session] + ) + + useEffect(() => { + const controller = new AbortController() + const signal = controller.signal + fetchData(signal).catch((err) => { + console.error(err) + }) + + return () => { + controller.abort() + } + }, [show, projectId, fetchData]) + + const handleSubmit = () => { + deleteProject().catch((err) => { + console.log(err) + }) + } + + const handleCloseDialog = () => { + setShow(!show) + setShowMessage(false) + setComment('') + if (reloadPage === true) { + window.location.reload() + } + } + + const handleUserComment = (e: any) => { + setComment(e.target.value) + } + + return ( + + + + + {t('Delete Project')} ? + + + + setShowMessage(false)} dismissible show={showMessage}> + {message} + +
+ + + {t.rich('Do you really want to delete the project?', { + name: project.name, + strong: (data) => {data}, + })} + + +
+ + {t('Please comment your changes')} + + +
+
+ + + + +
+ ) +} + +export default DeleteProjectDialog