1+ import { Card , CardContent , Grid , IconButton , Table , TableBody , TableCell , TableHead , TableRow } from '@material-ui/core' ;
2+ import { IStateList , ListComponent , TableCellSortable } from 'components/Abstract/List' ;
3+ import Toolbar from 'components/Layout/Toolbar' ;
4+ import FabButton from 'components/Shared/FabButton' ;
5+ import TableWrapper from 'components/Shared/TableWrapper' ;
6+ import { IPaginationParams } from 'interfaces/pagination' ;
7+ import { IUser } from 'interfaces/user' ;
8+ import AccountPlusIcon from 'mdi-react/AccountPlusIcon' ;
9+ import RefreshIcon from 'mdi-react/RefreshIcon' ;
10+ import React , { Fragment } from 'react' ;
11+ import rxjsOperators from 'rxjs-operators' ;
12+ import userService from 'services/user' ;
13+
14+ import UserFormDialog from '../UserFormDialog' ;
15+ import ListItem from './ListItem' ;
16+
17+ interface IState extends IStateList < IUser > {
18+ current ?: IUser ;
19+ formOpened ?: boolean ;
20+ }
21+
22+ export default class UserListPage extends ListComponent < { } , IState > {
23+ actions = [ {
24+ icon : AccountPlusIcon ,
25+ onClick : ( ) => this . handleCreate ( )
26+ } ] ;
27+
28+ constructor ( props : { } ) {
29+ super ( props , 'fullName' ) ;
30+ }
31+
32+ componentDidMount ( ) {
33+ this . loadData ( ) ;
34+ }
35+
36+ loadData = ( params : Partial < IPaginationParams > = { } ) => {
37+ this . setState ( { loading : true , error : null } ) ;
38+
39+ userService . list ( this . mergeParams ( params ) ) . pipe (
40+ rxjsOperators . logError ( ) ,
41+ rxjsOperators . bindComponent ( this )
42+ ) . subscribe ( items => {
43+ this . setPaginatedData ( items ) ;
44+ } , error => this . setError ( error ) ) ;
45+ }
46+
47+ handleRefresh = ( ) => this . loadData ( ) ;
48+
49+ handleCreate = ( ) => {
50+ this . setState ( { formOpened : true , current : null } ) ;
51+ }
52+
53+ handleEdit = ( current : IUser ) => {
54+ this . setState ( { formOpened : true , current } ) ;
55+ }
56+
57+ formCallback = ( user ?: IUser ) => {
58+ this . setState ( { formOpened : false } ) ;
59+
60+ this . state . current ?
61+ this . loadData ( ) :
62+ this . handleChangeTerm ( user . email ) ;
63+ }
64+
65+ formCancel = ( ) => {
66+ this . setState ( { formOpened : false } ) ;
67+ }
68+
69+ render ( ) {
70+ const { items, formOpened, loading, current } = this . state ;
71+
72+ return (
73+ < Fragment >
74+ < Toolbar title = 'Usuários' />
75+
76+ < Card >
77+ < FabButton actions = { this . actions } />
78+
79+ < UserFormDialog
80+ opened = { formOpened || false }
81+ user = { current }
82+ onComplete = { this . formCallback }
83+ onCancel = { this . formCancel }
84+ />
85+
86+ { this . renderLoader ( ) }
87+
88+ < CardContent >
89+ < Grid container >
90+ < Grid item xs = { 12 } sm = { 6 } lg = { 4 } >
91+ { this . renderSearch ( ) }
92+ </ Grid >
93+ </ Grid >
94+ </ CardContent >
95+
96+ < TableWrapper minWidth = { 500 } >
97+ < Table >
98+ < TableHead >
99+ < TableRow >
100+ < TableCellSortable { ...this . sortableProps } column = 'fullName' >
101+ Nome
102+ </ TableCellSortable >
103+ < TableCellSortable { ...this . sortableProps } column = 'email' >
104+ Email
105+ </ TableCellSortable >
106+ < TableCell >
107+ < IconButton disabled = { loading } onClick = { this . handleRefresh } >
108+ < RefreshIcon />
109+ </ IconButton >
110+ </ TableCell >
111+ </ TableRow >
112+ </ TableHead >
113+ < TableBody >
114+ { this . renderEmptyAndErrorMessages ( 3 ) }
115+ { items . map ( user =>
116+ < ListItem
117+ key = { user . id }
118+ user = { user }
119+ onEdit = { this . handleEdit }
120+ onDeleteComplete = { this . loadData }
121+ />
122+ ) }
123+ </ TableBody >
124+ </ Table >
125+ </ TableWrapper >
126+ { this . renderTablePagination ( ) }
127+ </ Card >
128+
129+ </ Fragment >
130+ ) ;
131+ }
132+ }
0 commit comments