|
| 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 Administration from '@/components/ProjectAddSummary/Administration' |
| 13 | +import LinkedReleasesAndProjects from '@/components/ProjectAddSummary/LinkedReleasesAndProjects' |
| 14 | +import Summary from '@/components/ProjectAddSummary/Summary' |
| 15 | +import { HttpStatus, InputKeyValue, Project, ToastData, Vendor } from '@/object-types' |
| 16 | +import { ApiUtils } from '@/utils' |
| 17 | +import { signOut, useSession } from 'next-auth/react' |
| 18 | +import { useTranslations } from 'next-intl' |
| 19 | +import { ToastMessage } from 'next-sw360' |
| 20 | +import { useRouter } from 'next/navigation' |
| 21 | +import { useState } from 'react' |
| 22 | +import { Button, Col, ListGroup, Row, Tab, ToastContainer } from 'react-bootstrap' |
| 23 | + |
| 24 | +function EditProjects() { |
| 25 | + const router = useRouter() |
| 26 | + const t = useTranslations('default') |
| 27 | + const { data: session, status } = useSession() |
| 28 | + const [vendor, setVendor] = useState<Vendor>({ |
| 29 | + id: '', |
| 30 | + fullName: '', |
| 31 | + }) |
| 32 | + |
| 33 | + const [externalUrls, setExternalUrls] = useState<InputKeyValue[]>([ |
| 34 | + { |
| 35 | + key: '', |
| 36 | + value: '', |
| 37 | + }, |
| 38 | + ]) |
| 39 | + |
| 40 | + const [externalIds, setExternalIds] = useState<InputKeyValue[]>([ |
| 41 | + { |
| 42 | + key: '', |
| 43 | + value: '', |
| 44 | + }, |
| 45 | + ]) |
| 46 | + |
| 47 | + const [additionalData, setAdditionalData] = useState<InputKeyValue[]>([ |
| 48 | + { |
| 49 | + key: '', |
| 50 | + value: '', |
| 51 | + }, |
| 52 | + ]) |
| 53 | + |
| 54 | + const [projectPayload, setProjectPayload] = useState<Project>({ |
| 55 | + name: '', |
| 56 | + description: '', |
| 57 | + version: '', |
| 58 | + visibility: 'EVERYONE', |
| 59 | + projectType: 'PRODUCT', |
| 60 | + tag: '', |
| 61 | + domain: '', |
| 62 | + leadArchitect: '', |
| 63 | + defaultVendorId: '', |
| 64 | + externalUrls: null, |
| 65 | + externalIds: null, |
| 66 | + additionalData: null, |
| 67 | + state: 'ACTIVE', |
| 68 | + phaseOutSince: '', |
| 69 | + moderators: null, |
| 70 | + contributors: null, |
| 71 | + clearingState: 'OPEN', |
| 72 | + businessUnit: 'CT', |
| 73 | + preevaluationDeadline: '', |
| 74 | + clearingSummary: '', |
| 75 | + specialRisksOSS: '', |
| 76 | + generalRisks3rdParty: '', |
| 77 | + specialRisks3rdParty: '', |
| 78 | + deliveryChannels: '', |
| 79 | + remarksAdditionalRequirements: '', |
| 80 | + systemTestStart: '', |
| 81 | + systemTestEnd: '', |
| 82 | + deliveryStart: '', |
| 83 | + licenseInfoHeaderText: '', |
| 84 | + linkedProjects: {}, |
| 85 | + }) |
| 86 | + |
| 87 | + const [toastData, setToastData] = useState<ToastData>({ |
| 88 | + show: false, |
| 89 | + type: '', |
| 90 | + message: '', |
| 91 | + contextual: '', |
| 92 | + }) |
| 93 | + |
| 94 | + const alert = (show_data: boolean, status_type: string, message: string, contextual: string) => { |
| 95 | + setToastData({ |
| 96 | + show: show_data, |
| 97 | + type: status_type, |
| 98 | + message: message, |
| 99 | + contextual: contextual, |
| 100 | + }) |
| 101 | + } |
| 102 | + |
| 103 | + const setExternalUrlsData = (externalUrls: Map<string, string>) => { |
| 104 | + const obj = Object.fromEntries(externalUrls) |
| 105 | + setProjectPayload({ |
| 106 | + ...projectPayload, |
| 107 | + externalUrls: obj, |
| 108 | + }) |
| 109 | + } |
| 110 | + |
| 111 | + const setExternalIdsData = (externalIds: Map<string, string>) => { |
| 112 | + const obj = Object.fromEntries(externalIds) |
| 113 | + setProjectPayload({ |
| 114 | + ...projectPayload, |
| 115 | + externalIds: obj, |
| 116 | + }) |
| 117 | + } |
| 118 | + |
| 119 | + const setAdditionalDataObject = (additionalData: Map<string, string>) => { |
| 120 | + const obj = Object.fromEntries(additionalData) |
| 121 | + setProjectPayload({ |
| 122 | + ...projectPayload, |
| 123 | + additionalData: obj, |
| 124 | + }) |
| 125 | + } |
| 126 | + |
| 127 | + const updateProject = async () => { |
| 128 | + const response = await ApiUtils.PATCH('projects', projectPayload, session.user.access_token) |
| 129 | + |
| 130 | + if (response.status == HttpStatus.OK) { |
| 131 | + await response.json() |
| 132 | + alert(true, 'success', t('Your project is updated'), 'success') |
| 133 | + // router.push('/projects') |
| 134 | + } else { |
| 135 | + alert(true, 'error', t('There are some errors while updating project'), 'danger') |
| 136 | + // router.push('/projects') |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + const handleCancelClick = () => { |
| 141 | + router.push('/projects') |
| 142 | + } |
| 143 | + |
| 144 | + if (status === 'unauthenticated') { |
| 145 | + signOut() |
| 146 | + } else { |
| 147 | + return ( |
| 148 | + <div className='container page-content'> |
| 149 | + <form |
| 150 | + action='' |
| 151 | + id='form_submit' |
| 152 | + method='post' |
| 153 | + onSubmit={(event) => { |
| 154 | + event.preventDefault() |
| 155 | + }} |
| 156 | + > |
| 157 | + <ToastContainer position='top-start'> |
| 158 | + <ToastMessage |
| 159 | + show={toastData.show} |
| 160 | + type={toastData.type} |
| 161 | + message={toastData.message} |
| 162 | + contextual={toastData.contextual} |
| 163 | + onClose={() => setToastData({ ...toastData, show: false })} |
| 164 | + setShowToast={setToastData} |
| 165 | + /> |
| 166 | + </ToastContainer> |
| 167 | + <div> |
| 168 | + <Tab.Container defaultActiveKey='summary'> |
| 169 | + <Row> |
| 170 | + <Col sm='auto' className='me-3'> |
| 171 | + <ListGroup> |
| 172 | + <ListGroup.Item action eventKey='summary'> |
| 173 | + <div className='my-2'>{t('Summary')}</div> |
| 174 | + </ListGroup.Item> |
| 175 | + <ListGroup.Item action eventKey='administration'> |
| 176 | + <div className='my-2'>{t('Administration')}</div> |
| 177 | + </ListGroup.Item> |
| 178 | + <ListGroup.Item action eventKey='linkedProjectsAndReleases'> |
| 179 | + <div className='my-2'>{t('Linked Releases and Projects')}</div> |
| 180 | + </ListGroup.Item> |
| 181 | + <ListGroup.Item action eventKey='attachments'> |
| 182 | + <div className='my-2'>{t('Attachments')}</div> |
| 183 | + </ListGroup.Item> |
| 184 | + <ListGroup.Item action eventKey='obligations'> |
| 185 | + <div className='my-2'>{t('Obligations')}</div> |
| 186 | + </ListGroup.Item> |
| 187 | + </ListGroup> |
| 188 | + </Col> |
| 189 | + <Col className='me-3'> |
| 190 | + <Row className='d-flex justify-content-between'> |
| 191 | + <Col lg={3}> |
| 192 | + <Row> |
| 193 | + <Button |
| 194 | + variant='primary' |
| 195 | + type='submit' |
| 196 | + className='me-2 col-auto' |
| 197 | + onClick={updateProject} |
| 198 | + > |
| 199 | + {t('Update Project')} |
| 200 | + </Button> |
| 201 | + <Button |
| 202 | + variant='danger' |
| 203 | + type='submit' |
| 204 | + className='me-2 col-auto' |
| 205 | + onClick={updateProject} |
| 206 | + > |
| 207 | + {t('Delete Project')} |
| 208 | + </Button> |
| 209 | + <Button |
| 210 | + variant='secondary' |
| 211 | + className='col-auto' |
| 212 | + onClick={handleCancelClick} |
| 213 | + > |
| 214 | + {t('Cancel')} |
| 215 | + </Button> |
| 216 | + </Row> |
| 217 | + </Col> |
| 218 | + <Col lg={4} className='text-truncate buttonheader-title'> |
| 219 | + {t('Update Project')} |
| 220 | + </Col> |
| 221 | + </Row> |
| 222 | + <Row className='mt-5'> |
| 223 | + <Tab.Content> |
| 224 | + <Tab.Pane eventKey='summary'> |
| 225 | + <Summary |
| 226 | + vendor={vendor} |
| 227 | + setVendor={setVendor} |
| 228 | + externalUrls={externalUrls} |
| 229 | + setExternalUrls={setExternalUrls} |
| 230 | + setExternalUrlsData={setExternalUrlsData} |
| 231 | + externalIds={externalIds} |
| 232 | + setExternalIds={setExternalIds} |
| 233 | + setExternalIdsData={setExternalIdsData} |
| 234 | + additionalData={additionalData} |
| 235 | + setAdditionalData={setAdditionalData} |
| 236 | + setAdditionalDataObject={setAdditionalDataObject} |
| 237 | + projectPayload={projectPayload} |
| 238 | + setProjectPayload={setProjectPayload} |
| 239 | + /> |
| 240 | + </Tab.Pane> |
| 241 | + <Tab.Pane eventKey='administration'> |
| 242 | + <Administration |
| 243 | + projectPayload={projectPayload} |
| 244 | + setProjectPayload={setProjectPayload} |
| 245 | + /> |
| 246 | + </Tab.Pane> |
| 247 | + <Tab.Pane eventKey='linkedProjectsAndReleases'> |
| 248 | + <LinkedReleasesAndProjects |
| 249 | + projectPayload={projectPayload} |
| 250 | + setProjectPayload={setProjectPayload} |
| 251 | + /> |
| 252 | + </Tab.Pane> |
| 253 | + <Tab.Pane eventKey='attachments'></Tab.Pane> |
| 254 | + <Tab.Pane eventKey='obligations'></Tab.Pane> |
| 255 | + </Tab.Content> |
| 256 | + </Row> |
| 257 | + </Col> |
| 258 | + </Row> |
| 259 | + </Tab.Container> |
| 260 | + </div> |
| 261 | + </form> |
| 262 | + </div> |
| 263 | + ) |
| 264 | + } |
| 265 | +} |
| 266 | + |
| 267 | +export default EditProjects |
0 commit comments