Form for instantiating a new Resource from some Class
({ classSubject }: NewFormProps)
| 76 | |
| 77 | /** Form for instantiating a new Resource from some Class */ |
| 78 | function NewForm({ classSubject }: NewFormProps): JSX.Element { |
| 79 | const klass = useResource(classSubject); |
| 80 | // TODO: Don't push to history, but replace, because currenlty back is broken |
| 81 | const [newSubject, setNewSubject] = useQueryParam('newSubject', StringParam); |
| 82 | const [parentSubject] = useQueryParam('parent', StringParam); |
| 83 | const klassTitle = useTitle(klass); |
| 84 | const [klassShortname] = useString(klass, properties.shortname); |
| 85 | const [klassDescription] = useString(klass, properties.description); |
| 86 | const [showDetails, setShowDetails] = useState(false); |
| 87 | const [subjectErr, setSubjectErr] = useState<Error>(null); |
| 88 | const store = useStore(); |
| 89 | const [newSubjectInput, setNewSubjectInput] = useState<string>(newSubject); |
| 90 | const resource = useResource(newSubject, { newResource: true }); |
| 91 | |
| 92 | useEffect(() => { |
| 93 | if (newSubject == undefined) { |
| 94 | setNewSubject(store.createSubject(klassShortname)); |
| 95 | } |
| 96 | }, [newSubject]); |
| 97 | |
| 98 | // Set the class for new resources |
| 99 | const [currentClass] = useArray(resource, properties.isA); |
| 100 | if (currentClass.length == 0) { |
| 101 | resource.set(properties.isA, [klass.getSubject()], store); |
| 102 | } |
| 103 | |
| 104 | /** Changes the URL of a subject. Updates the store */ |
| 105 | // Should be debounced as it is quite expensive, but getting that to work turned out to be really hard |
| 106 | async function handleSetSubject(newSubject: string) { |
| 107 | const oldSubject = resource.getSubject(); |
| 108 | if (oldSubject == newSubject) { |
| 109 | return; |
| 110 | } |
| 111 | setSubjectErr(null); |
| 112 | try { |
| 113 | // Expensive! |
| 114 | await store.renameSubject(oldSubject, newSubject); |
| 115 | setNewSubject(newSubject); |
| 116 | } catch (e) { |
| 117 | setSubjectErr(e); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | return ( |
| 122 | <> |
| 123 | <h2> |
| 124 | new <AtomicLink subject={classSubject}>{klassTitle}</AtomicLink>{' '} |
| 125 | <Button |
| 126 | onClick={() => setShowDetails(!showDetails)} |
| 127 | icon |
| 128 | subtle={!showDetails} |
| 129 | title='Toggle show Class details' |
| 130 | > |
| 131 | <FaInfo /> |
| 132 | </Button> |
| 133 | </h2> |
| 134 | {showDetails && klassDescription && <Markdown text={klassDescription} />} |
| 135 | <Field |
nothing calls this directly
no test coverage detected