184 lines
6.7 KiB
TypeScript
184 lines
6.7 KiB
TypeScript
import axios, { AxiosResponse } from 'axios';
|
|
var xml2jsParser = require('xml2js').parseString;
|
|
import * as I from "../types/types";
|
|
import Utils from './utils';
|
|
import * as UI from "./ui";
|
|
import { config } from "./config";
|
|
import * as Communication from "./communication";
|
|
|
|
|
|
export function getXMLPromise(url: string): Promise<unknown> {
|
|
return axios.get(url)
|
|
.then(function (response: void | AxiosResponse<any>) {
|
|
|
|
return new Promise(function (resolve: any, reject: any) {
|
|
const AxiosResponse = <AxiosResponse>response;
|
|
if (AxiosResponse.status === 200 && AxiosResponse.headers["content-type"] === "text/xml;charset=utf-8") {
|
|
xml2jsParser(AxiosResponse.data, { normalizeTags: true, normalize: true, mergeAttrs: true }, function (err: any, result: any) {
|
|
if (err)
|
|
reject(err);
|
|
else {
|
|
resolve(result);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
|
|
})
|
|
.catch(function (error) {
|
|
console.log(error);
|
|
})
|
|
}
|
|
|
|
export function getEventInfo(): I.EventInfo {
|
|
const venueXML = config.state.inVenueXML!;
|
|
const sectionArr = venueXML.master_config[0].section_inventory[0].section;
|
|
const eventObj: I.Event = venueXML.event[0];
|
|
const event = venueXML.event[0];
|
|
const venue_config = venueXML.venue_config[0];
|
|
const dateObj: Date = new Date(parseInt(event.year[0]), parseInt(event.month[0])-1, parseInt(event.day[0]), parseInt(event.hour[0]), parseInt(event.minute[0]));
|
|
const start: string[] = [ dateObj.toLocaleString(["de-DE"], { weekday: "long", day: "2-digit", month: "2-digit", year: "numeric", hour: "2-digit", minute: "2-digit", timeZoneName: "short" }) ];
|
|
const weekday = [ Utils.getDayName(dateObj, "de-DE") ];
|
|
const seats_available = sectionArr.map(item => parseInt(item.available[0])).reduce((prev, curr) => prev + curr);
|
|
|
|
|
|
const eventExtend: I.EventExtend = {
|
|
venue_config_capacity: venue_config.capacity,
|
|
venue_config_code: venue_config.code,
|
|
venue_config_desc: venue_config.desc,
|
|
venue_config_id: venue_config.id,
|
|
venue_config_nav_image: venue_config.nav_image,
|
|
venue_config_nav_image_height: venue_config.nav_image_height,
|
|
venue_config_nav_image_width: venue_config.nav_image_width,
|
|
start: start,
|
|
weekday: weekday,
|
|
seats_available: seats_available,
|
|
};
|
|
|
|
let eventInfo: I.EventInfo = { ...eventObj, ...eventExtend };
|
|
|
|
return eventInfo;
|
|
}
|
|
|
|
export function isValidSeatSelection() {
|
|
const inputsWithValue = config.state.inputsWithValue!;
|
|
console.log("checking seat selection");
|
|
console.log(inputsWithValue);
|
|
jQuery("#modalCart-overlay").hide();
|
|
|
|
if (!config.state.selectedSeatsArr.length)
|
|
return;
|
|
|
|
UI.showHideBtnCartLoading("show");
|
|
|
|
jQuery("#modalCart i").hide();
|
|
jQuery("#modalCart .uabb-button-text").addClass("dot-pulse");
|
|
|
|
const url = generateCheckoutUrl();
|
|
|
|
// const selectedSeatIndexes: string = generateSelectedSeatIndexes();
|
|
// const url: string = `${inputsWithValue["ticketPurchaseUrl"]}?user_context=${inputsWithValue.user_context}&pid=${inputsWithValue["pid"]}&selected_seat_indexes=${selectedSeatIndexes}&trxstate=148`;
|
|
|
|
const message: I.Message = {
|
|
message: {
|
|
url: url,
|
|
},
|
|
from: "child",
|
|
event: "child_needCheckoutResponse",
|
|
date: Date.now()
|
|
};
|
|
Communication.sendMessage(message, "parent");
|
|
}
|
|
|
|
export function generateCheckoutUrl(): string | undefined {
|
|
const inputsWithValue = config.state.inputsWithValue!;
|
|
console.log(inputsWithValue);
|
|
|
|
if (!config.state.selectedSeatsArr.length)
|
|
return;
|
|
else {
|
|
const selectedSeatIndexes: string = generateSelectedSeatIndexes();
|
|
return `${inputsWithValue["ticketPurchaseUrl"]}?user_context=${inputsWithValue.user_context}&pid=${inputsWithValue["pid"]}&selected_seat_indexes=${selectedSeatIndexes}&trxstate=148`;
|
|
}
|
|
}
|
|
|
|
function generateSelectedSeatIndexes(): string {
|
|
return (config.state.selectedSeatsArr.map(function (arr) {
|
|
return arr.join(",");
|
|
})).join("|");
|
|
}
|
|
|
|
export function getSectionDescBySectionID(sectionID: string): string | undefined {
|
|
const venueXML = config.state.inVenueXML!;
|
|
const sectionArr = venueXML.master_config[0].section_config[0].section;
|
|
|
|
const sectionDesc = sectionArr.find(arr => {
|
|
return sectionID === arr.id[0];
|
|
})?.desc[0];
|
|
|
|
return sectionDesc;
|
|
}
|
|
|
|
export function processSMAP() {
|
|
const inputsWithValue = config.state.inputsWithValue!;
|
|
if (!inputsWithValue.smap)
|
|
return;
|
|
|
|
const smapArr = inputsWithValue.smap.split("").map(Number);
|
|
|
|
if (!smapArr[0])
|
|
jQuery("#eventInfoCapacity").hide();
|
|
}
|
|
|
|
export function getVenuePricescalePropertyByPricescaleID(property: I.Pricescale2Properties, pricescaleID: string) {
|
|
const venueXML = config.state.inVenueXML!;
|
|
const venuePricescaleArr: I.Pricescale2[] = venueXML.venue[0].pricescales[0].pricescale;
|
|
|
|
return venuePricescaleArr.find(obj => {
|
|
if (obj.id[0] === pricescaleID)
|
|
return obj;
|
|
|
|
return undefined;
|
|
})?.[property][0];
|
|
}
|
|
|
|
export function getBuyerTypeCodeByBuyerTypeID(inBuyerTypeID: string): string | undefined {
|
|
const venueXML = config.state.inVenueXML!;
|
|
const venueBuyerTypeArr = venueXML.venue[0].buyer_types[0].buyer_type;
|
|
return venueBuyerTypeArr.find(arr => {
|
|
return inBuyerTypeID === arr.id[0];
|
|
})?.code[0];
|
|
}
|
|
|
|
export function getPriceByBuyertypeID(inBuyertypeID: string, inPricescaleObj: I.Pricescale5) {
|
|
const price = inPricescaleObj?.buyer_type.find(arr => {
|
|
return arr.id[0] === inBuyertypeID;
|
|
})?.price[0];
|
|
|
|
if (price)
|
|
return parseFloat(price);
|
|
|
|
return undefined;
|
|
}
|
|
|
|
export function getVenuePriceStructurePropertyByPricescaleID(inID: string): I.Pricescale5 | undefined {
|
|
const venueXML = config.state.inVenueXML!;
|
|
const venuePricescaleArr: I.Pricescale5[] = venueXML.price_structure[0].pricescale;
|
|
return venuePricescaleArr.find(obj => {
|
|
return obj.id[0] === inID;
|
|
});
|
|
}
|
|
|
|
export function generatePricescaleCSS(): string {
|
|
const venueXML = config.state.inVenueXML!;
|
|
const venuePricescalesArr: I.Pricescale2[] = venueXML.venue[0].pricescales[0].pricescale;
|
|
let cssArr: string[] = [];
|
|
|
|
venuePricescalesArr.forEach(element => {
|
|
const ID: string = element.id[0];
|
|
let color: string = `#${element.color[0]} !important`; // Update: Colors are always defined: fallback colors exist in system so every XML provides them
|
|
cssArr.push(`._${ID} { background-color: ${color}; }`);
|
|
});
|
|
|
|
return (cssArr.join("\r\n"));
|
|
} |