forked from bengesoff/ipcortex-dynamic-contacts-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
61 lines (57 loc) · 1.66 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import ContactList from './components/ContactList'
import {addContact, updateContact} from './actionCreators'
import React, { Component } from 'react'
import ReactDOM from 'react-dom'
import { createStore } from 'redux'
import { Provider } from 'react-redux'
// Reducer
function contacts_reducer(state = {contacts:[]}, action) {
switch(action.type) {
case 'NEW_CONTACT':
return Object.assign({}, state, {
contacts: [
...state.contacts,
action.contact
]
})
case 'UPDATE_CONTACT':
return Object.assign({}, state, {
contacts: state.contacts.map(contact => {
if (contact.cid === action.contact.cid) return action.contact
else return contact
})
})
default:
return state
}
return state
}
// Initialise API
IPCortex.PBX.Auth.setHost('https://pabx.hostname')
IPCortex.PBX.Auth.login().then(() => {
console.log('login success')
IPCortex.PBX.startFeed().then(() => {
console.log('feed started')
// Store
let store = createStore(contacts_reducer)
// Dispatch actions in response to Contact events
IPCortex.PBX.contacts.forEach(contact => {
contact.addListener('update', _contact => {
store.dispatch(updateContact(_contact.cID, _contact.name, _contact.blf))
})
})
IPCortex.Types.Contact.addListener('new', contact => {
store.dispatch(addContact(contact.cID, contact.name, contact.blf))
})
ReactDOM.render(
<Provider store={store}>
<ContactList />
</Provider>,
document.getElementById('root')
)
}, () => {
console.log('starting feed failed')
})
}, () => {
console.log('login failed')
})