-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
454 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
...a-browser/src/components/forms/NewForm/CustomCreateActions/CustomForms/NewDriveDialog.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import { core, useStore, server, dataBrowser, Resource } from '@tomic/react'; | ||
import { useState, useCallback, FormEvent, FC, useEffect } from 'react'; | ||
import { styled } from 'styled-components'; | ||
import { stringToSlug } from '../../../../../helpers/stringToSlug'; | ||
import { Button } from '../../../../Button'; | ||
import { | ||
useDialog, | ||
Dialog, | ||
DialogContent, | ||
DialogActions, | ||
} from '../../../../Dialog'; | ||
import Field from '../../../Field'; | ||
import { InputWrapper, InputStyled } from '../../../InputStyles'; | ||
import { CustomResourceDialogProps } from '../../useNewResourceUI'; | ||
import { useCreateAndNavigate } from '../../../../../hooks/useCreateAndNavigate'; | ||
import { useSettings } from '../../../../../helpers/AppSettings'; | ||
|
||
export const NewDriveDialog: FC<CustomResourceDialogProps> = ({ | ||
parent, | ||
onClose, | ||
}) => { | ||
const store = useStore(); | ||
const { setDrive } = useSettings(); | ||
const [name, setName] = useState(''); | ||
|
||
const createAndNavigate = useCreateAndNavigate(); | ||
|
||
const onSuccess = useCallback(async () => { | ||
if (!name.trim()) return; | ||
|
||
const agent = store.getAgent(); | ||
|
||
if (!agent || agent.subject === undefined) { | ||
throw new Error( | ||
'No agent set in the Store, required when creating a Drive', | ||
); | ||
} | ||
|
||
const newDrive = await createAndNavigate( | ||
server.classes.drive, | ||
{ | ||
[core.properties.name]: name, | ||
[core.properties.write]: [agent.subject], | ||
[core.properties.read]: [agent.subject], | ||
}, | ||
undefined, | ||
undefined, | ||
async resource => { | ||
// resources created with createAndNavigate have a parent by default which we don't want for drives. | ||
resource.remove(core.properties.parent); | ||
|
||
// Add drive to the agents drive list. | ||
const agentResource = await store.getResource(agent.subject!); | ||
agentResource.push(server.properties.drives, [resource.subject]); | ||
await agentResource.save(); | ||
|
||
// Create a default ontology. | ||
const ontologyName = stringToSlug(name); | ||
const ontology = await store.newResource({ | ||
subject: await store.buildUniqueSubjectFromParts( | ||
['defaultOntology'], | ||
resource.subject, | ||
), | ||
isA: core.classes.ontology, | ||
parent: resource.subject, | ||
propVals: { | ||
[core.properties.shortname]: ontologyName, | ||
[core.properties | ||
.description]: `Default ontology for the ${name} drive`, | ||
[core.properties.classes]: [], | ||
[core.properties.properties]: [], | ||
[core.properties.instances]: [], | ||
}, | ||
}); | ||
|
||
await ontology.save(); | ||
|
||
await resource.set(server.properties.defaultOntology, ontology.subject); | ||
await resource.set(dataBrowser.properties.subResources, [ | ||
ontology.subject, | ||
]); | ||
await resource.save(); | ||
}, | ||
); | ||
|
||
// Change current drive to new drive | ||
setDrive(newDrive.subject); | ||
|
||
onClose(); | ||
}, [name, createAndNavigate, onClose, parent, setDrive, store]); | ||
|
||
const [dialogProps, show, hide] = useDialog({ onSuccess, onCancel: onClose }); | ||
|
||
useEffect(() => { | ||
show(); | ||
}, []); | ||
|
||
return ( | ||
<Dialog {...dialogProps}> | ||
<H1>New Drive</H1> | ||
<DialogContent> | ||
<form | ||
onSubmit={(e: FormEvent) => { | ||
e.preventDefault(); | ||
hide(true); | ||
}} | ||
> | ||
<Field required label='Name'> | ||
<InputWrapper> | ||
<InputStyled | ||
placeholder='My Drive' | ||
value={name} | ||
autoFocus={true} | ||
onChange={e => setName(e.target.value)} | ||
/> | ||
</InputWrapper> | ||
</Field> | ||
</form> | ||
</DialogContent> | ||
<DialogActions> | ||
<Button onClick={() => hide(false)} subtle> | ||
Cancel | ||
</Button> | ||
<Button onClick={() => hide(true)} disabled={!name.trim()}> | ||
Create | ||
</Button> | ||
</DialogActions> | ||
</Dialog> | ||
); | ||
}; | ||
|
||
const H1 = styled.h1` | ||
margin: 0; | ||
`; |
Oops, something went wrong.