source: phonelux-frontend/src/components/SuperAdminComponent/SuperAdminComponent.js@ 5201690

Last change on this file since 5201690 was 7e88e46, checked in by Marko <Marko@…>, 22 months ago

Added more components

  • Property mode set to 100644
File size: 5.6 KB
Line 
1import { Pagination } from '@mui/material'
2import { styled, alpha } from '@mui/material/styles';
3import axios from 'axios'
4import React, { Component } from 'react'
5import HeaderComponent from '../HeaderComponent/HeaderComponent'
6import UserComponent from '../UserComponent/UserComponent'
7import "./SuperAdminComponent.css"
8import SearchIcon from '@mui/icons-material/Search';
9import InputBase from '@mui/material/InputBase';
10
11
12const Search = styled('div')(({ theme }) => ({
13 position: 'relative',
14 borderRadius: theme.shape.borderRadius,
15 backgroundColor: alpha(theme.palette.common.white, 0.15),
16 '&:hover': {
17 backgroundColor: alpha(theme.palette.common.white, 0.25),
18 },
19 marginRight: theme.spacing(2),
20 marginLeft: 0,
21 width: '100%',
22 [theme.breakpoints.up('sm')]: {
23 marginLeft: theme.spacing(3),
24 width: 'auto',
25 },
26}));
27
28const SearchIconWrapper = styled('div')(({ theme }) => ({
29 padding: theme.spacing(0, 2),
30 height: '100%',
31 position: 'absolute',
32 pointerEvents: 'none',
33 display: 'flex',
34 alignItems: 'center',
35 justifyContent: 'center',
36}));
37
38const StyledInputBase = styled(InputBase)(({ theme }) => ({
39 color: 'inherit',
40 '& .MuiInputBase-input': {
41 padding: theme.spacing(1, 1, 1, 0),
42 // vertical padding + font size from searchIcon
43 paddingLeft: `calc(1em + ${theme.spacing(4)})`,
44 transition: theme.transitions.create('width'),
45 width: '100%',
46 [theme.breakpoints.up('md')]: {
47 width: '20ch',
48 },
49 },
50}));
51
52
53export class SuperAdminComponent extends Component {
54
55 constructor(props) {
56 super(props)
57
58 this.state = {
59 users: [],
60 currentUsers: [],
61 usersPerPage:10,
62 numberOfPages: 0,
63 currentPage: 1,
64 // numberOfUsers: 0,
65 // numberOfAdmins: 0
66 }
67 }
68
69 componentDidMount(){
70 this.getUsers()
71 }
72
73 addAdmin = (id) => {
74 var config = {
75 method: 'put',
76 url: '/management/addadmin/'+id,
77 headers: {
78 'Authorization': 'Bearer '+localStorage.getItem('token')
79 }
80 };
81
82 axios(config)
83 .then(response => {
84 this.getUsers()
85 })
86 .catch(error => {
87 console.log(error);
88 });
89
90 }
91
92 removeAdmin = (id) => {
93 var config = {
94 method: 'put',
95 url: '/management/removeadmin/'+id,
96 headers: {
97 'Authorization': 'Bearer '+localStorage.getItem('token')
98 }
99 };
100
101 axios(config)
102 .then(response => {
103 this.getUsers()
104 })
105 .catch(error => {
106 console.log(error);
107 });
108 }
109
110 getUsers = (filter) =>{
111 let queryParams = '?'
112 if(filter)
113 {
114 queryParams+='searchValue='+filter
115 }
116 var config = {
117 method: 'get',
118 url: '/management/users'+queryParams,
119 headers: {
120 'Authorization': 'Bearer '+localStorage.getItem('token')
121 }
122 };
123
124 axios(config)
125 .then(response => {
126 this.setState({
127 users: response.data,
128 // numberOfUsers: response.data.filter(user => user.userRole == 'USER').length,
129 // numberOfAdmins: response.data.filter(user => user.userRole == 'ADMIN').length,
130 numberOfPages: Math.ceil(response.data.length / this.state.usersPerPage)
131 },(e) => this.setNewPage(e,this.state.currentPage))
132 })
133 .catch(error => {
134 console.log(error);
135 });
136 }
137
138 setNewPage = (event,page) => {
139
140 const indexOfLastUser = parseInt(page) * this.state.usersPerPage;
141 const indexOfFirstUser = indexOfLastUser - this.state.usersPerPage;
142
143 const currUsers = this.state.users.slice(indexOfFirstUser, indexOfLastUser)
144
145 this.setState({
146 currentPage: parseInt(page),
147 currentUsers: currUsers
148 })
149
150 }
151
152 render() {
153 return (
154 <div className='superadmin-section-main'>
155 <HeaderComponent/>
156 <div className='superadmin-section-header'>
157 <h1 className='superadmin-section-header-text'>
158 Менаџмент со корисници
159 </h1>
160 </div>
161 <div className='superadmin-users-section'>
162
163 <table cellPadding={20} className='superadmin-section-table'>
164 <thead className='superadmin-section-table-head'>
165 <tr>
166 <th>Име</th>
167 <th>Презиме</th>
168 <th>Е-маил адреса</th>
169 <th>Привилегии</th>
170 <th>
171 <Search onChange={(event) => this.getUsers(event.target.value)} className="search-user-field">
172 <SearchIconWrapper id="search-user-iconwrapper">
173 <SearchIcon />
174 </SearchIconWrapper>
175 <StyledInputBase
176 className="search-user-input"
177 placeholder="Пребарувај…"
178 inputProps={{ 'aria-label': 'search' }}/>
179 </Search>
180 </th>
181 </tr>
182 </thead>
183 <tbody>
184 {
185 this.state.currentUsers.map((user,idx) => <UserComponent key={idx} id={user.id} firstname={user.firstName}
186 lastname={user.lastName} email={user.email} role={user.userRole} addAdmin={this.addAdmin} removeAdmin={this.removeAdmin}/>)
187 }
188 </tbody>
189 </table>
190
191 </div>
192 <div className='pagination-wrapper'>
193 <Pagination className='superadmin-users-pagination' onChange={this.setNewPage} page={this.state.currentPage}
194 count={this.state.numberOfPages} color="primary" />
195 </div>
196 </div>
197 )
198 }
199}
200
201export default SuperAdminComponent
Note: See TracBrowser for help on using the repository browser.