Components/Calculation: add build-action

This commit is contained in:
Chika 2022-05-17 16:10:06 +03:00
parent 5d3d9808ac
commit 4068781ed5
5 changed files with 61 additions and 0 deletions

View File

@ -0,0 +1,20 @@
import { observer } from 'mobx-react-lite';
import { useStatus } from 'stores/calculation/statuses/hooks';
import { getAction } from '../config/map-actions';
import type { BuilderProps } from './types';
export default function buildAction({ elementName, Component, ...props }: BuilderProps) {
const actionName = getAction(elementName);
return observer(() => {
const { status } = useStatus(elementName);
return (
<Component
status={status}
{...props}
action={() => import(`process/${actionName}`).then((m) => m.default())}
/>
);
});
}

View File

@ -0,0 +1,11 @@
export const elementsToActions: Record<string, string> = {
btnCalculate: 'calculate',
btnCreateKP: 'create-kp',
};
export type Elements = keyof typeof elementsToActions;
export function getAction(elementName: Elements) {
const actionName = elementsToActions[elementName];
return actionName;
}

28
Elements/Button.tsx Normal file
View File

@ -0,0 +1,28 @@
import type { ButtonProps } from 'antd';
import { Button } from 'antd';
import { throttle } from 'lodash';
import type { Status } from './types';
type ElementProps = {
status: Status;
action: () => void;
text: string;
};
export default function Element({ status, action, text }: ElementProps) {
const throttledAction = throttle(action, 1200, {
trailing: false,
});
return (
<Button
disabled={status === 'Disabled'}
loading={status === 'Loading'}
onClick={throttledAction}
>
{text}
</Button>
);
}
export type { ButtonProps };

View File

@ -0,0 +1 @@
export default () => {};

View File

@ -0,0 +1 @@
export default () => {};