Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ ARG ENV
ENV ENV=$ENV
ARG MATOMO_SITE_ID
ENV MATOMO_SITE_ID=$MATOMO_SITE_ID
ARG AIRTABLE_API_KEY
ENV AIRTABLE_API_KEY=$AIRTABLE_API_KEY
ARG NOCODB_API_URL
ENV NOCODB_API_URL=$NOCODB_API_URL
ARG NOCODB_API_TOKEN
ENV NOCODB_API_TOKEN=$NOCODB_API_TOKEN
ARG NOCODB_TABLE_ID
ENV NOCODB_TABLE_ID=$NOCODB_TABLE_ID
ARG GIGA_METER_API_HOST
ENV GIGA_METER_API_HOST=$GIGA_METER_API_HOST

Expand Down
12 changes: 9 additions & 3 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ steps:
--build-arg=B2C_CLIENT_ID=$(B2C_CLIENT_ID_DEV)
--build-arg=ENV=$(ENV_DEV)
--build-arg=MATOMO_SITE_ID=$(MATOMO_SITE_ID_DEV)
--build-arg=AIRTABLE_API_KEY=$(AIRTABLE_API_KEY)
--build-arg=NOCODB_API_URL=$(NOCODB_API_URL_DEV)
--build-arg=NOCODB_API_TOKEN=$(NOCODB_API_TOKEN_DEV)
--build-arg=NOCODB_TABLE_ID=$(NOCODB_TABLE_ID_DEV)
--build-arg=GIGA_METER_API_HOST=$(GIGA_METER_API_HOST_DEV)
tags: |
$(tag)
Expand Down Expand Up @@ -96,7 +98,9 @@ steps:
--build-arg=B2C_CLIENT_ID=$(B2C_CLIENT_ID_STG)
--build-arg=ENV=$(ENV_STG)
--build-arg=MATOMO_SITE_ID=$(MATOMO_SITE_ID_STG)
--build-arg=AIRTABLE_API_KEY=$(AIRTABLE_API_KEY)
--build-arg=NOCODB_API_URL=$(NOCODB_API_URL_STG)
--build-arg=NOCODB_API_TOKEN=$(NOCODB_API_TOKEN_STG)
--build-arg=NOCODB_TABLE_ID=$(NOCODB_TABLE_ID_STG)
--build-arg=GIGA_METER_API_HOST=$(GIGA_METER_API_HOST_STG)
tags: |
$(tag)
Expand Down Expand Up @@ -130,7 +134,9 @@ steps:
--build-arg=B2C_CLIENT_ID=$(B2C_CLIENT_ID_PROD)
--build-arg=ENV=$(ENV_PROD)
--build-arg=MATOMO_SITE_ID=$(MATOMO_SITE_ID_PROD)
--build-arg=AIRTABLE_API_KEY=$(AIRTABLE_API_KEY)
--build-arg=NOCODB_API_URL=$(NOCODB_API_URL_PROD)
--build-arg=NOCODB_API_TOKEN=$(NOCODB_API_TOKEN_PROD)
--build-arg=NOCODB_TABLE_ID=$(NOCODB_TABLE_ID_PROD)
--build-arg=GIGA_METER_API_HOST=$(GIGA_METER_API_HOST_PROD)
tags: |
$(tag)
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@
"@carbon/styles": "1.39.0",
"@matt-block/react-recaptcha-v2": "^2.0.1",
"@withease/i18next": "^23.2.2",
"airtable": "^0.12.2",
"clsx": "^2.0.0",
"date-fns": "^2.30.0",
"effector": "^22.8.6",
Expand All @@ -177,4 +176,4 @@
"workbox-strategies": "^7.0.0",
"workbox-window": "^7.0.0"
}
}
}
75 changes: 48 additions & 27 deletions src/@/sidebar/config/airtable.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
import Airtable from 'airtable';
import { SchoolStatsType } from '~/api/types';
import { AIRTABLE_API_KEY, isProduction, isDevelopment } from '~/env';

