2024-01-10 21:39:02 +04:00

101 lines
2.9 KiB
JavaScript

import PropTypes from 'prop-types';
import { useLocation } from 'react-router-dom';
import levenshtein from 'fast-levenshtein';
import useLinesDeleteModal from '../hooks/LinesDeleteModalHook';
import useLinesFormModal from '../hooks/LinesFormModalHook';
import useLines from '../hooks/LinesHook';
import LinesTable from './CardsGroups.jsx';
import LinesTableRow from './ArendaCardsRender.jsx';
const useQuery = () => {
return new URLSearchParams(useLocation().search);
};
const Lines = ({
popularChecked, unpopularChecked, showerChecked, parkChecked,
}) => {
const query = useQuery();
const searchText = query.get('search')?.toLowerCase() || '';
const { lines, handleLinesChange } = useLines();
const filteredLines = lines.filter((line) => {
const lineNameLower = line.geolocation.name?.toLowerCase();
const distance = levenshtein.get(lineNameLower, searchText);
if (searchText && popularChecked && unpopularChecked && showerChecked && parkChecked) {
return (
line.propertyStatus === 'Аренда' && distance <= 7
);
}
if (!searchText && popularChecked && unpopularChecked && showerChecked && parkChecked) {
return (
line.propertyStatus === 'Аренда'
);
}
if (!searchText && popularChecked && unpopularChecked) {
return (
line.propertyStatus === 'Аренда'
);
}
let shouldShowLine = line.propertyStatus === 'Аренда';
if (popularChecked) {
shouldShowLine = shouldShowLine && line.popular === 'true';
}
if (unpopularChecked) {
shouldShowLine = shouldShowLine && line.popular !== 'true';
}
if (showerChecked) {
shouldShowLine = shouldShowLine && line.shower1 === 'true';
}
if (parkChecked) {
shouldShowLine = shouldShowLine && line.park >= 1;
}
if (searchText) {
shouldShowLine = shouldShowLine && distance <= 7;
}
return shouldShowLine;
});
const {
showDeleteModal,
} = useLinesDeleteModal(handleLinesChange);
const {
showFormModal,
} = useLinesFormModal(handleLinesChange);
return (
<>
<LinesTable>
{
filteredLines.map((line, index) => (
<LinesTableRow key={line.id}
index={index} line={line}
onDelete={() => showDeleteModal(line.id)}
onEdit={() => showFormModal(line.id)}
/>
))
}
</LinesTable>
</>
);
};
Lines.propTypes = {
popularChecked: PropTypes.bool.isRequired,
unpopularChecked: PropTypes.bool.isRequired,
showerChecked: PropTypes.bool.isRequired,
parkChecked: PropTypes.bool.isRequired,
};
export default Lines;