index.tsx
4.59 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { StackActions, useNavigation } from '@react-navigation/native';
import { get, isEmpty } from 'lodash/fp';
import { Spinner as NativeSpinner, View } from 'native-base';
import React, { useCallback, useEffect, useState } from 'react';
import {
DataProvider,
LayoutProvider,
RecyclerListView
} from 'recyclerlistview';
import { TabNavigation } from '../../../types/navigation';
import ListGrid, { HeaderRow } from '../../ListGrid';
import HeaderColumn from '../../ListGrid/HeaderColumn';
import { CardHeight } from '../../player-card/constants';
import { COLUMNS, DEFAULT_SORT } from '../constants';
import Item from './Item';
interface Props {
players: PlayerEdge[];
franchisePlayers: Dictionary<FranchisePlayer>;
onSort: (field: string, order: string) => void;
onFetchMore: VoidFunction;
isFetching: boolean;
}
const layoutProvider = new LayoutProvider(
() => 'MARKET_LIST',
(_, dim) => {
// eslint-disable-next-line no-param-reassign
dim.width = 500;
// eslint-disable-next-line no-param-reassign
dim.height = CardHeight + 1;
}
);
const MainList: React.FC<Props> = ({
isFetching,
onFetchMore,
franchisePlayers,
onSort,
players
}: Props) => {
const navigation = useNavigation<TabNavigation>();
const [sortBy, setSortBy] = useState<string>(DEFAULT_SORT.COLUMN);
const [sortDirection, setSortDirection] = useState<SortType>(
DEFAULT_SORT.DIRECTION
);
const [dataProvider, setDataProvider] = useState<DataProvider>(
new DataProvider((r1, r2) => r1 !== r2)
);
useEffect(() => {
if (sortBy && sortDirection) {
onSort(sortBy, sortDirection);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [sortBy, sortDirection]);
useEffect(() => {
if (!isEmpty(players)) {
setDataProvider(dataProvider.cloneWithRows(players));
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [players]);
const handleOnSort = (colName: string) => () => {
if (colName === sortBy) {
setSortDirection(sortDirection === 'ASC' ? 'DESC' : 'ASC');
} else {
setSortBy(colName);
setSortDirection('ASC');
}
};
const getColumnSortDirection = (colName: string) =>
colName === sortBy ? sortDirection : '';
const renderFooter = useCallback(
() => (isFetching ? <NativeSpinner /> : <View />),
[isFetching]
);
const navigateToProfile = (selectedIndex: number) => () => {
navigation.dispatch(
StackActions.push('PlayerView', {
players,
selectedIndex,
franchisePlayers
})
);
};
const renderRow = useCallback(
(_, data: PlayerEdge, index: number) => {
return (
<Item
data={data}
activeSort={sortBy}
isOwned={!isEmpty(get(data.node.id, franchisePlayers))}
onPress={navigateToProfile(index)}
/>
);
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[dataProvider]
);
const hasPlayerData = dataProvider.getSize() > 0;
return (
<ListGrid style={{ marginHorizontal: 6 }}>
<HeaderRow style={{ height: 45, marginBottom: 20 }}>
<HeaderColumn align="center" size={4} fontSize="sm" label="PLAYER" />
<HeaderColumn
sortable
size={2}
fontSize="sm"
label="Rank"
onSort={handleOnSort(COLUMNS.RANK)}
sortDirection={getColumnSortDirection(COLUMNS.RANK)}
/>
<HeaderColumn
sortable
size={2.5}
fontSize="sm"
label="FP"
helpText="FANTASY PTS"
onSort={handleOnSort(COLUMNS.FP)}
sortDirection={getColumnSortDirection(COLUMNS.FP)}
/>
<HeaderColumn
sortable
size={2.5}
fontSize="sm"
label="FPPG"
helpText={'FANTASY PTS\n(per game)'}
onSort={handleOnSort(COLUMNS.FPPG)}
sortDirection={getColumnSortDirection(COLUMNS.FPPG)}
/>
<HeaderColumn
sortable
size={2.3}
fontSize="sm"
label="Price"
helpText={'TREND\n(24hrs)'}
onSort={handleOnSort(COLUMNS.PRICE)}
sortDirection={getColumnSortDirection(COLUMNS.PRICE)}
/>
</HeaderRow>
{hasPlayerData && (
<RecyclerListView
style={{ flex: 1 }}
dataProvider={dataProvider}
layoutProvider={layoutProvider}
rowRenderer={renderRow}
initialRenderIndex={0}
renderAheadOffset={1500}
renderFooter={renderFooter}
onEndReached={onFetchMore}
onEndReachedThreshold={2000}
/>
)}
</ListGrid>
);
};
export default MainList;