-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make it easier to customize form definition
Adds an option to use autogenerated form definition rather than the custom one (may be useful on mobile devices and narrow screens). Also, adds a link to quickly go to the form definition. Fixes #1330
- Loading branch information
1 parent
2444ccf
commit d45cd16
Showing
8 changed files
with
125 additions
and
9 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
93 changes: 93 additions & 0 deletions
93
specifyweb/frontend/js_src/lib/components/formdefinition.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,93 @@ | ||
import React from 'react'; | ||
|
||
import { fetchCollection } from '../collection'; | ||
import { commonText } from '../localization/common'; | ||
import { formsText } from '../localization/forms'; | ||
import { schema } from '../schema'; | ||
import type { SpecifyModel } from '../specifymodel'; | ||
import { Button, Input, Label, Link } from './basic'; | ||
import { useAsyncState, useBooleanState } from './hooks'; | ||
import { Dialog } from './modaldialog'; | ||
import { useCachedState } from './statecache'; | ||
|
||
export function FormDefinition({ | ||
model, | ||
}: { | ||
readonly model: SpecifyModel; | ||
}): JSX.Element { | ||
const [isOpen, handleOpen, handleClose] = useBooleanState(); | ||
return ( | ||
<> | ||
<Button.Simple onClick={handleOpen}> | ||
{commonText('formDefinition')} | ||
</Button.Simple> | ||
{isOpen && <FormDefinitionDialog model={model} onClose={handleClose} />} | ||
</> | ||
); | ||
} | ||
|
||
function FormDefinitionDialog({ | ||
model, | ||
onClose: handleClose, | ||
}: { | ||
readonly model: SpecifyModel; | ||
readonly onClose: () => void; | ||
}): JSX.Element { | ||
const [globalConfig = {}, setGlobalConfig] = useCachedState({ | ||
category: 'forms', | ||
key: 'useCustomForm', | ||
defaultValue: {}, | ||
staleWhileRefresh: false, | ||
}); | ||
const useCustomForm = globalConfig[model.name] ?? true; | ||
const handleChange = (checked: boolean): void => | ||
setGlobalConfig({ ...globalConfig, [model.name]: !checked }); | ||
|
||
const viewDefinitionLink = useFormDefinition(); | ||
|
||
return ( | ||
<Dialog | ||
header={commonText('formDefinition')} | ||
buttons={commonText('close')} | ||
onClose={handleClose} | ||
> | ||
<Label.ForCheckbox> | ||
<Input.Checkbox checked={!useCustomForm} onValueChange={handleChange} /> | ||
{formsText('useAutoGeneratedForm')} | ||
</Label.ForCheckbox> | ||
{typeof viewDefinitionLink === 'string' && ( | ||
<Link.NewTab href={viewDefinitionLink}> | ||
{formsText('editFormDefinition')} | ||
</Link.NewTab> | ||
)} | ||
</Dialog> | ||
); | ||
} | ||
|
||
/* | ||
* BUG: this assumes that the ViewSet for current collection was the one used | ||
* to render this form. That is not always the case. | ||
*/ | ||
function useFormDefinition(): string | undefined { | ||
const [url] = useAsyncState( | ||
React.useCallback( | ||
async () => | ||
fetchCollection( | ||
'SpViewSetObj', | ||
{ | ||
limit: 1, | ||
}, | ||
{ | ||
spAppResourceDir__Collection: schema.domainLevelIds.collection, | ||
} | ||
) | ||
.then(({ records }) => records[0]?.id) | ||
.then((id) => | ||
typeof id === 'number' ? `/specify/viewsets/${id}/` : undefined | ||
), | ||
[] | ||
), | ||
false | ||
); | ||
return url; | ||
} |
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
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