let base: Airtable.Base | null = null;

try {
const baseId = isProduction ? 'apppNMLhNC48aqZBt' : 'appU5WGL7iucR55vv';
if (AIRTABLE_API_KEY) {
base = new Airtable({ apiKey: AIRTABLE_API_KEY }).base(baseId);
} else {
console.warn('Airtable API key not provided. Some features may be unavailable.');
}
} catch (error) {
console.error('Error initializing Airtable:', error);
}
import { NOCODB_API_URL, NOCODB_API_TOKEN, NOCODB_TABLE_ID } from '~/env';

// Define the statistics keys based on SchoolStatsType
const statisticsKeys: Array<keyof SchoolStatsType['statistics']> = [
Expand Down Expand Up @@ -51,25 +37,60 @@ export type CountryConfig = {
};

export const fetchConfig = async (): Promise<CountryConfig[]> => {
if (!base) {
console.warn('Airtable base not initialized. Returning empty config.');
if (!NOCODB_API_URL || !NOCODB_API_TOKEN || !NOCODB_TABLE_ID) {
console.warn('NocoDB config not provided. Returning empty config.');
return [];
}

try {
const records = await base('Country Configs').select().all();
const config: CountryConfig[] = records.map(record => {
const countryCode = record.get('Country Code') as number;
// console.log("statisticsKeys", statisticsKeys)
const enabledStatistics: CountryConfig['enabledStatistics'] = statisticsKeys.filter(stat =>
record.get(stat) === true
);
return { countryCode, enabledStatistics };
});
const response = await fetch(
`${NOCODB_API_URL}/tables/${NOCODB_TABLE_ID}/records?limit=200`,
{
headers: {
'xc-token': NOCODB_API_TOKEN,
},
}
);

if (!response.ok) {
throw new Error(`NocoDB API error: ${response.status}`);
}

const data = await response.json();
const records = data?.list ?? [];

const getFieldValue = (record: any, key: string) => {
// NocoDB shapes can differ slightly by version:
// - record[key]
// - record.fields[key]
if (record?.[key] !== undefined) return record[key];
if (record?.fields?.[key] !== undefined) return record.fields[key];
return undefined;
};

const isEnabled = (value: unknown) => {
// Some NocoDB versions return booleans, others return 1/0.
return value === true || value === 1 || value === '1' || value === 'true';
};

const config: CountryConfig[] = records
.map((record: any) => {
const countryCodeRaw = getFieldValue(record, 'Country Code');
const countryCode = Number(countryCodeRaw);
if (!Number.isFinite(countryCode)) return null;

const enabledStatistics = statisticsKeys.filter((statKey) => {
const value = getFieldValue(record, statKey as string);
return isEnabled(value);
});

return { countryCode, enabledStatistics };
})
.filter((row: CountryConfig | null): row is CountryConfig => row !== null);

return config;
} catch (error) {
console.error('Error fetching config:', error);
console.error('Error fetching config from NocoDB:', error);
return [];
}
};
5 changes: 3 additions & 2 deletions src/env.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isProd } from './lib/utils/index';
// See: https://www.npmjs.com/package/dotenv-webpack
// You can reference vars from .env as "process.env.VAR_NAME" (no destructuring)

Expand All @@ -17,7 +16,9 @@ export const API_BASE_URL =

export const GIGA_MERTER_API_HOST = process.env.GIGA_METER_API_HOST ?? 'https://uni-ooi-giga-meter-backend-dev.azurewebsites.net';

export const AIRTABLE_API_KEY = process.env.AIRTABLE_API_KEY ?? '';
export const NOCODB_API_URL = process.env.NOCODB_API_URL ?? '';
export const NOCODB_API_TOKEN = process.env.NOCODB_API_TOKEN ?? '';
export const NOCODB_TABLE_ID = process.env.NOCODB_TABLE_ID ?? '';

// export const RECAPTCHA_KEY = process.env.RECAPTCHA_KEY ?? '';

Expand Down