From 82d4dc34ee519a4534803a25f3beb9d08736212e Mon Sep 17 00:00:00 2001 From: zuies Date: Fri, 1 Dec 2023 13:45:53 +0300 Subject: [PATCH] fix issue during last debug --- .../platform/TcDisconnectPlatform.tsx | 22 +++++++-- .../platform/TcPeriodRange.tsx | 1 - .../communitySettings/platform/TcPlatform.tsx | 6 ++- .../pageIndex/ActiveMemberComposition.tsx | 1 + .../activeMembers/ActiveMemberBreakdown.tsx | 14 +++--- .../DisengagedMembersCompositionBreakdown.tsx | 14 +++--- .../OnboardingMembersBreakdown.tsx | 14 +++--- src/context/TokenContext.tsx | 47 ++++++++++++++++--- src/store/slices/breakdownsSlice.ts | 30 ++++-------- src/store/types/IBreakdown.ts | 7 ++- 10 files changed, 99 insertions(+), 57 deletions(-) diff --git a/src/components/communitySettings/platform/TcDisconnectPlatform.tsx b/src/components/communitySettings/platform/TcDisconnectPlatform.tsx index 220f5551..bf6e7c13 100644 --- a/src/components/communitySettings/platform/TcDisconnectPlatform.tsx +++ b/src/components/communitySettings/platform/TcDisconnectPlatform.tsx @@ -66,11 +66,18 @@ function TcDisconnectPlatform({ platform }: TcDisconnectPlatformProps) {
- + + Importing activities and members will be stopped. Historical + activities will be deleted. + } variant="body2" /> @@ -81,11 +88,18 @@ function TcDisconnectPlatform({ platform }: TcDisconnectPlatformProps) { />
- + + Importing activities and members will be stopped. Historical + activities will not be affected. + } variant="body2" /> diff --git a/src/components/communitySettings/platform/TcPeriodRange.tsx b/src/components/communitySettings/platform/TcPeriodRange.tsx index 0f940a37..bc02dd82 100644 --- a/src/components/communitySettings/platform/TcPeriodRange.tsx +++ b/src/components/communitySettings/platform/TcPeriodRange.tsx @@ -42,7 +42,6 @@ function TcPeriodRange({ handleSelectedDate, activePeriod }: ITcPeriodRange) { return '1Y'; } }; - console.log(findDefaultPeriod()); const [selected, setSelected] = useState('Last 35 days'); diff --git a/src/components/communitySettings/platform/TcPlatform.tsx b/src/components/communitySettings/platform/TcPlatform.tsx index bf541857..1c53a3b1 100644 --- a/src/components/communitySettings/platform/TcPlatform.tsx +++ b/src/components/communitySettings/platform/TcPlatform.tsx @@ -67,7 +67,7 @@ function TcPlatform({ platformName = 'Discord' }: TcPlatformProps) { const handlePatchCommunity = async () => { try { - await patchPlatformById({ + const data = await patchPlatformById({ id, metadata: { selectedChannels: currentTrueIDs, @@ -75,7 +75,9 @@ function TcPlatform({ platformName = 'Discord' }: TcPlatformProps) { analyzerStartedAt: new Date().toISOString(), }, }); - setOpenConfirmDialog(true); + if (data) { + setOpenConfirmDialog(true); + } await fetchPlatform(); } catch (error) {} }; diff --git a/src/components/pages/pageIndex/ActiveMemberComposition.tsx b/src/components/pages/pageIndex/ActiveMemberComposition.tsx index 37e6545e..78ac4e7d 100644 --- a/src/components/pages/pageIndex/ActiveMemberComposition.tsx +++ b/src/components/pages/pageIndex/ActiveMemberComposition.tsx @@ -17,6 +17,7 @@ const ActiveMemberComposition = () => { useEffect(() => { let endDate: moment.Moment = moment().subtract(1, 'day'); let startDate: moment.Moment = moment(endDate).subtract(7, 'days'); + const platformId = community?.platforms[0]; if (platformId) { diff --git a/src/components/pages/statistics/memberBreakdowns/activeMembers/ActiveMemberBreakdown.tsx b/src/components/pages/statistics/memberBreakdowns/activeMembers/ActiveMemberBreakdown.tsx index 06025e6b..c2da8896 100644 --- a/src/components/pages/statistics/memberBreakdowns/activeMembers/ActiveMemberBreakdown.tsx +++ b/src/components/pages/statistics/memberBreakdowns/activeMembers/ActiveMemberBreakdown.tsx @@ -6,6 +6,7 @@ import CustomTable from '../CustomTable'; import { Column, IActivityCompositionOptions, + ICommunity, } from '../../../../../utils/interfaces'; import CustomPagination from '../CustomPagination'; import CustomButton from '../../../../global/CustomButton'; @@ -17,6 +18,7 @@ import { downloadCSVFile, } from '../../../../../helpers/csvHelper'; import router from 'next/router'; +import { useToken } from '../../../../../context/TokenContext'; const columns: Column[] = [ { id: 'username', label: 'Name' }, @@ -36,6 +38,7 @@ const options: IActivityCompositionOptions[] = [ export default function ActiveMemberBreakdown() { const { getActiveMemberCompositionTable } = useAppStore(); + const { community } = useToken(); const tableTopRef = useRef(null); @@ -59,8 +62,7 @@ export default function ActiveMemberBreakdown() { totalResults: 0, totalPages: 0, }); - const user = StorageService.readLocalStorage('user'); - const guild = user?.guild; + const platformId = community?.platforms[0]; const handlePageChange = (selectedPage: number) => { setPage(selectedPage); @@ -70,13 +72,13 @@ export default function ActiveMemberBreakdown() { }; useEffect(() => { - if (!guild) { + if (!platformId) { return; } setLoading(true); const fetchData = async () => { const res = await getActiveMemberCompositionTable( - guild.guildId, + platformId, activityComposition, roles, username, @@ -149,7 +151,7 @@ export default function ActiveMemberBreakdown() { }; const handleDownloadCSV = async () => { - if (!guild) { + if (!platformId) { return; } @@ -157,7 +159,7 @@ export default function ActiveMemberBreakdown() { const limit = fetchedData.totalResults; const { results } = await getActiveMemberCompositionTable( - guild.guildId, + platformId, activityComposition, roles, username, diff --git a/src/components/pages/statistics/memberBreakdowns/disengagedMembersComposition/DisengagedMembersCompositionBreakdown.tsx b/src/components/pages/statistics/memberBreakdowns/disengagedMembersComposition/DisengagedMembersCompositionBreakdown.tsx index e026f4f6..35f70679 100644 --- a/src/components/pages/statistics/memberBreakdowns/disengagedMembersComposition/DisengagedMembersCompositionBreakdown.tsx +++ b/src/components/pages/statistics/memberBreakdowns/disengagedMembersComposition/DisengagedMembersCompositionBreakdown.tsx @@ -17,6 +17,7 @@ import { downloadCSVFile, } from '../../../../../helpers/csvHelper'; import router from 'next/router'; +import { useToken } from '../../../../../context/TokenContext'; const columns: Column[] = [ { id: 'username', label: 'Name' }, @@ -47,6 +48,7 @@ const options: IActivityCompositionOptions[] = [ export default function DisengagedMembersCompositionBreakdown() { const { getDisengagedMembersCompositionTable } = useAppStore(); + const { community } = useToken(); const tableTopRef = useRef(null); @@ -70,8 +72,8 @@ export default function DisengagedMembersCompositionBreakdown() { totalResults: 0, totalPages: 0, }); - const user = StorageService.readLocalStorage('user'); - const guild = user?.guild; + + const platformId = community?.platforms[0]; const handlePageChange = (selectedPage: number) => { setPage(selectedPage); @@ -81,13 +83,13 @@ export default function DisengagedMembersCompositionBreakdown() { }; useEffect(() => { - if (!guild) { + if (!platformId) { return; } setLoading(true); const fetchData = async () => { const res = await getDisengagedMembersCompositionTable( - guild.guildId, + platformId, disengagedComposition, roles, username, @@ -160,7 +162,7 @@ export default function DisengagedMembersCompositionBreakdown() { }; const handleDownloadCSV = async () => { - if (!guild) { + if (!platformId) { return; } @@ -168,7 +170,7 @@ export default function DisengagedMembersCompositionBreakdown() { const limit = fetchedData.totalResults; const { results } = await getDisengagedMembersCompositionTable( - guild.guildId, + platformId, disengagedComposition, roles, username, diff --git a/src/components/pages/statistics/memberBreakdowns/onboardingMembers/OnboardingMembersBreakdown.tsx b/src/components/pages/statistics/memberBreakdowns/onboardingMembers/OnboardingMembersBreakdown.tsx index bc14b8ae..525af241 100644 --- a/src/components/pages/statistics/memberBreakdowns/onboardingMembers/OnboardingMembersBreakdown.tsx +++ b/src/components/pages/statistics/memberBreakdowns/onboardingMembers/OnboardingMembersBreakdown.tsx @@ -17,6 +17,7 @@ import { downloadCSVFile, } from '../../../../../helpers/csvHelper'; import router from 'next/router'; +import { useToken } from '../../../../../context/TokenContext'; const columns: Column[] = [ { id: 'username', label: 'Name' }, @@ -35,6 +36,7 @@ const options: IActivityCompositionOptions[] = [ export default function OnboardingMembersBreakdown() { const { getOnboardingMemberCompositionTable } = useAppStore(); + const { community } = useToken(); const tableTopRef = useRef(null); @@ -58,8 +60,8 @@ export default function OnboardingMembersBreakdown() { totalResults: 0, totalPages: 0, }); - const user = StorageService.readLocalStorage('user'); - const guild = user?.guild; + + const platformId = community?.platforms[0]; const handlePageChange = (selectedPage: number) => { setPage(selectedPage); @@ -69,13 +71,13 @@ export default function OnboardingMembersBreakdown() { }; useEffect(() => { - if (!guild) { + if (!platformId) { return; } setLoading(true); const fetchData = async () => { const res = await getOnboardingMemberCompositionTable( - guild.guildId, + platformId, onboardingComposition, roles, username, @@ -147,7 +149,7 @@ export default function OnboardingMembersBreakdown() { }; const handleDownloadCSV = async () => { - if (!guild) { + if (!platformId) { return; } @@ -155,7 +157,7 @@ export default function OnboardingMembersBreakdown() { const limit = fetchedData.totalResults; const { results } = await getOnboardingMemberCompositionTable( - guild.guildId, + platformId, onboardingComposition, roles, username, diff --git a/src/context/TokenContext.tsx b/src/context/TokenContext.tsx index f594f659..953cf5a9 100644 --- a/src/context/TokenContext.tsx +++ b/src/context/TokenContext.tsx @@ -1,9 +1,9 @@ -'use client'; import React, { useState, useContext, createContext, useEffect, + useRef, ReactNode, } from 'react'; import { StorageService } from '../services/StorageService'; @@ -31,15 +31,18 @@ export const TokenProvider: React.FC = ({ children }) => { const [token, setToken] = useState(null); const [community, setCommunity] = useState(null); - let intervalId: string | number | NodeJS.Timer | undefined; + // Use useRef to persist the interval ID across renders + const intervalIdRef = useRef(null); useEffect(() => { const storedToken = StorageService.readLocalStorage('user'); const storedCommunity = StorageService.readLocalStorage('community'); + if (storedToken) { setToken(storedToken); } + if (storedCommunity) { setCommunity(storedCommunity); } @@ -61,14 +64,18 @@ export const TokenProvider: React.FC = ({ children }) => { } catch (error) { console.error('Error fetching community:', error); StorageService.removeLocalStorage('community'); - clearInterval(intervalId); + if (intervalIdRef.current) { + clearInterval(intervalIdRef.current); + } } }; - intervalId = setInterval(fetchAndUpdateCommunity, 5000); + intervalIdRef.current = setInterval(fetchAndUpdateCommunity, 5000); return () => { - clearInterval(intervalId); + if (intervalIdRef.current) { + clearInterval(intervalIdRef.current); + } }; }, []); @@ -77,9 +84,35 @@ export const TokenProvider: React.FC = ({ children }) => { setToken(newToken); }; - const updateCommunity = (newCommunity: ICommunity) => { - StorageService.writeLocalStorage('community', newCommunity); + const updateCommunity = async (newCommunity: ICommunity) => { + // Clear the existing interval + if (intervalIdRef.current) { + clearInterval(intervalIdRef.current); + } + + // Update the community and reset the interval setCommunity(newCommunity); + StorageService.writeLocalStorage('community', newCommunity); + + // Restart the interval + intervalIdRef.current = setInterval(async () => { + try { + const updatedCommunity = await retrieveCommunityById(newCommunity.id); + if (updatedCommunity) { + setCommunity(updatedCommunity); + StorageService.writeLocalStorage( + 'community', + updatedCommunity + ); + } + } catch (error) { + console.error('Error fetching community:', error); + StorageService.removeLocalStorage('community'); + if (intervalIdRef.current) { + clearInterval(intervalIdRef.current); + } + } + }, 5000); }; const clearToken = () => { diff --git a/src/store/slices/breakdownsSlice.ts b/src/store/slices/breakdownsSlice.ts index c9b416de..007a8e62 100644 --- a/src/store/slices/breakdownsSlice.ts +++ b/src/store/slices/breakdownsSlice.ts @@ -9,7 +9,7 @@ const createBreakdownsSlice: StateCreator = (set, get) => ({ isRolesLoading: false, roles: [], getActiveMemberCompositionTable: async ( - guild_id: string, + platformId: string, activityComposition: string[], roles: string[], username?: string, @@ -47,15 +47,15 @@ const createBreakdownsSlice: StateCreator = (set, get) => ({ params.append('ngu', username); } - const url = `/member-activity/${guild_id}/active-members-composition-table?${params.toString()}`; + const url = `/member-activity/${platformId}/active-members-composition-table?${params.toString()}`; - const { data } = await axiosInstance.get(url); + const { data } = await axiosInstance.post(url); return data; } catch (error) {} }, getOnboardingMemberCompositionTable: async ( - guild_id: string, + platformId: string, activityComposition: string[], roles: string[], username?: string, @@ -93,15 +93,15 @@ const createBreakdownsSlice: StateCreator = (set, get) => ({ params.append('ngu', username); } - const url = `/member-activity/${guild_id}/active-members-onboarding-table?${params.toString()}`; + const url = `/member-activity/${platformId}/active-members-onboarding-table?${params.toString()}`; - const { data } = await axiosInstance.get(url); + const { data } = await axiosInstance.post(url); return data; } catch (error) {} }, getDisengagedMembersCompositionTable: async ( - guild_id: string, + platformId: string, activityComposition: string[], roles: string[], username?: string, @@ -139,25 +139,13 @@ const createBreakdownsSlice: StateCreator = (set, get) => ({ params.append('ngu', username); } - const url = `/member-activity/${guild_id}/disengaged-members-composition-table?${params.toString()}`; + const url = `/member-activity/${platformId}/disengaged-members-composition-table?${params.toString()}`; - const { data } = await axiosInstance.get(url); + const { data } = await axiosInstance.post(url); return data; } catch (error) {} }, - getRoles: async (guild_id: string) => { - try { - set(() => ({ isRolesLoading: true })); - - const { data } = await axiosInstance.get(`/guilds/${guild_id}/roles`); - - set(() => ({ roles: data, isRolesLoading: false })); - return data; - } catch (error) { - set(() => ({ isRolesLoading: false })); - } - }, }); export default createBreakdownsSlice; diff --git a/src/store/types/IBreakdown.ts b/src/store/types/IBreakdown.ts index e008bfd5..bf7d77ca 100644 --- a/src/store/types/IBreakdown.ts +++ b/src/store/types/IBreakdown.ts @@ -5,7 +5,7 @@ export default interface IBreakdown { isRolesLoading: boolean; roles: any[]; getActiveMemberCompositionTable: ( - guild_id: string, + platformId: string, activityComposition: string[], roles: string[], username?: string, @@ -14,7 +14,7 @@ export default interface IBreakdown { limit?: number ) => any; getOnboardingMemberCompositionTable: ( - guild_id: string, + platformId: string, activityComposition: string[], roles: string[], username?: string, @@ -23,7 +23,7 @@ export default interface IBreakdown { limit?: number ) => any; getDisengagedMembersCompositionTable: ( - guild_id: string, + platformId: string, activityComposition: string[], roles: string[], username?: string, @@ -31,5 +31,4 @@ export default interface IBreakdown { page?: number, limit?: number ) => any; - getRoles: (guild_id: string) => any; }