HM-133. Рефакторинг (#64)

This commit is contained in:
Nikolay
2020-09-02 20:07:26 +03:00
committed by GitHub
parent caa7671b83
commit bc9b112904
132 changed files with 421 additions and 757 deletions

View 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;