wip hooks migration
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
parent
c6be339ba0
commit
c740b19e30
2 changed files with 18 additions and 11 deletions
|
@ -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 { Poll } from 'pl-api';
|
||||||
import type { AppDispatch, RootState } from 'pl-fe/store';
|
import type { AppDispatch, RootState } from 'pl-fe/store';
|
||||||
|
@ -18,7 +18,7 @@ const vote = (pollId: string, choices: number[]) =>
|
||||||
dispatch(voteRequest());
|
dispatch(voteRequest());
|
||||||
|
|
||||||
return getClient(getState()).polls.vote(pollId, choices).then((data) => {
|
return getClient(getState()).polls.vote(pollId, choices).then((data) => {
|
||||||
dispatch(importFetchedPoll(data));
|
importEntities({ polls: [data] });
|
||||||
dispatch(voteSuccess(data));
|
dispatch(voteSuccess(data));
|
||||||
}).catch(err => dispatch(voteFail(err)));
|
}).catch(err => dispatch(voteFail(err)));
|
||||||
};
|
};
|
||||||
|
@ -28,7 +28,7 @@ const fetchPoll = (pollId: string) =>
|
||||||
dispatch(fetchPollRequest());
|
dispatch(fetchPollRequest());
|
||||||
|
|
||||||
return getClient(getState()).polls.getPoll(pollId).then((data) => {
|
return getClient(getState()).polls.getPoll(pollId).then((data) => {
|
||||||
dispatch(importFetchedPoll(data));
|
importEntities({ polls: [data] });
|
||||||
dispatch(fetchPollSuccess(data));
|
dispatch(fetchPollSuccess(data));
|
||||||
}).catch(err => dispatch(fetchPollFail(err)));
|
}).catch(err => dispatch(fetchPollFail(err)));
|
||||||
};
|
};
|
||||||
|
|
|
@ -18,7 +18,6 @@ import type {
|
||||||
RelationshipSeveranceEvent,
|
RelationshipSeveranceEvent,
|
||||||
} from 'pl-api';
|
} from 'pl-api';
|
||||||
|
|
||||||
|
|
||||||
const minifyNotification = (notification: DeduplicatedNotification) => {
|
const minifyNotification = (notification: DeduplicatedNotification) => {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
const minifiedNotification: {
|
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: {
|
const importEntities = (entities: {
|
||||||
accounts?: Array<BaseAccount>;
|
accounts?: Array<BaseAccount>;
|
||||||
notifications?: Array<DeduplicatedNotification>;
|
notifications?: Array<DeduplicatedNotification>;
|
||||||
|
polls?: Array<BasePoll>;
|
||||||
statuses?: Array<BaseStatus>;
|
statuses?: Array<BaseStatus>;
|
||||||
relationships?: Array<BaseRelationship>;
|
relationships?: Array<BaseRelationship>;
|
||||||
}) => {
|
}) => {
|
||||||
|
@ -141,14 +146,16 @@ const importEntities = (entities: {
|
||||||
|
|
||||||
entities.accounts?.forEach(processAccount);
|
entities.accounts?.forEach(processAccount);
|
||||||
entities.notifications?.forEach(processNotification);
|
entities.notifications?.forEach(processNotification);
|
||||||
|
entities.polls?.forEach(poll => polls[poll.id] = poll);
|
||||||
|
entities.relationships?.forEach(relationship => relationships[relationship.id] = relationship);
|
||||||
entities.statuses?.forEach(processStatus);
|
entities.statuses?.forEach(processStatus);
|
||||||
|
|
||||||
dispatch(importAccounts(Object.values(accounts)));
|
if (!isEmpty(accounts)) dispatch(importAccounts(Object.values(accounts)));
|
||||||
dispatch(importGroups(Object.values(groups)));
|
if (!isEmpty(groups)) dispatch(importGroups(Object.values(groups)));
|
||||||
Object.values(notifications).forEach(importNotification);
|
if (!isEmpty(notifications)) Object.values(notifications).forEach(importNotification);
|
||||||
dispatch(importPolls(Object.values(polls)));
|
if (!isEmpty(polls)) dispatch(importPolls(Object.values(polls)));
|
||||||
dispatch(importStatuses(Object.values(statuses)));
|
if (!isEmpty(relationships)) dispatch(importEntityStoreEntities(Object.values(relationships), Entities.RELATIONSHIPS));
|
||||||
dispatch(importEntityStoreEntities(Object.values(relationships), Entities.RELATIONSHIPS));
|
if (!isEmpty(statuses)) dispatch(importStatuses(Object.values(statuses)));
|
||||||
};
|
};
|
||||||
|
|
||||||
export { importEntities };
|
export { importEntities };
|
||||||
|
|
Loading…
Reference in a new issue