-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
868963b
commit 967e702
Showing
16 changed files
with
247 additions
and
25 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const MessagePreviewIndex: React.FC = () => { | ||
return ( | ||
<div className="flex h-full flex-1 justify-center"> | ||
<div className="container grid h-full grid-cols-4"> | ||
<div>Show discussions</div> | ||
<div className="show messages"></div> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default MessagePreviewIndex; |
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,80 @@ | ||
import React, { createContext, useEffect, useMemo, useState } from "react"; | ||
|
||
import io, { Socket } from "socket.io-client"; | ||
import useAuth from "../hooks/useAuth"; | ||
import TokenService from "../services/token.service"; | ||
|
||
const tokenService = TokenService.getInstance(); | ||
|
||
export const MessageContext = createContext<{ | ||
isConnected: boolean; | ||
sendMessage: (announcement_id: number, contenu: string) => void; | ||
}>({ | ||
isConnected: false, | ||
sendMessage: () => {}, | ||
}); | ||
|
||
let socket: Socket<any>; | ||
|
||
const MessageProvider: React.FC<{ children: React.ReactNode }> = ({ | ||
children, | ||
}) => { | ||
const { currentUser } = useAuth(); | ||
|
||
const [isConnected, setIsConnected] = useState<boolean>(false); | ||
|
||
const sendMessage = (announcement_id: number, contenu: string) => { | ||
socket.send({ | ||
data: { | ||
announcement_id: announcement_id.toString(), | ||
objet: "", | ||
contenu, | ||
}, | ||
headers: { | ||
Authorization: "Bearer " + tokenService.getAccessToken(), | ||
}, | ||
}); | ||
}; | ||
|
||
useEffect(() => { | ||
socket = io(process.env.NEXT_PUBLIC_BACKEND_URL as string, { | ||
extraHeaders: { | ||
Authorization: "Bearer " + tokenService.getAccessToken(), | ||
}, | ||
}); | ||
|
||
socket.on("connect", () => { | ||
setIsConnected(true); | ||
console.log("Hello world"); | ||
}); | ||
|
||
socket.on("disconnect", () => { | ||
setIsConnected(false); | ||
}); | ||
|
||
socket.on("message", (data: any) => { | ||
console.log(data); | ||
}); | ||
|
||
return () => { | ||
socket.off("connect"); | ||
socket.off("disconnect"); | ||
socket.off("message"); | ||
}; | ||
}, []); | ||
|
||
const contextValue = useMemo( | ||
() => ({ isConnected, sendMessage }), | ||
[isConnected, sendMessage] | ||
); | ||
|
||
return currentUser ? ( | ||
<MessageContext.Provider value={contextValue}> | ||
{children} | ||
</MessageContext.Provider> | ||
) : ( | ||
<>{children}</> | ||
); | ||
}; | ||
|
||
export default MessageProvider; |
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,12 @@ | ||
import { useContext } from "react"; | ||
import { MessageContext } from "../context/MessageContext"; | ||
|
||
const useMessage = () => { | ||
if (MessageContext) { | ||
return useContext(MessageContext); | ||
} else { | ||
throw new Error("MessageProvider is required"); | ||
} | ||
}; | ||
|
||
export default useMessage; |
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 |
---|---|---|
@@ -1,12 +1,10 @@ | ||
import { NextPage } from "next"; | ||
import AnnouncementPreviewIndex from "../../components/announcements/preview-annoucement/AnnouncementPreviewIndex"; | ||
import useAuth from "../../hooks/useAuth"; | ||
|
||
const AnnouncementPreview: NextPage = () => { | ||
return ( | ||
<> | ||
<AnnouncementPreviewIndex /> | ||
</> | ||
); | ||
const { currentUser } = useAuth(); | ||
return <>{currentUser && <AnnouncementPreviewIndex />}</>; | ||
}; | ||
|
||
export default AnnouncementPreview; |
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,15 @@ | ||
import { NextPage } from "next"; | ||
import { NextSeo } from "next-seo"; | ||
import MessagePreviewIndex from "../../components/messages/MessagePreviewIndex"; | ||
import { ROUTES } from "../../constants/routes"; | ||
|
||
const MessagePreview: NextPage = () => { | ||
return ( | ||
<> | ||
<NextSeo title={ROUTES.MESSAGES.name} /> | ||
<MessagePreviewIndex /> | ||
</> | ||
); | ||
}; | ||
|
||
export default MessagePreview; |
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.