-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·223 lines (214 loc) · 6.16 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import React, { useRef } from 'react'
import Router from 'next/router'
import gql from 'graphql-tag'
import { Mutation } from 'react-apollo'
import NProgress from 'nprogress'
import { useQuery, useSubscription } from 'react-apollo-hooks'
import { withSnackbar } from 'notistack'
import { withRouter } from 'next/router'
//MUI
import withStyles from '@material-ui/core/styles/withStyles'
import { List, ListItem } from '@material-ui/core'
import navbarsStyle from '../../static/jss/material-kit-pro-react/views/componentsSections/navbarsStyle.jsx'
//Q&M
import User from '../Queries/User'
// styled components
import Button from '../../styledComponents/CustomButtons/Button.jsx'
import Header from '../../styledComponents/Header/Header.jsx'
import CustomDropdown from '../../styledComponents/CustomDropdown/CustomDropdown.jsx'
Router.onRouteChangeComplete = () => {
NProgress.done(true)
}
const SIGNOUT_MUTATION = gql`
mutation SIGNOUT_MUTATION {
signout {
message
}
}
`
const MY_CHAT_SUBSCRIPTION = gql`
subscription($id: String!) {
myChat(id: $id) {
mutation
node {
id
users {
id
firstName
img {
id
img_url
default
}
}
typing {
id
firstName
}
messages {
id
text
seen
createdAt
from {
id
firstName
img {
id
img_url
default
}
}
updatedAt
}
}
}
}
`
const MY_MESSAGE_SUBSCRIPTION = gql`
subscription($id: String!) {
myMessages(id: $id) {
mutation
node {
chat {
id
}
id
text
seen
createdAt
from {
id
firstName
img {
id
img_url
default
}
}
updatedAt
}
}
}
`
const Nav = ({ classes, color, router, enqueueSnackbar, user }) => {
const audioRef = useRef(null)
useSubscription(MY_CHAT_SUBSCRIPTION, {
variables: { id: user.id }
})
const subscription = useSubscription(MY_MESSAGE_SUBSCRIPTION, {
variables: { id: user.id },
onSubscriptionData: async ({ client, subscriptionData }) => {
//console.log(subscriptionData);
let from = subscriptionData.data.myMessages.node.from
if (from.id !== user.id) {
enqueueSnackbar(`New message from ${from.firstName}`, {
preventDuplicate: true,
anchorOrigin: {
vertical: 'bottom',
horizontal: 'left'
},
action: (
<Button
simple
onClick={() => {
Router.push(
{
pathname: router.pathname === '/' ? '/home' : router.pathname,
query: {
slug: router.query.slug,
user: from.id
}
},
router.query.slug
? `${router.pathname}/${router.query.slug}/user/${from.id}`
: router.pathname === '/'
? `/user/${from.id}`
: `${router.pathname}/user/${from.id}`,
{ shallow: true },
{ scroll: false }
)
}}
size="small"
>
{'View'}
</Button>
)
})
try {
await audioRef.current.play()
} catch (e) {
console.log(e.message)
}
}
}
})
return (
<User>
{({ data: { currentUser } }) => {
return (
<div>
<audio ref={audioRef} src="/static/quiet-knock.mp3" />
<Header
color={color}
fixed={color === 'transparent'}
changeColorOnScroll={
color === 'transparent'
? {
height: 300,
color: 'warning'
}
: null
}
links={
<List className={classes.list + ' ' + classes.mlAuto}>
<Mutation mutation={SIGNOUT_MUTATION} onCompleted={() => Router.push('/joinus')}>
{(signout, { client }) => {
return (
<ListItem
id="button"
style={{ marginLeft: '10px' }}
className={classes.listItem}
>
<CustomDropdown
left
caret={false}
dropdownHeader={currentUser && currentUser.firstName}
buttonText={
<img
src={
currentUser && currentUser.img.find(img => img.default).img_url
}
className={classes.img + ' ' + classes.imageProfile}
alt="profile"
/>
}
buttonProps={{
className: classes.navLink + ' ' + classes.imageDropdownButton,
color: 'transparent'
}}
dropdownList={['Dashboard', 'Sign out']}
onClick={e => {
if (e === 'Sign out') {
signout()
client.clearStore()
}
if (e === 'Dashboard') {
Router.push('/profile')
}
}}
/>
</ListItem>
)
}}
</Mutation>
</List>
}
/>
</div>
)
}}
</User>
)
}
export default withSnackbar(withRouter(withStyles(navbarsStyle)(Nav)))