2025-11-25 10:50:27 +01:00

99 lines
2.9 KiB
TypeScript

import { createSelector } from "@reduxjs/toolkit";
import { RootState } from "../store";
import { FieldGroupMap, SidebarLink } from "./metadataSlice";
import { COUNTRY_CODES, CountryCodeEntry } from "./constants";
export const selectMetadataState = (state: RootState) => state.metadata;
export const selectMetadataStatus = (state: RootState) =>
state.metadata?.status;
export const selectMetadataError = (state: RootState) => state.metadata?.error;
export const selectAppMetadata = (state: RootState) => state.metadata?.data;
export const selectFieldNames = (state: RootState): FieldGroupMap | undefined =>
state.metadata.data?.field_names;
export const selectSidebarLinks = (state: RootState): SidebarLink[] =>
state.metadata.data?.sidebar?.links ?? [];
export const selectJobTitles = (state: RootState): string[] =>
state.metadata.data?.job_titles ?? [];
export const selectGroups = (state: RootState): string[] =>
state.metadata.data?.groups ?? [];
export const selectMerchants = (state: RootState): string[] =>
state.metadata.data?.merchants ?? [];
export const selectCountries = (state: RootState): string[] =>
state.metadata.data?.countries ?? [];
export const selectTransactionStatuses = (state: RootState): string[] =>
state.metadata.data?.transaction_status ?? [];
export const selectNavigationSidebar = (state: RootState): SidebarLink[] =>
state.metadata.data?.sidebar?.links ?? [];
export const selectConditionOperators = (
state: RootState
): Record<string, string> | undefined =>
state.metadata.data?.field_names?.conditions;
export const selectTransactionFieldNames = (
state: RootState
): Record<string, string> | undefined =>
state.metadata.data?.field_names?.transactions;
// Re-Selectcrors
const normalizeCountryName = (value: string): string =>
value
.normalize("NFD")
.replace(/[\u0300-\u036f]/g, "")
.replace(/[^a-z0-9]/gi, "")
.toLowerCase();
const COUNTRY_CODE_LOOKUP = COUNTRY_CODES.reduce<
Record<string, CountryCodeEntry>
>((acc, entry) => {
acc[normalizeCountryName(entry.country)] = entry;
return acc;
}, {});
const findCountryMetadata = (
countryName: string
): CountryCodeEntry | undefined => {
const normalized = normalizeCountryName(countryName);
if (!normalized) {
return undefined;
}
if (COUNTRY_CODE_LOOKUP[normalized]) {
return COUNTRY_CODE_LOOKUP[normalized];
}
return COUNTRY_CODES.find(entry => {
const normalizedCountry = normalizeCountryName(entry.country);
return (
normalizedCountry &&
(normalizedCountry.includes(normalized) ||
normalized.includes(normalizedCountry))
);
});
};
export const selectPhoneNumberCountries = createSelector(
[selectCountries],
countries =>
countries.map(country => {
const metadata = findCountryMetadata(country);
return {
code: metadata?.code ?? "",
flag: metadata?.flag ?? "",
name: country,
};
})
);