mirror of
https://github.com/logos-messaging/js-waku.git
synced 2026-01-07 16:23:09 +00:00
We can send messages!
This commit is contained in:
parent
35db88a63e
commit
dc7b9ba5ae
@ -2,15 +2,19 @@ import React from 'react';
|
|||||||
import './App.css';
|
import './App.css';
|
||||||
import Room from './Room';
|
import Room from './Room';
|
||||||
import WakuMock from './WakuMock';
|
import WakuMock from './WakuMock';
|
||||||
|
import { WakuContext } from './WakuContext';
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
}
|
}
|
||||||
|
|
||||||
interface State {
|
interface State {
|
||||||
messages: string[]
|
messages: string[],
|
||||||
|
waku?: WakuMock
|
||||||
}
|
}
|
||||||
|
|
||||||
class App extends React.Component<Props, State> {
|
class App extends React.Component<Props, State> {
|
||||||
|
waku?: WakuMock;
|
||||||
|
|
||||||
constructor(props: Props) {
|
constructor(props: Props) {
|
||||||
super(props);
|
super(props);
|
||||||
|
|
||||||
@ -18,20 +22,25 @@ class App extends React.Component<Props, State> {
|
|||||||
messages: []
|
messages: []
|
||||||
};
|
};
|
||||||
|
|
||||||
WakuMock.create().then((wakuMock) => {
|
WakuMock.create().then((wakuMock) => {
|
||||||
wakuMock.on('message',(message)=>{
|
|
||||||
const messages = this.state.messages.slice();
|
this.setState({ waku: wakuMock, messages: this.state.messages });
|
||||||
messages.push(message.message);
|
|
||||||
this.setState({messages});
|
wakuMock.on('message', (message) => {
|
||||||
})
|
const messages = this.state.messages.slice();
|
||||||
});
|
messages.push(message.message);
|
||||||
|
this.setState({ messages });
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
return (
|
return (
|
||||||
<div className='App'>
|
<div className='App'>
|
||||||
<div className='chat-room'>
|
<div className='chat-room'>
|
||||||
<Room lines={this.state.messages} />
|
<WakuContext.Provider value={{ waku: this.state.waku }}>
|
||||||
|
<Room lines={this.state.messages} />
|
||||||
|
</WakuContext.Provider>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
import Send from './Send';
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
lines: string[]
|
lines: string[]
|
||||||
@ -7,11 +8,15 @@ interface Props {
|
|||||||
interface State {
|
interface State {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export default class Room extends React.Component<Props, State> {
|
export default class Room extends React.Component<Props, State> {
|
||||||
render() {
|
render() {
|
||||||
return (
|
return (
|
||||||
<div className='room'>
|
<div>
|
||||||
{this.renderLines(this.props.lines)}
|
<Send />
|
||||||
|
<div className='room'>
|
||||||
|
{this.renderLines(this.props.lines)}
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
21
web-chat/src/Send.tsx
Normal file
21
web-chat/src/Send.tsx
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import { useWaku } from './WakuContext';
|
||||||
|
|
||||||
|
interface Props {
|
||||||
|
}
|
||||||
|
|
||||||
|
interface State {
|
||||||
|
}
|
||||||
|
|
||||||
|
const Send = () => {
|
||||||
|
const { waku } = useWaku();
|
||||||
|
|
||||||
|
return (
|
||||||
|
<button className='sendButton' onClick={async () => {
|
||||||
|
await waku!.send('Hello world!');
|
||||||
|
}}>
|
||||||
|
</button>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default Send;
|
||||||
10
web-chat/src/WakuContext.ts
Normal file
10
web-chat/src/WakuContext.ts
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
import { createContext, useContext } from 'react';
|
||||||
|
import WakuMock from './WakuMock';
|
||||||
|
|
||||||
|
export type WakuContextType = {
|
||||||
|
waku?: WakuMock;
|
||||||
|
// setWaku: (waku: WakuMock) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const WakuContext = createContext<WakuContextType>({ waku: undefined });
|
||||||
|
export const useWaku = () => useContext(WakuContext);
|
||||||
Loading…
x
Reference in New Issue
Block a user