Skip to content

Commit

Permalink
fix issue on update twitter status
Browse files Browse the repository at this point in the history
  • Loading branch information
zuies committed Oct 3, 2023
1 parent 1fb3994 commit 8674dd4
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 15 deletions.
35 changes: 21 additions & 14 deletions src/components/pages/settings/ConnectedTwitter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,28 @@ function ConnectedTwitter({ twitter }: IConnectedTwitter) {
const { disconnectTwitter, getUserInfo } = useAppStore();

const handleDisconnect = async () => {
await disconnectTwitter();
const {
twitterConnectedAt,
twitterId,
twitterProfileImageUrl,
twitterUsername,
} = await getUserInfo();
try {
await disconnectTwitter();

StorageService.updateLocalStorageWithObject('user', 'twitter', {
twitterConnectedAt,
twitterId,
twitterProfileImageUrl,
twitterUsername,
});
StorageService.removeLocalStorage('lastTwitterMetricsRefreshDate');
const userInfo = await getUserInfo();
const {
twitterConnectedAt,
twitterId,
twitterProfileImageUrl,
twitterUsername,
} = userInfo;

StorageService.updateLocalStorageWithObject('user', 'twitter', {
twitterConnectedAt,
twitterId,
twitterProfileImageUrl,
twitterUsername,
});

StorageService.removeLocalStorage('lastTwitterMetricsRefreshDate');
} catch (error) {
console.error('Error handling disconnect:', error);
}
};

return (
Expand Down
9 changes: 9 additions & 0 deletions src/layouts/defaultLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const defaultLayout = ({ children }: IDefaultLayoutProps) => {
if (guildId) {
getGuildInfoByDiscord(guildId);
}

const fetchUserInfo = async () => {
const {
twitterConnectedAt,
Expand All @@ -52,7 +53,15 @@ export const defaultLayout = ({ children }: IDefaultLayoutProps) => {
twitterUsername,
});
};

// Immediately call fetchUserInfo once
fetchUserInfo();

// Set up the interval to call fetchUserInfo every 5 seconds
const intervalId = setInterval(fetchUserInfo, 5000);

// Return a cleanup function to clear the interval when the component unmounts
return () => clearInterval(intervalId);
}
}, []);

Expand Down
1 change: 0 additions & 1 deletion src/pages/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ function Settings(): JSX.Element {
fetchEmail();
const intervalId = setInterval(() => {
getGuilds();
getUserInfo();
}, 5000);

// Clean up the interval when the component unmounts
Expand Down
3 changes: 3 additions & 0 deletions src/store/slices/twitterSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { conf } from '../../configs';
const BASE_URL = conf.API_BASE_URL;

const createTwitterSlice: StateCreator<ITwitter> = (set, get) => ({
isLoading: false,
authorizeTwitter: async (discordId: string) => {
try {
location.replace(`${BASE_URL}/auth/twitter/login/user/${discordId}`);
Expand All @@ -15,7 +16,9 @@ const createTwitterSlice: StateCreator<ITwitter> = (set, get) => ({
},
disconnectTwitter: async () => {
try {
set(() => ({ isLoading: true }));
await axiosInstance.post(`twitter/disconnect`);
set(() => ({ isLoading: false }));
} catch (error) {}
},
refreshTwitterMetrics: async () => {
Expand Down
1 change: 1 addition & 0 deletions src/store/types/ITwitter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export default interface ITwitter {
isLoading: boolean;
authorizeTwitter: (discordId: string) => void;
disconnectTwitter: () => void;
refreshTwitterMetrics: () => void;
Expand Down

0 comments on commit 8674dd4

Please sign in to comment.