src/short-urls/SearchBar.js   A
last analyzed

Complexity

Total Complexity 1
Complexity/F 0

Size

Lines of Code 82
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 1
eloc 68
mnd 1
bc 1
fnc 0
dl 0
loc 82
ccs 14
cts 14
cp 1
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import { faTags as tagsIcon } from '@fortawesome/free-solid-svg-icons';
2
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
3
import React from 'react';
4
import { isEmpty, pipe } from 'ramda';
5
import PropTypes from 'prop-types';
6
import moment from 'moment';
7
import SearchField from '../utils/SearchField';
8
import Tag from '../tags/helpers/Tag';
9
import DateRangeRow from '../utils/DateRangeRow';
10
import { formatDate } from '../utils/helpers/date';
11
import { shortUrlsListParamsType } from './reducers/shortUrlsListParams';
12
import './SearchBar.scss';
13
14 1
const propTypes = {
15
  listShortUrls: PropTypes.func,
16
  shortUrlsListParams: shortUrlsListParamsType,
17
};
18
19 16
const dateOrUndefined = (date) => date ? moment(date) : undefined;
20
21 1
const SearchBar = (colorGenerator, ForServerVersion) => {
22 1
  const SearchBar = ({ listShortUrls, shortUrlsListParams }) => {
23 8
    const selectedTags = shortUrlsListParams.tags || [];
24 16
    const setDate = (dateName) => pipe(
25
      formatDate(),
26 2
      (date) => listShortUrls({ ...shortUrlsListParams, [dateName]: date })
27
    );
28
29 8
    return (
30
      <div className="search-bar-container">
31
        <SearchField
32
          onChange={
33 1
            (searchTerm) => listShortUrls({ ...shortUrlsListParams, searchTerm })
34
          }
35
        />
36
37
        <ForServerVersion minVersion="1.21.0">
38
          <div className="mt-3">
39
            <div className="row">
40
              <div className="col-lg-8 offset-lg-4 col-xl-6 offset-xl-6">
41
                <DateRangeRow
42
                  startDate={dateOrUndefined(shortUrlsListParams.startDate)}
43
                  endDate={dateOrUndefined(shortUrlsListParams.endDate)}
44
                  onStartDateChange={setDate('startDate')}
45
                  onEndDateChange={setDate('endDate')}
46
                />
47
              </div>
48
            </div>
49
          </div>
50
        </ForServerVersion>
51
52
        {!isEmpty(selectedTags) && (
53
          <h4 className="search-bar__selected-tag mt-3">
54
            <FontAwesomeIcon icon={tagsIcon} className="search-bar__tags-icon" />
55
            &nbsp;
56
            {selectedTags.map((tag) => (
57 4
              <Tag
58
                colorGenerator={colorGenerator}
59
                key={tag}
60
                text={tag}
61
                clearable
62 1
                onClose={() => listShortUrls(
63
                  {
64
                    ...shortUrlsListParams,
65 1
                    tags: selectedTags.filter((selectedTag) => selectedTag !== tag),
66
                  }
67
                )}
68
              />
69
            ))}
70
          </h4>
71
        )}
72
      </div>
73
    );
74
  };
75
76 1
  SearchBar.propTypes = propTypes;
77
78 1
  return SearchBar;
79
};
80
81
export default SearchBar;
82