From c740b19e30e1a7d80950fc29e6fcd193c60b58c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Sat, 28 Sep 2024 22:51:54 +0200 Subject: [PATCH] wip hooks migration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- packages/pl-fe/src/actions/polls.ts | 8 ++++---- packages/pl-fe/src/pl-hooks/importer.ts | 21 ++++++++++++++------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/packages/pl-fe/src/actions/polls.ts b/packages/pl-fe/src/actions/polls.ts index f343be4ec..f4f7e0c7a 100644 --- a/packages/pl-fe/src/actions/polls.ts +++ b/packages/pl-fe/src/actions/polls.ts @@ -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))); }; diff --git a/packages/pl-fe/src/pl-hooks/importer.ts b/packages/pl-fe/src/pl-hooks/importer.ts index 84f56786e..127284d35 100644 --- a/packages/pl-fe/src/pl-hooks/importer.ts +++ b/packages/pl-fe/src/pl-hooks/importer.ts @@ -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) => { + for (const i in object) return false; + return true; +}; + const importEntities = (entities: { accounts?: Array; notifications?: Array; + polls?: Array; statuses?: Array; relationships?: Array; }) => { @@ -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 };