-
Notifications
You must be signed in to change notification settings - Fork 0
/
Column.jsx
67 lines (58 loc) · 1.75 KB
/
Column.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import React, { useState, useEffect } from "react";
import { AddBtn } from "../AddBtn/AddBtn";
import { Currency } from "../Currency/Currency";
import "./Columns.scss";
//Mobile adaptation
import { isMobileOnly } from "react-device-detect";
//Animation
import { useTransition } from "react-spring";
//redux
import { useSelector } from "react-redux";
export const Column = ({ remove }) => {
//Currencies options
const [currencies, setCurrencies] = useState([Math.random()]);
const [canRemove, setCanRemove] = useState(true);
const columnsCount = useSelector((state) => state.columnsCount.count);
useEffect(() => {
if (currencies.length < 1) {
remove();
}
}, [currencies]);
//Column remove
useEffect(() => {
if (currencies.length < 2 && columnsCount < 2) {
setCanRemove(false);
} else {
setCanRemove(true);
}
}, [currencies, columnsCount]);
const addCurrency = () => {
setCurrencies([...currencies, Math.random()]);
};
const removeCurrency = (id) => {
if (canRemove) {
setCurrencies(currencies.filter((item) => item !== id));
}
};
//Animation
const transitions = useTransition(currencies, null, {
config: { duration: 250 },
from: { opacity: 0, height: 0, flexBasis: 0 },
enter: { opacity: 1, height: 96, flexBasis: 96 },
leave: { opacity: 0, height: 0, flexBasis: 0 },
});
return (
<div className={`surface__column ${isMobileOnly ? "mobile" : ""}`}>
{transitions.map(({ item, props }) => (
<Currency
style={{ ...props }}
key={item}
remove={() => removeCurrency(item)}
canRemove={canRemove}
baseStatus={false}
/>
))}
<AddBtn onClick={addCurrency} ariaLabel="Add currency" />
</div>
);
};