feat: assign userId
This commit is contained in:
parent
09d039d806
commit
74f11dae2a
@ -1,17 +1,23 @@
|
|||||||
import React from 'react'
|
import { useState } from 'react'
|
||||||
import { Routes, Route } from 'react-router-dom'
|
import { Routes, Route } from 'react-router-dom'
|
||||||
|
import { v4 as uuid } from 'uuid'
|
||||||
|
|
||||||
import { Home } from './pages/Home/'
|
import { Home } from './pages/Home/'
|
||||||
import { PublicRoom } from './pages/PublicRoom/'
|
import { PublicRoom } from './pages/PublicRoom/'
|
||||||
|
|
||||||
function Bootstrap() {
|
function Bootstrap() {
|
||||||
|
const [userId] = useState(uuid())
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="Chitchatter">
|
<div className="Chitchatter">
|
||||||
<Routes>
|
<Routes>
|
||||||
{['/', '/index.html'].map(path => (
|
{['/', '/index.html'].map(path => (
|
||||||
<Route key={path} path={path} element={<Home />} />
|
<Route key={path} path={path} element={<Home />} />
|
||||||
))}
|
))}
|
||||||
<Route path="/public/:roomId" element={<PublicRoom />} />
|
<Route
|
||||||
|
path="/public/:roomId"
|
||||||
|
element={<PublicRoom userId={userId} />}
|
||||||
|
/>
|
||||||
</Routes>
|
</Routes>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
|
@ -5,6 +5,8 @@ import { MemoryRouter as Router, Route, Routes } from 'react-router-dom'
|
|||||||
|
|
||||||
import { Room } from './'
|
import { Room } from './'
|
||||||
|
|
||||||
|
const stubUserId = 'user-id'
|
||||||
|
|
||||||
const mockGetUuid = jest.fn()
|
const mockGetUuid = jest.fn()
|
||||||
const mockMessagedSender = jest.fn()
|
const mockMessagedSender = jest.fn()
|
||||||
|
|
||||||
@ -42,7 +44,7 @@ describe('Room', () => {
|
|||||||
test('is available', () => {
|
test('is available', () => {
|
||||||
render(
|
render(
|
||||||
<RouteStub>
|
<RouteStub>
|
||||||
<Room />
|
<Room userId={stubUserId} />
|
||||||
</RouteStub>
|
</RouteStub>
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
@ -50,7 +52,7 @@ describe('Room', () => {
|
|||||||
test('send button is disabled', () => {
|
test('send button is disabled', () => {
|
||||||
render(
|
render(
|
||||||
<RouteStub>
|
<RouteStub>
|
||||||
<Room />
|
<Room userId={stubUserId} />
|
||||||
</RouteStub>
|
</RouteStub>
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -61,7 +63,7 @@ describe('Room', () => {
|
|||||||
test('inputting text enabled send button', () => {
|
test('inputting text enabled send button', () => {
|
||||||
render(
|
render(
|
||||||
<RouteStub>
|
<RouteStub>
|
||||||
<Room />
|
<Room userId={stubUserId} />
|
||||||
</RouteStub>
|
</RouteStub>
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -74,7 +76,7 @@ describe('Room', () => {
|
|||||||
test('sending a message clears the text input', () => {
|
test('sending a message clears the text input', () => {
|
||||||
render(
|
render(
|
||||||
<RouteStub>
|
<RouteStub>
|
||||||
<Room />
|
<Room userId={stubUserId} />
|
||||||
</RouteStub>
|
</RouteStub>
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -88,7 +90,10 @@ describe('Room', () => {
|
|||||||
test('message is sent to peer', () => {
|
test('message is sent to peer', () => {
|
||||||
render(
|
render(
|
||||||
<RouteStub>
|
<RouteStub>
|
||||||
<Room getUuid={mockGetUuid.mockImplementation(() => 'abc123')} />
|
<Room
|
||||||
|
getUuid={mockGetUuid.mockImplementation(() => 'abc123')}
|
||||||
|
userId={stubUserId}
|
||||||
|
/>
|
||||||
</RouteStub>
|
</RouteStub>
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -97,6 +102,7 @@ describe('Room', () => {
|
|||||||
userEvent.type(textInput, 'hello')
|
userEvent.type(textInput, 'hello')
|
||||||
userEvent.click(sendButton)
|
userEvent.click(sendButton)
|
||||||
expect(mockMessagedSender).toHaveBeenCalledWith({
|
expect(mockMessagedSender).toHaveBeenCalledWith({
|
||||||
|
authorId: stubUserId,
|
||||||
text: 'hello',
|
text: 'hello',
|
||||||
timeSent: 100,
|
timeSent: 100,
|
||||||
id: 'abc123',
|
id: 'abc123',
|
||||||
|
@ -13,9 +13,11 @@ import { UnsentMessage, ReceivedMessage } from 'models/chat'
|
|||||||
export interface RoomProps {
|
export interface RoomProps {
|
||||||
appId?: string
|
appId?: string
|
||||||
getUuid?: typeof uuid
|
getUuid?: typeof uuid
|
||||||
|
userId: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export function Room({
|
export function Room({
|
||||||
|
userId,
|
||||||
appId = `${encodeURI(window.location.origin)}_${process.env.REACT_APP_NAME}`,
|
appId = `${encodeURI(window.location.origin)}_${process.env.REACT_APP_NAME}`,
|
||||||
getUuid = uuid,
|
getUuid = uuid,
|
||||||
}: RoomProps) {
|
}: RoomProps) {
|
||||||
@ -45,7 +47,12 @@ export function Room({
|
|||||||
event: React.SyntheticEvent<HTMLFormElement>
|
event: React.SyntheticEvent<HTMLFormElement>
|
||||||
) => {
|
) => {
|
||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
sendMessage({ text: textMessage, timeSent: Date.now(), id: getUuid() })
|
sendMessage({
|
||||||
|
authorId: userId,
|
||||||
|
text: textMessage,
|
||||||
|
timeSent: Date.now(),
|
||||||
|
id: getUuid(),
|
||||||
|
})
|
||||||
setTextMessage('')
|
setTextMessage('')
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,9 +66,7 @@ export function Room({
|
|||||||
return (
|
return (
|
||||||
<div className="p-4">
|
<div className="p-4">
|
||||||
<Typography>Room ID: {roomId}</Typography>
|
<Typography>Room ID: {roomId}</Typography>
|
||||||
<Typography>
|
<Typography>Open this page in another tab.</Typography>
|
||||||
Open this page in another tab and open the console.
|
|
||||||
</Typography>
|
|
||||||
<form onSubmit={handleMessageSubmit} className="max-w-xl mt-8">
|
<form onSubmit={handleMessageSubmit} className="max-w-xl mt-8">
|
||||||
<FormControl fullWidth>
|
<FormControl fullWidth>
|
||||||
<TextField
|
<TextField
|
||||||
|
@ -2,6 +2,7 @@ export interface UnsentMessage {
|
|||||||
id: string
|
id: string
|
||||||
text: string
|
text: string
|
||||||
timeSent: number
|
timeSent: number
|
||||||
|
authorId: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ReceivedMessage extends UnsentMessage {
|
export interface ReceivedMessage extends UnsentMessage {
|
||||||
|
@ -1,5 +1,9 @@
|
|||||||
import { Room } from 'components/Room'
|
import { Room } from 'components/Room'
|
||||||
|
|
||||||
export function PublicRoom() {
|
interface PublicRoomProps {
|
||||||
return <Room />
|
userId: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export function PublicRoom({ userId }: PublicRoomProps) {
|
||||||
|
return <Room userId={userId} />
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user