This commit is contained in:
Владислав Чикалкин 2020-08-31 17:10:08 +03:00
parent ac7e6617a3
commit b135fe20d0
7 changed files with 55 additions and 34 deletions

View File

@ -1,3 +1,4 @@
import "mobx-react/batchingForReactDom";
import { StoreProvider } from "client/contexts/storeContext";
import theme from "client/UIKit/theme";
import React from "react";

View File

@ -29,7 +29,7 @@ export default [
name: "tbxSecond",
Component: withTitle("Sum")(Input),
props: {
readOnly: true,
readonly: true,
getValue: (calculationStore: CalculationStore) => {
const price = calculationStore.getValue("price");
const one = calculationStore.getValue("one");

View File

@ -1,18 +1,22 @@
import Background from "client/Elements/Background";
import React from "react";
import React, { useState } from "react";
import Sections from "./Sections";
import { Tabs } from "antd";
const { TabPane } = Tabs;
const Calculation = () => {
return (
<Background>
<Tabs type="line">
{Sections.map(({ title, elements }, i) => (
<div key={i}>
<p>{title}</p>
<TabPane tab={title} key={i}>
{elements.map(({ Component, props }, ie) => {
return <Component {...props} key={ie} />;
})}
</div>
</TabPane>
))}
</Tabs>
</Background>
);
};

View File

@ -4,19 +4,17 @@ import { TEffect } from "core/types/effect";
export const testEffectForPrice: TEffect = async (
calculationStore: CalculationStore
) => {
console.log("price");
const price = calculationStore.getValue("price");
if (price > 5000) {
calculationStore.setValue("one", 500);
if (parseInt(price) > 5000) {
calculationStore.setValue("one", 200);
}
};
export const testEffectForOne: TEffect = async (
calculationStore: CalculationStore
) => {
console.log("one");
const one = calculationStore.getValue("one");
if (one > 1000) {
if (parseInt(one) > 1000) {
calculationStore.setValue("price", 100500);
}
};

View File

@ -1,39 +1,58 @@
import React, { useState, useEffect } from "react";
import { Input as AntInput } from "antd";
import { useDebounce } from "use-debounce";
import { useObserver } from "mobx-react";
import { useObserver, observer } from "mobx-react";
import { useStores } from "client/hooks/useStores";
import runEffects from "client/tools/runEffects";
const Input = ({ placeholder, sourceValueName, getValue, Effects }) => {
const Input = ({
readonly,
placeholder,
sourceValueName,
getValue,
Effects,
}) => {
const { calculationStore } = useStores();
const [currentValue, setCurrentValue] = useState(undefined);
const [debouncedValue] = useDebounce(currentValue, 800);
const [debouncedValue] = useDebounce(currentValue, 850);
const sourceValue = calculationStore.getValue(sourceValueName);
// get Values
useEffect(() => {
if (sourceValueName) {
setCurrentValue(calculationStore.getValue(sourceValueName));
if (sourceValue) {
setCurrentValue(sourceValue);
}
}, [sourceValueName]);
}
if (getValue) {
const value = getValue(calculationStore);
setCurrentValue(value);
}
}, [sourceValue]);
// run Effects
useEffect(() => {
if (sourceValueName) {
if (debouncedValue) {
calculationStore.setValue(sourceValueName, debouncedValue);
if (Effects && Effects.length > 0) {
runEffects(Effects, calculationStore);
}
if (sourceValueName && debouncedValue) {
calculationStore.setValue(sourceValueName, debouncedValue);
}
console.log(calculationStore.values);
}, [debouncedValue]);
return useObserver(() => (
return (
<AntInput
placeholder={placeholder}
value={sourceValueName ? currentValue : getValue(calculationStore)}
value={getValue ? getValue(calculationStore) : currentValue}
onChange={(e) => {
if (!readonly) {
setCurrentValue(e.target.value);
}
}}
/>
));
);
};
export default Input;
export default observer(Input);

View File

@ -1,5 +1,4 @@
async function runEffects(effects, calculationStore) {
console.log(effects);
if (effects && effects.length > 0) {
for (let effect of effects) {
try {

View File

@ -2,7 +2,7 @@ import { ValuesMap } from "core/types/values";
const initialValues: ValuesMap = {
price: 10000,
one: 0,
// one: 0,
};
export default initialValues;