All files / src/Spotlight useSpotlight.js

100% Statements 26/26
71.43% Branches 5/7
87.5% Functions 7/8
100% Lines 24/24

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      12x 16x 16x 16x   16x 1x     16x 1x 1x 1x     16x 4x 4x 4x 4x     16x 1x 1x     16x 1x 1x             1x 1x     16x                        
import { useState } from 'react';
import * as R from 'ramda';
 
export const useSpotlight = (hasCommit, defaultTerm, defaultSpotlight, action = () => {}) => {
  const [term = '', setTerm] = useState(defaultTerm);
  const [spotlight = {}, setSpotlight] = useState(defaultSpotlight);
  const [isFocus, setFocus] = useState(false);
 
  const onKeyPress = event => {
    Eif (event.key === 'Enter') return action({ term, spotlight });
  };
 
  const onClearTerm = event => {
    event.preventDefault();
    setTerm('');
    return action({ term: '' });
  };
 
  const onChangeTerm = event => {
    event.preventDefault();
    const newTerm = R.propOr('', 'value')(event.target);
    setTerm(newTerm);
    return hasCommit ? null : action({ term: newTerm, spotlight });
  };
 
  const onCommitTerm = event => {
    event.preventDefault();
    return action({ term, spotlight });
  };
 
  const onChangeField = field => event => {
    event.preventDefault();
    const newSpotlight = {
      ...spotlight,
      fields: {
        ...spotlight.fields,
        [field.id]: { ...field, isSelected: !field.isSelected },
      },
    };
    setSpotlight(newSpotlight);
    return action({ term, spotlight: newSpotlight });
  };
 
  return {
    term,
    spotlight,
    isFocus,
    setFocus,
    onKeyPress,
    onClearTerm,
    onChangeTerm,
    onChangeField,
    onCommitTerm,
  };
};