-
Notifications
You must be signed in to change notification settings - Fork 63
/
withPlayerCards.tsx
80 lines (76 loc) · 2.68 KB
/
withPlayerCards.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { forEach } from 'lodash';
import { connect } from 'react-redux';
import { Results } from 'realm';
import { connectRealm, CardAndTabooSetResults } from 'react-native-realm';
import hoistNonReactStatic from 'hoist-non-react-statics';
import Card, { CardsMap } from '../data/Card';
import FaqEntry from '../data/FaqEntry';
import TabooSet from '../data/TabooSet';
import { AppState, getTabooSet } from '../reducers';
export interface PlayerCardProps {
realm: Realm;
cards: CardsMap;
investigators: CardsMap;
tabooSetId?: number;
tabooSets: Results<TabooSet>;
}
export interface TabooSetOverride {
tabooSetOverride?: number;
}
export default function withPlayerCards<Props, ExtraProps={}>(
WrappedComponent: React.ComponentType<Props & PlayerCardProps & ExtraProps>,
computeExtraProps?: (cards: Results<Card>) => ExtraProps
): React.ComponentType<Props & TabooSetOverride> {
interface ReduxProps {
tabooSetId?: number;
}
const mapStateToProps = (
state: AppState,
props: Props & TabooSetOverride
): ReduxProps => {
return {
tabooSetId: getTabooSet(state, props.tabooSetOverride),
};
};
const result = connect<ReduxProps, {}, Props & TabooSetOverride, AppState>(mapStateToProps)(
connectRealm<Props & ReduxProps, PlayerCardProps & ExtraProps, Card, FaqEntry, TabooSet>(
WrappedComponent, {
schemas: ['Card', 'TabooSet'],
mapToProps(
results: CardAndTabooSetResults<Card, TabooSet>,
realm: Realm,
props: Props & ReduxProps
): PlayerCardProps & ExtraProps {
const playerCards = results.cards.filtered(
`((type_code == "investigator" AND encounter_code == null) OR deck_limit > 0 OR bonded_name != null) and ${Card.tabooSetQuery(props.tabooSetId)}`
);
const investigators: CardsMap = {};
const cards: CardsMap = {};
forEach(
playerCards,
card => {
cards[card.code] = card;
if (card.type_code === 'investigator' && card.encounter_code === null) {
investigators[card.code] = card;
}
});
const playerCardProps: PlayerCardProps = {
realm,
cards,
investigators,
tabooSetId: props.tabooSetId,
tabooSets: results.tabooSets,
};
const extraProps: ExtraProps = computeExtraProps ?
computeExtraProps(playerCards) :
({} as ExtraProps);
return {
...extraProps,
...playerCardProps,
};
},
})
);
hoistNonReactStatic(result, WrappedComponent);
return result as React.ComponentType<Props & TabooSetOverride>;
}