Advertisement
danilosilvadev

SearchPage

Sep 19th, 2019
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { useEffect, useState, useContext } from 'react'
  2. import styled from 'styled-components'
  3. import _ from 'lodash'
  4. import { Link, withRouter } from 'react-router-dom'
  5. import { MobilePagination, DesktopPagination } from '../../components'
  6. import { dispatchSearchResults } from '../../middlewares'
  7. import { getPath, clearString } from '../../utils'
  8. import { Context, actionTypes, hooks, initialState } from '../../store'
  9.  
  10. function SearchPage({ history }) {
  11.   const initialHook = hooks()
  12.  
  13.   const {
  14.     state: { searchResults, searchTerm, activeSearchPage },
  15.     dispatch,
  16.   } = useContext(Context)
  17.  
  18.   useEffect(() => {
  19.     let searchQuery = ''
  20.     if (getPath.atPosition(1) === 'search') {
  21.       searchQuery = getPath.atPosition(2)
  22.       const urlPage = Number(clearString.onlyNumbers(getPath.atPosition(3)))
  23.       dispatch({
  24.         type: actionTypes.SET_ACTIVE_SEARCH_PAGE,
  25.         activeSearchPage: urlPage,
  26.       })
  27.     } else {
  28.       searchQuery = getPath.searchParam()
  29.       dispatch({
  30.         type: actionTypes.SET_ACTIVE_SEARCH_PAGE,
  31.         activeSearchPage: initialState.activeSearchPage,
  32.       })
  33.     }
  34.     if (searchTerm === searchQuery) return
  35.     dispatchSearchResults(searchQuery, dispatch, initialHook)
  36.   }, [])
  37.   return (
  38.     <>
  39.       {initialHook.error.value === '' && !initialHook.pending.value ? (
  40.         <ul className="c-bg-white m-4">
  41.           {searchResults.length !== 0 && searchResults !== undefined ? (
  42.             mountList(searchResults, activeSearchPage, history).map(item => (
  43.               <StyledLi
  44.                 key={item.id}
  45.                 className="f p-top-2 p-bottom-2 f-justify-center f-md-justify-start"
  46.                 onClick={() => {
  47.                   dispatch({
  48.                     type: actionTypes.SET_PRODUCT_ID,
  49.                     productID: item.id,
  50.                   })
  51.                 }}
  52.               >
  53.                 <Link
  54.                   to={`/items/${item.id}`}
  55.                   className="f f-column clear-link c-black f-md-row f-align-center"
  56.                 >
  57.                   <img
  58.                     src={item.image}
  59.                     alt="product"
  60.                     width="auto"
  61.                     height="auto"
  62.                   />
  63.                   <aside className="m-left-md-4 f f-column w-60 f-align-center f-md-align-start">
  64.                     <span className="m-bottom-2">{item.price}</span>
  65.                     <span>{item.name}</span>
  66.                   </aside>
  67.                 </Link>
  68.               </StyledLi>
  69.             ))
  70.           ) : (
  71.             <h3 className="c-grey-darker p-top-2 p-bottom-2 font-weight-1">
  72.               Nenhum resultado encontrado
  73.             </h3>
  74.           )}
  75.         </ul>
  76.       ) : (
  77.         <h2 className="c-red p-top-2 p-bottom-2 font-weight-1 c-bg-white m-4 p-4">
  78.           {initialHook.error.value}
  79.         </h2>
  80.       )}
  81.       <ul className="f m-top-2 m-bottom-4 f-justify-center">
  82.         <MobilePagination StyledLi={StyledLi} />
  83.         <DesktopPagination StyledLi={StyledLi} />
  84.       </ul>
  85.     </>
  86.   )
  87. }
  88.  
  89. const mountList = (fullList, activePage, history) => {
  90.   if (fullList.length < 5) return fullList
  91.   let arr = []
  92.   fullList.forEach((v, i) => {
  93.     if (Number.isInteger(i / 4) && i !== 0) {
  94.       arr.push(fullList.slice(i - 4, i))
  95.     }
  96.   })
  97.   if (!arr[activePage - 1]) {
  98.     history.push('/not_found')
  99.   }
  100.   return arr[activePage - 1] || arr[arr.length - 1]
  101. }
  102.  
  103. const StyledLi = styled.li`
  104.   cursor: pointer;
  105.   pointer-events: ${props => (props.item === '...' ? 'none' : 'auto')};
  106.   border-bottom: ${props => (props.item ? 'none' : '1px solid lightGrey')};
  107.   margin-right: 2rem;
  108.   overflow: ${props => (props.item ? 'hidden' : 'none')};
  109.   &:hover {
  110.     background-color: ${props =>
  111.       props.item && !props.isMobile ? 'rgb(192, 192, 192)' : ''};
  112.   }
  113. `
  114. export default withRouter(SearchPage)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement