All files / publisher/pages/Snaps SnapsFilter.tsx

41.66% Statements 5/12
25% Branches 1/4
37.5% Functions 3/8
45.45% Lines 5/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88                                                          16x                                 2x 2x   4x     2x                                                                      
import { KeyboardEvent } from "react";
 
import type { SnapsList } from "../../types/shared";
 
type SetSnapsInStoreFunc = (snaps: SnapsList) => void;
type SetOtherStoresFunc = (
  stores: Array<{ id: string; name: string; snaps: SnapsList }>,
) => void;
type GetStoreNameFunc = (storeId: string) => string;
 
type Props = {
  setSnapsInStore: SetSnapsInStoreFunc;
  snapsInStore: SnapsList;
  setOtherStores: SetOtherStoresFunc;
  otherStoreIds: Array<string>;
  getStoreName: GetStoreNameFunc;
  snaps: SnapsList;
  id: string;
};
 
function SnapsFilter({
  setSnapsInStore,
  snapsInStore,
  setOtherStores,
  otherStoreIds,
  getStoreName,
  snaps,
  id,
}: Props): React.JSX.Element {
  return (
    <div className="p-search-box">
      <label htmlFor="search-snaps" className="u-off-screen">
        Search snaps
      </label>
      <input
        type="search"
        className="p-search-box__input"
        name="search-snaps"
        id="search-snaps"
        placeholder="Search snaps"
        autoComplete="off"
        onKeyUp={(
          e: KeyboardEvent<HTMLInputElement> & {
            target: HTMLInputElement;
          },
        ) => {
          if (e.target.value) {
            setSnapsInStore(
              snapsInStore.filter((snap) =>
                snap?.name?.includes(e.target.value),
              ),
            );
            setOtherStores(
              otherStoreIds.map((storeId) => {
                return {
                  id: storeId,
                  name: getStoreName(storeId),
                  snaps: snaps.filter(
                    (snap) =>
                      snap.store === storeId &&
                      snap.name.includes(e.target.value),
                  ),
                };
              }),
            );
          } else E{
            setSnapsInStore(snaps.filter((snap) => snap.store === id));
            setOtherStores(
              otherStoreIds.map((storeId) => {
                return {
                  id: storeId,
                  name: getStoreName(storeId),
                  snaps: snaps.filter((snap) => snap.store === storeId),
                };
              }),
            );
          }
        }}
      />
      <button type="submit" className="p-search-box__button">
        <i className="p-icon--search"></i>
      </button>
    </div>
  );
}
 
export default SnapsFilter;