147 lines
3.3 KiB
JavaScript
Raw Normal View History

2019-10-16 16:56:44 +02:00
import React, { useState } from 'react';
2019-10-23 15:49:03 +02:00
import PropTypes from 'prop-types';
import { FormattedMessage } from 'react-intl';
import { isEmpty } from 'lodash';
import matchSorter from 'match-sorter';
2019-10-16 16:56:44 +02:00
import Wrapper from './Wrapper';
2019-10-23 15:49:03 +02:00
import List from './List';
import Search from './Search';
2019-10-16 16:56:44 +02:00
2019-10-24 14:41:06 +02:00
import LeftMenuLink from '../LeftMenuLink';
2019-10-23 15:49:03 +02:00
import LeftMenuSubList from '../LeftMenuSubList';
2019-10-16 16:56:44 +02:00
2019-10-24 14:41:06 +02:00
function LeftMenuList({ customLink, links, title }) {
2019-10-23 15:49:03 +02:00
const [search, setSearch] = useState('');
const [showSearch, setShowSearch] = useState(false);
2019-10-16 16:56:44 +02:00
2019-10-23 15:49:03 +02:00
const { Component, componentProps } = customLink;
2019-10-16 16:56:44 +02:00
2019-10-23 15:49:03 +02:00
const toggleSearch = () => setShowSearch(!showSearch);
const handleChange = ({ target: { value } }) => {
setSearch(value);
};
const clearSearch = () => {
setSearch('');
};
const hasChildObject = () => {
return links.find(obj => obj.links);
};
2019-10-16 16:56:44 +02:00
2019-10-23 15:49:03 +02:00
const handleClose = () => {
clearSearch();
toggleSearch();
};
const getList = () => {
if (hasChildObject()) {
return links.map(link => {
return {
...link,
links: matchSorter(link.links, search, { keys: ['name'] }),
};
});
}
return matchSorter(links, search, { keys: ['name'] });
};
const getCount = () => {
if (hasChildObject()) {
return links.reduce((acc, current) => {
return acc + current.links.length;
}, 0);
}
return links.length;
};
2019-10-24 14:41:06 +02:00
const getTitle = () =>
getCount() > 1 ? `${title.id}plural` : `${title.id}singular`;
2019-10-23 15:49:03 +02:00
const renderCompo = (link, i) => {
2019-10-24 14:41:06 +02:00
const { links, name, title } = link;
2019-10-23 15:49:03 +02:00
if (links) {
const isFiltered = !isEmpty(search) ? true : false;
return (
<LeftMenuSubList
key={name}
{...link}
isFiltered={isFiltered}
isFirstItem={i === 0}
/>
);
}
return (
<li key={name}>
2019-10-24 14:41:06 +02:00
<LeftMenuLink {...link}>{title}</LeftMenuLink>
2019-10-23 15:49:03 +02:00
</li>
);
};
2019-10-16 16:56:44 +02:00
return (
2019-10-23 15:49:03 +02:00
<Wrapper>
2019-10-16 16:56:44 +02:00
<div className="list-header">
2019-10-23 15:49:03 +02:00
{!showSearch ? (
<div className="title-wrapper">
<h3>
<FormattedMessage id={getTitle()} />
&nbsp;&nbsp;<span>{getCount()}</span>
</h3>
<button onClick={toggleSearch}>
<i className="fa fa-search"></i>
</button>
</div>
) : (
<div className="search-wrapper">
<i className="fa fa-search"></i>
<button onClick={toggleSearch}></button>
<Search
onChange={handleChange}
value={search}
placeholder="search…"
/>
<button onClick={handleClose}>
<i className="fa fa-close"></i>
</button>
</div>
)}
</div>
<div>
<List>{getList().map((link, i) => renderCompo(link, i))}</List>
<Component {...componentProps} />
2019-10-16 16:56:44 +02:00
</div>
</Wrapper>
);
}
2019-10-23 15:49:03 +02:00
LeftMenuList.defaultProps = {
customLink: {
Component: null,
componentProps: {
id: null,
onClick: () => {},
},
},
links: [],
2019-10-24 14:41:06 +02:00
title: 'models',
2019-10-23 15:49:03 +02:00
};
LeftMenuList.propTypes = {
customLink: PropTypes.shape({
Component: PropTypes.object,
componentProps: PropTypes.shape({
id: PropTypes.string,
onClick: PropTypes.func,
}),
}),
links: PropTypes.array,
2019-10-24 14:41:06 +02:00
title: PropTypes.string,
2019-10-23 15:49:03 +02:00
};
2019-10-16 16:56:44 +02:00
export default LeftMenuList;