wp-matchplay/src/edit.js

102 lines
3.0 KiB
JavaScript
Raw Normal View History

import {InspectorControls, useBlockProps} from '@wordpress/block-editor';
import {TextControl, PanelBody} from '@wordpress/components';
2023-04-19 21:14:01 +00:00
import './editor.scss';
/**
* The edit function describes the structure of your block in the context of the
* editor. This represents what the editor will render when the block is used.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-edit-save/#edit
*
* @return {WPElement} Element to render.
*/
export default function Edit({attributes, setAttributes}) {
2023-04-20 15:53:40 +00:00
function fetchTournamentData() {
2023-07-19 20:25:29 +00:00
const headers = {
"Authorization": `Bearer ${attributes.matchplay_api_key}`,
"Content-Type": "application/json",
"Accept": "application/json",
};
const tournamentUrl = new URL(`https://next.matchplay.events/api/tournaments/${attributes.tournament_id}`);
const standingsUrl = new URL(`https://next.matchplay.events/api/tournaments/${attributes.tournament_id}/standings`);
const params = {
"includePlayers": "1",
"includeArenas": "0",
"includeBanks": "0",
"includeScorekeepers": "0",
"includeSeries": "0",
"includeLocation": "1",
"includeRsvpConfiguration": "0",
"includeParent": "1",
"includePlayoffs": "1",
"includeShortcut": "0",
};
Object.keys(params).forEach(key => tournamentUrl.searchParams.append(key, params[key]));
// get players
2023-07-19 20:25:29 +00:00
fetch(tournamentUrl, {method: "GET", headers})
.then(response => response.json())
.then(response => {
let newData = {...response};
setAttributes({data: {players: newData}});
console.log(newData);
2023-07-19 20:25:29 +00:00
})
.catch(err => console.error(err));
2023-07-19 20:25:29 +00:00
// get standings
2023-07-19 20:25:29 +00:00
fetch(standingsUrl, {method: "GET", headers})
2023-04-20 15:53:40 +00:00
.then(response => response.json())
.then(response => {
let newData = {...response};
2023-07-20 21:27:36 +00:00
setAttributes({data: {standings: newData, ...attributes.data?.players}});
2023-04-20 15:53:40 +00:00
console.log(newData);
})
.catch(err => console.error(err));
2023-04-20 15:53:40 +00:00
}
2023-04-19 21:14:01 +00:00
return (
2023-04-20 15:53:40 +00:00
<>
<InspectorControls>
<PanelBody>
<TextControl
label="Tournament ID"
value={attributes.tournament_id}
onChange={val => setAttributes({tournament_id: val})}
2023-04-20 15:53:40 +00:00
/>
</PanelBody>
</InspectorControls>
<div {...useBlockProps()}>
<p>Results for tournament id {attributes.tournament_id}</p>
2023-04-20 15:53:40 +00:00
<button onClick={() => fetchTournamentData()}>Fetch Results</button>
2023-07-19 20:46:52 +00:00
{attributes.data?.players && attributes.data?.standings && (
2023-04-20 15:53:40 +00:00
<>
<table id="tournament-results-table">
<thead>
<tr>
<th>Place</th>
<th>Player</th>
<th>Points</th>
</tr>
2023-04-20 15:53:40 +00:00
</thead>
<tbody>
{Object.keys(attributes.data.standings).map((player, i) => {
return (
<tr key={player}>
<td>{attributes.data.standings[i].position}</td>
2023-07-19 20:49:49 +00:00
<td>{attributes.data.players.find(p => p.playerId === attributes.data.standings[i].playerId).name}</td>
<td>{attributes.data.standings[i].points}</td>
</tr>
);
})}
2023-04-20 15:53:40 +00:00
</tbody>
</table>
</>
)}
</div>
</>
2023-04-19 21:14:01 +00:00
);
}