-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Fix dynamic props in the Connect demo app #14887
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
Changes from all commits
ec9dfa1
48c0f5b
ef1cd33
df45457
54eef81
e8f9720
7949c2f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,11 +9,11 @@ import type { | |
| import { useFrontendClient } from "./frontend-client-context"; | ||
| import type { ComponentFormProps } from "../components/ComponentForm"; | ||
|
|
||
| export type DynamicProps<T extends ConfigurableProps> = { id: string; configurable_props: T; }; // TODO | ||
| export type DynamicProps<T extends ConfigurableProps> = { id: string; configurableProps: T; }; // TODO | ||
|
|
||
| export type FormContext<T extends ConfigurableProps> = { | ||
| component: V1Component<T>; | ||
| configurableProps: T; // dynamicProps.configurable_props || props.component.configurable_props | ||
| configurableProps: T; // dynamicProps.configurableProps || props.component.configurable_props | ||
| configuredProps: ConfiguredProps<T>; | ||
| dynamicProps?: DynamicProps<T>; // lots of calls require dynamicProps?.id, so need to expose | ||
| dynamicPropsQueryIsFetching?: boolean; | ||
|
|
@@ -117,11 +117,11 @@ export const FormContextProvider = <T extends ConfigurableProps>({ | |
| "dynamicProps", | ||
| ], | ||
| queryFn: async () => { | ||
| const { dynamic_props } = await client.componentReloadProps(componentReloadPropsInput); | ||
| const { dynamicProps } = await client.componentReloadProps(componentReloadPropsInput); | ||
| // XXX what about if null? | ||
| // TODO observation errors, etc. | ||
| if (dynamic_props) { | ||
| setDynamicProps(dynamic_props); | ||
| if (dynamicProps) { | ||
| setDynamicProps(dynamicProps); | ||
| } | ||
| setReloadPropIdx(undefined); | ||
| return []; // XXX ok to mutate above and not look at data? | ||
|
|
@@ -130,7 +130,7 @@ export const FormContextProvider = <T extends ConfigurableProps>({ | |
| }); | ||
|
|
||
| // XXX fix types of dynamicProps, props.component so this type decl not needed | ||
| let configurableProps: T = dynamicProps?.configurable_props || formProps.component.configurable_props || []; | ||
| let configurableProps: T = dynamicProps?.configurableProps || formProps.component.configurable_props || []; | ||
|
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. Fix inconsistent property naming and improve type safety. The line still uses snake_case - let configurableProps: T = dynamicProps?.configurableProps || formProps.component.configurable_props || [];
+ let configurableProps: T = dynamicProps?.configurableProps || formProps.component.configurableProps || [];Also, consider adding runtime type checking for the assigned value since the type assertion to
|
||
| if (propNames?.length) { | ||
| const _configurableProps = []; | ||
| for (const prop of configurableProps) { | ||
|
|
@@ -169,7 +169,7 @@ export const FormContextProvider = <T extends ConfigurableProps>({ | |
| errs.push("not a boolean"); | ||
| } | ||
| } else if (prop.type === "string") { | ||
| if (typeof value !== "string" ) { | ||
| if (typeof value !== "string") { | ||
| errs.push("not a string"); | ||
| } | ||
| } else if (prop.type === "app") { | ||
|
|
@@ -179,7 +179,7 @@ export const FormContextProvider = <T extends ConfigurableProps>({ | |
| }; | ||
|
|
||
| const updateConfiguredPropsQueryDisabledIdx = (configuredProps: ConfiguredProps<T>) => { | ||
| let _queryDisabledIdx = undefined; | ||
| let _queryDisabledIdx = undefined; | ||
| for (let idx = 0; idx < configurableProps.length; idx++) { | ||
| const prop = configurableProps[idx]; | ||
| if (prop.hidden || (prop.optional && !optionalPropIsEnabled(prop))) { | ||
|
|
@@ -241,7 +241,10 @@ export const FormContextProvider = <T extends ConfigurableProps>({ | |
| ]); | ||
|
|
||
| // clear all props on user change | ||
| const [prevUserId, setPrevUserId] = useState(userId) | ||
| const [ | ||
| prevUserId, | ||
| setPrevUserId, | ||
| ] = useState(userId) | ||
| useEffect(() => { | ||
| if (prevUserId !== userId) { | ||
| updateConfiguredProps({}); | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
🛠️ Refactor suggestion
Consider improving the query function implementation.
The current implementation has a few concerns:
dynamicPropsis null.Consider refactoring to:
Then handle the state updates in
onSuccesscallback: