Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 1158 | Ir a la última revisión | | 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";
6
import {axios} from "../../../../utils";
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;
55
         (resData);
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
 
72
  const fetchGroups = async (searchParam='') => {
73
    setLoading(true);
74
    await axios
75
      .get(
76
        "/group/my-groups?search="+searchParam)
77
      .then((response) => {
78
        const resData = response.data;
79
         (resData);
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
            >
152
              {Groups.map(
153
                (
154
                  { image, link_delete, link_edit, link_view, name, privacy },
155
                  id
156
                ) => (
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
              {loading && (
170
                <StyledSpinnerContainer>
171
                  <Spinner />
172
                </StyledSpinnerContainer>
173
              )}
174
            </div>
175
            {/* <!--product-feed-tab end--> */}
176
          </div>
177
        </div>
178
      </section>
179
      <AddGroupModal
180
        show={showAddGroupModal}
181
        onHide={handleShowAddGroupModal}
182
        fetchGroups={fetchGroups}
183
        groupTypes={groupTypes}
184
        industries={industries}
185
      />
186
    </React.Fragment>
187
  );
188
};
189
 
190
// const mapStateToProps = (state) => ({});
191
 
192
const mapDispatchToProps = {
193
  addNotification: (notification) => addNotification(notification),
194
};
195
 
196
export default connect(null, mapDispatchToProps)(MyGroups);