-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
2,442 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
/node_modules/ | ||
/docker/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { useAuth0 } from "@auth0/auth0-react"; | ||
import { useEffect, useState } from "react"; | ||
|
||
export const useAccessToken = (): string | undefined => { | ||
const { isAuthenticated, getAccessTokenSilently } = useAuth0(); | ||
const [accessToken, setAccessToken] = useState<string>(); | ||
useEffect(() => { | ||
if (isAuthenticated) { | ||
const getAccessToken = async () => { | ||
const accessToken = await getAccessTokenSilently({ | ||
audience: `https://auth0-test-api.jbrunton-aws.com`, | ||
scope: "openid profile email", | ||
}); | ||
setAccessToken(accessToken); | ||
} | ||
getAccessToken(); | ||
} | ||
}, [isAuthenticated]); | ||
return accessToken; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import React, { useState, KeyboardEventHandler } from "react"; | ||
import { useMessages, usePostMessage } from "../../data/messages"; | ||
import { useAccessToken } from "../auth/useAccessToken"; | ||
|
||
export type MessagesWidgetProps = { | ||
roomId: string; | ||
} | ||
|
||
export const MessagesWidget: React.FC<MessagesWidgetProps> = ({ roomId }) => { | ||
const { data: messages } = useMessages(roomId); | ||
const [content, setContent] = useState<string>(""); | ||
const accessToken = useAccessToken(); | ||
const { mutate } = usePostMessage(roomId, content, accessToken) | ||
const onKeyDown: KeyboardEventHandler = (e) => { | ||
if (e.key === 'Enter') { | ||
mutate(); | ||
setContent(""); | ||
} | ||
}; | ||
return ( | ||
<div> | ||
{messages && ( | ||
messages.map(msg => ( | ||
<div key={msg.id}> | ||
<p>{msg.content}</p> | ||
<span>{msg.time}</span> | ||
</div> | ||
)) | ||
)} | ||
<input value={content} onChange={(e) => setContent(e.target.value)} onKeyDown={onKeyDown} /> | ||
</div> | ||
) | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; | ||
|
||
const apiUrl = import.meta.env.VITE_API_URL || ""; | ||
|
||
export type Message = { | ||
id: string; | ||
content: string; | ||
time: number; | ||
} | ||
|
||
export const useMessages = (roomId: string) => | ||
useQuery({ | ||
queryKey: [`messages/${roomId}`], | ||
queryFn: () => fetch(`${apiUrl}/messages/${roomId}`).then((res) => res.json().then(msg => msg as Message[])), | ||
}); | ||
|
||
export const usePostMessage = (roomId: string, content?: string, accessToken?: string) => { | ||
const queryClient = useQueryClient(); | ||
return useMutation({ | ||
mutationFn: () => fetch(`${apiUrl}/messages`, { | ||
method: 'POST', | ||
headers: { | ||
Authorization: `Bearer ${accessToken}`, | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ content, roomId }), | ||
}).then((res) => res.json()), | ||
onSuccess: (message) => { | ||
queryClient.invalidateQueries([`messages/${roomId}`]); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
version: '3.8' | ||
services: | ||
dynamodb-local: | ||
command: "-jar DynamoDBLocal.jar -sharedDb -dbPath ./data" | ||
image: "amazon/dynamodb-local:latest" | ||
container_name: dynamodb-local | ||
ports: | ||
- "8000:8000" | ||
volumes: | ||
- "./docker/dynamodb:/home/dynamodblocal/data" | ||
working_dir: /home/dynamodblocal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.