-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from developmentseed/state-refactor
Refactor state management
- Loading branch information
Showing
14 changed files
with
314 additions
and
224 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { useContext } from "react"; | ||
import { SelectField, TextField } from "../components/form/fields"; | ||
import { ImageBuilder } from "./ImageBuilder"; | ||
import useSelectOptions from "../hooks/useSelectOptions"; | ||
import { SpawnerFormContext } from "../state"; | ||
|
||
const extraChoices = [ | ||
{ | ||
value: "dockerImage", | ||
label: "Specify an existing docker image", | ||
description: | ||
"Use a pre-existing docker image from a public docker registry", | ||
}, | ||
{ | ||
value: "buildImage", | ||
label: "Build your own image", | ||
description: | ||
"Use a mybinder.org compatible GitHub repo to build your own image", | ||
}, | ||
]; | ||
|
||
function ImageSelect({ config }) { | ||
const { profile } = useContext(SpawnerFormContext); | ||
const FIELD_ID = `profile-option-${profile.slug}--image`; | ||
const FIELD_ID_UNLISTED = `${FIELD_ID}--unlisted-choice`; | ||
const { display_name, choices } = config; | ||
|
||
const { options, defaultOption } = useSelectOptions(choices, extraChoices); | ||
|
||
const { | ||
image, | ||
setImage, | ||
customImage, | ||
setCustomImage, | ||
errors, | ||
touched, | ||
setTouched, | ||
} = useContext(SpawnerFormContext); | ||
|
||
return ( | ||
<> | ||
<SelectField | ||
id={FIELD_ID} | ||
label={display_name} | ||
options={options} | ||
defaultOption={defaultOption} | ||
error={touched[FIELD_ID] && errors[FIELD_ID]} | ||
onChange={(e) => setImage(e.value)} | ||
onBlur={() => setTouched(FIELD_ID, true)} | ||
/> | ||
{image === "dockerImage" && ( | ||
<TextField | ||
id={FIELD_ID_UNLISTED} | ||
label="Custom image" | ||
value={customImage} | ||
required | ||
pattern="^.+:.+$" | ||
error={touched[FIELD_ID_UNLISTED] && errors[FIELD_ID_UNLISTED]} | ||
onChange={(e) => setCustomImage(e.target.value)} | ||
onBlur={() => setTouched(FIELD_ID_UNLISTED, true)} | ||
/> | ||
)} | ||
{image === "buildImage" && ( | ||
<ImageBuilder name={FIELD_ID_UNLISTED} visible="true" /> | ||
)} | ||
</> | ||
); | ||
} | ||
|
||
export default ImageSelect; |
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 was deleted.
Oops, something went wrong.
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,27 @@ | ||
import { useContext } from "react"; | ||
import useSelectOptions from "./hooks/useSelectOptions"; | ||
import { SpawnerFormContext } from "./state"; | ||
import { SelectField } from "./components/form/fields"; | ||
|
||
function ResourceSelect({ config }) { | ||
const { display_name, choices } = config; | ||
|
||
const { options, defaultOption } = useSelectOptions(choices); | ||
const { setResource, profile, touched, setTouched, errors } = | ||
useContext(SpawnerFormContext); | ||
const FIELD_ID = `profile-option-${profile.slug}--resource`; | ||
|
||
return ( | ||
<SelectField | ||
id={FIELD_ID} | ||
label={display_name} | ||
options={options} | ||
defaultOption={defaultOption} | ||
error={touched[FIELD_ID] && errors[FIELD_ID]} | ||
onChange={(e) => setResource(e.value)} | ||
onBlur={() => setTouched(FIELD_ID, true)} | ||
/> | ||
); | ||
} | ||
|
||
export default ResourceSelect; |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.