pl-fe: migrate birthday reminders to tanstack query

Signed-off-by: mkljczk <git@mkljczk.pl>
This commit is contained in:
mkljczk 2024-12-04 18:10:14 +01:00
parent 443e15c591
commit fac30c2ea9
5 changed files with 51 additions and 84 deletions

View file

@ -22,7 +22,6 @@ import type { MinifiedSuggestion } from 'pl-fe/api/hooks/trends/use-suggested-ac
import type { MinifiedStatus } from 'pl-fe/reducers/statuses'; import type { MinifiedStatus } from 'pl-fe/reducers/statuses';
import type { AppDispatch, RootState } from 'pl-fe/store'; import type { AppDispatch, RootState } from 'pl-fe/store';
import type { History } from 'pl-fe/types/history'; import type { History } from 'pl-fe/types/history';
import type { Me } from 'pl-fe/types/pl-fe';
const ACCOUNT_CREATE_REQUEST = 'ACCOUNT_CREATE_REQUEST' as const; const ACCOUNT_CREATE_REQUEST = 'ACCOUNT_CREATE_REQUEST' as const;
const ACCOUNT_CREATE_SUCCESS = 'ACCOUNT_CREATE_SUCCESS' as const; const ACCOUNT_CREATE_SUCCESS = 'ACCOUNT_CREATE_SUCCESS' as const;
@ -72,10 +71,6 @@ const NOTIFICATION_SETTINGS_REQUEST = 'NOTIFICATION_SETTINGS_REQUEST' as const;
const NOTIFICATION_SETTINGS_SUCCESS = 'NOTIFICATION_SETTINGS_SUCCESS' as const; const NOTIFICATION_SETTINGS_SUCCESS = 'NOTIFICATION_SETTINGS_SUCCESS' as const;
const NOTIFICATION_SETTINGS_FAIL = 'NOTIFICATION_SETTINGS_FAIL' as const; const NOTIFICATION_SETTINGS_FAIL = 'NOTIFICATION_SETTINGS_FAIL' as const;
const BIRTHDAY_REMINDERS_FETCH_REQUEST = 'BIRTHDAY_REMINDERS_FETCH_REQUEST' as const;
const BIRTHDAY_REMINDERS_FETCH_SUCCESS = 'BIRTHDAY_REMINDERS_FETCH_SUCCESS' as const;
const BIRTHDAY_REMINDERS_FETCH_FAIL = 'BIRTHDAY_REMINDERS_FETCH_FAIL' as const;
const maybeRedirectLogin = (error: { response: PlfeResponse }, history?: History) => { const maybeRedirectLogin = (error: { response: PlfeResponse }, history?: History) => {
// The client is unauthorized - redirect to login. // The client is unauthorized - redirect to login.
if (history && error?.response?.status === 401) { if (history && error?.response?.status === 401) {
@ -565,50 +560,6 @@ const accountLookup = (acct: string, signal?: AbortSignal) =>
}); });
}; };
interface BirthdayRemindersFetchRequestAction {
type: typeof BIRTHDAY_REMINDERS_FETCH_REQUEST;
day: number;
month: number;
accountId: Me;
}
interface BirthdayRemindersFetchSuccessAction {
type: typeof BIRTHDAY_REMINDERS_FETCH_SUCCESS;
day: number;
month: number;
accountId: string;
accounts: Array<Account>;
}
interface BirthdayRemindersFetchFailAction {
type: typeof BIRTHDAY_REMINDERS_FETCH_FAIL;
day: number;
month: number;
accountId: Me;
}
const fetchBirthdayReminders = (month: number, day: number) =>
(dispatch: AppDispatch, getState: () => RootState) => {
if (!isLoggedIn(getState)) return;
const me = getState().me as string;
dispatch<BirthdayRemindersFetchRequestAction>({ type: BIRTHDAY_REMINDERS_FETCH_REQUEST, day, month, accountId: me });
return getClient(getState).accounts.getBirthdays(day, month).then(response => {
dispatch(importEntities({ accounts: response }));
dispatch<BirthdayRemindersFetchSuccessAction>({
type: BIRTHDAY_REMINDERS_FETCH_SUCCESS,
accounts: response,
day,
month,
accountId: me,
});
}).catch(() => {
dispatch<BirthdayRemindersFetchFailAction>({ type: BIRTHDAY_REMINDERS_FETCH_FAIL, day, month, accountId: me });
});
};
const biteAccount = (accountId: string) => const biteAccount = (accountId: string) =>
(dispatch: AppDispatch, getState: () => RootState) => { (dispatch: AppDispatch, getState: () => RootState) => {
const client = getClient(getState); const client = getClient(getState);
@ -652,10 +603,7 @@ type AccountsAction =
| AccountSearchFailAction | AccountSearchFailAction
| AccountLookupRequestAction | AccountLookupRequestAction
| AccountLookupSuccessAction | AccountLookupSuccessAction
| AccountLookupFailAction | AccountLookupFailAction;
| BirthdayRemindersFetchSuccessAction
| BirthdayRemindersFetchRequestAction
| BirthdayRemindersFetchFailAction
export { export {
ACCOUNT_CREATE_REQUEST, ACCOUNT_CREATE_REQUEST,
@ -694,9 +642,6 @@ export {
NOTIFICATION_SETTINGS_REQUEST, NOTIFICATION_SETTINGS_REQUEST,
NOTIFICATION_SETTINGS_SUCCESS, NOTIFICATION_SETTINGS_SUCCESS,
NOTIFICATION_SETTINGS_FAIL, NOTIFICATION_SETTINGS_FAIL,
BIRTHDAY_REMINDERS_FETCH_REQUEST,
BIRTHDAY_REMINDERS_FETCH_SUCCESS,
BIRTHDAY_REMINDERS_FETCH_FAIL,
createAccount, createAccount,
fetchAccount, fetchAccount,
fetchAccountByUsername, fetchAccountByUsername,
@ -716,7 +661,6 @@ export {
fetchPinnedAccounts, fetchPinnedAccounts,
accountSearch, accountSearch,
accountLookup, accountLookup,
fetchBirthdayReminders,
biteAccount, biteAccount,
type AccountsAction, type AccountsAction,
}; };

View file

@ -0,0 +1,22 @@
import { useQuery } from '@tanstack/react-query';
import { importEntities } from 'pl-fe/actions/importer';
import { useAppDispatch } from 'pl-fe/hooks/use-app-dispatch';
import { useClient } from 'pl-fe/hooks/use-client';
const useBirthdayReminders = (month: number, day: number) => {
const client = useClient();
const dispatch = useAppDispatch();
return useQuery({
queryKey: ['accountsLists', 'birthdayReminders', month, day],
queryFn: () => client.accounts.getBirthdays(day, month).then((accounts) => {
dispatch(importEntities({ accounts }));
return accounts.map(({ id }) => id);
}),
});
};
export { useBirthdayReminders };

View file

@ -1,11 +1,9 @@
import React, { useRef } from 'react'; import React, { useRef, useState } from 'react';
import { FormattedMessage } from 'react-intl'; import { FormattedMessage } from 'react-intl';
import { fetchBirthdayReminders } from 'pl-fe/actions/accounts'; import { useBirthdayReminders } from 'pl-fe/api/hooks/account-lists/use-birthday-reminders';
import Widget from 'pl-fe/components/ui/widget'; import Widget from 'pl-fe/components/ui/widget';
import AccountContainer from 'pl-fe/containers/account-container'; import AccountContainer from 'pl-fe/containers/account-container';
import { useAppDispatch } from 'pl-fe/hooks/use-app-dispatch';
import { useAppSelector } from 'pl-fe/hooks/use-app-selector';
const timeToMidnight = () => { const timeToMidnight = () => {
const now = new Date(); const now = new Date();
@ -14,31 +12,36 @@ const timeToMidnight = () => {
return midnight.getTime() - now.getTime(); return midnight.getTime() - now.getTime();
}; };
const getCurrentDate = () => {
const date = new Date();
const day = date.getDate();
const month = date.getMonth() + 1;
return [day, month];
};
interface IBirthdayPanel { interface IBirthdayPanel {
limit: number; limit: number;
} }
const BirthdayPanel = ({ limit }: IBirthdayPanel) => { const BirthdayPanel = ({ limit }: IBirthdayPanel) => {
const dispatch = useAppDispatch(); const [[day, month], setDate] = useState(getCurrentDate);
const birthdays = useAppSelector(state => state.user_lists.birthday_reminders[state.me as string]?.items || []); const { data: birthdays = [] } = useBirthdayReminders(month, day);
const birthdaysToRender = birthdays.slice(0, limit); const birthdaysToRender = birthdays.slice(0, limit);
const timeout = useRef<NodeJS.Timeout>(); const timeout = useRef<NodeJS.Timeout>();
const handleFetchBirthdayReminders = () => {
const date = new Date();
const day = date.getDate();
const month = date.getMonth() + 1;
dispatch(fetchBirthdayReminders(month, day))?.then(() => {
timeout.current = setTimeout(() => handleFetchBirthdayReminders(), timeToMidnight());
});
};
React.useEffect(() => { React.useEffect(() => {
handleFetchBirthdayReminders(); const updateTimeout = () => {
timeout.current = setTimeout(() => {
setDate(getCurrentDate);
updateTimeout();
}, timeToMidnight());
};
updateTimeout();
return () => { return () => {
if (timeout.current) { if (timeout.current) {
@ -65,4 +68,4 @@ const BirthdayPanel = ({ limit }: IBirthdayPanel) => {
); );
}; };
export { BirthdayPanel as default }; export { BirthdayPanel as default, getCurrentDate };

View file

@ -1,16 +1,18 @@
import React from 'react'; import React, { useState } from 'react';
import { FormattedMessage } from 'react-intl'; import { FormattedMessage } from 'react-intl';
import { useBirthdayReminders } from 'pl-fe/api/hooks/account-lists/use-birthday-reminders';
import { getCurrentDate } from 'pl-fe/components/birthday-panel';
import ScrollableList from 'pl-fe/components/scrollable-list'; import ScrollableList from 'pl-fe/components/scrollable-list';
import Modal from 'pl-fe/components/ui/modal'; import Modal from 'pl-fe/components/ui/modal';
import Spinner from 'pl-fe/components/ui/spinner'; import Spinner from 'pl-fe/components/ui/spinner';
import Account from 'pl-fe/features/birthdays/account'; import Account from 'pl-fe/features/birthdays/account';
import { useAppSelector } from 'pl-fe/hooks/use-app-selector';
import type { BaseModalProps } from '../modal-root'; import type { BaseModalProps } from '../modal-root';
const BirthdaysModal = ({ onClose }: BaseModalProps) => { const BirthdaysModal = ({ onClose }: BaseModalProps) => {
const accountIds = useAppSelector(state => state.user_lists.birthday_reminders[state.me as string]?.items); const [[day, month]] = useState(getCurrentDate);
const { data: accountIds } = useBirthdayReminders(month, day);
const onClickClose = () => { const onClickClose = () => {
onClose('BIRTHDAYS'); onClose('BIRTHDAYS');

View file

@ -6,7 +6,6 @@ import {
FOLLOW_REQUEST_AUTHORIZE_SUCCESS, FOLLOW_REQUEST_AUTHORIZE_SUCCESS,
FOLLOW_REQUEST_REJECT_SUCCESS, FOLLOW_REQUEST_REJECT_SUCCESS,
PINNED_ACCOUNTS_FETCH_SUCCESS, PINNED_ACCOUNTS_FETCH_SUCCESS,
BIRTHDAY_REMINDERS_FETCH_SUCCESS,
type AccountsAction, type AccountsAction,
} from 'pl-fe/actions/accounts'; } from 'pl-fe/actions/accounts';
import { FAMILIAR_FOLLOWERS_FETCH_SUCCESS, type FamiliarFollowersAction } from 'pl-fe/actions/familiar-followers'; import { FAMILIAR_FOLLOWERS_FETCH_SUCCESS, type FamiliarFollowersAction } from 'pl-fe/actions/familiar-followers';
@ -50,7 +49,7 @@ interface ReactionList {
} }
type ListKey = 'follow_requests'; type ListKey = 'follow_requests';
type NestedListKey = 'reblogged_by' | 'favourited_by' | 'disliked_by' | 'pinned' | 'birthday_reminders' | 'familiar_followers' | 'membership_requests' | 'group_blocks'; type NestedListKey = 'reblogged_by' | 'favourited_by' | 'disliked_by' | 'pinned' | 'familiar_followers' | 'membership_requests' | 'group_blocks';
type State = Record<ListKey, List> & Record<NestedListKey, Record<string, List>> & { type State = Record<ListKey, List> & Record<NestedListKey, Record<string, List>> & {
reactions: Record<string, ReactionList>; reactions: Record<string, ReactionList>;
@ -63,7 +62,6 @@ const initialState: State = {
reactions: {}, reactions: {},
follow_requests: { next: null, items: [], isLoading: false }, follow_requests: { next: null, items: [], isLoading: false },
pinned: {}, pinned: {},
birthday_reminders: {},
familiar_followers: {}, familiar_followers: {},
membership_requests: {}, membership_requests: {},
group_blocks: {}, group_blocks: {},
@ -155,8 +153,6 @@ const userLists = (state = initialState, action: AccountsAction | FamiliarFollow
return removeFromList(state, ['follow_requests'], action.accountId); return removeFromList(state, ['follow_requests'], action.accountId);
case PINNED_ACCOUNTS_FETCH_SUCCESS: case PINNED_ACCOUNTS_FETCH_SUCCESS:
return normalizeList(state, ['pinned', action.accountId], action.accounts, action.next); return normalizeList(state, ['pinned', action.accountId], action.accounts, action.next);
case BIRTHDAY_REMINDERS_FETCH_SUCCESS:
return normalizeList(state, ['birthday_reminders', action.accountId], action.accounts);
case FAMILIAR_FOLLOWERS_FETCH_SUCCESS: case FAMILIAR_FOLLOWERS_FETCH_SUCCESS:
return normalizeList(state, ['familiar_followers', action.accountId], action.accounts); return normalizeList(state, ['familiar_followers', action.accountId], action.accounts);
case GROUP_BLOCKS_FETCH_SUCCESS: case GROUP_BLOCKS_FETCH_SUCCESS: