Skip to content

Commit

Permalink
feat: try to fix community provider protection
Browse files Browse the repository at this point in the history
  • Loading branch information
unknownproperty committed Jul 13, 2024
1 parent 7ade2b3 commit 3deea44
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 2 deletions.
70 changes: 69 additions & 1 deletion apps/xi.front/app/(protected)/communities/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client';

import { redirect } from 'next/navigation';
import { redirect, useRouter } from 'next/navigation';
import { Logo } from 'pkg.logo';
import { useMainSt } from 'pkg.stores';
import { useEffect } from 'react';
Expand All @@ -9,11 +9,18 @@ import { toast } from 'sonner';
export default function CommunitiesLoading() {
const isLogin = useMainSt((state) => state.isLogin);
const initSocket = useMainSt((state) => state.initSocket);
const socket = useMainSt((state) => state.socket);
const updateCommunityMeta = useMainSt((state) => state.updateCommunityMeta);
const communityMeta = useMainSt((state) => state.communityMeta);
const onboardingStage = useMainSt((state) => state.user.onboardingStage);

const router = useRouter();

// Тоже костыль
useEffect(() => {
const toastTimerId = setTimeout(() => {
toast('Упс, проблемы с загрузкой');
initSocket();
}, 10000);

const redirectTimerId = setTimeout(() => {
Expand All @@ -29,6 +36,7 @@ export default function CommunitiesLoading() {
// Если вдруг что-то пошло не так, ещё раз иницируем соединение сокета
// В initSocket есть предотвращение инициализации нескольких соединений
useEffect(() => {
console.log('initSocket');
initSocket();
}, []);

Expand All @@ -38,6 +46,66 @@ export default function CommunitiesLoading() {
}
}, [isLogin]);

useEffect(() => {
console.log('onconnect', socket);
if (onboardingStage === 'completed') {
socket?.on('connect', () => {
socket.emit(
'retrieve-any-community',
(stats: number, { community, participant }: { community: any; participant: any }) => {
if (stats === 200) {
updateCommunityMeta({
id: community.id,
isOwner: participant.is_owner,
name: community.name,
description: community.description,
});
}

if (community.id) router.push(`/communities/${community.id}/home`);
},
);
});
}

if (socket?.connected === true && communityMeta.id === null) {
socket.emit(
'retrieve-any-community',
(stats: number, { community, participant }: { community: any; participant: any }) => {
if (stats === 200) {
updateCommunityMeta({
id: community.id,
isOwner: participant.is_owner,
name: community.name,
description: community.description,
});
}

if (community.id) router.push(`/communities/${community.id}/home`);
},
);
}
}, []);

useEffect(() => {
if (isLogin && socket?.connected === true) {
socket.emit('retrieve-any-community',
(stats: number, { community, participant }: { community: any; participant: any }) => {
if (stats === 200) {
updateCommunityMeta({
id: community.id,
isOwner: participant.is_owner,
name: community.name,
description: community.description,
});
}

if (community.id) router.push(`/communities/${community.id}/home`);
},
);
}
}, [isLogin, socket?.connected]);

return (
<div className="flex">
<div className="flex-col min-w-[350px] p-6">
Expand Down
22 changes: 21 additions & 1 deletion apps/xi.front/app/(protected)/providers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const ProtectedProvider = ({ children }: ProtectedProviderPropsT) => {
const socket = useMainSt((state) => state.socket);
const getUser = useMainSt((state) => state.getUser);
const updateCommunityMeta = useMainSt((state) => state.updateCommunityMeta);
const communityMeta = useMainSt((state) => state.communityMeta);
const onboardingStage = useMainSt((state) => state.user.onboardingStage);

const isLogin = useMainSt((state) => state.isLogin);
Expand Down Expand Up @@ -41,6 +42,24 @@ const ProtectedProvider = ({ children }: ProtectedProviderPropsT) => {
);
});
}

if (socket?.connected === true && communityMeta.id === null) {
socket.emit(
'retrieve-any-community',
(stats: number, { community, participant }: { community: any; participant: any }) => {
if (stats === 200) {
updateCommunityMeta({
id: community.id,
isOwner: participant.is_owner,
name: community.name,
description: community.description,
});
}

if (community.id) router.push(`/communities/${community.id}/home`);
},
);
}
}, [socket?.connected, onboardingStage]);

useEffect(() => {
Expand All @@ -51,13 +70,14 @@ const ProtectedProvider = ({ children }: ProtectedProviderPropsT) => {

useEffect(() => {
if (pathname !== '/communities' || (pathname === '/communities' && isLogin === null)) {
console.log('UUU', isLogin);
if (isLogin === false) {
redirect('/signin');
} else {
getUser();
}
}
}, []);
}, [isLogin]);

useEffect(() => {
console.log('onboardingStage', onboardingStage);
Expand Down

0 comments on commit 3deea44

Please sign in to comment.