Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
6770 stevensc 1
import React, { useState, useRef, useEffect } from 'react'
2
import { axios } from '../../utils'
3
import { useSelector } from 'react-redux'
4
import parse from 'html-react-parser'
5
import AddIcon from '@mui/icons-material/Add'
6
import IconButton from '@mui/material/IconButton'
7
import EditIcon from '@mui/icons-material/Edit'
8
import DeleteIcon from '@mui/icons-material/Delete'
9
 
10
import Spinner from '../UI/Spinner'
11
import EmptySection from '../UI/EmptySection'
12
import EducationModal from './EducationModal'
13
import ConfirmModal from '../modals/ConfirmModal'
14
 
15
const EducationsList = ({ educations, id, isEdit }) => {
16
  const [settedEducations, setSettedEducations] = useState(educations)
17
  const [settedDescription, setSettedDescription] = useState('')
18
  const [isModalOpen, setIsModalOpen] = useState(false)
19
  const [loading, setLoading] = useState(false)
20
  const labels = useSelector(({ intl }) => intl.labels)
21
  const postUrl = useRef('')
22
 
23
  const addEducation = () => {
24
    postUrl.current = `/profile/my-profiles/education/${id}/operation/add`
25
    setSettedDescription('')
26
    setIsModalOpen(true)
27
  }
28
 
29
  const handleDelete = async (delete_link) => {
30
    setLoading(true)
31
    axios
32
      .post(delete_link)
33
      .then(({ data: response }) => {
34
        if (response.success) setSettedEducations(response.data)
35
      })
36
      .finally(() => setLoading(false))
37
  }
38
 
39
  const handleEdit = async (edit_link) => {
40
    postUrl.current = edit_link
41
    const currentDescription = settedEducations.find(
42
      (education) => education.link_edit === edit_link
43
    )
44
    setSettedDescription(currentDescription.description)
45
    setIsModalOpen(true)
46
  }
47
 
48
  useEffect(() => {
49
    setSettedEducations(educations)
50
  }, [educations])
51
 
52
  return (
53
    <>
54
      <div className="profile-attr">
55
        <div className="profile-attr-header">
56
          <h2>{labels.education}</h2>
57
          {isEdit && (
58
            <IconButton onClick={addEducation}>
59
              <AddIcon />
60
            </IconButton>
61
          )}
62
        </div>
63
        {loading && <Spinner />}
64
        {settedEducations?.length ? (
65
          settedEducations.map((education) => (
66
            <EducationsList.Item
67
              key={education.link_edit}
68
              education={education}
69
              onDelete={handleDelete}
70
              onEdit={handleEdit}
71
              isEdit={isEdit}
72
            />
73
          ))
74
        ) : (
75
          <EmptySection align="left" message={labels.empty} />
76
        )}
77
      </div>
78
      <EducationModal
79
        closeModal={() => setIsModalOpen(false)}
80
        postUrl={postUrl.current}
81
        setEducations={(newEducations) => setSettedEducations(newEducations)}
82
        settedDescription={settedDescription}
83
        show={isModalOpen}
84
      />
85
    </>
86
  )
87
}
88
 
89
const Education = ({ education, onDelete, onEdit, isEdit }) => {
90
  const [isShowModal, setIsShowModal] = useState(false)
91
 
92
  const toggleConfirmModal = () => {
93
    setIsShowModal(!isShowModal)
94
  }
95
 
96
  return (
97
    <>
98
      <div className="education-item">
99
        <div className="education-item-header">
100
          <h3>{education.degree}</h3>
101
          {isEdit && (
102
            <div className="icon-buttons-group">
103
              <IconButton onClick={() => onEdit(education.link_edit)}>
104
                <EditIcon />
105
              </IconButton>
106
              <IconButton onClick={toggleConfirmModal}>
107
                <DeleteIcon />
108
              </IconButton>
109
            </div>
110
          )}
111
        </div>
112
        <h4>{education.university}</h4>
113
        <p>{`${education.from_year} - ${
114
          education.to_year ? education.to_year : 'Actualidad'
115
        }`}</p>
116
        {education.field_of_study && <p>{education.field_of_study}</p>}
117
        <p>{education.formatted_address}</p>
118
        {education.description && parse(education.description)}
119
      </div>
120
      <ConfirmModal
121
        show={isShowModal}
122
        onClose={toggleConfirmModal}
123
        onAccept={() => onDelete(education.link_delete)}
124
      />
125
    </>
126
  )
127
}
128
 
129
EducationsList.Item = Education
130
 
131
export default EducationsList