Skip to content

Commit

Permalink
fix issue during last debug
Browse files Browse the repository at this point in the history
  • Loading branch information
zuies committed Dec 1, 2023
1 parent e039aa2 commit 82d4dc3
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 57 deletions.
22 changes: 18 additions & 4 deletions src/components/communitySettings/platform/TcDisconnectPlatform.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,18 @@ function TcDisconnectPlatform({ platform }: TcDisconnectPlatformProps) {
</div>
<div className="flex flex-col md:flex-row justify-between space-y-4 md:space-y-0 md:space-x-5 pb-8 md:py-12">
<div className="shadow-xl rounded-md px-4 py-6 space-y-4">
<TcText text={'Disconnect and delete data'} variant="body1" />
<TcText
text={'Disconnect and delete data'}
variant="body1"
fontWeight="bold"
/>
<TcText
className="text-left"
text={
'Importing activities and members will be stopped. Historical activities will be deleted.'
<span>
Importing activities and members will be stopped. Historical
activities <b>will be deleted.</b>
</span>
}
variant="body2"
/>
Expand All @@ -81,11 +88,18 @@ function TcDisconnectPlatform({ platform }: TcDisconnectPlatformProps) {
/>
</div>
<div className="shadow-xl rounded-md px-4 py-6 space-y-4">
<TcText text={'Disconnect only'} variant="body1" />
<TcText
text={'Disconnect only'}
variant="body1"
fontWeight="bold"
/>
<TcText
className="text-left"
text={
'Importing activities and members will be stopped. Historical activities will not be affected.'
<span>
Importing activities and members will be stopped. Historical
activities <b>will not be affected.</b>
</span>
}
variant="body2"
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ function TcPeriodRange({ handleSelectedDate, activePeriod }: ITcPeriodRange) {
return '1Y';
}
};
console.log(findDefaultPeriod());

const [selected, setSelected] = useState<PeriodValue>('Last 35 days');

Expand Down
6 changes: 4 additions & 2 deletions src/components/communitySettings/platform/TcPlatform.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,17 @@ function TcPlatform({ platformName = 'Discord' }: TcPlatformProps) {

const handlePatchCommunity = async () => {
try {
await patchPlatformById({
const data = await patchPlatformById({
id,
metadata: {
selectedChannels: currentTrueIDs,
period: platfromAnalyzerDate,
analyzerStartedAt: new Date().toISOString(),
},
});
setOpenConfirmDialog(true);
if (data) {
setOpenConfirmDialog(true);
}
await fetchPlatform();
} catch (error) {}
};
Expand Down
1 change: 1 addition & 0 deletions src/components/pages/pageIndex/ActiveMemberComposition.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import CustomTable from '../CustomTable';
import {
Column,
IActivityCompositionOptions,
ICommunity,
} from '../../../../../utils/interfaces';
import CustomPagination from '../CustomPagination';
import CustomButton from '../../../../global/CustomButton';
Expand All @@ -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' },
Expand All @@ -36,6 +38,7 @@ const options: IActivityCompositionOptions[] = [

export default function ActiveMemberBreakdown() {
const { getActiveMemberCompositionTable } = useAppStore();
const { community } = useToken();

const tableTopRef = useRef<HTMLDivElement>(null);

Expand All @@ -59,8 +62,7 @@ export default function ActiveMemberBreakdown() {
totalResults: 0,
totalPages: 0,
});
const user = StorageService.readLocalStorage<IUser>('user');
const guild = user?.guild;
const platformId = community?.platforms[0];

const handlePageChange = (selectedPage: number) => {
setPage(selectedPage);
Expand All @@ -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,
Expand Down Expand Up @@ -149,15 +151,15 @@ export default function ActiveMemberBreakdown() {
};

const handleDownloadCSV = async () => {
if (!guild) {
if (!platformId) {
return;
}

try {
const limit = fetchedData.totalResults;

const { results } = await getActiveMemberCompositionTable(
guild.guildId,
platformId,
activityComposition,
roles,
username,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand Down Expand Up @@ -47,6 +48,7 @@ const options: IActivityCompositionOptions[] = [

export default function DisengagedMembersCompositionBreakdown() {
const { getDisengagedMembersCompositionTable } = useAppStore();
const { community } = useToken();

const tableTopRef = useRef<HTMLDivElement>(null);

Expand All @@ -70,8 +72,8 @@ export default function DisengagedMembersCompositionBreakdown() {
totalResults: 0,
totalPages: 0,
});
const user = StorageService.readLocalStorage<IUser>('user');
const guild = user?.guild;

const platformId = community?.platforms[0];

const handlePageChange = (selectedPage: number) => {
setPage(selectedPage);
Expand All @@ -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,
Expand Down Expand Up @@ -160,15 +162,15 @@ export default function DisengagedMembersCompositionBreakdown() {
};

const handleDownloadCSV = async () => {
if (!guild) {
if (!platformId) {
return;
}

try {
const limit = fetchedData.totalResults;

const { results } = await getDisengagedMembersCompositionTable(
guild.guildId,
platformId,
disengagedComposition,
roles,
username,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand All @@ -35,6 +36,7 @@ const options: IActivityCompositionOptions[] = [

export default function OnboardingMembersBreakdown() {
const { getOnboardingMemberCompositionTable } = useAppStore();
const { community } = useToken();

const tableTopRef = useRef<HTMLDivElement>(null);

Expand All @@ -58,8 +60,8 @@ export default function OnboardingMembersBreakdown() {
totalResults: 0,
totalPages: 0,
});
const user = StorageService.readLocalStorage<IUser>('user');
const guild = user?.guild;

const platformId = community?.platforms[0];

const handlePageChange = (selectedPage: number) => {
setPage(selectedPage);
Expand All @@ -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,
Expand Down Expand Up @@ -147,15 +149,15 @@ export default function OnboardingMembersBreakdown() {
};

const handleDownloadCSV = async () => {
if (!guild) {
if (!platformId) {
return;
}

try {
const limit = fetchedData.totalResults;

const { results } = await getOnboardingMemberCompositionTable(
guild.guildId,
platformId,
onboardingComposition,
roles,
username,
Expand Down
47 changes: 40 additions & 7 deletions src/context/TokenContext.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use client';
import React, {
useState,
useContext,
createContext,
useEffect,
useRef,
ReactNode,
} from 'react';
import { StorageService } from '../services/StorageService';
Expand Down Expand Up @@ -31,15 +31,18 @@ export const TokenProvider: React.FC<TokenProviderProps> = ({ children }) => {
const [token, setToken] = useState<IToken | null>(null);
const [community, setCommunity] = useState<ICommunity | null>(null);

let intervalId: string | number | NodeJS.Timer | undefined;
// Use useRef to persist the interval ID across renders
const intervalIdRef = useRef<NodeJS.Timer | null>(null);

useEffect(() => {
const storedToken = StorageService.readLocalStorage<IToken>('user');
const storedCommunity =
StorageService.readLocalStorage<ICommunity>('community');

if (storedToken) {
setToken(storedToken);
}

if (storedCommunity) {
setCommunity(storedCommunity);
}
Expand All @@ -61,14 +64,18 @@ export const TokenProvider: React.FC<TokenProviderProps> = ({ 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);
}
};
}, []);

Expand All @@ -77,9 +84,35 @@ export const TokenProvider: React.FC<TokenProviderProps> = ({ children }) => {
setToken(newToken);
};

const updateCommunity = (newCommunity: ICommunity) => {
StorageService.writeLocalStorage<ICommunity>('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<ICommunity>('community', newCommunity);

// Restart the interval
intervalIdRef.current = setInterval(async () => {
try {
const updatedCommunity = await retrieveCommunityById(newCommunity.id);
if (updatedCommunity) {
setCommunity(updatedCommunity);
StorageService.writeLocalStorage<ICommunity>(
'community',
updatedCommunity
);
}
} catch (error) {
console.error('Error fetching community:', error);
StorageService.removeLocalStorage('community');
if (intervalIdRef.current) {
clearInterval(intervalIdRef.current);
}
}
}, 5000);
};

const clearToken = () => {
Expand Down
Loading

0 comments on commit 82d4dc3

Please sign in to comment.