Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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