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, TAG_NAME} 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: TAG_NAME.DIV, parentNode: this.mainNode}); this.createUserButton = this.createElement({ tagName: TAG_NAME.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, EVENTS.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;