Skip to content

Commit

Permalink
fix(delete_project): Added list of linked package in delete project p…
Browse files Browse the repository at this point in the history
…opup
  • Loading branch information
amritkv committed Dec 22, 2023
1 parent 5572dd1 commit d824042
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 26 deletions.
1 change: 1 addition & 0 deletions messages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,7 @@
"There are some errors while creating project": "There are some errors while creating project",
"This currently only supports SPDX RDF/XML files with a uniq described top level node": "This currently only supports SPDX RDF/XML files with a uniq described top level node",
"This function is meant to be followed by a new license import": "This function is meant to be followed by a new license import.",
"This project contains": "This project <strong>{name}</strong> contains",
"This release contains": "This release <strong>{name}</strong> contains: ",
"Title": "Title",
"To": "To",
Expand Down
85 changes: 61 additions & 24 deletions src/app/[locale]/projects/components/DeleteProjectDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,17 @@ const DEFAULT_PROJECT_DATA: Project = {
'sw360:releases': [],
'sw360:projects': [],
'sw360:attachments': [],
'sw360:packages': [],
},
}

interface Data {
attachment?: number
project?: number
release?: number
package?: number
}

interface Props {
projectId?: string
show?: boolean
Expand All @@ -38,10 +46,12 @@ const DeleteProjectDialog = ({ projectId, show, setShow }: Props) => {
const t = useTranslations('default')
const router = useRouter()
const [project, setProject] = useState<Project>(DEFAULT_PROJECT_DATA)
const [internalData, setInternalData] = useState<Data>({ attachment: 0, project: 0, release: 0, package: 0 })
const [variant, setVariant] = useState('success')
const [message, setMessage] = useState('')
const [showMessage, setShowMessage] = useState(false)
const [reloadPage, setReloadPage] = useState(false)
const [visuallyHideLinkedData, setVisuallyHideLinkedData] = useState(true)
const [comment, setComment] = useState('')

const displayMessage = (variant: string, message: string) => {
Expand Down Expand Up @@ -76,35 +86,27 @@ const DeleteProjectDialog = ({ projectId, show, setShow }: Props) => {
}
}

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()
}
const fetchData = async (projectId: string) => {
if (session) {
const projectsResponse = await ApiUtils.GET(`projects/${projectId}`, session.user.access_token)
if (projectsResponse.status == HttpStatus.OK) {
const projectData = (await projectsResponse.json()) as Project
setProject(projectData)
handleInternalDataCount(projectData)
} 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) => {
fetchData(projectId).catch((err) => {
console.error(err)
})

return () => {
controller.abort()
}
}, [show, projectId, fetchData])
}, [show, projectId])

const handleSubmit = () => {
deleteProject().catch((err) => {
Expand All @@ -116,11 +118,33 @@ const DeleteProjectDialog = ({ projectId, show, setShow }: Props) => {
setShow(!show)
setShowMessage(false)
setComment('')
setVisuallyHideLinkedData(!visuallyHideLinkedData)
if (reloadPage === true) {
window.location.reload()
}
}

const handleInternalDataCount = (projectData: Project) => {
const dataCount: Data = {}
if (projectData._embedded['sw360:attachments']) {
dataCount.attachment = projectData._embedded['sw360:attachments'].length
setVisuallyHideLinkedData(false)
}
if (projectData._embedded['sw360:projects']) {
dataCount.project = projectData._embedded['sw360:projects'].length
setVisuallyHideLinkedData(false)
}
if (projectData._embedded['sw360:releases']) {
dataCount.release = projectData._embedded['sw360:releases'].length
setVisuallyHideLinkedData(false)
}
if (projectData._embedded['sw360:packages']) {
dataCount.package = projectData._embedded['sw360:packages'].length
setVisuallyHideLinkedData(false)
}
setInternalData(dataCount)
}

const handleUserComment = (e: any) => {
setComment(e.target.value)
}
Expand All @@ -145,6 +169,19 @@ const DeleteProjectDialog = ({ projectId, show, setShow }: Props) => {
strong: (data) => <b>{data}</b>,
})}
</Form.Label>
<br />
<Form.Label className='mb-1' visuallyHidden={visuallyHideLinkedData}>
{t.rich('This project contains', {
name: project.name,
strong: (data) => <b>{data}</b>,
visuallyHideLinkedData,
})}
<ul>
{Object.entries(internalData).map(([key, value]) => (
<li key={key}>{`${value} linked ${key}`}</li>
))}
</ul>
</Form.Label>
</Form.Group>
<hr />
<Form.Group className='mb-3'>
Expand Down
28 changes: 28 additions & 0 deletions src/object-types/Package.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 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

interface Package {
id?: string
rev?: string
type?: string
name: string
version?: string
purl?: string
packageType?: string
releaseId?: string
licenseIds?: Array<string>
description?: string
homepageUrl?: string
vcs?: string
createdOn?: string
createdBy?: string
packageManager?: string
}

export default Package
5 changes: 3 additions & 2 deletions src/object-types/Project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
// SPDX-License-Identifier: EPL-2.0
// License-Filename: LICENSE

import { Attachment, Links, Release, User } from '@/object-types'
import { Attachment, Links, Package, Release, User } from '@/object-types'

interface Project {
id: string
id?: string
name: string
considerReleasesFromExternalList?: boolean
additionalData?: object
Expand Down Expand Up @@ -58,6 +58,7 @@ interface Project {
'sw360:releases'?: Array<Release>
'sw360:attachments'?: Array<Attachment>
'sw360:projects'?: Array<Project>
'sw360:packages'?: Array<Package>
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/object-types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import Moderators from './Moderators'
import ModeratorsType from './ModeratorsType'
import NodeData from './NodeData'
import OAuthClient from './OAuthClient'
import Package from './Package'
import Project from './Project'
import ProjectReleaseEcc from './ProjectReleaseEcc'
import Release from './Release'
Expand Down Expand Up @@ -85,6 +86,7 @@ export type {
ModeratorsType,
NodeData,
OAuthClient,
Package,
Project,
ProjectReleaseEcc,
Release,
Expand Down

0 comments on commit d824042

Please sign in to comment.