From 1493cb19287101b59c95d8afeafe420eabc613d4 Mon Sep 17 00:00:00 2001 From: Callum West Date: Wed, 9 Aug 2023 14:28:06 +0100 Subject: [PATCH 1/2] Closed source hidden fields --- .../edit/information/SoftwareInformationForm.tsx | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/frontend/components/software/edit/information/SoftwareInformationForm.tsx b/frontend/components/software/edit/information/SoftwareInformationForm.tsx index df13865a8..b9c700f1f 100644 --- a/frontend/components/software/edit/information/SoftwareInformationForm.tsx +++ b/frontend/components/software/edit/information/SoftwareInformationForm.tsx @@ -45,6 +45,18 @@ export default function SoftwareInformationForm({editSoftware}: SoftwareInformat // console.log('formData...', formData) // console.groupEnd() + let repoURL, citationField + repoURL = citationField = <> + + if (formData.closed_source){ + repoURL = <> + citationField = <> + } + else { + repoURL = + citationField = + } + return (
- + {repoURL}
{/* add white space at the bottom */} @@ -143,7 +155,7 @@ export default function SoftwareInformationForm({editSoftware}: SoftwareInformat
- + {citationField}
From b65f90b85aebff94dfba8ebdae640a57bdb3c846 Mon Sep 17 00:00:00 2001 From: Callum West Date: Tue, 15 Aug 2023 16:58:36 +0100 Subject: [PATCH 2/2] Closed source license logic --- .../information/SoftwareInformationForm.tsx | 96 ++++++++++++++++--- 1 file changed, 83 insertions(+), 13 deletions(-) diff --git a/frontend/components/software/edit/information/SoftwareInformationForm.tsx b/frontend/components/software/edit/information/SoftwareInformationForm.tsx index b9c700f1f..3c9c29801 100644 --- a/frontend/components/software/edit/information/SoftwareInformationForm.tsx +++ b/frontend/components/software/edit/information/SoftwareInformationForm.tsx @@ -19,13 +19,18 @@ import AutosaveSoftwareLicenses from './AutosaveSoftwareLicenses' import AutosaveSoftwareMarkdown from './AutosaveSoftwareMarkdown' import AutosaveSoftwareLogo from './AutosaveSoftwareLogo' import AutosaveSoftwareSwitch from './AutosaveSoftwareSwitch' +import {addLicensesForSoftware, deleteLicense} from '~/utils/editSoftware' +import {getLicenseForSoftware} from '~/utils/getSoftware' +import useSnackbar from '~/components/snackbar/useSnackbar' +import {License} from '~/types/SoftwareTypes' +import {AutocompleteOption} from '~/components/form/AsyncAutocompleteSC' type SoftwareInformationFormProviderProps = { editSoftware: EditSoftwareItem } export default function SoftwareInformationForm({editSoftware}: SoftwareInformationFormProviderProps) { - const {user} = useSession() + const {user, token} = useSession() const methods = useForm({ mode: 'onChange', defaultValues: { @@ -39,22 +44,90 @@ export default function SoftwareInformationForm({editSoftware}: SoftwareInformat const {dirtyFields} = formState // watch form data changes (we use reset in useEffect) const formData = watch() + const {showErrorMessage} = useSnackbar() - // console.group('SoftwareInformationForm') - // console.log('editSoftware...', editSoftware) - // console.log('formData...', formData) - // console.groupEnd() + console.group('SoftwareInformationForm') + console.log('editSoftware...', editSoftware) + console.log('formData...', formData) + console.log('dirtyFields...', dirtyFields) + console.groupEnd() - let repoURL, citationField - repoURL = citationField = <> + let repoURL, citationField, licensesField + repoURL = citationField = licensesField = <> if (formData.closed_source){ - repoURL = <> - citationField = <> + repoURL = citationField = licensesField = <> + closedSourceLicense() } else { repoURL = citationField = + licensesField = + openSourceLicense() + } + + async function closedSourceLicense() { + let resp = await getLicenses() + console.log("closedSourceLicense_resp:") + console.log(resp) + if (!(resp.length === 1 && resp[0].license === "Proprietary")) { + console.log("CLOSED") + await removeAllLicenses(resp, token) + await addClosedSourceLicense(formData.id, token) + resp = await getLicenses() + console.log(resp) + } + } + + async function openSourceLicense() { + const resp = await getLicenses() + console.log("openSourceLicense_resp:") + console.log(resp) + if (resp.length === 1 && resp[0].license === "Proprietary") { + console.log("OPEN") + removeAllLicenses(resp, token) + } + } + + async function getLicenses() { + const resp = await getLicenseForSoftware(formData.id, true, token) + console.log("getLicenses_resp:") + console.log(resp) + if (!resp) { + showErrorMessage(`Failed to get licenses.`) + } + else { + return resp + } + } + + async function addClosedSourceLicense(software: string, token: string) { + console.log("POSTing") + const resp = await addLicensesForSoftware({ + software: software, + license: "Proprietary", + token: token, + }) + if (resp.status !== 201) { + showErrorMessage(`Failed to add Proprietary license. ${resp.message}`) + } + } + + async function removeAllLicenses(licenses: License[], token: string) { + console.log("Removing") + for (let i=0;i
- + {licensesField} {/* add white space at the bottom */}