Skip to content

Commit 2acdea2

Browse files
committed
Introduce RankingConfig
1 parent ee56ef4 commit 2acdea2

4 files changed

Lines changed: 126 additions & 78 deletions

File tree

src/blocks/sams-ranking/block.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
"html": false
1313
},
1414
"attributes": {
15-
"samsConfigId": {
16-
"type": "string"
17-
},
18-
"matchSeriesId": {
19-
"type": "string"
15+
"rankingConfig": {
16+
"type": "object",
17+
"default": {},
18+
"properties": {
19+
"samsConfigId": { "type": "string" },
20+
"matchSeriesId": { "type": "string" }
21+
}
2022
}
2123
},
2224
"textdomain": "sams-integration",

src/blocks/sams-ranking/edit.js

Lines changed: 53 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -32,52 +32,65 @@ import './editor.scss';
3232
*
3333
* @return {Element} Element to render.
3434
*/
35-
export default function Edit( { attributes, setAttributes } )
36-
{
37-
const { samsConfigId, matchSeriesId } = attributes;
35+
export default function Edit({ attributes, setAttributes }) {
36+
// rankingConfig: { samsConfigId, matchSeriesId }
37+
const { rankingConfig = {} } = attributes;
38+
const { samsConfigId, matchSeriesId } = rankingConfig;
3839

3940
const configs = useSelect(
4041
(select) => select('core').getEntityRecords('postType', 'sams_host_config', { per_page: 100 }),
4142
[]
4243
);
4344

44-
const selectedConfig = configs && samsConfigId ? configs.find(config => config.id == samsConfigId) : null;
45+
const selectedConfig
46+
= configs
47+
&& samsConfigId
48+
? configs.find(config => config.id == samsConfigId)
49+
: null;
4550

4651
return (
47-
<>
48-
<InspectorControls>
49-
<PanelBody title={ __( 'Settings', 'sams-integration' ) }>
50-
{ !configs ? (
51-
<p>{ __('Loading ...', 'sams-integration') }</p>
52-
) : (
53-
<select
54-
value={ samsConfigId || '' }
55-
onChange={ e => {
56-
setAttributes({
57-
samsConfigId: e.target.value,
58-
});
59-
}}
60-
>
61-
<option value="">{ __('Select SAMS Server', 'sams-integration') }</option>
62-
{ configs.map(config => (
63-
<option key={config.id} value={config.id}>
64-
{config.title?.rendered || config.id}
65-
</option>
66-
)) }
67-
</select>
68-
)}
69-
<TextControl
70-
label={ __('MatchSeriesId', 'sams-integration') }
71-
value={ matchSeriesId || '' }
72-
onChange={ ( value ) => setAttributes( { matchSeriesId: value } ) }
73-
/>
74-
</PanelBody>
75-
</InspectorControls>
76-
<p { ...useBlockProps() }>
77-
SAMS Ranking Vorschau:<br />
78-
{ __('Config:', 'sams-integration') } { selectedConfig ? (selectedConfig.title?.rendered || selectedConfig.id) : __('None selected', 'sams-integration') }<br />
79-
{ __('MatchSeriesId:', 'sams-integration') } { matchSeriesId || __('None', 'sams-integration') }
80-
</p>
81-
</>
82-
);
52+
<>
53+
<InspectorControls>
54+
<PanelBody title={__('Settings', 'sams-integration')}>
55+
{!configs ? (
56+
<p>{__('Loading ...', 'sams-integration')}</p>
57+
) : (
58+
<select
59+
value={samsConfigId || ''}
60+
onChange={e => {
61+
setAttributes({
62+
rankingConfig: {
63+
...rankingConfig,
64+
samsConfigId: e.target.value,
65+
}
66+
});
67+
}}
68+
>
69+
<option value="">{__('Select SAMS Server', 'sams-integration')}</option>
70+
{configs.map(config => (
71+
<option key={config.id} value={config.id}>
72+
{config.title?.rendered || config.id}
73+
</option>
74+
))}
75+
</select>
76+
)}
77+
<TextControl
78+
label={__('MatchSeriesId', 'sams-integration')}
79+
value={matchSeriesId || ''}
80+
onChange={(value) => setAttributes({
81+
rankingConfig: {
82+
...rankingConfig,
83+
matchSeriesId: value,
84+
}
85+
})}
86+
/>
87+
</PanelBody>
88+
</InspectorControls>
89+
<p {...useBlockProps()}>
90+
SAMS Ranking Vorschau:<br />
91+
{__('Config:', 'sams-integration')} {selectedConfig ? (selectedConfig.title?.rendered || selectedConfig.id) : __('None selected', 'sams-integration')}<br />
92+
{__('MatchSeriesId:', 'sams-integration')} {matchSeriesId || __('None', 'sams-integration')}
93+
</p>
94+
</>
95+
);
8396
}

src/blocks/sams-ranking/render.php

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,49 @@
44
*/
55

66
use SAMSPlugin\RankingFetcher;
7+
use SAMSPlugin\Editor\Models\RankingConfig;
78
?>
89

910
<div <?php echo get_block_wrapper_attributes(); ?>>
1011

11-
<?php
12-
13-
if (isset($attributes)
14-
&& isset($attributes['samsConfigId'])
15-
&& isset($attributes['matchSeriesId'])) {
16-
17-
$config_post = get_post($attributes['samsConfigId']);
18-
19-
if ($config_post) {
20-
$associationUrl = get_post_meta($config_post->ID, '_sams_host_config_url', true);
21-
$apiKey = get_post_meta($config_post->ID, '_sams_host_config_api_key', true);
22-
$matchSeriesId = $attributes['matchSeriesId'];
23-
24-
// Fetch the ranking data using the fetched configuration
25-
$fetcher = new RankingFetcher();
26-
$ranking = $fetcher->fetch(
27-
baseUrl: $associationUrl,
28-
apiKey: $apiKey,
29-
matchSeriesId: $matchSeriesId);
30-
31-
$template_path = sams_integration_get_template( 'ranking-template.php' );
32-
33-
if (file_exists($template_path)) {
34-
$sams_integration_ranking = $ranking;
35-
include $template_path;
12+
<?php
13+
14+
if (isset($attributes['rankingConfig']) && is_array($attributes['rankingConfig'])) {
15+
$configArr = $attributes['rankingConfig'];
16+
$rankingConfig = new RankingConfig(
17+
samsHostConfigId: $configArr['samsConfigId'] ?? null,
18+
matchSeriesId: $configArr['matchSeriesId'] ?? null,
19+
matchSeriesName: $configArr['matchSeriesName'] ?? "no match series name"
20+
);
21+
22+
if ($rankingConfig->is_valid()) {
23+
$config_post = get_post($rankingConfig->samsHostConfigId);
24+
25+
if ($config_post) {
26+
$associationUrl = get_post_meta($config_post->ID, '_sams_host_config_url', true);
27+
$apiKey = get_post_meta($config_post->ID, '_sams_host_config_api_key', true);
28+
29+
$fetcher = new RankingFetcher();
30+
$ranking = $fetcher->fetch(
31+
baseUrl: $associationUrl,
32+
apiKey: $apiKey,
33+
matchSeriesId: $rankingConfig->matchSeriesId
34+
);
35+
36+
$template_path = sams_integration_get_template('ranking-template.php');
37+
if (file_exists($template_path)) {
38+
$sams_integration_ranking = $ranking;
39+
include $template_path;
40+
}
41+
} else {
42+
esc_html_e('Error in SAMS Ranking: Configuration not found', 'sams-integration');
43+
}
44+
} else {
45+
esc_html_e('Error in SAMS Ranking: Configuration missing or incomplete', 'sams-integration');
3646
}
3747
} else {
38-
esc_html_e('Error in SAMS Ranking: Configuration not found', 'sams-integration');
48+
esc_html_e('Error in SAMS Ranking: Configuration missing or incomplete', 'sams-integration');
3949
}
50+
?>
4051

41-
} else {
42-
// Display error message on invalid configuration
43-
esc_html_e('Error in SAMS Ranking: Configuration missing or incomplete', 'sams-integration');
44-
}
45-
?>
46-
47-
</div>
52+
</div>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace SAMSPlugin\Editor\Models;
4+
5+
class RankingConfig {
6+
public ?int $samsHostConfigId;
7+
public ?string $matchSeriesId;
8+
public string $matchSeriesName;
9+
10+
public function __construct(
11+
?int $samsHostConfigId,
12+
?string $matchSeriesId,
13+
?string $matchSeriesName = "") {
14+
15+
$this->samsHostConfigId = $samsHostConfigId;
16+
$this->matchSeriesId = $matchSeriesId;
17+
$this->matchSeriesName = $matchSeriesName;
18+
}
19+
20+
public function is_valid() : bool {
21+
22+
return isset($this->samsHostConfigId)
23+
&& isset($this->matchSeriesId)
24+
&& isset($this->matchSeriesName);
25+
}
26+
}
27+
28+
?>

0 commit comments

Comments
 (0)