Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix toast at wrong location #110

Merged
merged 6 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/hooks/useMatchUsers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useSession } from "next-auth/react";
import { useRouter } from "next/router";
import { useEffect, useState } from "react";
import toast from "react-hot-toast";
import { api } from "~/utils/api";

type UseMatchUsersResult = {
Expand All @@ -23,6 +24,7 @@ export default function useMatchUsers(): UseMatchUsersResult {
data.user2 === session?.user.id
) {
console.log("Routing");
toast.success("Redirecting to room...");
void router.push(`/collab/rooms/${data.sessionId}`);
}
},
Expand Down
77 changes: 40 additions & 37 deletions src/pages/collab/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,40 +60,6 @@ const MatchRequestPage = () => {
};
});

useEffect(() => {
if (time === REQUEST_EXPIRY_TIME_SECS && curUserMatchRequest) {
const { difficulty, category, matchType } = curUserMatchRequest;
const sameRequest = { difficulty, category, matchType };
deleteMatchRequest({ matchType: curUserMatchRequest.matchType });
toast(
(t) => (
<div className="flex flex-col justify-evenly text-slate-800">
<span className="text-center mb-2">No requests found 🫠.</span>
<div className="flex justify-stretch">
<button
className="border bg-blue-500 rounded-md text-slate-100 p-2 mr-1"
onClick={() => {
toast.dismiss(t.id);
createMatchRequest(sameRequest);
}}
type="button"
>
Recreate again
</button>
<button
className="border bg-stone-500 rounded-md text-slate-100 p-2"
onClick={() => toast.dismiss(t.id)}
type="button"
>
Dismiss
</button>
</div>
</div>
),
{ duration: 60000, id: "timeout", position: "top-center" },
);
}
}, [time]);

const matchUsers = useMatchUsers();
const [isCreatingMatchRequest, setIsCreatingMatchRequest] = useState(false);
Expand All @@ -107,7 +73,7 @@ const MatchRequestPage = () => {
const { data: numOfOnlineUsers = 0, refetch: refetchGetNumOfMatchReqs } =
api.matchRequest.getNumOfMatchRequests.useQuery();

const { data: curUserMatchRequest, refetch: refetchCurrentUserRequest } =
const { data: curUserMatchRequest, refetch: refetchCurrentUserRequest, isFetching: isCurUserMatchFetching } =
api.matchRequest.getCurrentUserRequest.useQuery(undefined, {
onError() {
toast.error("Error loading current user's match request");
Expand Down Expand Up @@ -173,12 +139,49 @@ const MatchRequestPage = () => {
api.matchRequest.subscribeToMyRequestSuccess.useSubscription(undefined, {
onData(data) {
matchUsers.setMatchedUsers(data.userId1, data.userId2);
toast.success("Redirecting to room...");
},
});
// subscprtions api -- END


useEffect(() => {
if (time >= REQUEST_EXPIRY_TIME_SECS && curUserMatchRequest && !isCurUserMatchFetching) {
const { difficulty, category, matchType } = curUserMatchRequest;
const sameRequest = { difficulty, category, matchType };
deleteMatchRequest({ matchType: curUserMatchRequest.matchType });
stopTimer();
toast(
(t) => (
<div className="flex flex-col justify-evenly text-slate-800">
<span className="text-center mb-2">No requests found 🫠.</span>
<div className="flex justify-stretch">
<button
className="border bg-blue-500 rounded-md text-slate-100 p-2 mr-1"
onClick={() => {
toast.dismiss(t.id);
createMatchRequest(sameRequest);
}}
type="button"
>
Recreate again
</button>
<button
className="border bg-stone-500 rounded-md text-slate-100 p-2"
onClick={() => toast.dismiss(t.id)}
type="button"
>
Dismiss
</button>
</div>
</div>
),
{ duration: 60000, id: "timeout", position: "top-center" },
);
}
}, [time, isCurUserMatchFetching]);

useEffect(() => {
if (isCurUserMatchFetching) return;
if (!curUserMatchRequest) {
if (intervalRef.current) {
clearInterval(intervalRef.current);
Expand All @@ -200,7 +203,7 @@ const MatchRequestPage = () => {
intervalRef.current = setInterval(() => {
setTime((prev) => prev + 1);
}, 1000);
}, [curUserMatchRequest]);
}, [curUserMatchRequest, isCurUserMatchFetching]);

const {
data: manualRequests = [],
Expand Down
Loading