Toggle navigation
Toggle navigation
This project
Loading...
Sign in
2020-1-capstone-design1
/
Triz_Project1
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
1
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
sdy
2020-06-03 23:02:00 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
057cfba3485040c85da270d7fa7726d6aee74772
057cfba3
1 parent
154364f7
update subscription
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
27 additions
and
5 deletions
front/src/Routes/Chat/ChatContainer.js
front/src/Routes/Chat/ChatContainer.js
View file @
057cfba
import
React
from
"react"
;
import
React
,
{
useEffect
,
useState
}
from
"react"
;
import
{
useMutation
,
useQuery
}
from
"@apollo/react-hooks"
;
import
ChatPresenter
from
"./ChatPresenter"
;
import
{
withRouter
}
from
"react-router-dom"
;
...
...
@@ -12,6 +12,7 @@ import useInput from "../../Hooks/useInput";
import
{
toast
}
from
"react-toastify"
;
export
default
withRouter
(({
location
})
=>
{
const
[
messageArr
,
setMessageArr
]
=
useState
([]);
const
{
pathname
}
=
location
;
const
roomName
=
pathname
.
slice
(
1
,
pathname
.
length
);
const
[
createMsg
]
=
useMutation
(
CREATE_MESSAGE
);
...
...
@@ -30,7 +31,12 @@ export default withRouter(({ location }) => {
roomNum
=
Number
(
roomId
);
}
const
{
subscribeToMore
,
data
:
messageList
}
=
useQuery
(
SEE_ALL_MESSAGE
,
{
const
{
subscribeToMore
,
data
:
messageList
,
error
:
msgQueryError
,
loading
:
msgQueryyLoading
,
}
=
useQuery
(
SEE_ALL_MESSAGE
,
{
variables
:
{
roomId
:
roomNum
},
});
...
...
@@ -43,12 +49,27 @@ export default withRouter(({ location }) => {
messageArray
=
messageList
;
}
let
resultObj
=
subscribeToMore
({
useEffect
(()
=>
{
if
(
msgQueryError
)
{
console
.
error
(
msgQueryError
);
}
if
(
messageArr
)
{
setMessageArr
(
messageArr
.
seeAllMessage
);
}
},
[
msgQueryyLoading
]);
const
subscribeToNewMessage
=
()
=>
{
subscribeToMore
({
document
:
SUBSCRIPTION_MSG
,
updateQuery
:
(
prev
,
{
subscriptionData
})
=>
{
if
(
!
subscriptionData
.
data
)
return
prev
;
updateQuery
:
(
currentMessages
,
{
subscriptionData
})
=>
{
if
(
!
subscriptionData
.
data
)
return
currentMessages
;
const
newMessage
=
subscriptionData
.
data
.
subMessage
;
const
updateMessages
=
currentMessages
.
seeAllMessage
.
concat
(
newMessage
);
setMessageArr
(
updateMessages
);
return
{
seeAllMessage
:
updateMessages
};
},
});
};
const
onSubmit
=
async
(
e
)
=>
{
e
.
preventDefault
();
...
...
@@ -75,6 +96,7 @@ export default withRouter(({ location }) => {
message
=
{
message
}
onSubmit
=
{
onSubmit
}
messageArray
=
{
messageArray
}
subscribeToNewMessage
=
{
subscribeToNewMessage
}
/
>
);
});
...
...
Please
register
or
login
to post a comment