wip hooks migration

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak 2024-09-28 22:51:54 +02:00
parent c6be339ba0
commit c740b19e30
2 changed files with 18 additions and 11 deletions

View file

@ -1,6 +1,6 @@
import { getClient } from '../api';
import { importEntities } from 'pl-fe/pl-hooks/importer';
import { importFetchedPoll } from './importer';
import { getClient } from '../api';
import type { Poll } from 'pl-api';
import type { AppDispatch, RootState } from 'pl-fe/store';
@ -18,7 +18,7 @@ const vote = (pollId: string, choices: number[]) =>
dispatch(voteRequest());
return getClient(getState()).polls.vote(pollId, choices).then((data) => {
dispatch(importFetchedPoll(data));
importEntities({ polls: [data] });
dispatch(voteSuccess(data));
}).catch(err => dispatch(voteFail(err)));
};
@ -28,7 +28,7 @@ const fetchPoll = (pollId: string) =>
dispatch(fetchPollRequest());
return getClient(getState()).polls.getPoll(pollId).then((data) => {
dispatch(importFetchedPoll(data));
importEntities({ polls: [data] });
dispatch(fetchPollSuccess(data));
}).catch(err => dispatch(fetchPollFail(err)));
};

View file

@ -18,7 +18,6 @@ import type {
RelationshipSeveranceEvent,
} from 'pl-api';
const minifyNotification = (notification: DeduplicatedNotification) => {
// @ts-ignore
const minifiedNotification: {
@ -98,9 +97,15 @@ const importNotification = (notification: DeduplicatedNotification) => {
);
};
const isEmpty = (object: Record<string, any>) => {
for (const i in object) return false;
return true;
};
const importEntities = (entities: {
accounts?: Array<BaseAccount>;
notifications?: Array<DeduplicatedNotification>;
polls?: Array<BasePoll>;
statuses?: Array<BaseStatus>;
relationships?: Array<BaseRelationship>;
}) => {
@ -141,14 +146,16 @@ const importEntities = (entities: {
entities.accounts?.forEach(processAccount);
entities.notifications?.forEach(processNotification);
entities.polls?.forEach(poll => polls[poll.id] = poll);
entities.relationships?.forEach(relationship => relationships[relationship.id] = relationship);
entities.statuses?.forEach(processStatus);
dispatch(importAccounts(Object.values(accounts)));
dispatch(importGroups(Object.values(groups)));
Object.values(notifications).forEach(importNotification);
dispatch(importPolls(Object.values(polls)));
dispatch(importStatuses(Object.values(statuses)));
dispatch(importEntityStoreEntities(Object.values(relationships), Entities.RELATIONSHIPS));
if (!isEmpty(accounts)) dispatch(importAccounts(Object.values(accounts)));
if (!isEmpty(groups)) dispatch(importGroups(Object.values(groups)));
if (!isEmpty(notifications)) Object.values(notifications).forEach(importNotification);
if (!isEmpty(polls)) dispatch(importPolls(Object.values(polls)));
if (!isEmpty(relationships)) dispatch(importEntityStoreEntities(Object.values(relationships), Entities.RELATIONSHIPS));
if (!isEmpty(statuses)) dispatch(importStatuses(Object.values(statuses)));
};
export { importEntities };