Skip to content

Commit

Permalink
Merge pull request #53 from andybywire/47-add-concept-id-generator
Browse files Browse the repository at this point in the history
feat: add identifier generator input field
  • Loading branch information
andybywire committed Jun 6, 2023
2 parents 45d8535 + ae0d6dc commit a72bab9
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 16 deletions.
34 changes: 34 additions & 0 deletions src/components/inputs/Identifier.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Create Unique Identifier
* For schemes and concepts created in previous versions of the
* plugin.
* - Input is only visible if no identifier has been assigned
* - Input disappears once an ID is generated
*/

import {useCallback} from 'react'
import {set} from 'sanity'
import {Button, Inline, Stack, useToast} from '@sanity/ui'
import {randomKey} from '@sanity/util/content'

export const Identifier = (props: any) => {
const {onChange} = props
const toast = useToast()

const handleChange = useCallback(() => {
onChange(set(randomKey(6)))
toast.push({
status: 'success',
title: 'Identifier created.',
closable: true,
})
}, [onChange, toast])

return (
<Stack space={2}>
<Inline space={[3, 3, 4]}>
<Button tone="primary" fontSize={2} onClick={handleChange} text="Generate Identifier" />
</Inline>
</Stack>
)
}
1 change: 1 addition & 0 deletions src/components/inputs/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './RdfUri'
export * from './Identifier'
21 changes: 13 additions & 8 deletions src/skosConcept.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {AiFillTag, AiFillTags} from 'react-icons/ai'
import {defineType, defineField} from 'sanity'
import {DescriptionDetail} from './styles'
import baseIriField from './modules/baseIriField'
import {Identifier} from './components/inputs'
import {randomKey} from '@sanity/util/content'

export default function skosConcept(baseUri?: string) {
Expand Down Expand Up @@ -43,14 +44,6 @@ export default function skosConcept(baseUri?: string) {
}
},
fields: [
defineField({
name: 'conceptId',
title: 'Concept ID',
type: 'string',
initialValue: () => `${randomKey(6)}`,
hidden: true,
readOnly: true,
}),
defineField({
name: 'prefLabel',
title: 'Preferred Label',
Expand Down Expand Up @@ -115,6 +108,18 @@ export default function skosConcept(baseUri?: string) {
validation: (Rule) => Rule.unique(),
}),
...baseIriField,
defineField({
name: 'conceptId',
title: 'Identifier',
description: 'This concept does not yet have a unique identifier.',
type: 'string',
initialValue: () => `${randomKey(6)}`,
hidden: ({document}) => !!document?.conceptId,
readOnly: ({document}) => !!document?.conceptId,
components: {
input: Identifier,
},
}),
defineField({
name: 'conceptIriBase',
title: 'Edit the base IRI',
Expand Down
21 changes: 13 additions & 8 deletions src/skosConceptScheme.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import {RiNodeTree} from 'react-icons/ri'
import {defineArrayMember, defineField, defineType} from 'sanity'
import baseIriField from './modules/baseIriField'
import {Identifier} from './components/inputs'
import {randomKey} from '@sanity/util/content'

export default function skosConceptScheme(baseUri?: string) {
Expand All @@ -29,14 +30,6 @@ export default function skosConceptScheme(baseUri?: string) {
}
},
fields: [
defineField({
name: 'schemeId',
title: 'Scheme ID',
type: 'string',
initialValue: () => `${randomKey(6)}`,
hidden: true,
readOnly: true,
}),
defineField({
name: 'title',
title: 'Title',
Expand All @@ -59,6 +52,18 @@ export default function skosConceptScheme(baseUri?: string) {
initialValue: true,
}),
...baseIriField,
defineField({
name: 'schemeId',
title: 'Identifier',
description: 'This scheme does not yet have a unique identifier.',
type: 'string',
initialValue: () => `${randomKey(6)}`,
hidden: ({document}) => !!document?.schemeId,
readOnly: ({document}) => !!document?.schemeId,
components: {
input: Identifier,
},
}),
defineField({
name: 'topConcepts',
title: 'Top Concepts',
Expand Down

0 comments on commit a72bab9

Please sign in to comment.