feat(andebol): surface FPA national agenda on homepage via bridge
Enable fpa_agenda_enabled on andebol.net domain config. The bridge fetches ws.fpa.pt getAgendaJogosFAP with SWR caching, normalises rows, and attaches fpa_agenda to the homepage JSON. The institutional theme renders a responsive fixtures table with FPA attribution. Co-Authored-By: Paperclip <[email protected]> Made-with: Cursor
This commit is contained in:
1 parent
0a845c41cc
commit
1e0de85486
9 files changed
+373
-4
No files matched your search
@@ -3,7 +3,8 @@ import { get_domain_config } from '../config/domain.js';
|
||||
import { resolve_site_host } from '../lib/request-host.js';
|
||||
import { WpClient } from '../services/wp-client.js';
|
||||
import { swr_fetch } from '../services/cache.js';
|
||||
import type { ArticleListItem, HomepageData } from '../types.js';
|
||||
import type { ArticleListItem, FpaAgendaGame, HomepageData } from '../types.js';
|
||||
import { fetch_fpa_agenda } from '../services/fpa-agenda.js';
|
||||
|
||||
// Number of articles per section
|
||||
const HERO_COUNT = 3;
|
||||
@@ -12,7 +13,7 @@ const FEATURED_COUNT = 4;
|
||||
const LATEST_COUNT = 12;
|
||||
const BY_CATEGORY_COUNT = 4;
|
||||
|
||||
const empty_homepage = (): HomepageData => ({
|
||||
const empty_homepage = (): Omit<HomepageData, 'fpa_agenda'> => ({
|
||||
hero: [],
|
||||
trending: [],
|
||||
featured: [],
|
||||
@@ -26,7 +27,7 @@ export async function homepage_routes(app: FastifyInstance): Promise<void> {
|
||||
const config = get_domain_config(host);
|
||||
const client = new WpClient(config.wp_endpoint);
|
||||
|
||||
const data = await swr_fetch<HomepageData>(
|
||||
const base = await swr_fetch<Omit<HomepageData, 'fpa_agenda'>>(
|
||||
`${host}:homepage`,
|
||||
async () => {
|
||||
try {
|
||||
@@ -82,6 +83,18 @@ export async function homepage_routes(app: FastifyInstance): Promise<void> {
|
||||
},
|
||||
);
|
||||
|
||||
const fpa_agenda: FpaAgendaGame[] | null = config.fpa_agenda_enabled
|
||||
? await swr_fetch<FpaAgendaGame[]>(`${host}:fpa:agenda`, async () => {
|
||||
try {
|
||||
return await fetch_fpa_agenda();
|
||||
} catch (err) {
|
||||
app.log.warn({ err, host }, 'FPA agenda fetch failed');
|
||||
return [];
|
||||
}
|
||||
})
|
||||
: null;
|
||||
|
||||
const data: HomepageData = { ...base, fpa_agenda };
|
||||
return reply.send(data);
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
import type { FpaAgendaGame } from '../types.js';
|
||||
|
||||
/** Public FPA JSON feed used by portal.fpa.pt for national competition fixtures. */
|
||||
const FPA_AGENDA_URL =
|
||||
process.env['FPA_AGENDA_URL'] ?? 'https://ws.fpa.pt/index.php/getAgendaJogosFAP/';
|
||||
|
||||
const FETCH_TIMEOUT_MS = parseInt(process.env['FPA_FETCH_TIMEOUT_MS'] ?? '20000', 10);
|
||||
const MAX_GAMES = Math.min(parseInt(process.env['FPA_AGENDA_MAX'] ?? '48', 10), 200);
|
||||
|
||||
function as_trimmed_string(value: unknown): string {
|
||||
if (value === null || value === undefined) {
|
||||
return '';
|
||||
}
|
||||
return String(value).trim();
|
||||
}
|
||||
|
||||
function parse_goals(value: unknown): number | null {
|
||||
if (value === null || value === undefined || value === '') {
|
||||
return null;
|
||||
}
|
||||
const n = Number(value);
|
||||
return Number.isFinite(n) ? n : null;
|
||||
}
|
||||
|
||||
function sort_key(date: string, time: string): string {
|
||||
return `${date} ${time}`.padEnd(20, '0');
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch and normalise FPA agenda JSON. Caller should wrap in cache (e.g. swr_fetch).
|
||||
* Throws on network/HTTP errors or invalid payload shape.
|
||||
*/
|
||||
export async function fetch_fpa_agenda(): Promise<FpaAgendaGame[]> {
|
||||
const res = await fetch(FPA_AGENDA_URL, {
|
||||
headers: { Accept: 'application/json' },
|
||||
signal: AbortSignal.timeout(FETCH_TIMEOUT_MS),
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
throw new Error(`FPA agenda HTTP ${res.status}`);
|
||||
}
|
||||
|
||||
const raw: unknown = await res.json();
|
||||
if (!Array.isArray(raw)) {
|
||||
throw new Error('FPA agenda: expected JSON array');
|
||||
}
|
||||
|
||||
const games: FpaAgendaGame[] = [];
|
||||
|
||||
for (const row of raw) {
|
||||
if (row === null || typeof row !== 'object') {
|
||||
continue;
|
||||
}
|
||||
const o = row as Record<string, unknown>;
|
||||
const match_id = as_trimmed_string(o['ID_PROVA_JOGO']);
|
||||
const date = as_trimmed_string(o['JOG_DATA']);
|
||||
const time = as_trimmed_string(o['JOG_HORA']);
|
||||
if (!match_id || !date) {
|
||||
continue;
|
||||
}
|
||||
|
||||
games.push({
|
||||
match_id,
|
||||
date,
|
||||
time: time || '—',
|
||||
home_team: as_trimmed_string(o['EQUIPA_CASA']) || '—',
|
||||
away_team: as_trimmed_string(o['EQUIPA_FORA']) || '—',
|
||||
home_goals: parse_goals(o['GOLOS_CASA']),
|
||||
away_goals: parse_goals(o['GOLOS_FORA']),
|
||||
venue: as_trimmed_string(o['LOCAL']) || '—',
|
||||
competition: as_trimmed_string(o['PROVA']) || '—',
|
||||
phase: as_trimmed_string(o['FASE']) || '',
|
||||
});
|
||||
}
|
||||
|
||||
games.sort((a, b) => sort_key(a.date, a.time).localeCompare(sort_key(b.date, b.time)));
|
||||
return games.slice(0, MAX_GAMES);
|
||||
}
|
||||
@@ -15,6 +15,22 @@ export interface DomainConfig {
|
||||
ads_enabled: boolean;
|
||||
analytics_id: string | null;
|
||||
auth_links?: boolean;
|
||||
/** When true, homepage includes `fpa_agenda` from ws.fpa.pt (cached server-side). */
|
||||
fpa_agenda_enabled?: boolean;
|
||||
}
|
||||
|
||||
/** Normalised row from FPA `getAgendaJogosFAP` JSON. */
|
||||
export interface FpaAgendaGame {
|
||||
match_id: string;
|
||||
date: string;
|
||||
time: string;
|
||||
home_team: string;
|
||||
away_team: string;
|
||||
home_goals: number | null;
|
||||
away_goals: number | null;
|
||||
venue: string;
|
||||
competition: string;
|
||||
phase: string;
|
||||
}
|
||||
|
||||
export interface CategoryConfig {
|
||||
@@ -78,6 +94,8 @@ export interface HomepageData {
|
||||
featured: ArticleListItem[];
|
||||
latest: ArticleListItem[];
|
||||
by_category: Record<string, ArticleListItem[]>;
|
||||
/** Present when `fpa_agenda_enabled`; otherwise null. */
|
||||
fpa_agenda: FpaAgendaGame[] | null;
|
||||
}
|
||||
|
||||
// Raw WordPress REST API types (minimal subset we use)
|
||||
|
||||
@@ -25,5 +25,6 @@
|
||||
"timezone": "Europe/Lisbon",
|
||||
"ads_enabled": false,
|
||||
"analytics_id": null,
|
||||
"auth_links": true
|
||||
"auth_links": true,
|
||||
"fpa_agenda_enabled": true
|
||||
}
|
||||
@@ -131,6 +131,7 @@ export class MockBridgeClient extends BridgeClient {
|
||||
basketball: this.mockArticles(4),
|
||||
formula1: this.mockArticles(4),
|
||||
},
|
||||
fpa_agenda: null,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -42,6 +42,15 @@ export type UiStrings = {
|
||||
search_results_count: (n: number) => string;
|
||||
explore_sections: string;
|
||||
discover_lead: string;
|
||||
fpa_agenda_title: string;
|
||||
fpa_agenda_lead: string;
|
||||
fpa_col_when: string;
|
||||
fpa_col_match: string;
|
||||
fpa_col_score: string;
|
||||
fpa_col_venue: string;
|
||||
fpa_col_competition: string;
|
||||
fpa_agenda_empty: string;
|
||||
fpa_source_credit: string;
|
||||
};
|
||||
|
||||
const en: UiStrings = {
|
||||
@@ -83,6 +92,15 @@ const en: UiStrings = {
|
||||
search_results_count: n => `${n} results`,
|
||||
explore_sections: 'Explore',
|
||||
discover_lead: 'Browse by topic or find an article by keyword.',
|
||||
fpa_agenda_title: 'Upcoming matches',
|
||||
fpa_agenda_lead: 'National competition fixtures from the Portuguese Handball Federation feed.',
|
||||
fpa_col_when: 'When',
|
||||
fpa_col_match: 'Match',
|
||||
fpa_col_score: 'Score',
|
||||
fpa_col_venue: 'Venue',
|
||||
fpa_col_competition: 'Competition',
|
||||
fpa_agenda_empty: 'Fixture list is temporarily unavailable.',
|
||||
fpa_source_credit: 'Data: Portuguese Handball Federation (ws.fpa.pt).',
|
||||
};
|
||||
|
||||
const pt: UiStrings = {
|
||||
@@ -124,6 +142,15 @@ const pt: UiStrings = {
|
||||
search_results_count: n => (n === 1 ? '1 resultado' : `${n} resultados`),
|
||||
explore_sections: 'Explorar',
|
||||
discover_lead: 'Navegue por tema ou encontre um artigo por palavra-chave.',
|
||||
fpa_agenda_title: 'Próximos jogos',
|
||||
fpa_agenda_lead: 'Agenda de competições nacionais (feed público da Federação Portuguesa de Andebol).',
|
||||
fpa_col_when: 'Quando',
|
||||
fpa_col_match: 'Jogo',
|
||||
fpa_col_score: 'Resultado',
|
||||
fpa_col_venue: 'Local',
|
||||
fpa_col_competition: 'Competição',
|
||||
fpa_agenda_empty: 'A agenda não está disponível de momento.',
|
||||
fpa_source_credit: 'Dados: Federação Portuguesa de Andebol (ws.fpa.pt).',
|
||||
};
|
||||
|
||||
export function getUiStrings(locale: string): UiStrings {
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
import Base from '../layouts/Base.astro';
|
||||
import LatestGrid from '../components/home/LatestGrid.astro';
|
||||
import DiscoverSection from '../themes/institutional/DiscoverSection.astro';
|
||||
import FpaAgendaSection from '../themes/institutional/FpaAgendaSection.astro';
|
||||
import { getDomainConfig } from '../lib/domain.js';
|
||||
import { getThemeComponents } from '../lib/theme.js';
|
||||
import { BridgeClient, MockBridgeClient, bridgeRequestHeaders } from '../lib/bridge.js';
|
||||
@@ -54,6 +55,10 @@ const website_schema = {
|
||||
<TrendingBar articles={homepage.trending} />
|
||||
<HeroSection articles={homepage.hero} locale={config.locale} />
|
||||
|
||||
{config.fpa_agenda_enabled && homepage.fpa_agenda != null && (
|
||||
<FpaAgendaSection games={homepage.fpa_agenda} locale={config.locale} />
|
||||
)}
|
||||
|
||||
{config.ads_enabled && (
|
||||
<div class="container" style="padding-block: 1rem">
|
||||
<div class="ad-slot ad-slot--leaderboard" aria-label="Advertisement">Advertisement</div>
|
||||
|
||||
@@ -0,0 +1,208 @@
|
||||
---
|
||||
import type { FpaAgendaGame } from '../../types/index.js';
|
||||
import { getUiStrings } from '../../lib/ui-i18n.js';
|
||||
|
||||
interface Props {
|
||||
games: FpaAgendaGame[];
|
||||
locale: string;
|
||||
}
|
||||
|
||||
const { games, locale } = Astro.props;
|
||||
const ui = getUiStrings(locale);
|
||||
|
||||
function format_score(game: FpaAgendaGame): string {
|
||||
if (game.home_goals !== null && game.away_goals !== null) {
|
||||
return `${game.home_goals} – ${game.away_goals}`;
|
||||
}
|
||||
return '—';
|
||||
}
|
||||
---
|
||||
<section class="fpa-agenda" aria-labelledby="fpa-agenda-title">
|
||||
<div class="container">
|
||||
<header class="fpa-agenda__head">
|
||||
<h2 id="fpa-agenda-title" class="fpa-agenda__title">{ui.fpa_agenda_title}</h2>
|
||||
<p class="fpa-agenda__lead">{ui.fpa_agenda_lead}</p>
|
||||
</header>
|
||||
|
||||
{games.length === 0 ? (
|
||||
<p class="fpa-agenda__empty" role="status">{ui.fpa_agenda_empty}</p>
|
||||
) : (
|
||||
<div class="fpa-agenda__scroll" tabindex="0">
|
||||
<table class="fpa-agenda__table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col">{ui.fpa_col_when}</th>
|
||||
<th scope="col">{ui.fpa_col_match}</th>
|
||||
<th scope="col">{ui.fpa_col_score}</th>
|
||||
<th scope="col" class="fpa-agenda__hide-narrow">{ui.fpa_col_venue}</th>
|
||||
<th scope="col" class="fpa-agenda__hide-narrow">{ui.fpa_col_competition}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{games.map((g) => (
|
||||
<tr>
|
||||
<td class="fpa-agenda__when">
|
||||
<span class="fpa-agenda__date">{g.date}</span>
|
||||
<span class="fpa-agenda__time">{g.time}</span>
|
||||
</td>
|
||||
<td class="fpa-agenda__match">
|
||||
<span class="fpa-agenda__team fpa-agenda__team--home">{g.home_team}</span>
|
||||
<span class="fpa-agenda__vs" aria-hidden="true">vs</span>
|
||||
<span class="fpa-agenda__team fpa-agenda__team--away">{g.away_team}</span>
|
||||
{g.phase && <span class="fpa-agenda__phase">{g.phase}</span>}
|
||||
</td>
|
||||
<td class="fpa-agenda__score">{format_score(g)}</td>
|
||||
<td class="fpa-agenda__hide-narrow">{g.venue}</td>
|
||||
<td class="fpa-agenda__hide-narrow fpa-agenda__comp">{g.competition}</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<p class="fpa-agenda__credit">{ui.fpa_source_credit}</p>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<style>
|
||||
.fpa-agenda {
|
||||
padding: var(--space-10) 0;
|
||||
background: var(--color-bg-card);
|
||||
border-block: 1px solid var(--color-border);
|
||||
}
|
||||
|
||||
.fpa-agenda__head {
|
||||
margin-bottom: var(--space-6);
|
||||
max-width: 40rem;
|
||||
}
|
||||
|
||||
.fpa-agenda__title {
|
||||
font-family: var(--font-heading, inherit);
|
||||
font-size: clamp(1.25rem, 2.2vw, 1.65rem);
|
||||
font-weight: 600;
|
||||
color: var(--color-primary);
|
||||
margin: 0 0 var(--space-2);
|
||||
line-height: 1.2;
|
||||
}
|
||||
|
||||
.fpa-agenda__lead {
|
||||
margin: 0;
|
||||
font-size: var(--text-sm);
|
||||
line-height: 1.55;
|
||||
color: var(--color-text-muted);
|
||||
}
|
||||
|
||||
.fpa-agenda__empty {
|
||||
margin: 0 0 var(--space-4);
|
||||
color: var(--color-text-muted);
|
||||
font-size: var(--text-sm);
|
||||
}
|
||||
|
||||
.fpa-agenda__scroll {
|
||||
overflow-x: auto;
|
||||
margin-bottom: var(--space-4);
|
||||
border: 1px solid var(--color-border);
|
||||
border-radius: 8px;
|
||||
background: var(--color-bg, #fff);
|
||||
}
|
||||
|
||||
.fpa-agenda__table {
|
||||
width: 100%;
|
||||
min-width: 520px;
|
||||
border-collapse: collapse;
|
||||
font-size: var(--text-sm);
|
||||
}
|
||||
|
||||
.fpa-agenda__table th {
|
||||
text-align: left;
|
||||
padding: var(--space-3) var(--space-3);
|
||||
font-family: var(--font-heading, inherit);
|
||||
font-weight: 600;
|
||||
font-size: var(--text-xs);
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.04em;
|
||||
color: var(--color-text-muted);
|
||||
background: var(--inst-surface-muted, var(--color-bg-secondary));
|
||||
border-bottom: 1px solid var(--color-border);
|
||||
}
|
||||
|
||||
.fpa-agenda__table td {
|
||||
padding: var(--space-3) var(--space-3);
|
||||
vertical-align: top;
|
||||
border-bottom: 1px solid var(--color-border);
|
||||
color: var(--color-text);
|
||||
}
|
||||
|
||||
.fpa-agenda__table tbody tr:last-child td {
|
||||
border-bottom: none;
|
||||
}
|
||||
|
||||
.fpa-agenda__when {
|
||||
white-space: nowrap;
|
||||
font-variant-numeric: tabular-nums;
|
||||
}
|
||||
|
||||
.fpa-agenda__date {
|
||||
display: block;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.fpa-agenda__time {
|
||||
display: block;
|
||||
font-size: var(--text-xs);
|
||||
color: var(--color-text-muted);
|
||||
}
|
||||
|
||||
.fpa-agenda__match {
|
||||
min-width: 12rem;
|
||||
}
|
||||
|
||||
.fpa-agenda__team {
|
||||
display: block;
|
||||
line-height: 1.35;
|
||||
}
|
||||
|
||||
.fpa-agenda__vs {
|
||||
display: block;
|
||||
font-size: var(--text-xs);
|
||||
color: var(--color-text-muted);
|
||||
margin: 0.15rem 0;
|
||||
text-transform: lowercase;
|
||||
}
|
||||
|
||||
.fpa-agenda__phase {
|
||||
display: block;
|
||||
margin-top: var(--space-2);
|
||||
font-size: var(--text-xs);
|
||||
color: var(--color-secondary);
|
||||
line-height: 1.3;
|
||||
}
|
||||
|
||||
.fpa-agenda__score {
|
||||
font-variant-numeric: tabular-nums;
|
||||
font-weight: 600;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.fpa-agenda__comp {
|
||||
max-width: 14rem;
|
||||
line-height: 1.35;
|
||||
}
|
||||
|
||||
.fpa-agenda__credit {
|
||||
margin: 0;
|
||||
font-size: var(--text-xs);
|
||||
color: var(--color-text-muted);
|
||||
}
|
||||
|
||||
@media (max-width: 640px) {
|
||||
.fpa-agenda__hide-narrow {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.fpa-agenda__table {
|
||||
min-width: 0;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
@@ -30,6 +30,22 @@ export interface DomainConfig {
|
||||
analytics_id: string | null;
|
||||
/** When true, show login/register in chrome and expose /entrar + /registar → WordPress. */
|
||||
auth_links?: boolean;
|
||||
/** Homepage includes FPA fixture feed (bridge proxies ws.fpa.pt). */
|
||||
fpa_agenda_enabled?: boolean;
|
||||
}
|
||||
|
||||
/** Normalised FPA agenda row (bridge → homepage JSON). */
|
||||
export interface FpaAgendaGame {
|
||||
match_id: string;
|
||||
date: string;
|
||||
time: string;
|
||||
home_team: string;
|
||||
away_team: string;
|
||||
home_goals: number | null;
|
||||
away_goals: number | null;
|
||||
venue: string;
|
||||
competition: string;
|
||||
phase: string;
|
||||
}
|
||||
|
||||
export interface Article {
|
||||
@@ -100,4 +116,6 @@ export interface HomepageData {
|
||||
featured: ArticleListItem[];
|
||||
latest: ArticleListItem[];
|
||||
by_category: Record<string, ArticleListItem[]>;
|
||||
/** Omitted by older bridge versions; treat as null if missing. */
|
||||
fpa_agenda?: FpaAgendaGame[] | null;
|
||||
}
|
||||
Reference in new issue
Block a user