Skip to content

Commit

Permalink
feat: store author details
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrunton committed Jan 1, 2023
1 parent 61aeafb commit 8c1ef6a
Show file tree
Hide file tree
Showing 33 changed files with 695 additions and 156 deletions.
20 changes: 18 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,25 @@ jobs:
- uses: actions/checkout@v3
- run: npm ci
- run: npm run test


e2e-test:
runs-on: ubuntu-latest
strategy:
matrix:
directory: ["services/api"]
defaults:
run:
working-directory: ${{ matrix.directory }}
steps:
- uses: actions/checkout@v3
- uses: rrainn/[email protected]
with:
port: 8001
- run: npm ci
- run: npm run test:e2e

build-api:
needs: [lint, test]
needs: [lint, test, e2e-test]
if: github.ref == 'refs/heads/develop'
uses: jbrunton/workflows/.github/workflows/build-image.yml@develop
with:
Expand Down
25 changes: 16 additions & 9 deletions client/src/components/messages/MessagesList.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
import { List, ListIcon, ListItem } from '@chakra-ui/react'
import React from 'react'
import { Message } from '../../data/messages'
import { Message, User } from '../../data/messages'
import { UserIcon } from '../icons/User'

export type MessagesListProps = {
messages: Message[]
data: {
messages: Message[]
authors: Record<string, User>
}
}

export const MessagesList: React.FC<MessagesListProps> = ({ messages }) => {
export const MessagesList: React.FC<MessagesListProps> = ({ data: { messages, authors } }) => {
return (
<List spacing={3}>
{messages.map((message) => (
<ListItem key={message.id}>
<ListIcon as={UserIcon} color='green.500' />
{message.content}
</ListItem>
))}
{messages.map((message) => {
const author = authors[message.authorId]
return (
<ListItem key={message.id}>
<ListIcon as={UserIcon} color='green.500' />
<span>{author?.name ?? 'Anon'}: </span>
{message.content}
</ListItem>
)
})}
</List>
)
}
4 changes: 2 additions & 2 deletions client/src/components/messages/MessagesWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const MessagesWidget: React.FC<MessagesWidgetProps> = ({ roomId }) => {
const accessToken = useAccessToken()
const inputRef = useRef<HTMLInputElement>(null)
const [content, setContent] = useState<string>('')
const { data: messages, isError } = useMessages(roomId, accessToken)
const { data, isError } = useMessages(roomId, accessToken)
const { mutate, isLoading } = usePostMessage(roomId, content, accessToken)
const [isSending, setIsSending] = useState<boolean>(false)

Expand All @@ -37,7 +37,7 @@ export const MessagesWidget: React.FC<MessagesWidgetProps> = ({ roomId }) => {
}
return (
<div>
{messages && !isError && <MessagesList messages={messages} />}
{data && !isError && <MessagesList data={data} />}
<Flex>
<Input
ref={inputRef}
Expand Down
16 changes: 13 additions & 3 deletions client/src/data/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ export type Message = {
id: string
content: string
time: number
authorId: string
}

export type User = {
id: string
name: string
picture: string
}

export const useMessages = (roomId: string, accessToken?: string) => {
Expand All @@ -16,10 +23,13 @@ export const useMessages = (roomId: string, accessToken?: string) => {
},
})
if (response.ok) {
const messages = await response.json()
return messages as Message[]
const data = await response.json()
return data as { messages: Message[]; authors: Record<string, User> }
}
return {
messages: [],
authors: {},
}
return []
}
return useQuery({
queryKey: [`messages/${roomId}`],
Expand Down
10 changes: 10 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,13 @@ services:
volumes:
- "./docker/dynamodb:/home/dynamodblocal/data"
working_dir: /home/dynamodblocal

dynamodb-test:
command: "-jar DynamoDBLocal.jar -inMemory -port 8001"
image: "amazon/dynamodb-local:latest"
container_name: dynamodb-test
ports:
- "8001:8001"
# volumes:
# - "./docker/dynamodb:/home/dynamodblocal/data"
# working_dir: /home/dynamodblocal
108 changes: 108 additions & 0 deletions services/api/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions services/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@
"start": "node dist/src/main",
"start:dev": "NODE_ENV=development nest start --watch",
"start:debug": "NODE_ENV=development nest start --debug --watch",
"db:init": "NODE_ENV=development ts-node ./scripts/db/init.ts",
"db:drop": "NODE_ENV=development ts-node ./scripts/db/drop.ts",
"db:test:drop": "NODE_ENV=test ts-node ./scripts/dropdb.ts",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:int": "jest --config ./jest.int.config.js",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
Expand All @@ -27,6 +31,7 @@
"@aws-sdk/credential-provider-node": "3.238.0",
"@aws-sdk/lib-dynamodb": "3.238.0",
"@nestjs/common": "^9.0.0",
"@nestjs/config": "2.2.0",
"@nestjs/core": "^9.0.0",
"@nestjs/mapped-types": "*",
"@nestjs/passport": "9.0.0",
Expand Down Expand Up @@ -59,6 +64,7 @@
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^4.0.0",
"jest": "28.1.3",
"jest-mock-extended": "3.0.1",
"prettier": "^2.3.2",
"source-map-support": "^0.5.20",
"supertest": "^6.1.3",
Expand Down
14 changes: 14 additions & 0 deletions services/api/scripts/db/drop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { DataModule } from '../../src/data/data.module';
import { DynamoDBAdapter } from '../../src/data/adapters/dynamodb.adapter';
import { runWithContext } from '../runWithContext';

runWithContext({
rootModule: DataModule,
run: async (app, logger) => {
const db = app.get(DynamoDBAdapter);
const { TableDescription } = await db.destroy();
logger.log(
`Dropped table ${TableDescription?.TableName} (${TableDescription?.TableArn})`,
);
},
});
14 changes: 14 additions & 0 deletions services/api/scripts/db/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { DynamoDBAdapter } from '../../src/data/adapters/dynamodb.adapter';
import { DataModule } from '../../src/data/data.module';
import { runWithContext } from '../runWithContext';

runWithContext({
rootModule: DataModule,
run: async (app, logger) => {
const db = app.get(DynamoDBAdapter);
const { TableDescription } = await db.create();
logger.log(
`Created table ${TableDescription?.TableName} (${TableDescription?.TableArn})`,
);
},
});
15 changes: 0 additions & 15 deletions services/api/scripts/dropdb.ts

This file was deleted.

Loading

0 comments on commit 8c1ef6a

Please sign in to comment.