From ff04341c2bd475a77983f04587406c620095b833 Mon Sep 17 00:00:00 2001 From: Chika Date: Thu, 23 Jun 2022 17:48:37 +0300 Subject: [PATCH] InsuranceTable: add validation --- .../Form/Insurance/InsuranceTable/index.jsx | 46 ++++++++++++++----- Elements/Alert.js | 3 ++ stores/tables/insurance/index.ts | 3 ++ stores/tables/insurance/validation.ts | 29 ++++++++++++ 4 files changed, 69 insertions(+), 12 deletions(-) create mode 100644 Elements/Alert.js create mode 100644 stores/tables/insurance/validation.ts diff --git a/Components/Calculation/Form/Insurance/InsuranceTable/index.jsx b/Components/Calculation/Form/Insurance/InsuranceTable/index.jsx index e1f75cb..3289be4 100644 --- a/Components/Calculation/Form/Insurance/InsuranceTable/index.jsx +++ b/Components/Calculation/Form/Insurance/InsuranceTable/index.jsx @@ -1,31 +1,53 @@ +import Alert from 'Elements/Alert'; import Table from 'Elements/Table'; +import { observer } from 'mobx-react-lite'; import { useStore } from 'stores/hooks'; import styled from 'styled-components'; +import { Flex } from 'UIKit/grid'; import { columns } from './config'; -const Wrapper = styled.div` +const Grid = styled(Flex)` + flex-direction: column; +`; + +const TableWrapper = styled.div` td > * { margin: 0; } `; +const Validation = observer(() => { + const store = useStore(); + + const messages = store.$tables.insurance.validation.getMessages(); + + if (messages?.length) { + return ; + } + + return null; +}); + function InsuranceTable() { const store = useStore(); const { values } = store.$tables.insurance; return ( - - - + + +
+ + + ); } diff --git a/Elements/Alert.js b/Elements/Alert.js new file mode 100644 index 0000000..f012da8 --- /dev/null +++ b/Elements/Alert.js @@ -0,0 +1,3 @@ +/* eslint-disable unicorn/filename-case */ +/* eslint-disable no-restricted-exports */ +export { Alert as default } from 'antd'; diff --git a/stores/tables/insurance/index.ts b/stores/tables/insurance/index.ts index 5f91f3f..9f09213 100644 --- a/stores/tables/insurance/index.ts +++ b/stores/tables/insurance/index.ts @@ -4,6 +4,7 @@ import * as insuranceTableConfig from 'config/tables/insurance-table'; import { mergeWith } from 'lodash'; import { makeAutoObservable } from 'mobx'; import type RootStore from 'stores/root'; +import Validation from './validation'; export interface InsuranceTableData { values: Insurance.RowValues[]; @@ -13,12 +14,14 @@ export interface InsuranceTableData { export default class InsuranceTable { root: RootStore; + validation: Validation; values: Insurance.RowValues[] = insuranceTableConfig.defaultValues; options: Insurance.RowOptions[] = insuranceTableConfig.defaultOptions; statuses: Insurance.RowStatuses[] = insuranceTableConfig.defaultStatuses; constructor(rootStore: RootStore) { this.root = rootStore; + this.validation = new Validation(); makeAutoObservable(this); } diff --git a/stores/tables/insurance/validation.ts b/stores/tables/insurance/validation.ts new file mode 100644 index 0000000..f024b80 --- /dev/null +++ b/stores/tables/insurance/validation.ts @@ -0,0 +1,29 @@ +import { makeAutoObservable, observable } from 'mobx'; + +export default class Validation { + messages = observable([]); + + constructor() { + makeAutoObservable(this); + } + + getMessages() { + return this.messages; + } + + addError = (message: string) => { + this.messages.push(message); + + const messageIndex = this.messages.length; + + const removeError = () => { + this.messages.splice(messageIndex - 1, 1); + }; + + return removeError; + }; + + clearErrors = () => { + this.messages.clear(); + }; +}