Skip to content

Commit

Permalink
Merge pull request #30 from TeamSynergyy/29/feat
Browse files Browse the repository at this point in the history
29/feat
  • Loading branch information
yeoularu authored May 7, 2024
2 parents 7a7a4e3 + db6d824 commit 6a97dc7
Show file tree
Hide file tree
Showing 21 changed files with 511 additions and 287 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"@mantine/form": "6.0.21",
"@mantine/hooks": "6.0.21",
"@reduxjs/toolkit": "^1.9.7",
"@stomp/stompjs": "^7.0.0",
"@tabler/icons-react": "^2.39.0",
"axios": "^1.5.1",
"clsx": "^2.0.0",
Expand All @@ -33,6 +32,7 @@
"react-kakao-maps-sdk": "^1.1.21",
"react-redux": "^8.1.3",
"react-router-dom": "^6.16.0",
"socket.io-client": "^4.7.5",
"vite-plugin-svgr": "^2.4.0"
},
"devDependencies": {
Expand Down
71 changes: 66 additions & 5 deletions pnpm-lock.yaml

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

4 changes: 2 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ import RecentProject from "pages/RecentProject";
import Search from "pages/Search";
import PostDetail from "pages/PostDetail";
import Following from "pages/Following";
import OauthRedirect from "pages/OauthRedirect";
import ProjectNotice from "components/project/ProjectNotice";
import ProjectSchedule from "components/project/ProjectSchedule";
import ProjectPeerRating from "components/project/ProjectPeerRating";
import ProjectTaskBoard from "components/project/task/ProjectTaskBoard";

import { Welcome } from "pages/Welcome";
import SignUpAuthCode from "pages/SignUpAuthCode";

const PrivateRoutes = () => {
const dispatch = useDispatch();
Expand Down Expand Up @@ -86,7 +86,7 @@ export default function App() {

<Route path="/welcome" element={<Welcome />} />
<Route path="/auth" element={<Auth />} />
<Route path="/oauth/redirect" element={<OauthRedirect />} />
<Route path="/auth/code" element={<SignUpAuthCode />} />
<Route path="/home/recent" element={<Layout />}>
<Route path="post" element={<RecentPost />} />
<Route path="project" element={<RecentProject />} />
Expand Down
37 changes: 37 additions & 0 deletions src/app/SocketContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { createContext, useEffect, useRef } from "react";
import io, { Socket } from "socket.io-client";
import { useDispatch, useSelector } from "react-redux";
import { messageReceived } from "app/socketSlice";
import { api } from "./api";
import { selectCurrentToken } from "./authSlice";

export const SocketContext = createContext<Socket | null>(null);

export const SocketProvider = ({ children }: { children: JSX.Element }) => {
const dispatch = useDispatch();
const token = useSelector(selectCurrentToken);
const { data: chatRooms } = api.useGetMyChatRoomsQuery(null);
const socketRef = useRef<Socket | null>(null);

useEffect(() => {
if (!socketRef.current) {
socketRef.current = io(import.meta.env.VITE_WEBSOCKET_URL, {
query: { token },
});
}

socketRef.current.on("message", (topic, message) => {
dispatch(messageReceived({ topic, body: message }));
});

return () => {
socketRef.current?.disconnect();
};
}, [token, dispatch, chatRooms]);

return (
<SocketContext.Provider value={socketRef.current}>
{children}
</SocketContext.Provider>
);
};
80 changes: 0 additions & 80 deletions src/app/StompContext.tsx

This file was deleted.

76 changes: 38 additions & 38 deletions src/app/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,46 +35,46 @@ const baseQuery = fetchBaseQuery({
},
});

const baseQueryWithReauth = async (
args: string | FetchArgs,
api: BaseQueryApi,
extraOptions: object
) => {
let result = await baseQuery(args, api, extraOptions);
if (result.error && result.error.status === 401) {
const state = api.getState() as RootState;
console.log("state", state);
const token = state.auth.token;
console.log("token", token);
const refreshResult: {
header: { code: number; message: string };
body: { token: string };
} = await axios.get(
import.meta.env.VITE_API_URL + "/api/v1/auth/reissue-with-accesstoken",
{
headers: {
Authorization: `Bearer ${token}`,
},
}
);

if (refreshResult.body?.token) {
const newAccessToken = refreshResult.body.token;
api.dispatch(setAccessToken(newAccessToken));
result = await baseQuery(args, api, extraOptions);
}

if (refreshResult.header?.code === 400) {
console.log(refreshResult);
// window.location.href = "/auth";
}
}

return result;
};
// const baseQueryWithReauth = async (
// args: string | FetchArgs,
// api: BaseQueryApi,
// extraOptions: object
// ) => {
// let result = await baseQuery(args, api, extraOptions);
// if (result.error && result.error.status === 401) {
// const state = api.getState() as RootState;
// console.log("state", state);
// const token = state.auth.token;
// console.log("token", token);
// const refreshResult: {
// header: { code: number; message: string };
// body: { token: string };
// } = await axios.get(
// import.meta.env.VITE_API_URL + "/api/v1/auth/reissue-with-accesstoken",
// {
// headers: {
// Authorization: `Bearer ${token}`,
// },
// }
// );

// if (refreshResult.body?.token) {
// const newAccessToken = refreshResult.body.token;
// api.dispatch(setAccessToken(newAccessToken));
// result = await baseQuery(args, api, extraOptions);
// }

// if (refreshResult.header?.code === 400) {
// console.log(refreshResult);
// // window.location.href = "/auth";
// }
// }

// return result;
// };

export const api = createApi({
baseQuery: baseQueryWithReauth,
baseQuery,
tagTypes: [
"MyInfo",
"Post",
Expand Down
12 changes: 6 additions & 6 deletions src/app/stompSlice.ts → src/app/socketSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ interface Message {
body: string;
}

interface StompState {
interface SocketState {
messages: Message[];
}

const initialState: StompState = {
const initialState: SocketState = {
messages: [],
};

export const stompSlice = createSlice({
name: "stomp",
export const socketSlice = createSlice({
name: "socket",
initialState,
reducers: {
messageReceived: (state, action: PayloadAction<Message>) => {
Expand All @@ -23,6 +23,6 @@ export const stompSlice = createSlice({
},
});

export const { messageReceived } = stompSlice.actions;
export const { messageReceived } = socketSlice.actions;

export default stompSlice.reducer;
export default socketSlice.reducer;
Loading

0 comments on commit 6a97dc7

Please sign in to comment.