Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
9.8 kB
1
Indexable
Never
/*******************************************************************************
*
*  Copyright (C) Capgemini Engineering ACT S.A.S. 2017-2023.  All rights reserved.
*
********************************************************************************/
/*******************************************************************************
*
*  Copyright (C) Altran ACT S.A.S. 2018,2019,2020,2021.  All rights reserved.
*
********************************************************************************/

import { Component, Inject, OnInit } from '@angular/core';
import { DataTableV2, Modal } from 'carbon-components';
import { AppService } from '../../../app.service';
import { ConfigConstant } from '../../../app.constant';
import { OverflowMenuModel, CommonUtilitiesLib, StorageUtilityLib, AuthticationLib ,
    SearchModel, PaginationModel } from '../../../shared';
import { Router, ActivatedRoute } from '@angular/router';
import { AppCommonService } from '../../../app-common.service';
import { DeleteConfirmationModel } from '../../../shared/models';
import { TranslateService } from '@ngx-translate/core';
import { environment } from '../../../../environments/environment';
import { ToastrService } from 'ngx-toastr';
import { Trans } from '../../../translate-service.constant';

/**
 * Class contain the property and functions of list Nodes  component
 *
 * @export
 * @class AppAdminNodeListComponent
 */
@Component({
    templateUrl: './admin.listing.component.html',
    styleUrls: ['./admin.listing.component.scss']
})
export class AppAdminNodeListComponent implements OnInit {

    tableHeaderKeys = ['adminNodeName', 'nodeIpAddress', 'port', 'adminProtocol', 'nodeDescription'];
    tableData = [];
    editDeleteOverFlowMenuConf: OverflowMenuModel;
    searchConf: SearchModel;
    deleteConfirmationTab: DeleteConfirmationModel;
    deleteAuthModal: Modal;
    rowNumbToDelete: any;
    textLimit: number = ConfigConstant.getData('grid').maxColumnText;
    paginationConf: PaginationModel;
    sourceData: any;
    exportData = [];
    showMoreRecordNotification: Boolean = false;
    headerListSearch = [];
    listlength = 0;
    keyListTemp: any[] = [];
    constructor(
        protected appService: AppService,
        private router: Router,
        private toastr: ToastrService,
        private commonUtilitiesLib: CommonUtilitiesLib,
        private appCommonService: AppCommonService,
        private translateService: TranslateService,
        private activeRoute: ActivatedRoute,
        private authticationLib: AuthticationLib,
        @Inject(Trans) private trans: Trans
    ) {


    }

    ngOnInit() {
        this.deleteAuthModal = Modal.create(document.querySelector('[data-modal].delete-confirm'));
        const that = this;
        this.getAllNodesAdmin();
        this.editDeleteOverFlowMenuConf = {
            menuImage: 'assets/source-images/menu_icon.svg',
            menuAltImage: 'assets/source-images/menu_colored_icon.svg',
            menuStyleConf: {
                parentClass: 'custom-datatable-menu',
                optionsClass: 'custom-datatable-menu-options'
            },
            menuOptions: [
                {
                    label: 'adminListing.edit',
                    actionType: 'edit'
                },
                {
                    label: 'adminListing.delete',
                    actionType: 'delete'
                }
            ],
            actionsHandler: {
                doAction: (rowItem, actionType) => {
                    switch (actionType) {
                        case 'delete':
                        that.deleteAuthModal.show();
                        this.rowNumbToDelete = rowItem;
                            break;
                        case 'edit':
                            this.editNode(rowItem);
                            break;
                    }
                }
            }
        };
        this.searchConf = {
            searchText: ''
        };

        this.deleteConfirmationTab = {
            primaryHeadingNormal: 'adminListing.deleteConfirmation',
            primaryHeadingBold: 'adminListing.deleteConfirmationLabel',
            primaryHeadingUrl: '',
            primarybutton: {
                label: 'adminListing.yesButton',
                url: [],
                activeUrl: '',
                customAction: false,
                actionType: 'primary',
                className: '',
                showActive: false,
                activeLogo: '',
                showAlert: false,
                alertClass: '',
                alertlogo: ''
            },
            secondarybutton: {
                label: 'adminListing.noButton',
                url: [],
                activeUrl: '',
                customAction: false,
                actionType: 'secondary',
                className: '',
                showActive: false,
                activeLogo: '',
                showAlert: false,
                alertClass: '',
                alertlogo: ''
            },
            actionsHandler: {
                decideAction: function (actionType) {
                    switch (actionType) {
                        case 'primary':
                        that.deleteNode(that.rowNumbToDelete);
                        break;
                        case 'secondary':
                            break;
                    }
                }
            }
        };
        this.paginationConf = {
            showTotalItems: false,
            showTotalPage: false,
            disablePreviousBtn: true,
            disableForwardBtn: true,
            itemLabel: 'adminListing.nodesPerPage',
            itemPerPageOption: [10, 20, 30, 40, 50],
            itemPerPage: 10,
            pageNumber: 1,
            actionsHandler: {
                doAction: (pageNo, itemPerPage) => {
                    this.displayData(pageNo, itemPerPage);
                }
            }
        };

    }

