|
- import { Component, OnInit } from '@angular/core';
- import { PartnerProfileService } from '../../services/partner-profile.service';
- import { Router } from '@angular/router';
- import * as Papa from 'papaparse';
- import * as XLSX from 'xlsx'
- import { UserData, UserDataOption } from 'src/shared/structure/user';
- import { COUNTRIES, STATES, COMMUNITIES } from 'src/shared/data/form-options';
- import { PartnerType, PARTNER_MAPPING } from 'src/shared/data/partner-mapping';
-
- type exportType = "CSV" | "XLSX";
-
- @Component({
- selector: 'app-table',
- templateUrl: './table.component.html',
- styleUrls: ['./table.component.scss']
- })
-
- export class TableComponent implements OnInit {
- userData: Array<UserData> = [];
- tempUserData: Array<UserData> = [];
- showExportOptions: boolean = false;
- shouldHaveImplementationData: boolean = false;
- searchText: string = '';
- selectedPartnerList: Array<UserData> = [];
- isProfileData: boolean = true;
- isImplementationData: boolean = false;
- isBothData: boolean = false;
-
- communities = COMMUNITIES;
- countries = COUNTRIES;
- states = STATES;
- districts: Array<UserDataOption> = [];
- partnerTypes = [PartnerType.ENABLER, PartnerType.IMPLEMENTER, PartnerType.PROVIDER];
-
- filteredCommunities: Array<string> = [];
- filteredCountries: Array<string> = [];
- filteredStates: Array<string> = [];
- filteredDistricts: Array<string> = [];
- filteredPartnerTypes: Array<PartnerType> = [];
-
- exportData: Array<any> = [];
- exportSurveyCtoData: Array<any> = [];
-
- constructor(
- private partnerProfileService: PartnerProfileService,
- private router: Router
- ) { }
-
- ngOnInit(): void {
- this.getFilteredData();
- }
-
- updateDistricts(states: Array<{ districts: Array<UserDataOption> }>) {
- console.log(states);
- this.districts = states.map(state => state.districts).flat();
- this.getFilteredData();
- }
-
- getFilteredData() {
- this.partnerProfileService.getPartnersData().then((data) => {
- this.userData = data;
-
- const searchText = this.searchText.toLowerCase().trim();
-
- const filteredCommunitySet = new Set(this.filteredCommunities);
- const filteredCountrySet = new Set(this.filteredCountries);
- const filteredStateSet = new Set(this.filteredStates);
- const filteredDistrictSet = new Set(this.filteredDistricts);
- const filteredPartnerTypeSet = new Set(this.filteredPartnerTypes);
-
- this.userData = this.userData.filter(user => {
-
- if (this.shouldHaveImplementationData) {
- const hasImplementationData = (user.surveyCtoData.hiiData && user.surveyCtoData.hiiData.length > 0) ||
- (user.surveyCtoData.spData && user.surveyCtoData.spData.length > 0) ||
- (user.surveyCtoData.spSchemeData && user.surveyCtoData.spSchemeData.length > 0)
-
- if (!hasImplementationData) {
- return false;
- }
- }
-
- if (filteredCommunitySet.size > 0) {
- const communitiesForUser = user.detailedProfile.communities.map(community => community.name);
- const hasCommunities = communitiesForUser.some(community => filteredCommunitySet.has(community));
-
- if (!hasCommunities) {
- return false;
- }
- }
-
- if (filteredCountrySet.size > 0) {
- const countriesForUser = user.detailedProfile.branchLocationCountries.map(country => country.name).concat(user.detailedProfile.partnerLocation);
- const hasCountries = countriesForUser.some(country => filteredCountrySet.has(country));
-
- if (!hasCountries) {
- return false;
- }
- }
-
- if (filteredStateSet.size > 0) {
- const statesForUser = user.detailedProfile.states.map(state => state.name).concat(user.detailedProfile.state);
- const hasStates = statesForUser.some(state => filteredStateSet.has(state));
-
- if (!hasStates) {
- return false;
- }
- }
-
- if (filteredDistrictSet.size > 0) {
- const districtsForUser = user.detailedProfile.districts.map(district => district.name).concat(user.detailedProfile.district);
- const hasDistricts = districtsForUser.some(district => filteredDistrictSet.has(district));
-
- if (!hasDistricts) {
- return false;
- }
- }
-
- if (filteredPartnerTypeSet.size > 0) {
- const partnerTypeOfUser = PARTNER_MAPPING.find(mapping => mapping.portalId === user.portalId)?.type;
- const hasPartnerType = partnerTypeOfUser && filteredPartnerTypeSet.has(partnerTypeOfUser);
-
- if (!hasPartnerType) {
- return false;
- }
- }
-
-
- if (this.searchText) {
- const hasSearchText = (!!user.organizationBasicInfo.name && user.organizationBasicInfo.name.toLowerCase().trim().includes(searchText));
-
- if (!hasSearchText) {
- return false;
- }
- }
-
- return true;
- });
-
- this.partialLoad(this.userData.slice(0, 100))
-
- }, (e) => console.log(e));
- this.selectedPartnerList = [];
- }
-
- partialLoad(data: Array<UserData>) {
- this.tempUserData = data;
- }
-
- loadMore() {
- this.partialLoad(this.userData.slice(0, this.tempUserData.length + 10));
- }
-
- showPartnerDetails(partner: any) {
- this.router.navigate(['dashboard/partners/partner-details'], { queryParams: { data: JSON.stringify(partner) } });
- }
-
- selectPartner(partner: any) {
- if (this.selectedPartnerList) {
- if (this.selectedPartnerList.some(x => JSON.stringify(x) === JSON.stringify(partner))) {
- this.selectedPartnerList = this.selectedPartnerList.filter(x => JSON.stringify(x) !== JSON.stringify(partner))
- } else this.selectedPartnerList.push(partner)
- }
- }
-
- loadExportData() {
- let exportData: Array<any> = [];
- let exportHiiData: Array<any> = [];
- let exportSpData: Array<any> = [];
- let exportSPSchemaData: Array<any> = [];
-
- let surveyCtoData: Array<any> = [];
-
- for (const partner of this.selectedPartnerList) {
- let partnerDetails = {
- "PortalID": partner.portalId ? partner.portalId : '-',
-
- // Primary Contact
-
- "Primary Name": partner.primaryContact.name,
- "Primary Contact Number": partner.primaryContact.contactNumber,
- "Primary Email": partner.primaryContact.email,
- "Primary Designation": partner.primaryContact.designation,
-
- // Basic Info
-
- "Areas Of Work": partner.organizationBasicInfo.areasOfWork ? partner.organizationBasicInfo.areasOfWork.toString() : '',
- "Name": partner.organizationBasicInfo.name,
- "Reason For Becoming Member": partner.organizationBasicInfo.reasonForBecomingMember,
- "Referral Name": partner.organizationBasicInfo.referralName,
- "Source": partner.organizationBasicInfo.source,
- "Type": partner.organizationBasicInfo.type,
- "Website": partner.organizationBasicInfo.website,
- "Would Like Updates": partner.organizationBasicInfo.wouldLikeUpdates,
-
- // Alternative Contact
-
- 'Alternative Name': partner.alternateContact.name,
- 'Alternative ContactNumber': partner.alternateContact.contactNumber,
- 'Alternative Email': partner.alternateContact.email,
- 'Alternative Designation': partner.alternateContact.designation,
-
- // Detailed Profile
-
- 'Bio': partner.detailedProfile.bio,
- 'Branch Location Countries': partner.detailedProfile.branchLocationCountries ? partner.detailedProfile.branchLocationCountries.toString() : '',
- "Communities": partner.detailedProfile.communities ? partner.detailedProfile.communities.toString() : '',
- "District": partner.detailedProfile.district,
- "Districts": partner.detailedProfile.districts ? partner.detailedProfile.districts.toString() : '',
- "Files": partner.detailedProfile.files,
- "Have Branches In Other Districts": partner.detailedProfile.haveBranchesInOtherDistricts,
- "Logo": partner.detailedProfile.logo,
- "partner Location": partner.detailedProfile.partnerLocation,
- "Preferred Languages": partner.detailedProfile.preferredLanguages ? partner.detailedProfile.preferredLanguages.toString() : '',
- "Preferred Mode Of Communications": partner.detailedProfile.preferredModeOfCommunications ? partner.detailedProfile.preferredModeOfCommunications.toString() : '',
- "State": partner.detailedProfile.state,
- "States": partner.detailedProfile.states ? partner.detailedProfile.states.toString() : '',
- "Total Reach Of Organization": partner.detailedProfile.totalReachOfOrganization,
- "Year Of Establishment": partner.detailedProfile.yearOfEstablishment,
-
- // Strength
-
- "Other Specific Support Required": partner.strengthAndCapability.otherSpecificSupportRequired,
- "Primary Areas Of Support Offered": partner.strengthAndCapability.otherSpecificSupportRequired ? partner.strengthAndCapability.otherSpecificSupportRequired.toString() : '',
- "Primary Areas Of Support Offered Description": partner.strengthAndCapability.primaryAreasOfSupportOffered,
- "Primary Areas Of Support Offered Other": partner.strengthAndCapability.primaryAreasOfSupportOfferedDescription,
- "Primary Areas Of Support Required": partner.strengthAndCapability.primaryAreasOfSupportOfferedOther ? partner.strengthAndCapability.primaryAreasOfSupportOfferedOther.toString() : '',
- "Primary Areas Of Support Required Description": partner.strengthAndCapability.primaryAreasOfSupportRequired,
- "Primary Areas Of Support Required Other": partner.strengthAndCapability.primaryAreasOfSupportRequiredOther,
- }
- exportData.push(partnerDetails);
-
- let surveyCtoHIIData = [];
- for (const hiidata of partner.surveyCtoData.hiiData) {
- let partnerHiiDetails = {
- "HII Id": hiidata.id,
- "HII Name": hiidata.name,
- "HII Packages Health": hiidata.packagesHealth,
- "HII Implementation Status": hiidata.implementationStatus,
- "HII Disaggregation Note": hiidata.disaggregationNote,
- "HII No Of Males": hiidata.id,
- "HII No Of Females": hiidata.noOfMales,
- "HII No Of Transgender": hiidata.noOfTransgender,
- "HII Health Remarks": hiidata.healthRemarks,
- "HII Relevant Documents": hiidata.relevantDocuments,
- }
- surveyCtoHIIData.push(partnerHiiDetails)
- }
- surveyCtoHIIData.forEach(data => {
- exportHiiData.push(data)
- })
-
- let surveyCtoSpData = []
- for (const spData of partner.surveyCtoData.spData) {
- let partnerSpDetails = {
- "SP Id": spData.id,
- "SP Name": spData.name,
- "SP Status": spData.status,
- 'SP Reason Name': spData.reasonName,
- "SP Female Number": spData.femaleNo ? spData.femaleNo : '',
- "SP Male Number": spData.maleNo ? spData.maleNo : '',
- "Tg Number": spData.tgNo ? spData.tgNo : '',
- "SP TotalAggregation": spData.totalAggregation,
- "SP OtherRemarks": spData.otherRemarks,
- "SP RelevantDocuments": spData.relevantDocuments,
- }
-
- surveyCtoSpData.push(partnerSpDetails)
- }
- surveyCtoSpData.forEach(data => {
- exportSpData.push(data)
- })
-
- let surveyCtoSpSchemaData = []
- for (const spSchema of partner.surveyCtoData.spSchemeData) {
- let partnerSpSchemaDetails = {
- "SPSchema SchemeId": spSchema.schemeId,
- "SPSchema Count": spSchema.count,
- }
-
- surveyCtoSpSchemaData.push(partnerSpSchemaDetails)
- }
- surveyCtoSpSchemaData.forEach(data => {
- exportSPSchemaData.push(data)
- })
- }
-
-
- surveyCtoData.push(exportHiiData)
- surveyCtoData.push(exportSpData)
- surveyCtoData.push(exportSPSchemaData)
-
- this.exportData = exportData;
- this.exportSurveyCtoData = surveyCtoData
- }
-
- isInputChecked(partner: any) {
- return this.selectedPartnerList.some(x => JSON.stringify(x) === JSON.stringify(partner));
- }
-
- isAllInputChecked() {
- return JSON.stringify(this.selectedPartnerList) === JSON.stringify(this.tempUserData);
- }
-
- selectAllPartner() {
- JSON.stringify(this.selectedPartnerList) === JSON.stringify(this.tempUserData) ? this.selectedPartnerList = [] : this.selectedPartnerList = this.tempUserData;
- }
-
- exportProfileData(exportType: exportType) {
- this.loadExportData();
-
- const fileTypeCSV = 'text/csv;charset=utf-8;',
- element = document.createElement('a');
- let blob;
-
- const partnerData = XLSX.utils.json_to_sheet(this.exportData);
- const hiiData = XLSX.utils.json_to_sheet(this.exportSurveyCtoData[0]);
- const spData = XLSX.utils.json_to_sheet(this.exportSurveyCtoData[1]);
- const spSchema = XLSX.utils.json_to_sheet(this.exportSurveyCtoData[2]);
- const wb = XLSX.utils.book_new();
-
-
- if (exportType === 'CSV') {
- if (this.isProfileData) {
- let csvData = Papa.unparse(this.exportData);
- blob = new Blob([csvData], { type: fileTypeCSV });
- let url = URL.createObjectURL(blob);
- element.href = url;
- element.setAttribute('download', 'ProfileData' + '.csv');
- element.click();
- } else if (this.isImplementationData) {
- this.exportJsonToCSV(this.exportSurveyCtoData, ['HiiData', 'SPData', 'SPSchemaData'])
- } else {
- let completeData = this.exportSurveyCtoData
- completeData.push(this.exportData)
-
- console.log(completeData)
- this.exportJsonToCSV(completeData, ['HiiData', 'SPData', 'SPSchemaData', 'ProfileData',])
- }
- } else {
- if (this.isProfileData) {
- XLSX.utils.book_append_sheet(wb, partnerData, "Partner Profile");
- XLSX.writeFile(wb, 'PartnerProfile.xlsx');
- } else if (this.isImplementationData) {
- XLSX.utils.book_append_sheet(wb, hiiData, "Hii Data");
- XLSX.utils.book_append_sheet(wb, spData, "Sp DATA");
- XLSX.utils.book_append_sheet(wb, spSchema, "SP Schema Data");
- XLSX.writeFile(wb, 'PartnerProfile.xlsx');
- } else {
- XLSX.utils.book_append_sheet(wb, partnerData, "PartneData");
- XLSX.utils.book_append_sheet(wb, hiiData, "HiiData");
- XLSX.utils.book_append_sheet(wb, spData, "SPData");
- XLSX.utils.book_append_sheet(wb, spSchema, "SPSchemaData");
- XLSX.writeFile(wb, 'PartnerProfile.xlsx');
- }
- }
- }
-
- exportJsonToCSV(csvData: any, fileName?: any) {
- const fileTypeCSV = 'text/csv;charset=utf-8;',
- element = document.createElement('a');
- let blob;
-
- csvData.forEach((exportData: any, index: number) => {
- console.log(exportData)
- let csvData = Papa.unparse(exportData);
- blob = new Blob([csvData], { type: fileTypeCSV });
- let url = URL.createObjectURL(blob);
- element.href = url;
- element.setAttribute('download', fileName[index] + '.csv');
- element.click();
- });
- }
- }
|