Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed source UI #23

Draft
wants to merge 2 commits into
base: closed-source-ui
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<EditSoftwareItem>({
mode: 'onChange',
defaultValues: {
Expand All @@ -39,11 +44,91 @@ 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, licensesField
repoURL = citationField = licensesField = <></>

if (formData.closed_source){
repoURL = citationField = licensesField = <></>
closedSourceLicense()
}
else {
repoURL = <AutosaveRepositoryUrl />
citationField = <AutosaveConceptDoi />
licensesField = <AutosaveSoftwareLicenses
items={formData.licenses}
concept_doi={formData.concept_doi ?? undefined}
/>
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<licenses.length;i++) {
const resp = await deleteLicense({
id: String(licenses[i].id),
token: token,
})
console.log("loop",i,resp)
if (resp.status !== 200) {
showErrorMessage(`Failed to remove license. ${resp.message}`)
}
}
}

return (
<FormProvider {...methods}>
Expand Down Expand Up @@ -134,7 +219,7 @@ export default function SoftwareInformationForm({editSoftware}: SoftwareInformat
rules={config.get_started_url.validation}
/>
<div className="py-2"></div>
<AutosaveRepositoryUrl />
{repoURL}
<div className="py-2"></div>
<AutosaveSoftwareMarkdown />
{/* add white space at the bottom */}
Expand All @@ -143,7 +228,7 @@ export default function SoftwareInformationForm({editSoftware}: SoftwareInformat
<div className="py-4 min-w-[21rem] xl:my-0">
<AutosaveSoftwarePageStatus />
<div className="py-4"></div>
<AutosaveConceptDoi />
{citationField}
<div className="py-4"></div>
<AutosaveSoftwareLogo />
<div className="py-4"></div>
Expand All @@ -153,10 +238,7 @@ export default function SoftwareInformationForm({editSoftware}: SoftwareInformat
items={formData.keywords ?? []}
/>
<div className="py-4"></div>
<AutosaveSoftwareLicenses
items={formData.licenses}
concept_doi={formData.concept_doi ?? undefined}
/>
{licensesField}
{/* add white space at the bottom */}
<div className="py-4"></div>
</div>
Expand Down