Skip to content

Commit

Permalink
feat: Show flags next to earthquake data sources
Browse files Browse the repository at this point in the history
  • Loading branch information
bclswl0827 committed Sep 5, 2024
1 parent ae5790f commit e81a8b5
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 29 deletions.
13 changes: 13 additions & 0 deletions frontend/src/src/helpers/i18n/getFlagByCountry.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export const getFlagByCountry = (countryCode: string) => {
if (!countryCode || countryCode.length !== 2 || !/^[a-zA-Z]+$/.test(countryCode)) {
return "🏳️";
}

const code = countryCode.toUpperCase();
const offset = 127397;
const flag = Array.from(code)
.map((letter) => String.fromCodePoint(letter.charCodeAt(0) + offset))
.join("");

return flag;
};
14 changes: 10 additions & 4 deletions frontend/src/src/models/response/common/trace/0.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
{
"time": "1970-01-01T23:59:59Z",
"time": "2024-09-05T19:26:27Z",
"status": 200,
"error": false,
"path": "/api/v1/trace",
"message": "Successfully read available data source list",
"message": "Successfully read available data source properties",
"data": [
{
"name": "null",
"value": "null"
"id": "hko",
"country": "HK",
"default": "en-US",
"locales": {
"en-US": "Hong Kong Observatory",
"zh-TW": "天文台全球地震資訊網",
"zh-CN": "天文台全球地震信息网"
}
}
]
}
69 changes: 44 additions & 25 deletions frontend/src/src/views/History/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { apiConfig, traceCommonResponseModel1 } from "../../config/api";
import { i18nConfig } from "../../config/i18n";
import { RouterComponentProps } from "../../config/router";
import { ReduxStoreProps } from "../../config/store";
import { getFlagByCountry } from "../../helpers/i18n/getFlagByCountry";
import { sendPromiseAlert } from "../../helpers/interact/sendPromiseAlert";
import { sendUserAlert } from "../../helpers/interact/sendUserAlert";
import { requestRestApi } from "../../helpers/request/requestRestApi";
Expand All @@ -31,6 +32,8 @@ import { handleSetCharts } from "./handleSetCharts";
import { handleSetLabels } from "./handleSetLabels";

const History = (props: RouterComponentProps) => {
const { fallback: fallbackLocale } = i18nConfig;
const { locale } = props;
const { t } = useTranslation();

const { station } = useSelector(({ station }: ReduxStoreProps) => station);
Expand Down Expand Up @@ -406,28 +409,47 @@ const History = (props: RouterComponentProps) => {
}));
};

setForm((prev) => ({
...prev,
open: true,
selectOptions: res.data
.sort((a, b) => {
if ("name" in a && "value" in a && "name" in b && "value" in b) {
return a.name.localeCompare(b.name);
}
return 0;
})
.map((source) => {
if ("name" in source && "value" in source) {
return { label: source.name, value: source.value };
}
return { label: "", value: "" };
}),
onSubmit: handleSubmitForm,
title: "views.history.forms.choose_source.title",
cancelText: "views.history.forms.choose_source.cancel",
submitText: "views.history.forms.choose_source.submit",
placeholder: "views.history.forms.choose_source.placeholder"
}));
setForm((prev) => {
const currentLocale = locale ?? fallbackLocale;
return {
...prev,
open: true,
selectOptions: res.data
.sort((a, b) => {
if ("locales" in a && "locales" in b) {
const aLocale =
a.locales[currentLocale as keyof typeof a.locales] ??
a.locales[a.default as keyof typeof a.locales];
const bLocale =
b.locales[currentLocale as keyof typeof b.locales] ??
b.locales[b.default as keyof typeof b.locales];
return aLocale.localeCompare(bLocale);
}
return 0;
})
.map((seisSource) => {
if ("locales" in seisSource) {
const source =
seisSource.locales[
currentLocale as keyof typeof seisSource.locales
] ??
seisSource.locales[
seisSource.default as keyof typeof seisSource.locales
];
return {
label: `${getFlagByCountry(seisSource.country)} ${source}`,
value: seisSource.id
};
}
return { label: "", value: "" };
}),
onSubmit: handleSubmitForm,
title: "views.history.forms.choose_source.title",
cancelText: "views.history.forms.choose_source.cancel",
submitText: "views.history.forms.choose_source.submit",
placeholder: "views.history.forms.choose_source.placeholder"
};
});
};

const handleGetShareLink = async () => {
Expand All @@ -451,9 +473,6 @@ const History = (props: RouterComponentProps) => {
);
};

const { locale } = props;
const { fallback: fallbackLocale } = i18nConfig;

return (
<>
<Container
Expand Down

0 comments on commit e81a8b5

Please sign in to comment.