From 431f41edcd5354e11fb4edd5688f2276a2442e93 Mon Sep 17 00:00:00 2001 From: Chika Date: Tue, 5 Jul 2022 19:28:26 +0300 Subject: [PATCH] PaymentsTable: connect rows to store --- .../Form/Payments/PaymentsTable/builders.tsx | 14 ++++++++++ .../Form/Payments/PaymentsTable/config.tsx | 13 ++++++--- .../Form/Payments/PaymentsTable/hooks.js | 28 +++++++++++++++++++ .../Form/Payments/PaymentsTable/index.jsx | 11 +++++++- .../Form/Payments/PaymentsTable/types.ts | 5 ---- stores/tables/payments/hooks.js | 21 ++++++++++++++ stores/tables/payments/index.ts | 25 +++++++++-------- 7 files changed, 96 insertions(+), 21 deletions(-) create mode 100644 Components/Calculation/Form/Payments/PaymentsTable/builders.tsx create mode 100644 Components/Calculation/Form/Payments/PaymentsTable/hooks.js delete mode 100644 Components/Calculation/Form/Payments/PaymentsTable/types.ts create mode 100644 stores/tables/payments/hooks.js diff --git a/Components/Calculation/Form/Payments/PaymentsTable/builders.tsx b/Components/Calculation/Form/Payments/PaymentsTable/builders.tsx new file mode 100644 index 0000000..6dc13b8 --- /dev/null +++ b/Components/Calculation/Form/Payments/PaymentsTable/builders.tsx @@ -0,0 +1,14 @@ +/* eslint-disable import/prefer-default-export */ +import { observer } from 'mobx-react-lite'; +import type { ComponentType } from 'react'; +import { useRowStatus } from 'stores/tables/payments/hooks'; +import { usePaymentValue } from './hooks'; + +export function buildValueComponent(index: number, Component: ComponentType) { + return observer((props: T) => { + const [value, setValue] = usePaymentValue(index); + const status = useRowStatus(index); + + return ; + }); +} diff --git a/Components/Calculation/Form/Payments/PaymentsTable/config.tsx b/Components/Calculation/Form/Payments/PaymentsTable/config.tsx index 6e28412..09fe405 100644 --- a/Components/Calculation/Form/Payments/PaymentsTable/config.tsx +++ b/Components/Calculation/Form/Payments/PaymentsTable/config.tsx @@ -1,20 +1,25 @@ /* eslint-disable import/prefer-default-export */ import type { ColumnsType } from 'antd/lib/table'; import InputNumber from 'Elements/InputNumber'; -import type { Payment } from './types'; -export const columns: ColumnsType = [ +import { buildValueComponent } from './builders'; + +export const columns: ColumnsType = [ { key: 'num', dataIndex: 'num', title: '#', width: '7%', - render: (_value, payment) => payment.num + 1, + render: (_value, _payment, index) => index + 1, }, { key: 'paymentRelation', dataIndex: 'paymentRelation', title: '% платежа', - render: (_value, payment) => , + render: (_value, _payment, index) => { + const Component = buildValueComponent(index, InputNumber); + + return ; + }, }, ]; diff --git a/Components/Calculation/Form/Payments/PaymentsTable/hooks.js b/Components/Calculation/Form/Payments/PaymentsTable/hooks.js new file mode 100644 index 0000000..6ee7091 --- /dev/null +++ b/Components/Calculation/Form/Payments/PaymentsTable/hooks.js @@ -0,0 +1,28 @@ +/* eslint-disable import/prefer-default-export */ +import { useEffect, useState } from 'react'; +import { useRowValue } from 'stores/tables/payments/hooks'; +import { useDebouncedCallback } from 'use-debounce'; + +export function usePaymentValue(index) { + const [storeValue, setStoreValue] = useRowValue(index); + const [value, setValue] = useState(storeValue); + + // eslint-disable-next-line object-curly-newline + const debouncedSetStoreValue = useDebouncedCallback(setStoreValue, 350, { maxWait: 1000 }); + + useEffect( + () => { + if (storeValue !== value) { + debouncedSetStoreValue(value); + } + }, + // eslint-disable-next-line react-hooks/exhaustive-deps + [value] + ); + + useEffect(() => { + setValue(storeValue); + }, [storeValue]); + + return [value, setValue]; +} diff --git a/Components/Calculation/Form/Payments/PaymentsTable/index.jsx b/Components/Calculation/Form/Payments/PaymentsTable/index.jsx index 1606c36..1b51a0b 100644 --- a/Components/Calculation/Form/Payments/PaymentsTable/index.jsx +++ b/Components/Calculation/Form/Payments/PaymentsTable/index.jsx @@ -1,6 +1,7 @@ import { MAX_LEASING_PERIOD } from 'constants/values'; import Alert from 'Elements/Alert'; import Table from 'Elements/Table'; +import { toJS } from 'mobx'; import { observer } from 'mobx-react-lite'; import { useStore } from 'stores/hooks'; import styled from 'styled-components'; @@ -34,12 +35,20 @@ const PaymentsTable = observer(() => { const store = useStore(); const { payments } = store.$tables; + const values = toJS(payments.values); + + const dataSource = values.map((value, index) => ({ + key: index, + num: index, + paymentRelation: value, + })); + return ( ; + values: IObservableArray; statuses: IObservableArray; constructor(rootStore: RootStore) { this.root = rootStore; this.validation = new Validation(); - this.values = observable([]); + this.values = observable([]); this.statuses = observable([]); makeAutoObservable(this); } - getValue(num: number) { - const rowIndex = this.values.findIndex((value) => value.num === num); - - return this.values[rowIndex]; + getValue(index: number) { + return this.values[index]; } - setValues = (values: Payment[]) => { + setValues = (values: number[]) => { this.values.replace(values); }; - getStatus(num: number) { - const rowIndex = this.values.findIndex((value) => value.num === num); + setValue = (index: number, value: number) => { + this.values[index] = value; + }; - return this.statuses[rowIndex]; + getStatus(index: number) { + return this.statuses[index]; } setStatuses = (statuses: Status[]) => { this.statuses.replace(statuses); }; + + setStatus = (index: number, status: Status) => { + this.statuses[index] = status; + }; }