Move legacy functions into separate utils file
This commit is contained in:
parent
e789b44792
commit
89c9e32b59
2 changed files with 69 additions and 60 deletions
|
@ -1,5 +1,4 @@
|
|||
import { Record as ImmutableRecord } from 'immutable';
|
||||
import { default as lodashGet } from 'lodash/get';
|
||||
import { combineReducers } from 'redux-immutable';
|
||||
import { createSelector } from 'reselect';
|
||||
|
||||
|
@ -7,6 +6,7 @@ import { AUTH_LOGGED_OUT } from 'soapbox/actions/auth';
|
|||
import * as BuildConfig from 'soapbox/build-config';
|
||||
import { Entities } from 'soapbox/entity-store/entities';
|
||||
import entities from 'soapbox/entity-store/reducer';
|
||||
import { immutableizeStore } from 'soapbox/utils/legacy';
|
||||
|
||||
import account_notes from './account-notes';
|
||||
import accounts_counters from './accounts-counters';
|
||||
|
@ -75,65 +75,6 @@ import type { AnyAction, Reducer } from 'redux';
|
|||
import type { EntityStore } from 'soapbox/entity-store/types';
|
||||
import type { Account } from 'soapbox/schemas';
|
||||
|
||||
interface LegacyMap {
|
||||
get(key: any): unknown
|
||||
getIn(keyPath: any[]): unknown
|
||||
toJS(): any
|
||||
}
|
||||
|
||||
interface LegacyStore<T> extends LegacyMap {
|
||||
get(key: any): T & LegacyMap | undefined
|
||||
getIn(keyPath: any[]): unknown
|
||||
find(predicate: (value: T & LegacyMap, key: string) => boolean): T & LegacyMap | undefined
|
||||
filter(predicate: (value: T & LegacyMap, key: string) => boolean): (T & LegacyMap)[]
|
||||
}
|
||||
|
||||
function immutableizeEntity<T extends Record<any, any>>(entity: T): T & LegacyMap {
|
||||
return {
|
||||
...entity,
|
||||
|
||||
get(key: any): unknown {
|
||||
return entity[key];
|
||||
},
|
||||
|
||||
getIn(keyPath: any[]): unknown {
|
||||
return lodashGet(entity, keyPath);
|
||||
},
|
||||
|
||||
toJS() {
|
||||
return entity;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function immutableizeStore<T, S extends Record<string, T | undefined>>(state: S): S & LegacyStore<T> {
|
||||
return {
|
||||
...state,
|
||||
|
||||
get(id: any): T & LegacyMap | undefined {
|
||||
const entity = state[id];
|
||||
return entity ? immutableizeEntity(entity) : undefined;
|
||||
},
|
||||
|
||||
getIn(keyPath: any[]): unknown {
|
||||
return lodashGet(state, keyPath);
|
||||
},
|
||||
|
||||
find(predicate: (value: T & LegacyMap, key: string) => boolean): T & LegacyMap | undefined {
|
||||
const result = Object.entries(state).find(([key, value]) => value && predicate(immutableizeEntity(value), key))?.[1];
|
||||
return result ? immutableizeEntity(result) : undefined;
|
||||
},
|
||||
|
||||
filter(predicate: (value: T & LegacyMap, key: string) => boolean): (T & LegacyMap)[] {
|
||||
return Object.entries(state).filter(([key, value]) => value && predicate(immutableizeEntity(value), key)).map(([key, value]) => immutableizeEntity(value!));
|
||||
},
|
||||
|
||||
toJS() {
|
||||
return state;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const reducers = {
|
||||
account_notes,
|
||||
accounts_counters,
|
||||
|
|
68
app/soapbox/utils/legacy.ts
Normal file
68
app/soapbox/utils/legacy.ts
Normal file
|
@ -0,0 +1,68 @@
|
|||
import { default as lodashGet } from 'lodash/get';
|
||||
|
||||
interface LegacyMap {
|
||||
get(key: any): unknown
|
||||
getIn(keyPath: any[]): unknown
|
||||
toJS(): any
|
||||
}
|
||||
|
||||
interface LegacyStore<T> extends LegacyMap {
|
||||
get(key: any): T & LegacyMap | undefined
|
||||
getIn(keyPath: any[]): unknown
|
||||
find(predicate: (value: T & LegacyMap, key: string) => boolean): T & LegacyMap | undefined
|
||||
filter(predicate: (value: T & LegacyMap, key: string) => boolean): (T & LegacyMap)[]
|
||||
}
|
||||
|
||||
function immutableizeEntity<T extends Record<any, any>>(entity: T): T & LegacyMap {
|
||||
return {
|
||||
...entity,
|
||||
|
||||
get(key: any): unknown {
|
||||
return entity[key];
|
||||
},
|
||||
|
||||
getIn(keyPath: any[]): unknown {
|
||||
return lodashGet(entity, keyPath);
|
||||
},
|
||||
|
||||
toJS() {
|
||||
return entity;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function immutableizeStore<T, S extends Record<string, T | undefined>>(state: S): S & LegacyStore<T> {
|
||||
return {
|
||||
...state,
|
||||
|
||||
get(id: any): T & LegacyMap | undefined {
|
||||
const entity = state[id];
|
||||
return entity ? immutableizeEntity(entity) : undefined;
|
||||
},
|
||||
|
||||
getIn(keyPath: any[]): unknown {
|
||||
return lodashGet(state, keyPath);
|
||||
},
|
||||
|
||||
find(predicate: (value: T & LegacyMap, key: string) => boolean): T & LegacyMap | undefined {
|
||||
const result = Object.entries(state).find(([key, value]) => value && predicate(immutableizeEntity(value), key))?.[1];
|
||||
return result ? immutableizeEntity(result) : undefined;
|
||||
},
|
||||
|
||||
filter(predicate: (value: T & LegacyMap, key: string) => boolean): (T & LegacyMap)[] {
|
||||
return Object.entries(state).filter(([key, value]) => value && predicate(immutableizeEntity(value), key)).map(([key, value]) => immutableizeEntity(value!));
|
||||
},
|
||||
|
||||
toJS() {
|
||||
return state;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
export {
|
||||
immutableizeStore,
|
||||
immutableizeEntity,
|
||||
type LegacyMap,
|
||||
type LegacyStore,
|
||||
};
|
Loading…
Reference in a new issue