Merge pull request #182 from lowdefy/requests-graphql

Frontend fixes
This commit is contained in:
Gervwyk 2020-11-11 19:38:37 +02:00 committed by GitHub
commit 882e7e5d8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 73 additions and 64 deletions

View File

@ -32,7 +32,14 @@ const IconBlock = ({ actions, blockId, methods, properties, ...props }) => {
className: methods.makeCssClass(properties.style),
rotate: propertiesCopy.rotate,
spin: propertiesCopy.spin,
...omit(props, ['registerAction', 'registerMethod', 'loading', 'schemaErrors']),
...omit(props, [
'homePageId',
'loading',
'pageId',
'registerAction',
'registerMethod',
'schemaErrors',
]),
};
const IconComp = memo(lazy(() => import(`./icons/${propertiesCopy.name}`)));
return (

View File

@ -79,6 +79,7 @@ class Actions {
serializer.deserializeFromString(this.context.State.frozenState)
);
this.context.update();
// Consider firing onReset and onResetAsync actions
} catch (error) {
// log e
return Promise.reject({ errorMessage: errorMessage || 'Failed to reset page.', error });

View File

@ -20,7 +20,6 @@ import { ErrorBoundary, Loading } from '@lowdefy/block-tools';
import { get } from '@lowdefy/helpers';
import LoadBlock from './LoadBlock';
import Defaults from './Defaults';
import CategorySwitch from './CategorySwitch';
import WatchCache from './WatchCache';
@ -38,22 +37,17 @@ const Block = ({ block, Blocks, context, pageId, rootContext }) => {
<LoadBlock
meta={block.meta}
render={(Comp) => (
<Defaults
Component={Comp}
render={(CompWithDefaults) => (
<WatchCache
<WatchCache
block={block}
rootContext={rootContext}
render={() => (
<CategorySwitch
Component={Comp}
block={block}
Blocks={Blocks}
context={context}
pageId={pageId}
rootContext={rootContext}
render={() => (
<CategorySwitch
Component={CompWithDefaults}
block={block}
Blocks={Blocks}
context={context}
pageId={pageId}
rootContext={rootContext}
/>
)}
/>
)}
/>

View File

@ -95,7 +95,6 @@ const CategorySwitch = ({ block, Blocks, Component, context, pageId, rootContext
}}
actions={block.eval.actions}
blockId={block.blockId}
Components={rootContext.Components}
homePageId={rootContext.homePageId}
key={block.blockId}
loading={block.loading}
@ -127,7 +126,6 @@ const CategorySwitch = ({ block, Blocks, Component, context, pageId, rootContext
}}
actions={block.eval.actions}
blockId={block.blockId}
Components={rootContext.Components}
homePageId={rootContext.homePageId}
key={block.blockId}
loading={block.loading}

View File

@ -68,7 +68,6 @@ const Container = ({ block, Blocks, Component, context, pageId, rootContext }) =
}}
actions={block.eval.actions}
blockId={block.blockId}
Components={rootContext.Components}
content={content}
homePageId={rootContext.homePageId}
key={block.blockId}

View File

@ -14,14 +14,43 @@
limitations under the License.
*/
import React from 'react';
import React, { useEffect, useState } from 'react';
import { Loading } from '@lowdefy/block-tools';
import { get } from '@lowdefy/helpers';
import getContext from '@lowdefy/engine';
import useContext from './useContext';
import OnEnter from './OnEnter';
const contexts = {};
const Context = ({ block, contextId, pageId, render, rootContext }) => {
const { context, loading, error } = useContext({ block, pageId, rootContext, contextId });
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
useEffect(() => {
let mounted = true;
const mount = async () => {
try {
const ctx = await getContext({
block,
contextId,
pageId,
rootContext,
});
if (mounted) {
contexts[contextId] = ctx;
setLoading(false);
}
} catch (err) {
setError(err);
}
};
mount();
return () => {
mounted = false;
};
}, [block, pageId, rootContext, contextId]);
if (loading)
return (
<Loading
@ -31,7 +60,8 @@ const Context = ({ block, contextId, pageId, render, rootContext }) => {
);
if (error) throw error;
return render(context);
return <OnEnter block={block} context={contexts[contextId]} render={render} />;
};
export default Context;

View File

@ -1,24 +0,0 @@
/*
Copyright 2020 Lowdefy, Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { blockDefaultProps } from '@lowdefy/block-tools';
const Defaults = ({ Component, render }) => {
Component.defaultProps = blockDefaultProps;
return render(Component);
};
export default Defaults;

View File

@ -75,7 +75,6 @@ const List = ({ block, Blocks, Component, context, pageId, rootContext }) => {
}}
actions={block.eval.actions}
blockId={block.blockId}
Components={rootContext.Components}
list={contentList}
homePageId={rootContext.homePageId}
key={block.blockId}

View File

@ -14,8 +14,9 @@
limitations under the License.
*/
import { useEffect, useState } from 'react';
import getContext from '@lowdefy/engine';
import React, { useEffect, useState } from 'react';
import { Loading } from '@lowdefy/block-tools';
import { get } from '@lowdefy/helpers';
const callAction = ({ action, context }) => {
return context.RootBlocks.areas.root.blocks[0].callAction({
@ -23,23 +24,17 @@ const callAction = ({ action, context }) => {
});
};
const useContext = ({ block, pageId, rootContext, contextId }) => {
const OnEnter = ({ block, context, render }) => {
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
const [context, setContext] = useState(null);
useEffect(() => {
let mounted = true;
const mount = async () => {
try {
const ctx = await getContext({
block,
contextId,
pageId,
rootContext,
});
if (mounted) await callAction({ action: 'onEnter', context: ctx });
await callAction({ action: 'onEnter', context });
if (mounted) {
callAction({ action: 'onEnterAsync', context: ctx });
setContext(ctx);
callAction({ action: 'onEnterAsync', context });
setLoading(false);
}
} catch (err) {
setError(err);
@ -49,9 +44,19 @@ const useContext = ({ block, pageId, rootContext, contextId }) => {
return () => {
mounted = false;
};
}, [block, pageId, rootContext, contextId]);
}, [context]);
return { error, context, loading: !context && !error };
if (loading)
return (
<Loading
properties={get(block, 'meta.loading.properties')}
type={get(block, 'meta.loading.type')}
/>
);
if (error) throw error;
return render(context);
};
export default useContext;
export default OnEnter;