remove hasFetched in fetch trait and add disabled property

This commit is contained in:
Bowen Tan 2022-04-13 17:37:09 +08:00
parent b36927778c
commit 13a5894be3
4 changed files with 43 additions and 15 deletions

View File

@ -6,6 +6,7 @@ import {
FormLabel,
Switch,
IconButton,
Text,
} from '@chakra-ui/react';
import { AddIcon } from '@chakra-ui/icons';
import { FormikHelpers, FormikHandlers, FormikState } from 'formik';
@ -18,6 +19,7 @@ import {
import { Static, Type } from '@sinclair/typebox';
import { EditorServices } from '../../../types';
import { ComponentSchema } from '@sunmao-ui/core';
import { SpecWidget } from '@sunmao-ui/editor-sdk';
type Values = Static<typeof FetchTraitPropertiesSpec>;
type EventHandler = Static<typeof EventCallBackHandlerSpec>;
@ -94,7 +96,7 @@ const Handler = (props: HandlerProps) => {
};
export const Basic: React.FC<Props> = props => {
const { formik } = props;
const { formik, api, services } = props;
const onAddHandler = (type: HandlerType) => {
const newHandler: EventHandler = {
@ -145,6 +147,32 @@ export const Basic: React.FC<Props> = props => {
onBlur={() => formik.handleSubmit()}
/>
</FormControl>
<FormControl display="flex" alignItems="center">
<FormLabel margin="0" marginRight="2">
Disabled
</FormLabel>
<SpecWidget
component={api}
spec={Type.Boolean({ widgetOptions: { isShowAsideExpressionButton: true } })}
value={formik.values.disabled}
path={[]}
level={1}
services={services}
onChange={val => {
formik.setFieldValue('disabled', val);
formik.handleSubmit();
}}
>
{{
title: (
<Text fontSize="lg" fontWeight="bold" display="inline">
Body
</Text>
),
}}
</SpecWidget>
</FormControl>
{generateHandlers('onComplete')}
{generateHandlers('onError')}
</VStack>

View File

@ -150,13 +150,19 @@ const _ImplWrapper = React.forwardRef<HTMLDivElement, ImplWrapperProps>((props,
return stop;
}, [c.properties, stateManager]);
useEffect(() => {
if (unmount) {
delete stateManager.store[c.id];
}
return () => {
delete stateManager.store[c.id];
};
}, [c.id, stateManager.store]);
}, [c.id, stateManager.store, unmount]);
const mergedProps = useMemo(() => {
const allProps: Record<string, any> = { ...evaledComponentProperties, ...propsFromTraits };
const allProps: Record<string, any> = {
...evaledComponentProperties,
...propsFromTraits,
};
for (const key in allProps) {
if (allProps[key] instanceof ExpressionError) {

View File

@ -9,7 +9,6 @@ import { generateCallback } from './Event';
const FetchTraitFactory: TraitImplFactory<Static<typeof FetchTraitPropertiesSpec>> =
() => {
const hasFetchedMap = new Map<string, boolean>();
return ({
trait,
url,
@ -24,14 +23,14 @@ const FetchTraitFactory: TraitImplFactory<Static<typeof FetchTraitPropertiesSpec
services,
subscribeMethods,
componentId,
disabled,
}) => {
const hashId = `#${componentId}@${'fetch'}`;
const hasFetched = hasFetchedMap.get(hashId);
const lazy = _lazy === undefined ? true : _lazy;
const fetchData = () => {
console.log('disabled', disabled);
if (disabled) return;
// TODO: clear when component destroy
hasFetchedMap.set(hashId, true);
// FIXME: listen to the header change
const headers = new Headers();
if (_headers) {
@ -155,7 +154,7 @@ const FetchTraitFactory: TraitImplFactory<Static<typeof FetchTraitPropertiesSpec
};
// non lazy query, listen to the change and query;
if (!lazy && url && !hasFetched) {
if (!lazy && url) {
fetchData();
}
@ -166,13 +165,7 @@ const FetchTraitFactory: TraitImplFactory<Static<typeof FetchTraitPropertiesSpec
});
return {
props: {
effects: [
() => {
hasFetchedMap.set(hashId, false);
},
],
},
props: null,
};
};
};

View File

@ -50,6 +50,7 @@ export const FetchTraitPropertiesSpec = Type.Object({
{ title: 'Method' },
), // {pattern: /^(get|post|put|delete)$/i}
lazy: Type.Boolean({ title: 'Lazy' }),
disabled: Type.Boolean({ title: 'Disabled' }),
headers: Type.Record(Type.String(), Type.String(), {
title: 'Headers',
}),