    getAllNodesAdmin() {
        const dummyGrpJson = {};
        dummyGrpJson['adminNodeName'] = '*ALL';

        this.appService.masterWrapperAdmin(
            this.appCommonService.designMaterRequest({
                'allparameters': this.commonUtilitiesLib.copy(dummyGrpJson),
                'verbtype': 'get',
                'apisname': ['ADMINUSERNODENAMES'],
                'responseformat': 'rest'
            })
        ).subscribe((success: any) => {
            const nodes = success.data[0].ADMINUSERNODENAMES;
            if(!nodes[0]['messageCode']) {
                this.exportData = this.commonUtilitiesLib.copy(nodes);
                for (let i = 0; i < nodes.length; i++) {
                    nodes[i].adminProtocol = nodes[i].adminProtocol.toUpperCase();
                    this.tableData[i] = nodes[i];
                    this.tableData[i]['highlight'] = false;
                }
            }
            this.sourceData = this.tableData;
            this.paginationConf.pageNumber = 1;
            this.displayData( this.paginationConf.pageNumber, this.paginationConf.itemPerPage);
        }, error => {
            this.tableData = [];
        });
    }

    exportNode() {
        if (this.exportData.length == 0) {
            this.toastr.success('No data to be exported.', '', { easeTime: 3000 });
        } else {
            this.appCommonService.exportNodeList(this.exportData);
        }
    }

    editNode(object: any) {
        this.router.navigate(['/admin', 'new', object.item.adminNodeName, 'editAdminNode']);
    }

    deleteNode(objectID: any) {
        this.appService.masterWrapperAdmin(
            {
                params: {
                    'allparameters': this.commonUtilitiesLib.copy(objectID.item),
                    'allconfig': this.commonUtilitiesLib.copy({
                        'verbtype': 'delete',
                        'apiname': ['ADMINUSERNODENAMES;'],
                        'responseformat': {
                            'format': 'rest'
                        }
                    })
                }
            }
        ).subscribe((success: any) => {
            this.toastr.success(this.translateService.instant('adminListing.deleteSuccesssMessage'));
            this.getAllNodesAdmin();
            this.tableData = [];
            this.deleteAuthModal.hide();
        }, error => {
            this.toastr.error(this.translateService.instant('adminListing.deleteFailMessage'));
        });
    }

    displayData(page, itemPerPage) { 
        if (page === 1) {
            this.paginationConf.disablePreviousBtn = true;
        } else {
            this.paginationConf.disablePreviousBtn = false;
        }

        if (page * itemPerPage < this.sourceData.length) {
            this.paginationConf.disableForwardBtn = false;
        } else {
            this.paginationConf.disableForwardBtn = true;
        }

        if ((page - 1) * itemPerPage <= this.sourceData.length) {
            this.tableData = this.sourceData.slice((page - 1) * itemPerPage, page * itemPerPage);
            this.tableData = (this.tableData[0] && this.tableData[0].adminNodeName) ? this.tableData : [];
            this.listlength = this.tableData.length;
            this.keyListTemp = this.trans.getData('keyList').keyListAll;
            this.keyListTemp.forEach(element => {
                this.headerListSearch.push(element.key);
            });
       } else {
            if (this.sourceData.length > 500) {
                this.showMoreRecordNotification = true;
            }
        }
    }
}