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

Provide list of licenses #586

Merged
merged 4 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
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
87 changes: 70 additions & 17 deletions client/components/Editors/Resource/Sections/Licenses.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import * as React from 'react'
// https://licenses.opendefinition.org/licenses/groups/all.json
import openDefinitionLicenses from './licenses.json'
import { find, last } from 'lodash'
import Box from '@mui/material/Box'
import Columns from '../../../Parts/Grids/Columns'
import InputField from '../../../Parts/Fields/Input'
import EditorItem from '../../Base/Item'
import EditorList from '../../Base/List'
import EditorListItem from '../../Base/ListItem'
import Dialog from '@mui/material/Dialog'
import DialogContent from '@mui/material/DialogContent'
import DialogTitle from '@mui/material/DialogTitle'
import TextField from '@mui/material/TextField'
import FormControl from '@mui/material/FormControl'
import Autocomplete from '@mui/material/Autocomplete'
import { useStore, selectors, select } from '../store'
import validator from 'validator'

Expand All @@ -14,24 +23,66 @@ export default function Licenses() {
}

function LicenseList() {
const [dialogOpen, setDialogOpen] = React.useState(false)

const query = useStore((state) => state.licenseState.query)
const licenseItems = useStore(selectors.licenseItems)
const updateLicenseState = useStore((state) => state.updateLicenseState)
const addLicense = useStore((state) => state.addLicense)
const removeLicense = useStore((state) => state.removeLicense)

return (
<>
<EditorList kind="license" query={query} onAddClick={() => setDialogOpen(true)}>
{licenseItems.map(({ index, license }) => (
<EditorListItem
key={index}
kind="license"
name={license.name}
type="license"
onClick={() => updateLicenseState({ index })}
onRemoveClick={() => removeLicense(index)}
/>
))}
</EditorList>
<LicenseDialog open={dialogOpen} onClose={() => setDialogOpen(false)} />
</>
)
}

function LicenseDialog(props: { open: boolean; onClose: () => void }) {
const addLicense = useStore((state) => state.addLicense)

const licenses = Object.values(openDefinitionLicenses).map((license) => ({
name: license.id,
path: license.url,
title: license.title,
}))

const handleSelect = (_event: any, title: string | null) => {
if (!title) return
const license = find(licenses, { title }) || last(licenses)!
addLicense(license)
props.onClose()
}

return (
<EditorList kind="license" query={query} onAddClick={() => addLicense()}>
{licenseItems.map(({ index, license }) => (
<EditorListItem
key={index}
kind="license"
name={license.name}
type="license"
onClick={() => updateLicenseState({ index })}
onRemoveClick={() => removeLicense(index)}
/>
))}
</EditorList>
<Box>
<Dialog open={props.open} onClose={props.onClose}>
<DialogTitle>Select the license</DialogTitle>
<DialogContent>
<Box component="form">
<FormControl sx={{ my: 1, minWidth: '30em' }}>
<Autocomplete
autoSelect
onChange={handleSelect}
options={licenses.map((license) => license.title)}
renderInput={(params) => <TextField {...params} label="Type to search" />}
></Autocomplete>
</FormControl>
</Box>
</DialogContent>
</Dialog>
</Box>
)
}

Expand Down Expand Up @@ -64,17 +115,19 @@ function Name() {
const name = useStore(select(selectors.license, (license) => license.name))
const updateHelp = useStore((state) => state.updateHelp)
const updateLicense = useStore((state) => state.updateLicense)
const [isValid, setIsValid] = React.useState(isValidName())
function isValidName() {
return name ? validator.isSlug(name) : false
const [isValid, setIsValid] = React.useState(isValidLicenseName())

function isValidLicenseName() {
return Object.keys(openDefinitionLicenses).includes(name)
}

return (
<InputField
label="Name"
value={name}
onFocus={() => updateHelp('resource/licenses/name')}
onBlur={() => {
setIsValid(isValidName())
setIsValid(isValidLicenseName())
}}
onChange={(value) => updateLicense({ name: value || 'name' })}
helperText={!isValid ? 'Name is not valid.' : ''}
Expand Down
Loading
Loading