-
Notifications
You must be signed in to change notification settings - Fork 0
custom variable type widget: single resource collection (1/3) #49
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
Open
interim17
wants to merge
10
commits into
run-format
Choose a base branch
from
feature/resource
base: run-format
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
37fd2cf
register custom widget for variable type resources
interim17 5e1c540
use resourceDetails in nested fields on resource markdown
interim17 9f43b2b
Update src/cms/widgets/VariableResourceWidget/copyResourceNameHandler.ts
interim17 a345633
Update static/admin/config.yml
interim17 478c946
reference resourceDetails in relational widget
interim17 e0fd5b2
Merge branch 'main' of https://github.com/AllenCell/idea-board into f…
interim17 cc336ee
remove redundant fields from config
interim17 6653ea5
fix typo
interim17 3e89540
Merge branch 'run-format' of https://github.com/AllenCell/idea-board …
interim17 211c55a
update comment
interim17 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,9 +1,25 @@ | ||
| import CMS from "decap-cms-app"; | ||
| import CMS from "decap-cms-app"; | ||
|
|
||
| import AboutPagePreview from "./preview-templates/AboutPagePreview"; | ||
| import IdeaPostPreview from "./preview-templates/IdeaPostPreview"; | ||
| import IndexPagePreview from "./preview-templates/IndexPagePreview"; | ||
| import VariableResourceUnionControl from "./widgets/VariableResourceWidget/VariableResourceUnionControl"; | ||
| import copyResourceNameHandler from "./widgets/VariableResourceWidget/copyResourceNameHandler"; | ||
|
|
||
| // Register custom widgets, with optional preview components | ||
| // and global styles. | ||
| CMS.registerWidget({ | ||
| name: "resource_union", | ||
| controlComponent: VariableResourceUnionControl, | ||
| }); | ||
|
|
||
| CMS.registerPreviewTemplate("index", IndexPagePreview); | ||
| CMS.registerPreviewTemplate("about", AboutPagePreview); | ||
| CMS.registerPreviewTemplate("idea", IdeaPostPreview); | ||
|
|
||
| // Decap exposes a number of lifecycle stages we can hook into and register. | ||
| CMS.registerEventListener({ | ||
| name: "preSave", | ||
| handler: copyResourceNameHandler, | ||
| }); |
31 changes: 31 additions & 0 deletions
31
src/cms/widgets/VariableResourceWidget/VariableResourceUnionControl.tsx
This file contains hidden or 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,31 @@ | ||
| import React from "react"; | ||
|
|
||
|
|
||
|
|
||
| import type { CmsWidgetControlProps } from "decap-cms-core"; | ||
|
|
||
|
|
||
|
|
||
| import VariableTypeWidgetControl from "../VariableTypeWidget/VariableTypeWidgetControl"; | ||
| import { VARIABLE_TYPE_RESOURCE_CONFIG } from "./constants"; | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| /** | ||
| * Implementation of VariableTypeWidgetControl for a union of different | ||
| * resource types (software tools, datasets, etc.). | ||
| * Config defined in VARIABLE_TYPE_RESOURCE_CONFIG. | ||
| */ | ||
| const ResourceUnionControl = (props: CmsWidgetControlProps) => { | ||
| return ( | ||
| <VariableTypeWidgetControl | ||
| {...props} | ||
| types={VARIABLE_TYPE_RESOURCE_CONFIG} | ||
| defaultType="softwareTool" | ||
| /> | ||
| ); | ||
| }; | ||
|
|
||
| export default ResourceUnionControl; |
This file contains hidden or 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,70 @@ | ||
| import { TypeConfig } from "../VariableTypeWidget/types"; | ||
|
|
||
| const SOFTWARE_STATUS_OPTIONS = [ | ||
| "Public", | ||
| "In development", | ||
| "Internal use only", | ||
| ]; | ||
|
|
||
| const DATASET_STATUS_OPTIONS = [ | ||
| "Public", | ||
| "Not QCed", | ||
| "Preliminary", | ||
| "Need to request data directly", | ||
| ]; | ||
|
|
||
| // Optional: override baseFields per type, | ||
| // defaults to name, description, link. | ||
| export const VARIABLE_TYPE_RESOURCE_CONFIG: TypeConfig[] = [ | ||
| { | ||
| value: "softwareTool", | ||
| label: "Software Tool", | ||
| fields: [ | ||
| { | ||
| label: "README/Quickstart Link", | ||
| name: "readmeLink", | ||
| type: "input", | ||
| }, | ||
| { | ||
| label: "Status", | ||
| name: "status", | ||
| type: "select", | ||
| options: SOFTWARE_STATUS_OPTIONS, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| value: "dataset", | ||
| label: "Dataset", | ||
| fields: [ | ||
| { | ||
| label: "Status", | ||
| name: "status", | ||
| type: "select", | ||
| options: DATASET_STATUS_OPTIONS, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| value: "protocolLink", | ||
| label: "Protocol (Link)", | ||
| fields: [], | ||
| }, | ||
| { | ||
| value: "protocolFile", | ||
| label: "Protocol (File)", | ||
| fields: [ | ||
| { | ||
| label: "File Path", | ||
| name: "file", | ||
| type: "file", | ||
| hint: "Use Media Library to upload, then paste path here", | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| value: "cellLine", | ||
| label: "Cell Line", | ||
| fields: [], | ||
| }, | ||
| ]; |
41 changes: 41 additions & 0 deletions
41
src/cms/widgets/VariableResourceWidget/copyResourceNameHandler.ts
This file contains hidden or 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,41 @@ | ||
| import CMS from "decap-cms-core"; | ||
|
|
||
| // For some reason I could not import CmsEventListener as a type | ||
| // because it was resolving to the global CMS object, so this is the workaround. | ||
| type CmsEventListenerHandler = Parameters< | ||
| typeof CMS.registerEventListener | ||
| >[0]["handler"]; | ||
| type CmsEventListenerHandlerArg = Parameters<CmsEventListenerHandler>[0]; | ||
|
|
||
| /** | ||
| Certain fields are required to be top-level by the CMS, which clutters the UI | ||
| when we have a redundant field in widget. This preSave hook copies the resource.name | ||
| field to the top-level from the nested widget. | ||
| */ | ||
| export const copyResourceNameHandler = ({ | ||
| entry, | ||
| }: CmsEventListenerHandlerArg) => { | ||
| const collection = entry.get("collection"); | ||
|
|
||
| // Only apply to resources collection | ||
| if (collection !== "resources") { | ||
| return entry.get("data"); | ||
| } | ||
|
|
||
| const data = entry.get("data"); | ||
| const resource = data.get("resourceDetails"); | ||
|
|
||
| if (resource) { | ||
| // Copy resource.name to top-level name | ||
| const resourceName = resource.get | ||
| ? resource.get("name") | ||
| : resource.name; | ||
| if (resourceName) { | ||
| return data.set("name", resourceName); | ||
| } | ||
| } | ||
|
|
||
| return data; | ||
| }; | ||
|
|
||
| export default copyResourceNameHandler; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as resolved.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.