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

Last change on this file was 47f4eaf, checked in by Marko <Marko@…>, 20 months ago

Final features implemented

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