Skip to content

Commit

Permalink
Merge pull request #523 from research-software-directory/518-license-…
Browse files Browse the repository at this point in the history
…import

518 fix: extract licenses without rightsIdentifier from DOI
  • Loading branch information
dmijatovic authored Sep 14, 2022
2 parents 574c081 + dbb481c commit fccd628
Show file tree
Hide file tree
Showing 11 changed files with 241 additions and 244 deletions.
3 changes: 1 addition & 2 deletions frontend/components/software/edit/contributors/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
getAvatarUrl, getContributorsForSoftware,
prepareContributorData, updateContributorInDb
} from '~/utils/editContributors'
import useOnUnsaveChange from '~/utils/useOnUnsavedChange'
import {getDisplayName} from '~/utils/getDisplayName'
import {sortOnStrProp} from '~/utils/sortFn'
import {getPropsFromObject} from '~/utils/getPropsFromObject'
Expand Down Expand Up @@ -216,7 +215,7 @@ export default function SoftwareContributors({slug}: { slug: string }) {
setLoading(true)

const contribDoi: Contributor[] = await getContributorsFromDoi(
software?.id, software?.concept_doi
software?.id ?? '', software?.concept_doi ?? ''
)

if (!contribDoi || contribDoi.length === 0) {
Expand Down
8 changes: 4 additions & 4 deletions frontend/components/software/edit/editSoftwareContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import {editSoftwarePage, EditSoftwarePageProps} from './editSoftwareSteps'
import {EditSoftwareAction, editSoftwareReducer} from './editSoftwareReducer'

export type SoftwareInfo = {
id?: string,
slug?: string,
brand_name?: string,
concept_doi?: string,
id: string | null,
slug: string | null,
brand_name: string | null,
concept_doi: string | null,
}

export type EditSoftwareState = {
Expand Down
42 changes: 31 additions & 11 deletions frontend/components/software/edit/information/SoftwareLicenses.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,28 +62,46 @@ export default function SoftwareLicenses(
const licenses = await getLicensesFromDoi(concept_doi)
// find licenses SPDX keys that match items in the options
for (const license of licenses) {
// exlude if already in fields
const find = fields.filter(item => item.key.toLowerCase() === license.toLowerCase())
if (find.length > 0) {
// find license by identifier
let spdx = allOptions.find(item => item.key.toLocaleLowerCase() === license.toLowerCase())
if (typeof spdx == 'undefined') {
// if not found by identifier try to find it by name
spdx = allOptions.find(item => item.data.name.toLocaleLowerCase() === license.toLowerCase())
}
let find
if (typeof spdx !== 'undefined') {
// exlude if already in fields based on spdx key
find = fields.find(item => item.key.toLowerCase() === spdx?.key.toLowerCase())
} else {
// exlude if already in fields based on key
find = fields.find(item => item.key.toLowerCase() === license.toLowerCase())
if (typeof find == 'undefined') {
// try to match on label
find = fields.find(item => item.label.toLowerCase() === license.toLowerCase())
}
}
if (find) {
// go to next license thisone is already proccessed
continue
}
// add to collection
added++
// improve identifier
const spdx = allOptions.find(item=>item.key.toLocaleLowerCase()===license.toLowerCase())
// add to fields collection
append({
key: spdx?.key ?? license,
label: spdx?.key ?? license,
data: {
id: undefined,
software,
license: spdx?.key ?? license
license: spdx?.key ?? license,
name: spdx?.data.name ?? license
}
})
}
setDoiLoad(false)
if (added > 0) {
showSuccessMessage(`${added} liceses imported from DOI ${concept_doi}`)
if (added === 1) {
showSuccessMessage(`${added} license imported from DOI ${concept_doi}`)
} else if (added > 1) {
showSuccessMessage(`${added} licenses imported from DOI ${concept_doi}`)
} else {
showInfoMessage(`No (additional) license to import from DOI ${concept_doi}`)
}
Expand All @@ -105,7 +123,8 @@ export default function SoftwareLicenses(
id: undefined,
software,
license: item.label,
deprecated: item.data.deprecated
deprecated: item.data.deprecated,
name: item.data.name
}
}
})
Expand Down Expand Up @@ -140,7 +159,8 @@ export default function SoftwareLicenses(
data: {
id: undefined,
software,
license: newInputValue
license: newInputValue,
name: newInputValue
}
})
}
Expand Down
25 changes: 17 additions & 8 deletions frontend/components/software/edit/information/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import SoftwareKeywords from './SoftwareKeywords'
import {getKeywordChanges} from './softwareKeywordsChanges'
import ConceptDoi from './ConceptDoi'
import useSoftwareContext from '../useSoftwareContext'
import useSoftwareToEdit from './useSoftwareToEdit'
import useSoftwareToEdit, {getSoftwareInfoForEdit} from './useSoftwareToEdit'

export default function SoftwareInformation({slug}: {slug: string}) {
const {token,user} = useSession()
Expand All @@ -39,7 +39,7 @@ export default function SoftwareInformation({slug}: {slug: string}) {
// destructure methods from react-hook-form
const {
register, handleSubmit, watch, formState, reset,
control, setValue, getValues
control, setValue
} = useFormContext<EditSoftwareItem>()

const {update: updateKeyword} = useFieldArray({
Expand Down Expand Up @@ -68,7 +68,8 @@ export default function SoftwareInformation({slug}: {slug: string}) {
setSoftwareInfo({
id: initalState.id,
slug: initalState.slug,
brand_name: initalState.brand_name
brand_name: initalState.brand_name,
concept_doi: initalState.concept_doi,
})
setLoading(false)
}
Expand Down Expand Up @@ -109,12 +110,20 @@ export default function SoftwareInformation({slug}: {slug: string}) {
})
// if OK
if (resp.status === 200) {
// reload software from api (confirm all changes saved and retreive new ids)
const updated = await getSoftwareInfoForEdit({slug, token})
if (updated) {
// update local state (useEffect will then reset form to new values)
setEditSoftware(updated)
// update shared software info
setSoftwareInfo({
id: updated.id,
slug: updated.slug,
brand_name: updated.brand_name,
concept_doi: updated.concept_doi,
})
}
showSuccessMessage(`${formData?.brand_name} saved`)
// update software state
// reset form to remove dirty states with latest form data
const latestFormData = getValues()
// to be equal to data in the form
setEditSoftware(latestFormData)
} else {
showErrorMessage(`Failed to save. ${resp.message}`)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function prepareLicenses(rawLicense: License[]=[]) {
}


async function getSoftwareInfoForEdit({slug, token}: { slug: string, token: string }) {
export async function getSoftwareInfoForEdit({slug, token}: { slug: string, token: string }) {
const software = await getSoftwareToEdit({slug, token})

if (software) {
Expand Down
2 changes: 1 addition & 1 deletion frontend/components/software/edit/organisations/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export default function SoftwareOganisations() {
// if it has id
if (organisation?.id) {
const resp = await deleteOrganisationFromSoftware({
software: software?.id,
software: software?.id ?? undefined,
organisation: organisation.id,
token
})
Expand Down
10 changes: 7 additions & 3 deletions frontend/types/SoftwareTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,15 @@ export type KeywordForSoftware = {
* LiCENSES
*/

export type License = {
id?: string,
export type LicenseForSoftware = {
software: string
license: string
deprecated?: boolean
}

export type License = LicenseForSoftware & {
id?: string,
deprecated?: boolean,
name: string
}


Expand Down
5 changes: 3 additions & 2 deletions frontend/utils/editSoftware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {
EditSoftwareItem,
License,
SoftwareItemFromDB,
KeywordForSoftware
KeywordForSoftware,
LicenseForSoftware
} from '../types/SoftwareTypes'
import {getPropsFromObject} from './getPropsFromObject'
import {AutocompleteOption} from '../types/AutocompleteOptions'
Expand Down Expand Up @@ -383,7 +384,7 @@ export async function createKeywordAndAddToSoftware({data, token, updateKeyword}
}

export async function addLicensesForSoftware({software, data, token}:
{software: string, data: License[], token: string}) {
{ software: string, data: LicenseForSoftware[], token: string}) {
try {
const url = `/api/v1/license_for_software?software=eq.${software}`
const resp = await fetch(url, {
Expand Down
Loading

0 comments on commit fccd628

Please sign in to comment.