Skip to content

Commit

Permalink
feat(release): Edit Release - Move function handle Url to CommonUitls
Browse files Browse the repository at this point in the history
Signed-off-by: tuannn2 <[email protected]>
  • Loading branch information
tuannn2 committed Sep 6, 2023
1 parent 884bb34 commit 0688d3b
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import COTSDetails from '@/object-types/COTSDetails'
import ToastData from '@/object-types/ToastData'
import { ToastContainer } from 'react-bootstrap'
import ToastMessage from '@/components/sw360/ToastContainer/Toast'
import CommonUtils from '@/utils/common.utils'

interface Props {
session?: Session
Expand Down Expand Up @@ -161,16 +162,12 @@ const AddRelease = ({ session, componentId }: Props) => {
})
}, [componentId, fetchData])

const handleId = (id: string): string => {
return id.split('/').at(-1)
}

const submit = async () => {
const response = await ApiUtils.POST('releases', releasePayload, session.user.access_token)
if (response.status == HttpStatus.CREATED) {
const data = await response.json()
alert(true, 'Success', t('Release is created'), 'success')
const releaseId: string = handleId(data._links.self.href)
const releaseId: string = CommonUtils.getIdFromUrl(data._links.self.href)
router.push('/components/releases/detail/' + releaseId)
} else if (response.status == HttpStatus.CONFLICT) {
alert(true, 'Duplicate', t('Release is Duplicate'), 'warning')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import Vendor from '@/object-types/Vendor'
import DocumentTypes from '@/object-types/enums/DocumentTypes'
import HttpStatus from '@/object-types/enums/HttpStatus'
import ApiUtils from '@/utils/api/api.util'
import CommonUtils from '@/utils/common.utils'
import { signOut } from 'next-auth/react'
import { useTranslations } from 'next-intl'
import { notFound } from 'next/navigation'
Expand Down Expand Up @@ -211,10 +212,6 @@ export default function ReleaseEditSummary({
return inputRoles
}

const handleId = (id: string): string => {
return id.split('/').at(-1)
}

useEffect(() => {
if (typeof release.roles !== 'undefined') {
setRoles(convertObjectToMapRoles(release.roles))
Expand All @@ -238,7 +235,7 @@ export default function ReleaseEditSummary({

let vendorId = ''
if (typeof release['_embedded']['sw360:vendors'] !== 'undefined') {
vendorId = handleId(release['_embedded']['sw360:vendors'][0]._links.self.href)
vendorId = CommonUtils.getIdFromUrl(release['_embedded']['sw360:vendors'][0]._links.self.href)
const vendor: Vendor = {
id: vendorId,
fullName: release['_embedded']['sw360:vendors'][0].fullName,
Expand All @@ -258,7 +255,7 @@ export default function ReleaseEditSummary({

let componentId = ''
if (typeof release['_links']['sw360:component']['href'] !== 'undefined') {
componentId = handleId(release['_links']['sw360:component']['href'])
componentId = CommonUtils.getIdFromUrl(release['_links']['sw360:component']['href'])
}

const releasePayload: ReleasePayload = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import React from 'react'
import { _ } from '@/components/sw360'
import LinkedRelease from '@/object-types/LinkedRelease'
import LinkedReleasesTable from './LinkedReleasesTable'
import CommonUtils from '@/utils/common.utils'

interface Props {
releases?: any[]
Expand All @@ -33,7 +34,7 @@ const SelectTableLinkedReleases = ({ releases, setLinkedReleases, linkedReleases
const releaseLinks: LinkedRelease[] = []
linkedReleases.forEach((item: any) => {
const releaseLink: LinkedRelease = {
id: handleId(item._links.self.href),
id: CommonUtils.getIdFromUrl(item._links.self.href),
name: item.name,
version: item.version,
mainlineState: item.mainlineState,
Expand All @@ -46,10 +47,6 @@ const SelectTableLinkedReleases = ({ releases, setLinkedReleases, linkedReleases
setLinkedReleases(releaseLinks)
}

const handleId = (id: string): string => {
return id.split('/').at(-1)
}

const columns = [
{
id: 'releaseId',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { Table, _ } from '@/components/sw360'
import LicensesTable from './MainLicensesTable'
import Licenses from '@/object-types/Licenses'
import { LicensesType } from '@/object-types/LicensesType'
import CommonUtils from '@/utils/common.utils'

interface Props {
licenseDatas?: any[]
Expand All @@ -35,7 +36,7 @@ const SelectTableMainLicenses = ({ licenseDatas, setLicenses, fullnames }: Props
const licensesId: string[] = []
fullnames.forEach((item) => {
fullNameLicenses.push(item.fullName)
licensesId.push(handleId(item._links.self.href))
licensesId.push(CommonUtils.getIdFromUrl(item._links.self.href))
})
const licensesName: string = fullNameLicenses.join(' , ')
const licensesResponse: Licenses = {
Expand All @@ -45,10 +46,6 @@ const SelectTableMainLicenses = ({ licenseDatas, setLicenses, fullnames }: Props
setLicenses(licensesResponse)
}

const handleId = (id: string): string => {
return id.split('/').at(-1)
}

const columns = [
{
id: 'licenseId',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { _ } from '@/components/sw360'
import LicensesTable from './OtherLicensesTable'
import { LicensesType } from '@/object-types/LicensesType'
import Licenses from '@/object-types/Licenses'
import CommonUtils from '@/utils/common.utils'

interface Props {
licenseDatas?: any[]
Expand All @@ -35,7 +36,7 @@ const SelectTableOtherLicenses = ({ licenseDatas, setLicenses, fullnames }: Prop
const licensesId: string[] = []
fullnames.forEach((item) => {
fullNameLicenses.push(item.fullName)
licensesId.push(handleId(item._links.self.href))
licensesId.push(CommonUtils.getIdFromUrl(item._links.self.href))
})
const licensesName: string = fullNameLicenses.join(' , ')
const licensesResponse: Licenses = {
Expand All @@ -45,10 +46,6 @@ const SelectTableOtherLicenses = ({ licenseDatas, setLicenses, fullnames }: Prop
setLicenses(licensesResponse)
}

const handleId = (id: string): string => {
return id.split('/').at(-1)
}

const columns = [
{
id: 'licenseId',
Expand Down
7 changes: 2 additions & 5 deletions src/components/sw360/SearchVendorsModal/SelectTableVendor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import React from 'react'
import { Table, _ } from '@/components/sw360'
import Vendor from '@/object-types/Vendor'
import { VendorType } from '@/object-types/VendorType'
import CommonUtils from '@/utils/common.utils'

interface Props {
vendors: any[]
Expand All @@ -24,18 +25,14 @@ interface Props {
const SelectTableVendor = ({ vendors, setVendor }: Props) => {
// item._links.self.href
const handlerRadioButton = (item: any) => {
const vendorId: string = handleId(item._links.self.href)
const vendorId: string = CommonUtils.getIdFromUrl(item._links.self.href)
const vendorResponse: Vendor = {
id: vendorId,
fullName: item.fullName,
}
setVendor(vendorResponse)
}

const handleId = (id: string): string => {
return id.split('/').at(-1)
}

const columns = [
{
id: 'vendorId',
Expand Down
5 changes: 5 additions & 0 deletions src/utils/common.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,16 @@ const isNullEmptyOrUndefinedArray = (arr: Array<any>) => {
return false;
}

const getIdFromUrl = (url: string): string => {
return url.split('/').at(-1)
}

const CommonUtils = {
isNullOrUndefined,
isNullEmptyOrUndefinedString,
createUrlWithParams,
isNullEmptyOrUndefinedArray,
getIdFromUrl
}

export default CommonUtils;

0 comments on commit 0688d3b

Please sign in to comment.