Source: features/pokemons/PokemonSearch.jsx

  1. import { TYPES_LIST } from '../../common/utils/constants/TYPES_LIST'
  2. import { ABILITIES_LIST } from '../../common/utils/constants/ABILITIES_LIST'
  3. import { DebounceInput } from 'react-debounce-input';
  4. import { useDispatch, useSelector } from 'react-redux';
  5. import { useState } from 'react'
  6. import {
  7. updateSearchQuery,
  8. updateTypeFilter,
  9. updateAbilityFilter
  10. } from './pokemonsSlice';
  11. import { updateSortBy } from './pokemonsSlice';
  12. export default function PokemonSearch() {
  13. const {typeFilter, abilityFilter} = useSelector(state => state.pokemons);
  14. const [input, setInput] = useState('');
  15. const [sortBy, setSortBy] = useState('');
  16. const dispatch = useDispatch();
  17. /**
  18. * Dispatches the action to update the search query and performs the search.
  19. * @func handleQueryChange
  20. * @param {Object} event - The event to get the value in the form.
  21. */
  22. const handleQueryChange = event => {
  23. dispatch(updateSearchQuery(event.target.value));
  24. setInput(event.target.value);
  25. };
  26. /**
  27. * Dispatches the action to update type filter and performs the filtering by type.
  28. * @func handleTypeFilterChange
  29. * @param {Object} event - The event to get the value from the selected type.
  30. */
  31. const handleTypeFilterChange = event => {
  32. dispatch(updateTypeFilter(event.target.value));
  33. };
  34. /**
  35. * Dispatches the action to update ability filter and performs the filtering by ability.
  36. * @func handleAbilityFilterChange
  37. * @param {Object} event - The event to get the value from the selected ability.
  38. */
  39. const handleAbilityFilterChange = event => {
  40. dispatch(updateAbilityFilter(event.target.value));
  41. };
  42. /**
  43. * Dispatches the action to sort alphabetically.
  44. * @func handleSortByName
  45. */
  46. const handleSortByName = () => {
  47. dispatch(updateSortBy('name'));
  48. setSortBy('name');
  49. };
  50. /**
  51. * Dispatches the action to sort by HP(high to low).
  52. * @func handleSortByHP
  53. */
  54. const handleSortByHP = () => {
  55. dispatch(updateSortBy('hp'));
  56. setSortBy('hp');
  57. };
  58. /**
  59. * Dispatches actions to reset all the filters.
  60. * @func handleResetFilters
  61. */
  62. const handleResetFilters = () => {
  63. dispatch(updateSearchQuery(''));
  64. dispatch(updateTypeFilter(''));
  65. dispatch(updateAbilityFilter(''));
  66. dispatch(updateSortBy(''));
  67. dispatch(updateSortBy(''));
  68. setInput('');
  69. setSortBy('');
  70. }
  71. /**
  72. * Prevents the default submitting of the form if the user presses Enter.
  73. * @func handleOnKeyDownSearchForm
  74. */
  75. const handleOnKeyDownSearchForm = (event) => {
  76. if (event.key === 'Enter') {
  77. event.preventDefault();
  78. }
  79. };
  80. return (
  81. <section className="search">
  82. <h2>Search filters</h2>
  83. <form className="search--form" action="">
  84. <DebounceInput
  85. className="search--input"
  86. placeholder="🐞 Type to search for a Pokémon."
  87. minLength={0}
  88. debounceTimeout={400}
  89. value={input}
  90. type="text"
  91. onChange={handleQueryChange}
  92. onKeyDown={handleOnKeyDownSearchForm}
  93. />
  94. </form>
  95. <form className='search--advanced'>
  96. <select
  97. className="search--filter"
  98. value={typeFilter}
  99. onChange={handleTypeFilterChange}
  100. >
  101. {TYPES_LIST.map(type => (
  102. <option key={type} value={type}>
  103. {type ? type : 'Type'}
  104. </option>
  105. ))}
  106. </select>
  107. <select
  108. className="search--filter"
  109. value={abilityFilter}
  110. onChange={handleAbilityFilterChange}
  111. >
  112. {ABILITIES_LIST.map(ability => (
  113. <option key={ability} value={ability}>
  114. {ability ? ability : 'Ability'}
  115. </option>
  116. ))}
  117. </select>
  118. <button
  119. className={sortBy === 'name' ? 'button--chosen' : 'button--sort'}
  120. type='button'
  121. onClick={handleSortByName}
  122. >
  123. Sort by name
  124. </button>
  125. <button
  126. className={sortBy === 'hp' ? 'button--chosen' : 'button--sort'}
  127. type='button'
  128. onClick={handleSortByHP}
  129. >
  130. Sort by HP
  131. </button>
  132. <button
  133. className='button'
  134. type='button'
  135. onClick={handleResetFilters}
  136. >
  137. Reset filters
  138. </button>
  139. </form>
  140. </section>
  141. );
  142. }