HM-133. Рефакторинг (#64)
This commit is contained in:
78
src/pages/users/components/page/Page.js
Normal file
78
src/pages/users/components/page/Page.js
Normal file
@ -0,0 +1,78 @@
|
||||
import Component from '../../../../core/components/component/Component';
|
||||
import UsersTable from '../users-table/UsersTable';
|
||||
import usersServiceApi from '../../api/UsersServiceAPI';
|
||||
import UserViewForm from '../user-view-form/UserViewForm';
|
||||
import {EVENTS, MODES} from '../../../../core/consts';
|
||||
import routeService from '../../../../services/RouteService';
|
||||
import userInfoService from '../../../../services/UserInfoService';
|
||||
|
||||
class UsersPage extends Component {
|
||||
constructor (mainNodeSelector, parentNode) {
|
||||
super(mainNodeSelector, parentNode);
|
||||
|
||||
this.usersForm = this.createComponent(UserViewForm);
|
||||
|
||||
this.createElement({tagName: 'div', parentNode: this.mainNode});
|
||||
|
||||
this.createUserButton = this.createElement({
|
||||
tagName: 'button',
|
||||
parentNode: this.mainNode,
|
||||
options: {
|
||||
className: 'btn btn-primary m-3',
|
||||
textContent: 'Создать пользователя',
|
||||
},
|
||||
args: {
|
||||
type: 'button',
|
||||
},
|
||||
});
|
||||
|
||||
this.addSubscribe(userInfoService, EVENTS.CHANGE_USER_INFO, ({is_admin}) => {
|
||||
this.createUserButton.disabled = !is_admin;
|
||||
});
|
||||
|
||||
this.addEventListener(this.createUserButton, 'click', () => {
|
||||
routeService.pushQuery({mode: MODES.Create}, true);
|
||||
});
|
||||
|
||||
this.addSubscribe(this.usersForm, EVENTS.CREATE_USER, async (user) => {
|
||||
await usersServiceApi.create(user);
|
||||
this.initPage();
|
||||
});
|
||||
|
||||
this.addSubscribe(this.usersForm, EVENTS.SAVE_USER, async (user) => {
|
||||
await usersServiceApi.update(user);
|
||||
this.initPage();
|
||||
});
|
||||
|
||||
this.addSubscribe(this.usersForm, EVENTS.DELETE_USER, async (login) => {
|
||||
await usersServiceApi.remove(login);
|
||||
this.initPage();
|
||||
});
|
||||
|
||||
this.usersTable = this.createComponent(UsersTable, this.mainNode);
|
||||
|
||||
this.addSubscribe(this.usersTable, EVENTS.ROW_DOUBLE_CLICK, (_, row) => {
|
||||
routeService.pushQuery({
|
||||
mode: MODES.View,
|
||||
login: row.login,
|
||||
});
|
||||
});
|
||||
|
||||
this.initPage();
|
||||
}
|
||||
|
||||
initPage = async () => {
|
||||
const user = await userInfoService.getUserInfo();
|
||||
this.createUserButton.disabled = !user.is_admin;
|
||||
this.userList = await usersServiceApi.request();
|
||||
this.renderTable();
|
||||
}
|
||||
|
||||
renderTable = () => {
|
||||
this.render(() => {
|
||||
this.usersTable.render(this.userList);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export default UsersPage;
|
||||
Reference in New Issue
Block a user