Proyectos de Subversion LeadersLinked - SPA

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1363 stevensc 1
import React from 'react'
5 stevensc 2
import { useDispatch } from 'react-redux'
1363 stevensc 3
import { Avatar, IconButton } from '@mui/material'
1364 stevensc 4
import { AddRounded, ArrowBackIos } from '@mui/icons-material'
5 stevensc 5
import styled from 'styled-components'
6
 
1363 stevensc 7
import { axios } from '../../utils'
8
import { addNotification } from '../../redux/notification/notification.actions'
9
import useFetch from '../../hooks/useFetch'
10
 
5 stevensc 11
import Spinner from '../UI/Spinner'
12
import WidgetLayout from '../widgets/WidgetLayout'
13
 
14
const ContactContainer = styled(WidgetLayout)`
15
  padding: 1rem;
16
  position: relative;
17
  flex-grow: 1;
18
  ul {
19
    display: flex;
20
    flex-direction: column;
21
    gap: 0.5rem;
22
    max-height: 300px;
23
    overflow: auto;
24
    margin-top: 1rem;
25
  }
26
`
27
 
28
const ContactTemplate = styled.div`
29
  display: flex;
30
  align-items: center;
31
  justify-content: space-between;
32
  gap: 0.5rem;
33
  .info {
34
    display: flex;
35
    align-items: center;
36
    gap: 0.5rem;
37
  }
38
  h3 {
39
    font-size: 1rem;
40
    color: var(--subtitle-color);
41
    font-weight: 600;
42
  }
43
`
44
 
45
const AddMember = ({ group, changeTab }) => {
46
  const { url_add_user_to_group, url_get_contacts_availables_for_group } = group
1363 stevensc 47
  const {
48
    data: contacts,
49
    isLoading,
50
    refetch
51
  } = useFetch(url_get_contacts_availables_for_group, [])
5 stevensc 52
  const dispatch = useDispatch()
53
 
653 stevensc 54
  const handleAddMember = (id) => {
5 stevensc 55
    const formData = new FormData()
56
    formData.append('uid', id)
57
 
58
    axios
59
      .post(url_add_user_to_group, formData)
652 stevensc 60
      .then(({ data: responseData }) => {
61
        const { data, success } = responseData
62
 
5 stevensc 63
        if (!success) {
652 stevensc 64
          throw new Error(data)
5 stevensc 65
        }
66
 
1365 stevensc 67
        dispatch(addNotification({ style: 'success', msg: 'Usuario agregado' }))
1363 stevensc 68
        refetch()
5 stevensc 69
      })
70
      .catch((err) => {
655 stevensc 71
        dispatch(addNotification({ style: 'danger', msg: err.message }))
5 stevensc 72
      })
73
  }
74
 
75
  return (
76
    <ContactContainer>
77
      <IconButton onClick={() => changeTab('DEFAULT')}>
1364 stevensc 78
        <ArrowBackIos />
5 stevensc 79
      </IconButton>
80
 
81
      <ul>
82
        {contacts.map((member) => (
83
          <li key={member.id}>
84
            <AddMember.Item
85
              {...member}
86
              onClick={() => handleAddMember(member.id)}
87
            />
88
          </li>
89
        ))}
90
      </ul>
1365 stevensc 91
 
1363 stevensc 92
      {isLoading && <Spinner />}
5 stevensc 93
    </ContactContainer>
94
  )
95
}
96
 
97
const Item = ({ image, name, onClick }) => {
98
  return (
99
    <ContactTemplate>
652 stevensc 100
      <div className='info'>
5 stevensc 101
        <Avatar src={image} alt={`user_${name}-image`} />
102
        <h3>{name}</h3>
103
      </div>
104
      <IconButton onClick={onClick}>
105
        <AddRounded />
106
      </IconButton>
107
    </ContactTemplate>
108
  )
109
}
110
 
111
AddMember.Item = Item
112
 
113
export default AddMember