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 issue on update twitter status #200

Merged
merged 1 commit into from
Oct 3, 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
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
Loading