|
| 1 | +// Copyright (C) Siemens AG, 2023. Part of the SW360 Frontend Project. |
| 2 | + |
| 3 | +// This program and the accompanying materials are made |
| 4 | +// available under the terms of the Eclipse Public License 2.0 |
| 5 | +// which is available at https://www.eclipse.org/legal/epl-2.0/ |
| 6 | + |
| 7 | +// SPDX-License-Identifier: EPL-2.0 |
| 8 | +// License-Filename: LICENSE |
| 9 | + |
| 10 | +'use client' |
| 11 | + |
| 12 | +import { HttpStatus, Project } from '@/object-types' |
| 13 | +import { ApiUtils } from '@/utils' |
| 14 | +import { signOut, useSession } from 'next-auth/react' |
| 15 | +import { useTranslations } from 'next-intl' |
| 16 | +import { useRouter } from 'next/navigation' |
| 17 | +import { useCallback, useEffect, useState } from 'react' |
| 18 | +import { Alert, Button, Form, Modal } from 'react-bootstrap' |
| 19 | +import { AiOutlineQuestionCircle } from 'react-icons/ai' |
| 20 | + |
| 21 | +const DEFAULT_PROJECT_DATA: Project = { |
| 22 | + name: '', |
| 23 | + _embedded: { |
| 24 | + 'sw360:releases': [], |
| 25 | + 'sw360:projects': [], |
| 26 | + 'sw360:attachments': [], |
| 27 | + }, |
| 28 | +} |
| 29 | +interface Data { |
| 30 | + attachment?: number |
| 31 | + project?: number |
| 32 | + release?: number |
| 33 | +} |
| 34 | + |
| 35 | +interface Props { |
| 36 | + projectId?: string |
| 37 | + show?: boolean |
| 38 | + setShow?: React.Dispatch<React.SetStateAction<boolean>> |
| 39 | +} |
| 40 | + |
| 41 | +const DeleteProjectDialog = ({ projectId, show, setShow }: Props) => { |
| 42 | + const { data: session } = useSession() |
| 43 | + const t = useTranslations('default') |
| 44 | + const router = useRouter() |
| 45 | + const [project, setProject] = useState<Project>(DEFAULT_PROJECT_DATA) |
| 46 | + const [internalData, setInternalData] = useState<Data>({ attachment: 0, project: 0, release: 0 }) |
| 47 | + const [variant, setVariant] = useState('success') |
| 48 | + const [message, setMessage] = useState('') |
| 49 | + const [showMessage, setShowMessage] = useState(false) |
| 50 | + const [reloadPage, setReloadPage] = useState(false) |
| 51 | + const [hasSubProject, setHasSubProject] = useState(false) |
| 52 | + const [comment, setComment] = useState('') |
| 53 | + |
| 54 | + const displayMessage = (variant: string, message: string) => { |
| 55 | + setVariant(variant) |
| 56 | + setMessage(message) |
| 57 | + setShowMessage(true) |
| 58 | + } |
| 59 | + |
| 60 | + const handleError = useCallback(() => { |
| 61 | + displayMessage('danger', t('Error when processing')) |
| 62 | + setReloadPage(true) |
| 63 | + }, [t]) |
| 64 | + |
| 65 | + const deleteProject = async () => { |
| 66 | + const response = await ApiUtils.DELETE(`projects/${projectId}`, session.user.access_token) |
| 67 | + try { |
| 68 | + if (response.status == HttpStatus.OK) { |
| 69 | + displayMessage('success', t('Delete project successful!')) |
| 70 | + router.push('/projects') |
| 71 | + setReloadPage(true) |
| 72 | + } else if (response.status == HttpStatus.ACCEPTED) { |
| 73 | + displayMessage('info', t('Moderation request is created!')) |
| 74 | + } else if (response.status == HttpStatus.CONFLICT) { |
| 75 | + displayMessage('danger', t('The project cannot be deleted, since it is used by another project!')) |
| 76 | + } else if (response.status == HttpStatus.UNAUTHORIZED) { |
| 77 | + await signOut() |
| 78 | + } else { |
| 79 | + displayMessage('danger', t('Error when processing')) |
| 80 | + } |
| 81 | + } catch (err) { |
| 82 | + handleError() |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + const fetchData = useCallback( |
| 87 | + async (signal: AbortSignal) => { |
| 88 | + if (session) { |
| 89 | + const projectsResponse = await ApiUtils.GET(`projects/${projectId}`, session.user.access_token, signal) |
| 90 | + if (projectsResponse.status == HttpStatus.OK) { |
| 91 | + const project = (await projectsResponse.json()) as Project |
| 92 | + setProject(project) |
| 93 | + handleInternalDataCount() |
| 94 | + } else if (projectsResponse.status == HttpStatus.UNAUTHORIZED) { |
| 95 | + await signOut() |
| 96 | + } else { |
| 97 | + setProject(DEFAULT_PROJECT_DATA) |
| 98 | + handleError() |
| 99 | + } |
| 100 | + } |
| 101 | + }, |
| 102 | + [projectId, handleError, session] |
| 103 | + ) |
| 104 | + |
| 105 | + useEffect(() => { |
| 106 | + const controller = new AbortController() |
| 107 | + const signal = controller.signal |
| 108 | + fetchData(signal).catch((err) => { |
| 109 | + console.error(err) |
| 110 | + }) |
| 111 | + |
| 112 | + return () => { |
| 113 | + controller.abort() |
| 114 | + } |
| 115 | + }, [show, projectId, fetchData]) |
| 116 | + |
| 117 | + const handleSubmit = () => { |
| 118 | + deleteProject().catch((err) => { |
| 119 | + console.log(err) |
| 120 | + }) |
| 121 | + } |
| 122 | + |
| 123 | + const handleCloseDialog = () => { |
| 124 | + setShow(!show) |
| 125 | + setShowMessage(false) |
| 126 | + setComment('') |
| 127 | + if (reloadPage === true) { |
| 128 | + window.location.reload() |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + const handleInternalDataCount = () => { |
| 133 | + const dataCount: Data = {} |
| 134 | + if (project._embedded['sw360:attachments']) { |
| 135 | + const attachmentCount = project._embedded['sw360:attachments'].length |
| 136 | + dataCount.attachment = attachmentCount |
| 137 | + } else if (project._embedded['sw360:projects']) { |
| 138 | + const projectCount = project._embedded['sw360:projects'].length |
| 139 | + dataCount.project = projectCount |
| 140 | + setHasSubProject(true) |
| 141 | + } else if (project._embedded['sw360:releases']) { |
| 142 | + const releaseCount = project._embedded['sw360:releases'].length |
| 143 | + dataCount.release = releaseCount |
| 144 | + } |
| 145 | + setInternalData(dataCount) |
| 146 | + } |
| 147 | + |
| 148 | + const handleUserComment = (e: any) => { |
| 149 | + setComment(e.target.value) |
| 150 | + } |
| 151 | + |
| 152 | + return ( |
| 153 | + <Modal show={show} onHide={handleCloseDialog} backdrop='static' centered size='lg'> |
| 154 | + <Modal.Header closeButton style={{ color: 'red' }}> |
| 155 | + <Modal.Title> |
| 156 | + <AiOutlineQuestionCircle style={{ marginBottom: '5px' }} /> |
| 157 | + {t('Delete Project')} ? |
| 158 | + </Modal.Title> |
| 159 | + </Modal.Header> |
| 160 | + <Modal.Body> |
| 161 | + <Alert variant={variant} onClose={() => setShowMessage(false)} dismissible show={showMessage}> |
| 162 | + {message} |
| 163 | + </Alert> |
| 164 | + <Form> |
| 165 | + <Form.Group> |
| 166 | + <Form.Label className='mb-3'> |
| 167 | + {t.rich('Do you really want to delete the project?', { |
| 168 | + name: project.name, |
| 169 | + strong: (data) => <b>{data}</b>, |
| 170 | + })} |
| 171 | + </Form.Label> |
| 172 | + <Form.Label className='mb-3' visuallyHidden={hasSubProject}> |
| 173 | + {t.rich('This project contains', { |
| 174 | + name: project.name, |
| 175 | + strong: (data) => <b>{data}</b>, |
| 176 | + hasSubProject, |
| 177 | + })} |
| 178 | + <ul> |
| 179 | + {Object.entries(internalData).map(([key, value]) => ( |
| 180 | + <li key={key}>{`${value} linked ${key}`}</li> |
| 181 | + ))} |
| 182 | + </ul> |
| 183 | + </Form.Label> |
| 184 | + </Form.Group> |
| 185 | + <hr /> |
| 186 | + <Form.Group className='mb-3'> |
| 187 | + <Form.Label style={{ fontWeight: 'bold' }}>{t('Please comment your changes')}</Form.Label> |
| 188 | + <Form.Control |
| 189 | + as='textarea' |
| 190 | + aria-label='With textarea' |
| 191 | + placeholder='Comment your message...' |
| 192 | + onChange={handleUserComment} |
| 193 | + /> |
| 194 | + </Form.Group> |
| 195 | + </Form> |
| 196 | + </Modal.Body> |
| 197 | + <Modal.Footer className='justify-content-end'> |
| 198 | + <Button className='delete-btn' variant='light' onClick={handleCloseDialog}> |
| 199 | + {' '} |
| 200 | + {t('Close')}{' '} |
| 201 | + </Button> |
| 202 | + <Button |
| 203 | + className='login-btn' |
| 204 | + variant='danger' |
| 205 | + disabled={!comment} |
| 206 | + onClick={() => handleSubmit()} |
| 207 | + hidden={reloadPage} |
| 208 | + > |
| 209 | + {t('Delete Project')} |
| 210 | + </Button> |
| 211 | + </Modal.Footer> |
| 212 | + </Modal> |
| 213 | + ) |
| 214 | +} |
| 215 | + |
| 216 | +export default DeleteProjectDialog |
0 commit comments