forked from mattermost/mattermost-webapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
abstract_list.tsx
182 lines (161 loc) · 6.09 KB
/
abstract_list.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React from 'react';
import {FormattedMessage} from 'react-intl';
import {ActionFunc} from 'mattermost-redux/types/actions';
import NextIcon from 'components/widgets/icons/fa_next_icon';
import PreviousIcon from 'components/widgets/icons/fa_previous_icon';
import './abstract_list.scss';
const PAGE_SIZE = 10;
type Props = {
userId: string;
headerLabels: Record<string, any>[];
data: Record<string, any>[];
onPageChangedCallback?: (paging: Paging) => void;
total: number;
renderRow: (item: {[x: string]: string}) => JSX.Element;
emptyListTextId: string;
emptyListTextDefaultMessage: string;
actions: {
getTeamsData: (userId: string) => ActionFunc & Partial<{then: (func: () => void) => void}> | Promise<Record<string, any>>;
removeGroup?: () => void;
};
}
type State = {
loading: boolean;
page: number;
}
type Paging = {
startCount: number;
endCount: number;
total: number;
}
export default class AbstractList extends React.PureComponent<Props, State> {
public static defaultProps = {
data: [],
};
public constructor(props: Props) {
super(props);
this.state = {
loading: true,
page: 0,
};
}
public componentDidMount() {
this.performSearch(this.state.page);
}
private previousPage = async (e: React.MouseEvent<HTMLButtonElement>): Promise<void> => {
e.preventDefault();
const page = this.state.page < 1 ? 0 : this.state.page - 1;
this.setState({page, loading: true});
this.performSearch(page);
}
private nextPage = async (e: React.MouseEvent<HTMLButtonElement>): Promise<void> => {
e.preventDefault();
const page = this.state.page + 1;
this.setState({page, loading: true});
this.performSearch(page);
}
private performSearch = (page: number): void => {
const newState = {...this.state};
const userId = this.props.userId;
delete newState.page;
newState.loading = true;
this.setState(newState);
this.props.actions.getTeamsData(userId).then!(() => {
if (this.props.onPageChangedCallback) {
this.props.onPageChangedCallback(this.getPaging());
}
this.setState({loading: false});
});
}
private getPaging(): Paging {
const startCount = (this.state.page * PAGE_SIZE) + 1;
let endCount = (this.state.page * PAGE_SIZE) + PAGE_SIZE;
const total = this.props.total;
if (endCount > total) {
endCount = total;
}
return {startCount, endCount, total};
}
private renderHeaderLabels = (): React.ReactFragment => {
return (
<React.Fragment>
{this.props.headerLabels.map((headerLabel, id) => (
<div
key={id}
className='AbstractList__header-label'
style={headerLabel.style}
>{headerLabel.default}</div>
))}
</React.Fragment>
);
}
private renderRows = (): JSX.Element | JSX.Element[] => {
if (this.state.loading) {
return (
<div className='AbstractList__loading'>
<i className='fa fa-spinner fa-pulse fa-2x'/>
</div>
);
}
if (this.props.data.length === 0) {
return (
<div className='AbstractList__empty'>
<FormattedMessage
id={this.props.emptyListTextId}
defaultMessage={this.props.emptyListTextDefaultMessage}
/>
</div>
);
}
const pageStart = this.state.page < 1 ? 0 : (this.state.page * PAGE_SIZE); // ie 0, 10, 20, etc.
const pageEnd = this.state.page < 1 ? PAGE_SIZE : (this.state.page + 1) * PAGE_SIZE; // ie 10, 20, 30, etc.
const pageData = this.props.data.slice(pageStart, pageEnd).map(this.props.renderRow); // ie 0-10, 10-20, etc.
return pageData;
}
public render = (): JSX.Element => {
const {startCount, endCount, total} = this.getPaging();
const lastPage = endCount === total;
const firstPage = this.state.page === 0;
return (
<div className='AbstractList'>
<div className='AbstractList__header'>
{this.renderHeaderLabels()}
</div>
<div className='AbstractList__body'>
{this.renderRows()}
</div>
{total > 0 &&
<div className='AbstractList__footer'>
<div className='counter'>
<FormattedMessage
id='admin.team_channel_settings.list.paginatorCount'
defaultMessage='{startCount, number} - {endCount, number} of {total, number}'
values={{
startCount,
endCount,
total,
}}
/>
</div>
<button
className={'btn btn-link prev ' + (firstPage ? 'disabled' : '')}
onClick={firstPage ? () => null : this.previousPage}
disabled={firstPage}
>
<PreviousIcon/>
</button>
<button
className={'btn btn-link next ' + (lastPage ? 'disabled' : '')}
onClick={lastPage ? () => null : this.nextPage}
disabled={lastPage}
>
<NextIcon/>
</button>
</div>
}
</div>
);
}
}