Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3568 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
3719 stevensc 1
import React, { useEffect, useState } from 'react';
2
import { useSelector } from 'react-redux';
3
import { Grid, Typography } from '@mui/material';
4
 
5
import { api } from '@shared/libs';
6
import { useAlert } from '@shared/hooks';
7
 
8
import { Input, Select } from '@shared/components';
9
import { CapsulesList } from '@marketplace/components';
10
 
11
export function MarketPlacePage() {
12
  const labels = useSelector(({ intl }) => intl.labels);
13
 
14
  const [capsules, setCapsules] = useState([]);
15
  const [rewards, setRewards] = useState([]);
16
  const [categories, setCategories] = useState([]);
17
  const [searchParams, setSearchParams] = useState({
18
    search: '',
19
    category: 'capsules'
20
  });
21
 
22
  const { showError } = useAlert();
23
 
24
  const handleChange = ({ target }) => {
25
    setSearchParams({ ...searchParams, [target.name]: target.value });
26
  };
27
 
28
  const load = async (_search = searchParams.search, _entity = searchParams.category) => {
29
    api
30
      .get(`/marketplace?search=${_search}&entity=${_entity}`)
31
      .then((data) => (_entity === 'capsules' ? setCapsules(data) : setRewards(data)))
32
      .catch((error) => showError(error.message));
33
  };
34
 
35
  const getCategories = async () => {
36
    const categories = await api.get('/marketplace/categories');
37
    if (categories) {
38
      const _categories = Object.keys(categories).map((element) => {
39
        return {
40
          label: categories[element],
41
          value: element
42
        };
43
      });
44
      setCategories(_categories);
45
    }
46
  };
47
 
48
  const checkParams = () => {
49
    const url = window.location.href;
50
    const hasEntity = url.includes('?entity=');
51
    if (hasEntity) {
52
      const value = url.split('?entity=')[1];
53
      if (value) {
54
        setSearchParams({ ...searchParams, category: value });
55
      }
56
    }
57
  };
58
 
59
  useEffect(() => {
60
    getCategories();
61
    checkParams();
62
  }, []);
63
 
64
  useEffect(() => {
65
    load(searchParams.search, searchParams.category);
66
  }, [searchParams]);
67
 
68
  return (
69
    <>
70
      <Grid container spacing={1} display='flex' justifyContent='end'>
71
        <Grid size={{ xs: 12, md: 3 }}>
72
          <Select
73
            name='category'
74
            onChange={handleChange}
75
            value={searchParams.category}
76
            options={categories}
77
            color='secondary'
78
          />
79
        </Grid>
80
 
81
        <Grid size={{ xs: 12, md: 3 }}>
82
          <Input
83
            name='search'
84
            placeholder={labels.search}
85
            value={searchParams.search}
86
            onChange={handleChange}
87
          />
88
        </Grid>
89
      </Grid>
90
 
91
      <Typography variant='h2' sx={{ mt: 2 }}>
92
        {searchParams.category === 'capsules' ? 'Cápsulas' : 'Recompensas'} de Microaprendizaje
93
      </Typography>
94
 
95
      <CapsulesList
96
        capsules={searchParams.category === 'capsules' ? capsules : rewards}
97
        onEnroll={load}
98
      />
99
    </>
100
  );
101
}