From 5773c1ef49ca5b044de43422492972a546f003cb Mon Sep 17 00:00:00 2001 From: isUnknown Date: Fri, 26 Jul 2024 08:46:47 +0200 Subject: [PATCH] calendar strip - move api call to proxy route --- assets/js/calendar.js | 51 ++++++++++++++++++++++---------- site/config/config.php | 8 +++++ site/config/routes/mapado.php | 34 +++++++++++++++++++++ site/plugins/helpers/index.php | 33 +++++++++++++++++++++ site/snippets/calendar-strip.php | 10 +++---- 5 files changed, 116 insertions(+), 20 deletions(-) create mode 100644 site/config/config.php create mode 100644 site/config/routes/mapado.php create mode 100644 site/plugins/helpers/index.php diff --git a/assets/js/calendar.js b/assets/js/calendar.js index de2f155..0232321 100644 --- a/assets/js/calendar.js +++ b/assets/js/calendar.js @@ -14,19 +14,6 @@ function getDatesInMonth(month) { } async function getMapadoDates(monthNumb) { - const myHeaders = new Headers(); - - const mapadoToken = - "9bc321f5f47cf366c1c69eab4db69fe2a46819c75018237570473e2c961e210e227cc1e8cd8da7ba"; - - myHeaders.append("Authorization", "Bearer " + mapadoToken); - - const requestOptions = { - method: "GET", - headers: myHeaders, - redirect: "follow", - }; - const contractId = "1941"; const firstDayOfMonth = dayjs() @@ -37,10 +24,44 @@ async function getMapadoDates(monthNumb) { .add(1, "month") .format("YYYY-MM-DD"); - const url = `https://ticketing.mapado.net/v1/event_dates?itemsPerPage=100&contract=${contractId}&after=${firstDayOfMonth}&before=${firstDayOfNextMonth}&order=asc&fields=startDate,bookableStock,ticketing{@id,title,venue{@id,name,seatingName,address,zipCode,city,countryCode,timezone}}`; + const requestEndPoint = 'event_dates' + const requestParams = [ + { name: 'itemsPerPage', value: 100 }, + { name: 'contract', value: contractId }, + { name: 'after', value: firstDayOfMonth }, + { name: 'before', value: firstDayOfNextMonth }, + { name: 'order', value: 'asc' } + ] + + const requestFields = [ + { name: 'startDate', }, + { name: 'bookableStock', }, + { name: 'ticketing', subfields: [ + { name: "@id", }, + { name: "title", }, + { name: "venue", subfields: [ + { name: "@id" }, + { name: "address" }, + { name: "zipCode" }, + { name: "city" }, + { name: "countryCode" }, + { name: "timezone" }, + ] + }, + ] + }, + ]; + + const requestOptions = { + method: 'POST', + body: JSON.stringify({requestEndPoint, requestParams, requestFields}) + } - const response = await fetch(url, requestOptions); + const response = await fetch('/mapado.json', requestOptions); + console.log(response) const json = await response.json(); + console.log(json) + const eventDates = json["hydra:member"]; console.log( diff --git a/site/config/config.php b/site/config/config.php new file mode 100644 index 0000000..7a51d19 --- /dev/null +++ b/site/config/config.php @@ -0,0 +1,8 @@ + true, + 'routes' => [ + require_once(__DIR__ . '/routes/mapado.php') + ] +]; \ No newline at end of file diff --git a/site/config/routes/mapado.php b/site/config/routes/mapado.php new file mode 100644 index 0000000..9d28c02 --- /dev/null +++ b/site/config/routes/mapado.php @@ -0,0 +1,34 @@ + '/mapado.json', + 'method' => 'POST', + 'action' => function () { + $jsonRequest = file_get_contents("php://input"); + $request = json_decode($jsonRequest, true); + + $token = site()->mapadoToken(); + $contractId = site()->mapadoContractId(); + $requestEndPoint = $request['requestEndPoint']; + $requestParams = $request['requestParams']; + $requestFields = $request['requestFields']; + + $url = "https://ticketing.mapado.net/v1/$requestEndPoint?contract=$contractId&"; + + $url .= buildParamsString($requestParams); + $url .= buildFieldsString($requestFields); + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_HTTPHEADER, [ + "Authorization: Bearer " . $token + ]); + + $responseString = curl_exec($ch); + curl_close($ch); + + $responseJson = json_encode(json_decode($responseString)); + + return $responseJson; + } +]; diff --git a/site/plugins/helpers/index.php b/site/plugins/helpers/index.php new file mode 100644 index 0000000..f28c8ce --- /dev/null +++ b/site/plugins/helpers/index.php @@ -0,0 +1,33 @@ + { - const day = parseInt(mapadoDate.day) - this.dates[day].push(mapadoDate.name) - }) + // mapadoDates.forEach(mapadoDate => { + // const day = parseInt(mapadoDate.day) + // this.dates[day].push(mapadoDate.name) + // }) - console.log('dates', this.dates) + // console.log('dates', this.dates) } }"