@@ -93,24 +93,27 @@ export class PortalApiError extends Error {
|
||||
constructor(status: number, errors: Record<string, string[]> = {}) {
|
||||
super(
|
||||
status === 429
|
||||
? "Foram enviados demasiados pedidos. Aguarde um minuto e tente novamente."
|
||||
? 'Foram enviados demasiados pedidos. Aguarde um minuto e tente novamente.'
|
||||
: status === 404
|
||||
? "O registo solicitado já não está disponível."
|
||||
? 'O registo solicitado já não está disponível.'
|
||||
: status === 422
|
||||
? "Verifique os campos assinalados."
|
||||
: "Não foi possível comunicar com o portal. Tente novamente.",
|
||||
? 'Verifique os campos assinalados.'
|
||||
: 'Não foi possível comunicar com o portal. Tente novamente.',
|
||||
);
|
||||
this.status = status;
|
||||
this.errors = errors;
|
||||
}
|
||||
}
|
||||
|
||||
export async function portalRequest<T>(url: string, options: RequestInit = {}): Promise<T> {
|
||||
export async function portalRequest<T>(
|
||||
url: string,
|
||||
options: RequestInit = {},
|
||||
): Promise<T> {
|
||||
const headers = new Headers(options.headers);
|
||||
headers.set("Accept", "application/json");
|
||||
headers.set('Accept', 'application/json');
|
||||
const response = await fetch(url, {
|
||||
...options,
|
||||
credentials: "same-origin",
|
||||
credentials: 'same-origin',
|
||||
headers,
|
||||
});
|
||||
const body = await response.json().catch(() => null);
|
||||
@@ -128,7 +131,7 @@ export function toOpportunity(record: OpportunityRecord): Opportunity {
|
||||
slug: record.slug,
|
||||
reference: record.reference,
|
||||
title: record.title,
|
||||
sectorName: record.sector?.name ?? "Sector não indicado",
|
||||
sectorName: record.sector?.name ?? 'Sector não indicado',
|
||||
province: record.province,
|
||||
maturity: record.maturity_label,
|
||||
amount: record.estimated_investment_amount,
|
||||
@@ -142,13 +145,13 @@ export function toOpportunity(record: OpportunityRecord): Opportunity {
|
||||
}
|
||||
|
||||
export function toFeaturedOpportunity(
|
||||
record: HomeResponse["data"]["featuredOpportunities"][number],
|
||||
record: HomeResponse['data']['featuredOpportunities'][number],
|
||||
): Opportunity {
|
||||
return {
|
||||
slug: record.url.split("/").filter(Boolean).at(-1) ?? "",
|
||||
slug: record.url.split('/').filter(Boolean).at(-1) ?? '',
|
||||
reference: record.reference,
|
||||
title: record.title,
|
||||
sectorName: record.sector ?? "Sector não indicado",
|
||||
sectorName: record.sector ?? 'Sector não indicado',
|
||||
province: record.province,
|
||||
maturity: record.maturity,
|
||||
amount: record.estimatedInvestment.amount,
|
||||
@@ -162,14 +165,14 @@ export function toFeaturedOpportunity(
|
||||
}
|
||||
|
||||
export function searchUrl(filters: Filters, page: number): string {
|
||||
const params = new URLSearchParams({ page: String(page), per_page: "12" });
|
||||
if (filters.query.trim()) params.set("q", filters.query.trim());
|
||||
if (filters.sector) params.set("sector", filters.sector);
|
||||
if (filters.province) params.set("province", filters.province);
|
||||
if (filters.maturity) params.set("maturity_level", filters.maturity);
|
||||
if (filters.minAmount) params.set("min_amount", filters.minAmount);
|
||||
if (filters.maxAmount) params.set("max_amount", filters.maxAmount);
|
||||
if (filters.currency) params.set("currency", filters.currency);
|
||||
const params = new URLSearchParams({ page: String(page), per_page: '12' });
|
||||
if (filters.query.trim()) params.set('q', filters.query.trim());
|
||||
if (filters.sector) params.set('sector', filters.sector);
|
||||
if (filters.province) params.set('province', filters.province);
|
||||
if (filters.maturity) params.set('maturity_level', filters.maturity);
|
||||
if (filters.minAmount) params.set('min_amount', filters.minAmount);
|
||||
if (filters.maxAmount) params.set('max_amount', filters.maxAmount);
|
||||
if (filters.currency) params.set('currency', filters.currency);
|
||||
return `/api/v1/opportunities/search?${params.toString()}`;
|
||||
}
|
||||
|
||||
@@ -183,27 +186,29 @@ export function newSubmissionKey(): string {
|
||||
const bytes = crypto.getRandomValues(new Uint8Array(16));
|
||||
bytes[6] = (bytes[6] & 0x0f) | 0x40;
|
||||
bytes[8] = (bytes[8] & 0x3f) | 0x80;
|
||||
const hex = Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");
|
||||
const hex = Array.from(bytes, (byte) =>
|
||||
byte.toString(16).padStart(2, '0'),
|
||||
).join('');
|
||||
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
||||
}
|
||||
|
||||
export function fieldErrors(error: PortalApiError): FormErrors {
|
||||
const result: FormErrors = {};
|
||||
const fields: (keyof InterestFormData)[] = [
|
||||
"name",
|
||||
"email",
|
||||
"phone",
|
||||
"investorType",
|
||||
"country",
|
||||
"sectorInterest",
|
||||
"opportunityReference",
|
||||
"investmentRange",
|
||||
"message",
|
||||
"privacyConsent",
|
||||
'name',
|
||||
'email',
|
||||
'phone',
|
||||
'investorType',
|
||||
'country',
|
||||
'sectorInterest',
|
||||
'opportunityReference',
|
||||
'investmentRange',
|
||||
'message',
|
||||
'privacyConsent',
|
||||
];
|
||||
for (const field of fields) {
|
||||
const messages = error.errors[field === "name" ? "fullName" : field];
|
||||
if (Array.isArray(messages) && typeof messages[0] === "string") {
|
||||
const messages = error.errors[field === 'name' ? 'fullName' : field];
|
||||
if (Array.isArray(messages) && typeof messages[0] === 'string') {
|
||||
result[field] = messages[0];
|
||||
}
|
||||
}
|
||||
@@ -211,11 +216,12 @@ export function fieldErrors(error: PortalApiError): FormErrors {
|
||||
}
|
||||
|
||||
export function money(amount: string | null, currency: string): string {
|
||||
if (amount === null || !/^-?\d+(\.\d+)?$/.test(amount)) return "Por indicar";
|
||||
if (amount === null || !/^-?\d+(\.\d+)?$/.test(amount))
|
||||
return 'Por indicar';
|
||||
try {
|
||||
// Intl accepts decimal strings without converting them to a lossy Number.
|
||||
const formatter = new Intl.NumberFormat("pt-MZ", {
|
||||
style: "currency",
|
||||
const formatter = new Intl.NumberFormat('pt-MZ', {
|
||||
style: 'currency',
|
||||
currency,
|
||||
maximumFractionDigits: 2,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user