-
Notifications
You must be signed in to change notification settings - Fork 716
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
feat: add resource control for webhook form action #4333
Open
TrySound
wants to merge
4
commits into
main
Choose a base branch
from
webhook-form.staging
base: main
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
4 commits
Select commit
Hold shift + click to select a range
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 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
265 changes: 265 additions & 0 deletions
265
apps/builder/app/builder/features/settings-panel/controls/resource-control.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,265 @@ | ||
import { nanoid } from "nanoid"; | ||
import { computed } from "nanostores"; | ||
import { | ||
forwardRef, | ||
useId, | ||
useRef, | ||
useState, | ||
type ComponentProps, | ||
} from "react"; | ||
import { | ||
EnhancedTooltip, | ||
Flex, | ||
InputField, | ||
NestedInputButton, | ||
theme, | ||
} from "@webstudio-is/design-system"; | ||
import { useStore } from "@nanostores/react"; | ||
import { encodeDataSourceVariable, Prop, Resource } from "@webstudio-is/sdk"; | ||
import { GearIcon } from "@webstudio-is/icons"; | ||
import { $resources, $selectedPage } from "~/shared/nano-states"; | ||
import { updateWebstudioData } from "~/shared/instance-utils"; | ||
import { FloatingPanel } from "~/builder/shared/floating-panel"; | ||
import { | ||
BindingPopoverProvider, | ||
evaluateExpressionWithinScope, | ||
} from "~/builder/shared/binding-popover"; | ||
import { | ||
humanizeAttribute, | ||
Label, | ||
ResponsiveLayout, | ||
setPropMutable, | ||
type ControlProps, | ||
} from "../shared"; | ||
import { | ||
$selectedInstanceResourceScope, | ||
Headers, | ||
MethodField, | ||
parseResource, | ||
UrlField, | ||
} from "../resource-panel"; | ||
|
||
const ResourceButton = forwardRef< | ||
HTMLButtonElement, | ||
ComponentProps<typeof NestedInputButton> | ||
>((props, ref) => { | ||
return ( | ||
<EnhancedTooltip content="Edit Resource"> | ||
<NestedInputButton {...props} ref={ref} aria-label="Edit Resource"> | ||
<GearIcon /> | ||
</NestedInputButton> | ||
</EnhancedTooltip> | ||
); | ||
}); | ||
ResourceButton.displayName = "ResourceButton"; | ||
|
||
// resource scope has access to system parameter | ||
// which cannot be used in action resource | ||
const $scope = computed( | ||
[$selectedInstanceResourceScope, $selectedPage], | ||
({ scope, aliases }, page) => { | ||
if (page === undefined) { | ||
return { scope, aliases }; | ||
} | ||
const newScope: Record<string, unknown> = { ...scope }; | ||
const newAliases = new Map(aliases); | ||
const systemIdentifier = encodeDataSourceVariable(page.systemDataSourceId); | ||
delete newScope[systemIdentifier]; | ||
newAliases.delete(systemIdentifier); | ||
return { scope: newScope, aliases: newAliases }; | ||
} | ||
); | ||
|
||
const ResourceForm = ({ resource }: { resource: undefined | Resource }) => { | ||
const bindingPopoverContainerRef = useRef<HTMLDivElement>(null); | ||
// @todo exclude collection item and system | ||
// basically all parameter variables | ||
const { scope, aliases } = useStore($scope); | ||
const [url, setUrl] = useState(resource?.url ?? `""`); | ||
const [method, setMethod] = useState<Resource["method"]>( | ||
resource?.method ?? "post" | ||
); | ||
const [headers, setHeaders] = useState<Resource["headers"]>( | ||
resource?.headers ?? [] | ||
); | ||
return ( | ||
<Flex | ||
ref={bindingPopoverContainerRef} | ||
direction="column" | ||
css={{ | ||
width: theme.spacing[30], | ||
overflow: "hidden", | ||
gap: theme.spacing[9], | ||
p: theme.spacing[9], | ||
}} | ||
> | ||
<BindingPopoverProvider | ||
value={{ containerRef: bindingPopoverContainerRef }} | ||
> | ||
<UrlField | ||
scope={scope} | ||
aliases={aliases} | ||
value={url} | ||
onChange={setUrl} | ||
onCurlPaste={(curl) => { | ||
// update all feilds when curl is paste into url field | ||
setUrl(JSON.stringify(curl.url)); | ||
setMethod(curl.method); | ||
setHeaders( | ||
curl.headers.map((header) => ({ | ||
name: header.name, | ||
value: JSON.stringify(header.value), | ||
})) | ||
); | ||
}} | ||
/> | ||
<MethodField value={method} onChange={setMethod} /> | ||
<Headers | ||
scope={scope} | ||
aliases={aliases} | ||
headers={headers} | ||
onChange={setHeaders} | ||
/> | ||
</BindingPopoverProvider> | ||
</Flex> | ||
); | ||
}; | ||
|
||
const setResource = ({ | ||
instanceId, | ||
propId, | ||
propName, | ||
resource, | ||
}: { | ||
instanceId: Prop["instanceId"]; | ||
propId?: Prop["id"]; | ||
propName: Prop["name"]; | ||
resource: Resource; | ||
}) => { | ||
updateWebstudioData((data) => { | ||
setPropMutable({ | ||
data, | ||
update: { | ||
id: propId ?? nanoid(), | ||
instanceId, | ||
name: propName, | ||
type: "resource", | ||
value: resource.id, | ||
}, | ||
}); | ||
data.resources.set(resource.id, resource); | ||
}); | ||
}; | ||
|
||
const areAllFormErrorsVisible = (form: null | HTMLFormElement) => { | ||
if (form === null) { | ||
return true; | ||
} | ||
// check all errors in form fields are visible | ||
for (const element of form.elements) { | ||
if ( | ||
element instanceof HTMLInputElement || | ||
element instanceof HTMLTextAreaElement | ||
) { | ||
// field is invalid and the error is not visible | ||
if ( | ||
element.validity.valid === false && | ||
// rely on data-color=error convention in webstudio design system | ||
element.getAttribute("data-color") !== "error" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. looks like dirty hack There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is though it works and I didn't figure out another way. |
||
) { | ||
return false; | ||
} | ||
} | ||
} | ||
return true; | ||
}; | ||
|
||
export const ResourceControl = ({ | ||
meta, | ||
prop, | ||
instanceId, | ||
propName, | ||
deletable, | ||
onDelete, | ||
}: ControlProps<"resource">) => { | ||
const resources = useStore($resources); | ||
const { scope } = useStore($scope); | ||
const resourceId = prop?.type === "resource" ? prop.value : undefined; | ||
const resource = resources.get(resourceId ?? ""); | ||
const urlExpression = resource?.url ?? `""`; | ||
const url = String(evaluateExpressionWithinScope(urlExpression, scope)); | ||
const id = useId(); | ||
const label = humanizeAttribute(meta.label ?? propName); | ||
const [isResourceOpen, setIsResourceOpen] = useState(false); | ||
const form = useRef<HTMLFormElement>(null); | ||
|
||
return ( | ||
<ResponsiveLayout | ||
label={ | ||
<Label htmlFor={id} description={meta.description}> | ||
{label} | ||
</Label> | ||
} | ||
deletable={deletable} | ||
onDelete={onDelete} | ||
> | ||
<InputField | ||
id={id} | ||
disabled={true} | ||
suffix={ | ||
<FloatingPanel | ||
title="Edit Resource" | ||
isOpen={isResourceOpen} | ||
onIsOpenChange={(isOpen) => { | ||
if (isOpen) { | ||
setIsResourceOpen(true); | ||
return; | ||
} | ||
// attempt to save form on close | ||
if (areAllFormErrorsVisible(form.current)) { | ||
form.current?.requestSubmit(); | ||
setIsResourceOpen(false); | ||
} else { | ||
form.current?.checkValidity(); | ||
// prevent closing when not all errors are shown to user | ||
} | ||
}} | ||
content={ | ||
<form | ||
ref={form} | ||
// ref={formRef} | ||
noValidate={true} | ||
// exclude from the flow | ||
style={{ display: "contents" }} | ||
onSubmit={(event) => { | ||
event.preventDefault(); | ||
if (event.currentTarget.checkValidity()) { | ||
const formData = new FormData(event.currentTarget); | ||
const resource = parseResource({ | ||
id: resourceId ?? nanoid(), | ||
name: propName, | ||
formData, | ||
}); | ||
setResource({ | ||
instanceId, | ||
propId: prop?.id, | ||
propName: propName, | ||
resource, | ||
}); | ||
} | ||
}} | ||
> | ||
{/* submit is not triggered when press enter on input without submit button */} | ||
<button hidden></button> | ||
<ResourceForm resource={resource} /> | ||
</form> | ||
} | ||
> | ||
<ResourceButton /> | ||
</FloatingPanel> | ||
} | ||
value={url} | ||
/> | ||
</ResponsiveLayout> | ||
); | ||
}; |
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like huge error if form is not available at the moment