Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 6993 | Rev 6996 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
6993 stevensc 1
import React, { useEffect, useState } from 'react'
2
import { axios } from '../../utils'
6995 stevensc 3
import { Col, Container, Row } from 'react-bootstrap'
6993 stevensc 4
import { addNotification } from '../../redux/notification/notification.actions'
5
import { useDispatch, useSelector } from 'react-redux'
6
 
7
import CapsulesList from '../../components/marketplace/CapsulesList'
8
 
9
const MarketPlacePage = () => {
10
  const [capsules, setCapsules] = useState([])
11
  const [rewards, setRewards] = useState([])
12
  const [categories, setCategories] = useState([])
13
  const [searchParams, setSearchParams] = useState({
14
    search: '',
15
    category: 'capsules',
16
  })
17
  const labels = useSelector(({ intl }) => intl.labels)
18
  const dispatch = useDispatch()
19
 
20
  const handleChange = ({ target }) => {
21
    setSearchParams({ ...searchParams, [target.name]: target.value })
22
  }
23
 
24
  const load = async (
25
    _search = searchParams.search,
26
    _entity = searchParams.category
27
  ) => {
28
    axios
29
      .get(`/marketplace?search=${_search}&entity=${_entity}`)
30
      .then((response) => {
31
        const { success, data } = response.data
32
 
33
        if (!success) {
34
          dispatch(addNotification({ style: 'danger', msg: data }))
35
          return
36
        }
37
 
38
        _entity === 'capsules' ? setCapsules(data) : setRewards(data)
39
      })
40
      .catch((error) => {
41
        console.log('>>: error > ', error)
42
        dispatch(addNotification({ style: 'danger', msg: error }))
43
        throw new Error(error)
44
      })
45
  }
46
 
47
  const getCategories = async () => {
48
    const categories = await axios.get('/marketplace/categories')
49
    if (categories.data.data) {
50
      const _categories = Object.keys(categories.data.data).map((element) => {
51
        return {
52
          label: categories.data.data[element],
53
          value: element,
54
        }
55
      })
56
      setCategories(_categories)
57
    }
58
  }
59
 
60
  const checkParams = () => {
61
    const url = window.location.href
62
    const hasEntity = url.includes('?entity=')
63
    if (hasEntity) {
64
      const value = url.split('?entity=')[1]
65
      if (value) {
66
        setSearchParams({ ...searchParams, category: value })
67
      }
68
    }
69
  }
70
 
71
  useEffect(() => {
72
    getCategories()
73
    checkParams()
74
  }, [])
75
 
76
  useEffect(() => {
77
    load(searchParams.search, searchParams.category)
78
  }, [searchParams])
79
 
80
  return (
81
    <Container>
6995 stevensc 82
      <Row className="justify-content-end">
83
        <Col md="3">
84
          <div className="form-group search_categories">
85
            <select
86
              name="category"
87
              className="form-control"
88
              value={searchParams.category}
89
              onChange={(e) => handleChange(e)}
90
            >
91
              <option disabled value="">
92
                {labels.select_category}
6993 stevensc 93
              </option>
6995 stevensc 94
              {categories.map((element, i) => (
95
                <option value={element.value} key={i}>
96
                  {element.label}
97
                </option>
98
              ))}
99
            </select>
100
          </div>
101
        </Col>
102
        <Col md="3">
103
          <div className="form-group search_input">
104
            <input
105
              type="text"
106
              name="search"
107
              className="form-control"
108
              onChange={(e) => handleChange(e)}
109
              value={searchParams.search}
110
            />
111
            <button className="search-btn">{labels.search}</button>
112
          </div>
113
        </Col>
6993 stevensc 114
      </Row>
6995 stevensc 115
      <h2 className="mt-3 font-bold">
6993 stevensc 116
        {searchParams.category === 'capsules' ? 'Cápsulas' : 'Recompensas'} de
117
        Microaprendizaje
6995 stevensc 118
      </h2>
6993 stevensc 119
      <CapsulesList
120
        capsules={searchParams.category === 'capsules' ? capsules : rewards}
121
        onEnroll={load}
122
      />
123
    </Container>
124
  )
125
}
126
 
127
export default MarketPlacePage