Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

Rev Autor Línea Nro. Línea
1 www 1
import React from "react";
2
import { useEffect, useState } from "react";
3
import { connect } from "react-redux";
4
import { useForm } from "react-hook-form";
5
import styled from "styled-components";
1158 stevensc 6
import { axios } from "../../../../utils";
1 www 7
import Group from "./group/Group";
8
import AddGroupModal from "./add-group-modal/AddGroupModal";
9
import { addNotification } from "../../../../redux/notification/notification.actions";
10
import Spinner from "../../../../shared/loading-spinner/Spinner";
11
 
12
const StyledSpinnerContainer = styled.div`
13
  position: absolute;
14
  left: 0;
15
  top: 0;
16
  width: 100%;
17
  height: 100%;
18
  background: rgba(255, 255, 255, 0.4);
19
  display: flex;
20
  justify-content: center;
21
  align-items: center;
22
  z-index: 300;
23
`;
24
 
25
const MyGroups = (props) => {
26
  // props destructuring
27
  const { groupTypes, industries } = props.backendVars;
28
 
29
  // states
30
  const [Groups, setGroups] = useState([]);
31
  const [loading, setLoading] = useState(true);
32
  const [showAddGroupModal, setShowAddGroupModal] = useState(false);
33
 
34
  // redux destructuring
35
  const { addNotification } = props;
36
 
37
  // React hook form
38
  const { register, getValues } = useForm();
39
 
40
  let axiosThrottle = null;
41
 
42
  useEffect(() => {
43
    fetchGroups();
44
    return () => {
45
      clearTimeout(axiosThrottle);
46
    };
47
  }, []);
48
 
49
  const handleDelete = (link) => {
50
    setLoading(true);
51
    axios
52
      .post(link)
53
      .then((response) => {
54
        const resData = response.data;
1158 stevensc 55
        (resData);
1 www 56
        if (resData.success) {
57
          const msg = resData.data;
58
          addNotification({
59
            style: "success",
60
            msg: msg,
61
          });
62
          fetchGroups();
63
        } else {
64
          setLoading(false);
65
        }
66
      })
67
      .catch((error) => {
68
        setLoading(false);
69
      });
70
  };
71
 
1158 stevensc 72
  const fetchGroups = async (searchParam = '') => {
1 www 73
    setLoading(true);
74
    await axios
75
      .get(
1158 stevensc 76
        "/group/my-groups?search=" + searchParam)
1 www 77
      .then((response) => {
78
        const resData = response.data;
1158 stevensc 79
        (resData);
1 www 80
        if (resData.success) {
81
          setGroups(resData.data);
82
        }
83
      });
84
    setLoading(false);
85
  };
86
 
87
  const handleSearch = () => {
88
    //  (getValues());
89
    clearTimeout(axiosThrottle);
90
    // setLoading(true);
91
    const searchValue = getValues("search");
92
    axiosThrottle = setTimeout(() => {
93
      fetchGroups(searchValue);
94
    }, 500);
95
  };
96
 
97
  const handleShowAddGroupModal = () => {
98
    setShowAddGroupModal(!showAddGroupModal);
99
  };
100
 
101
  return (
102
    <React.Fragment>
103
      <section className="companies-info">
104
        <div className="container">
105
          <div className="company-title">
106
            <div className="section_admin_title_buttons">
107
              <div style={{ float: "left" }}>
108
                <h1 className="title">Mis grupos</h1>
109
              </div>
110
              <div style={{ float: "right" }}>
111
                <button
112
                  type="button"
113
                  className="btn btn-primary btn-add"
114
                  onClick={handleShowAddGroupModal}
115
                >
116
                  Agregar
117
                </button>
118
              </div>
119
            </div>
120
          </div>
121
 
122
          <div className="company-title">
123
            <div className="section_admin_title_buttons">
124
              <form
125
                name="form-connection-search"
126
                id="form-connection-search"
127
                onSubmit={(event) => event.preventDefault()}
128
              >
129
                <div className="form-group">
130
                  <input
131
                    type="text"
132
                    name="search"
133
                    id="search"
134
                    className="form-control"
135
                    placeholder="Buscar"
136
                    ref={register}
137
                    onChange={handleSearch}
138
                  />
139
                </div>
140
              </form>
141
            </div>
142
          </div>
143
 
144
          <div className="companies-list">
145
            <div
146
              className="row"
147
              id="profiles-container"
148
              style={{
149
                position: "relative",
150
              }}
151
            >
1158 stevensc 152
              {
153
                Groups.length
154
                  ?
155
                  Groups.map(
156
                    ({ image, link_delete, link_edit, link_view, name, privacy }, id) => (
157
                      <Group
158
                        image={image}
159
                        name={name}
160
                        link_view={link_view}
161
                        link_edit={link_edit}
162
                        link_delete={link_delete}
163
                        privacy={privacy}
164
                        key={id}
165
                        handleDelete={handleDelete}
166
                      />
167
                    ))
168
                  :
169
                  <div style={{ margin: "auto", textAlign: "center" }}>
170
                    Ningún registro coincidio con su consulta
171
                  </div>
172
              }
1 www 173
              {loading && (
174
                <StyledSpinnerContainer>
175
                  <Spinner />
176
                </StyledSpinnerContainer>
177
              )}
178
            </div>
179
            {/* <!--product-feed-tab end--> */}
180
          </div>
181
        </div>
182
      </section>
183
      <AddGroupModal
184
        show={showAddGroupModal}
185
        onHide={handleShowAddGroupModal}
186
        fetchGroups={fetchGroups}
187
        groupTypes={groupTypes}
188
        industries={industries}
189
      />
190
    </React.Fragment>
191
  );
192
};
193
 
194
// const mapStateToProps = (state) => ({});
195
 
196
const mapDispatchToProps = {
197
  addNotification: (notification) => addNotification(notification),
198
};
199
 
200
export default connect(null, mapDispatchToProps)(MyGroups);