From 6627ec88b2a02b522511b85eb38d21225098cfad Mon Sep 17 00:00:00 2001
From: eug-vs <eug-vs@keemail.me>
Date: Sun, 18 Apr 2021 20:36:19 +0300
Subject: feat: support date filters

---
 src/lib/ServiceFilters.tsx | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

(limited to 'src/lib/ServiceFilters.tsx')

diff --git a/src/lib/ServiceFilters.tsx b/src/lib/ServiceFilters.tsx
index 8a1e160..b3cfae9 100644
--- a/src/lib/ServiceFilters.tsx
+++ b/src/lib/ServiceFilters.tsx
@@ -5,13 +5,13 @@ import hooks from '../hooks/useAPIClient';
 import useQuery from '../hooks/useQuery';
 import ServiceContext, { Filter } from './ServiceContext';
 import { SelectBase } from '../components/Select';
+import { InputBase } from '../components/Input';
 
 const getOptionLabel = (item: any, filter: Filter) => {
   const value = _.get(item, filter.as || filter.key);
   return filter.transform ? filter.transform(value) : value;
 };
 
-
 const ServiceFilters: React.FC = () => {
   const service = useContext(ServiceContext);
   const { query, setQuery } = useQuery();
@@ -32,12 +32,12 @@ const ServiceFilters: React.FC = () => {
       label: filter.label,
     });
 
-    const value = _.get(query, filter.key) || '-';
+    const value = _.get(query, filter.key, '-');
 
     return { ...filter, options, value };
   });
 
-  const handleFilterChange = (key: string) => (event: React.ChangeEvent<HTMLSelectElement>) => {
+  const handleFilterChange = (key: string) => (event: React.ChangeEvent<HTMLSelectElement | HTMLInputElement>) => {
     const { value } = event.target;
     const updatedQuery = { ...query, [key]: value };
     if (value === '-') delete updatedQuery[key];
@@ -57,14 +57,21 @@ const ServiceFilters: React.FC = () => {
           Сбросить фильтры
         </span>
       )}
-      {filters?.map(filter => (
+      {filters?.map(filter => (filter.date ? (
+        <InputBase
+          type="date"
+          key={filter.key}
+          onChange={handleFilterChange(filter.key)}
+          value={filter.value}
+        />
+      ) : (
         <SelectBase
           key={filter.key}
           options={filter.options || []}
           value={filter.value}
           onChange={handleFilterChange(filter.key)}
         />
-      ))}
+      )))}
     </div>
   );
 };
-- 
cgit v1.2.3

