Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

Rev Autor Línea Nro. Línea
888 stevensc 1
import React, { useEffect, useState } from 'react'
1 www 2
import ChatMail from "./inmail/inmail"
3
import Sidebar from './inmail/sidebar'
776 stevensc 4
import { axios } from '../utils'
1 www 5
import { Modal } from 'react-bootstrap'
6
 
7
export default props => {
888 stevensc 8
    const [conversations, setConversations] = useState([])
9
    const [showConversations, setShowConversations] = useState(false)
10
    const [selectedConversation, setSelectedConversation] = useState(null)
889 stevensc 11
    const [page, setPage] = useState({ current: 1, last: 1 })
888 stevensc 12
    const [messages, setMessages] = useState([])
13
    const [isGettingMessages, setIsGettingMessages] = useState(false)
14
    const [visible, setVisible] = useState(false)
15
    const [searchActive, setSearchActive] = useState(false)
16
    const [inmailPersons, setInmailPersons] = useState([])
17
    const [search, setSearch] = useState('')
1 www 18
    const load = async () => {
19
        try {
20
            const res = await axios.get(window.location.href)
776 stevensc 21
            if (res.data.success) {
1 www 22
                setConversations(res.data.data)
23
            }
24
        } catch (error) {
25
            console.log('>>: error > ', error)
26
        }
27
    }
888 stevensc 28
    useEffect(() => {
776 stevensc 29
        const interval = setInterval(() => {
1 www 30
            load()
31
        }, 3000)
32
        return () => clearInterval(interval)
33
    }, [])
34
 
889 stevensc 35
    const getMessages = async (conversation = selectedConversation, _page = page.current, justPaginate = false) => {
1 www 36
        try {
860 stevensc 37
            const res = await axios.get(conversation.messages_link + '?page=' + _page)
776 stevensc 38
            if (justPaginate) {
849 stevensc 39
                setMessages([...messages, ...res.data.data])
776 stevensc 40
            } else {
1 www 41
                setMessages(res.data.data || [])
42
            }
860 stevensc 43
            if (res.data.pagination?.last !== page.last) {
1 www 44
                setPage({
776 stevensc 45
                    ...page,
1 www 46
                    last: res.data.pagination.last
47
                })
48
            }
49
        } catch (error) {
50
            console.log('>>: error > ', error)
776 stevensc 51
        } finally {
52
            if (isGettingMessages) {
1 www 53
                setIsGettingMessages(false)
54
            }
55
        }
56
    }
727 stevensc 57
 
1 www 58
    const handleChangeConversation = async conversation => {
889 stevensc 59
        setPage({current: 1, last:1})
1 www 60
        setSelectedConversation(conversation)
889 stevensc 61
        getMessages(conversation, page.current)
1 www 62
    }
63
 
64
    const handleSend = async message => {
776 stevensc 65
        if (selectedConversation) {
1 www 66
            try {
67
                const params = {
68
                    message
69
                }
70
                const formData = new FormData();
71
                Object.entries(params).map(([key, value]) => {
72
                    formData.append(key, value);
73
                });
74
                const res = await axios.post(selectedConversation.send_link, formData)
777 stevensc 75
                setMessages([res.data.data, ...messages])
1 www 76
            } catch (error) {
77
                console.log('>>: err0r > ', error)
885 stevensc 78
            } finally {
889 stevensc 79
                getMessages(selectedConversation, 1)
1 www 80
            }
81
        }
82
    }
83
 
84
    const getMoreMessages = () => {
888 stevensc 85
        console.log('Try');
86
        console.log(selectedConversation);
890 stevensc 87
        console.log(page);
888 stevensc 88
        if (page.last > page.current) {
89
            let { current } = page
90
            current += 1
91
            if (!isGettingMessages) {
92
                setIsGettingMessages(true)
93
                setPage({
94
                    ...page,
95
                    current
96
                })
97
                getMessages(selectedConversation, current, true)
98
            }
884 stevensc 99
        }
1 www 100
    }
888 stevensc 101
 
1 www 102
    const onClose = () => {
103
        setVisible(false)
104
    }
105
 
106
    const handleChange = async e => {
107
        const value = e.target.value
108
        setSearch(value)
109
        try {
776 stevensc 110
            const res = await axios.get('/helpers/search-people?search=' + value)
111
            if (res.data.success) {
1 www 112
                setInmailPersons(res.data.data)
113
            }
114
        } catch (error) {
115
            console.log('>>: error > ', error)
116
        }
117
    }
118
 
119
    const handleInmailPerson = uuid => {
120
        console.log('>>: uuid > ', uuid)
121
        onClose()
776 stevensc 122
        window.location.href = '/inmail/' + uuid
1 www 123
        setTimeout(() => {
124
            window.location.reload()
125
        }, 1000)
126
    }
127
 
844 stevensc 128
    const handleShowConversation = (arg) => {
129
        setShowConversations(arg)
130
    }
1 www 131
 
776 stevensc 132
    return (
1 www 133
        <>
134
            <Modal
135
                show={visible}
136
                onHide={onClose}
137
            >
138
                <div className="card">
139
                    <div className="card-body">
140
                        <h3 className="card-title font-weight-bold">Crear sala de Inmail</h3>
141
                        <div class="form-group">
142
                            <label for="search-people">Email address</label>
143
                            <input
144
                                type="email"
145
                                className="form-control"
146
                                id="search-people"
147
                                aria-describedby="Buscador de personas"
148
                                placeholder="Escribe el nombre de la persona"
149
                                onChange={handleChange}
150
                            />
151
                        </div>
152
                        <div
153
                            className='container'
154
                        >
155
                            {
156
                                inmailPersons.map(element => {
776 stevensc 157
                                    return (
1 www 158
                                        <div
159
                                            className='row'
160
                                            key={element.value}
161
                                        >
162
                                            <div
163
                                                className='col-8'
164
                                            >
165
                                                <p> {element.text} </p>
166
                                            </div>
167
                                            <div
168
                                                className='col-4'
169
                                            >
170
                                                <button
171
                                                    className='btn btn-primary'
172
                                                    onClick={() => handleInmailPerson(element.value)}
173
                                                >
174
                                                    <i className='fa fa-check' />
175
                                                </button>
176
                                            </div>
177
                                        </div>
178
                                    )
179
                                })
180
                            }
181
                        </div>
182
                        <a href="#" onClick={onClose} className="btn btn-danger">
183
                            Cerrar
184
                        </a>
185
                    </div>
186
                </div>
187
            </Modal>
188
            <section className="messages-page">
833 stevensc 189
                <div className="container msgs-container">
1 www 190
                    <div
831 stevensc 191
                        className='w-100'
1 www 192
                    >
831 stevensc 193
                        <h1>Sala de Intercambio Profesional</h1>
1 www 194
                    </div>
195
                    <div className="messages-sec">
196
                        <div className="row">
804 stevensc 197
                            <div className="col-lg-4 col-md-12 inmail_conversations-list">
1 www 198
                                <div className="msgs-list">
199
                                    <div
821 stevensc 200
                                        className="msg-title"
1 www 201
                                    >
202
                                        <h3>Personas</h3>
821 stevensc 203
 
204
                                        <div className="msgs_icons-container">
847 stevensc 205
                                            {
206
                                                !showConversations
207
                                                &&
208
                                                <i
209
                                                    className='fa fa-search icon'
210
                                                    onClick={() => setSearchActive(!searchActive)}
211
                                                />
212
                                            }
821 stevensc 213
                                            <i
828 stevensc 214
                                                className='fa fa-plus icon'
821 stevensc 215
                                                onClick={() => setVisible(true)}
216
                                            />
217
                                        </div>
1 www 218
                                    </div>
219
                                    <Sidebar
842 stevensc 220
                                        click={handleChangeConversation}
1 www 221
                                        conversations={conversations}
222
                                        selectedConversation={selectedConversation}
821 stevensc 223
                                        searchActive={searchActive}
843 stevensc 224
                                        showConversation={showConversations}
845 stevensc 225
                                        handleShowConversation={handleShowConversation}
1 www 226
                                    />
227
                                </div>
228
                            </div>
843 stevensc 229
                            <div className={`col-lg-8 col-md-12 pd-right-none pd-left-none ${!showConversations && 'msgs-hide'}`}>
1 www 230
                                <ChatMail
888 stevensc 231
                                    getMoreMessages={() => getMoreMessages()}
1 www 232
                                    backendVars={props.backendVars}
233
                                    onSend={text => handleSend(text)}
234
                                    messages={messages}
235
                                    selectedConversation={selectedConversation}
844 stevensc 236
                                    handleShowConversation={handleShowConversation}
1 www 237
                                />
238
                            </div>
239
                        </div>
240
                    </div>
241
                </div>
242
            </section>
243
        </>
244
    )
245
}