Display messages

This commit is contained in:
Franck Royer 2021-07-28 13:21:03 +10:00
parent 8a7f2401ea
commit 7c715d8fa7
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
1 changed files with 25 additions and 1 deletions

View File

@ -4,9 +4,17 @@ import * as React from 'react';
const ContentTopic = `/relay-guide/1/chat/proto`;
const initialMessageState = {
messages: [],
};
function App() {
const [waku, setWaku] = React.useState(undefined);
const [wakuStatus, setWakuStatus] = React.useState('NotStarted');
const [messagesState, dispatchMessages] = React.useReducer(
reduceMessages,
initialMessageState
);
React.useEffect(() => {
if (!!waku) return;
@ -25,7 +33,7 @@ function App() {
// Need to keep the same reference around to add and delete from relay observer
const processIncomingMessage = React.useCallback((wakuMessage) => {
console.log('Message Received', wakuMessage);
dispatchMessages({ type: 'Add', message: wakuMessage.payloadAsUtf8 });
}, []);
React.useEffect(() => {
@ -53,6 +61,11 @@ function App() {
<button onClick={sendMessageOnClick} disabled={wakuStatus !== 'Ready'}>
Send Message
</button>
<ul>
{messagesState.messages.map((msg) => {
return <li>{msg}</li>;
})}
</ul>
</header>
</div>
);
@ -69,3 +82,14 @@ async function sendMessage(message, waku) {
const wakuMessage = await WakuMessage.fromUtf8String(message, ContentTopic);
await waku.relay.send(wakuMessage);
}
function reduceMessages(state, action) {
switch (action.type) {
case 'Add':
const messages = state.messages.slice();
messages.push(action.message);
return { ...state, messages };
default:
return state;
}
}