store: add useErrors hook
Calculation/render: override btnCalculate, btnCreateKP, btnCreateKPMini
This commit is contained in:
parent
2d0117bc4c
commit
5c8560e266
@ -32,9 +32,9 @@ export function buildAction<T>(
|
||||
<Component
|
||||
onClick={throttledAction}
|
||||
status={status}
|
||||
{...props}
|
||||
disabled={status === 'Disabled'}
|
||||
loading={status === 'Loading'}
|
||||
{...props}
|
||||
/>
|
||||
);
|
||||
});
|
||||
|
||||
@ -6,7 +6,7 @@ import types from '../elements-types';
|
||||
import map from '../map';
|
||||
import type { RenderProps } from './types';
|
||||
import { Container, Head } from '@/Components/Layout/Element';
|
||||
import { useStore } from '@/stores/hooks';
|
||||
import { useErrors, useStore } from '@/stores/hooks';
|
||||
import { useIsFetching } from '@tanstack/react-query';
|
||||
import { observer } from 'mobx-react-lite';
|
||||
import type { ComponentProps } from 'react';
|
||||
@ -25,6 +25,108 @@ const formatter = Intl.NumberFormat('ru', {
|
||||
}).format;
|
||||
|
||||
const overrideRender: Partial<Record<keyof typeof map, RenderProps>> = {
|
||||
btnCalculate: {
|
||||
render: () => {
|
||||
const elementName = 'btnCalculate';
|
||||
const title = titles.btnCalculate;
|
||||
const valueName = map.btnCalculate;
|
||||
const Component = components.btnCalculate;
|
||||
const props = elementsProps.btnCalculate;
|
||||
const { builder } = types.btnCalculate();
|
||||
|
||||
const Element = builder(Component, {
|
||||
elementName,
|
||||
valueName,
|
||||
});
|
||||
|
||||
const RenderedComponent = observer(() => {
|
||||
const { $process } = useStore();
|
||||
const { hasErrors } = useErrors();
|
||||
|
||||
return (
|
||||
<Container>
|
||||
<Head htmlFor={elementName} title={title} />
|
||||
<Element
|
||||
{...props}
|
||||
loading={$process.has('Calculate') || $process.has('CreateKP')}
|
||||
disabled={$process.has('LoadKP') || (!$process.has('Unlimited') && hasErrors)}
|
||||
/>
|
||||
</Container>
|
||||
);
|
||||
});
|
||||
|
||||
return <RenderedComponent />;
|
||||
},
|
||||
},
|
||||
|
||||
btnCreateKP: {
|
||||
render: () => {
|
||||
const elementName = 'btnCreateKP';
|
||||
const title = titles.btnCreateKP;
|
||||
const valueName = map.btnCreateKP;
|
||||
const Component = components.btnCreateKP;
|
||||
const props = elementsProps.btnCreateKP;
|
||||
const { builder } = types.btnCreateKP();
|
||||
|
||||
const Element = builder(Component, {
|
||||
elementName,
|
||||
valueName,
|
||||
});
|
||||
|
||||
const RenderedComponent = observer(() => {
|
||||
const { $process } = useStore();
|
||||
const { hasErrors } = useErrors();
|
||||
|
||||
return (
|
||||
<Container>
|
||||
<Head htmlFor={elementName} title={title} />
|
||||
<Element
|
||||
{...props}
|
||||
loading={$process.has('Calculate') || $process.has('CreateKP')}
|
||||
disabled={$process.has('LoadKP') || (!$process.has('Unlimited') && hasErrors)}
|
||||
/>
|
||||
</Container>
|
||||
);
|
||||
});
|
||||
|
||||
return <RenderedComponent />;
|
||||
},
|
||||
},
|
||||
|
||||
btnCreateKPMini: {
|
||||
render: () => {
|
||||
const elementName = 'btnCreateKPMini';
|
||||
const title = titles.btnCreateKPMini;
|
||||
const valueName = map.btnCreateKPMini;
|
||||
const Component = components.btnCreateKPMini;
|
||||
const props = elementsProps.btnCreateKPMini;
|
||||
const { builder } = types.btnCreateKPMini();
|
||||
|
||||
const Element = builder(Component, {
|
||||
elementName,
|
||||
valueName,
|
||||
});
|
||||
|
||||
const RenderedComponent = observer(() => {
|
||||
const { $process } = useStore();
|
||||
const { hasErrors } = useErrors();
|
||||
|
||||
return (
|
||||
<Container>
|
||||
<Head htmlFor={elementName} title={title} />
|
||||
<Element
|
||||
{...props}
|
||||
loading={$process.has('Calculate') || $process.has('CreateKP')}
|
||||
disabled={$process.has('LoadKP') || (!$process.has('Unlimited') && hasErrors)}
|
||||
/>
|
||||
</Container>
|
||||
);
|
||||
});
|
||||
|
||||
return <RenderedComponent />;
|
||||
},
|
||||
},
|
||||
|
||||
selectHighSeasonStart: {
|
||||
render: () => {
|
||||
const elementName = 'selectHighSeasonStart';
|
||||
|
||||
@ -2,7 +2,7 @@ import PaymentsTable from './PaymentsTable';
|
||||
import Results from './Results';
|
||||
import Validation from './Validation';
|
||||
import Background from '@/Components/Layout/Background';
|
||||
import { useStore } from '@/stores/hooks';
|
||||
import { useErrors, useStore } from '@/stores/hooks';
|
||||
import { min } from '@/styles/mq';
|
||||
import { observer } from 'mobx-react-lite';
|
||||
import { useEffect, useState } from 'react';
|
||||
@ -15,12 +15,7 @@ const items = outputTabs.map(({ Component, id, title }) => {
|
||||
|
||||
if (id === 'validation') {
|
||||
Label = observer(() => {
|
||||
const { $calculation, $tables } = useStore();
|
||||
const hasErrors =
|
||||
$calculation.hasErrors ||
|
||||
$tables.fingap.hasErrors ||
|
||||
$tables.insurance.hasErrors ||
|
||||
$tables.payments.hasErrors;
|
||||
const { hasErrors } = useErrors();
|
||||
|
||||
return (
|
||||
<Badge offset={[5, 0]} dot={hasErrors}>
|
||||
@ -48,16 +43,11 @@ const Wrapper = styled(Background)`
|
||||
`;
|
||||
|
||||
const Output = observer(() => {
|
||||
const { $calculation, $tables, $results } = useStore();
|
||||
const { $results } = useStore();
|
||||
const [activeKey, setActiveKey] = useState(undefined);
|
||||
const { hasErrors } = useErrors();
|
||||
|
||||
useEffect(() => {
|
||||
const hasErrors =
|
||||
$calculation.hasErrors ||
|
||||
$tables.fingap.hasErrors ||
|
||||
$tables.insurance.hasErrors ||
|
||||
$tables.payments.hasErrors;
|
||||
|
||||
if ($results.payments.length > 0) {
|
||||
setActiveKey('payments-table');
|
||||
}
|
||||
@ -65,13 +55,7 @@ const Output = observer(() => {
|
||||
if (hasErrors) {
|
||||
setActiveKey('validation');
|
||||
}
|
||||
}, [
|
||||
$calculation.hasErrors,
|
||||
$results.payments.length,
|
||||
$tables.fingap.hasErrors,
|
||||
$tables.insurance.hasErrors,
|
||||
$tables.payments.hasErrors,
|
||||
]);
|
||||
}, [$results.payments.length, hasErrors]);
|
||||
|
||||
return (
|
||||
<Wrapper>
|
||||
|
||||
@ -11,9 +11,6 @@ export async function action({ store, trpcClient }: ProcessContext) {
|
||||
|
||||
$process.add('Calculate');
|
||||
|
||||
$calculation.$status.setStatus('btnCalculate', 'Loading');
|
||||
$calculation.$status.setStatus('btnCreateKP', 'Loading');
|
||||
$calculation.$status.setStatus('btnCreateKPMini', 'Loading');
|
||||
$results.clear();
|
||||
|
||||
const values = $calculation.$values.getValues();
|
||||
@ -59,10 +56,6 @@ export async function action({ store, trpcClient }: ProcessContext) {
|
||||
});
|
||||
})
|
||||
.finally(() => {
|
||||
$calculation.$status.setStatus('btnCalculate', 'Default');
|
||||
$calculation.$status.setStatus('btnCreateKP', 'Default');
|
||||
$calculation.$status.setStatus('btnCreateKPMini', 'Default');
|
||||
|
||||
$process.delete('Calculate');
|
||||
});
|
||||
}
|
||||
|
||||
@ -19,35 +19,6 @@ function hasInvalidValueOrOptions(value: unknown, options: Array<BaseOption<unkn
|
||||
export default function reactions({ store }: ProcessContext) {
|
||||
const { $calculation, $tables, $process } = store;
|
||||
|
||||
disposableReaction(
|
||||
() => $process.has('Unlimited'),
|
||||
() => {
|
||||
const hasElementsErrors = Object.values($calculation.$validation).some(
|
||||
(validation) => validation.hasErrors
|
||||
);
|
||||
|
||||
const hasPaymentsErrors = $tables.payments.validation.hasErrors;
|
||||
const hasInsuranceErrors = $tables.insurance.validation.hasErrors;
|
||||
const hasFingapErrors = $tables.fingap.validation.hasErrors;
|
||||
|
||||
return hasElementsErrors || hasPaymentsErrors || hasInsuranceErrors || hasFingapErrors;
|
||||
},
|
||||
(hasErrors) => {
|
||||
if (hasErrors) {
|
||||
$calculation.$status.setStatus('btnCalculate', 'Disabled');
|
||||
$calculation.$status.setStatus('btnCreateKP', 'Disabled');
|
||||
$calculation.$status.setStatus('btnCreateKPMini', 'Disabled');
|
||||
} else {
|
||||
$calculation.$status.setStatus('btnCalculate', 'Default');
|
||||
$calculation.$status.setStatus('btnCreateKP', 'Default');
|
||||
$calculation.$status.setStatus('btnCreateKPMini', 'Default');
|
||||
}
|
||||
},
|
||||
{
|
||||
fireImmediately: true,
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* Проверяем, что выбранное значение элемента есть в списке
|
||||
*/
|
||||
|
||||
@ -12,9 +12,6 @@ export function action({ store, trpcClient, apolloClient }: ProcessContext) {
|
||||
|
||||
$process.add('CreateKP');
|
||||
|
||||
$calculation.$status.setStatus('btnCalculate', 'Loading');
|
||||
$calculation.$status.setStatus('btnCreateKP', 'Loading');
|
||||
$calculation.$status.setStatus('btnCreateKPMini', 'Loading');
|
||||
$results.clear();
|
||||
|
||||
const values = $calculation.$values.getValues();
|
||||
@ -71,10 +68,6 @@ export function action({ store, trpcClient, apolloClient }: ProcessContext) {
|
||||
});
|
||||
})
|
||||
.finally(() => {
|
||||
$calculation.$status.setStatus('btnCalculate', 'Default');
|
||||
$calculation.$status.setStatus('btnCreateKP', 'Default');
|
||||
$calculation.$status.setStatus('btnCreateKPMini', 'Default');
|
||||
|
||||
$process.delete('CreateKP');
|
||||
});
|
||||
}
|
||||
|
||||
@ -96,11 +96,4 @@ export function common({ store, trpcClient, apolloClient }: ProcessContext) {
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
reaction(
|
||||
() => $process.has('LoadKP'),
|
||||
(isLoadKP) => {
|
||||
$calculation.$status.setStatus('selectQuote', isLoadKP ? 'Disabled' : 'Default');
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@ -9,3 +9,16 @@ export function useStore() {
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
export function useErrors() {
|
||||
const { $calculation, $tables } = useStore();
|
||||
|
||||
const hasElementsErrors = $calculation.hasErrors;
|
||||
const hasPaymentsErrors = $tables.payments.validation.hasErrors;
|
||||
const hasInsuranceErrors = $tables.insurance.validation.hasErrors;
|
||||
const hasFingapErrors = $tables.fingap.validation.hasErrors;
|
||||
|
||||
return {
|
||||
hasErrors: hasElementsErrors || hasPaymentsErrors || hasInsuranceErrors || hasFingapErrors,
|
||||
};
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user