Skip to content

Commit

Permalink
Minor refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
p3rcypj committed Sep 17, 2024
1 parent 6bc565a commit 89d4935
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 50 deletions.
4 changes: 2 additions & 2 deletions src/app-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ export const appConfig: AppConfig = {
buttonPosition: "bottom-end",
},
},
storage: "dataStore",
storage: "constants",
};

export interface AppConfig {
interface AppConfig {
id: string;
appearance: {
showShareButton: boolean;
Expand Down
22 changes: 9 additions & 13 deletions src/data/repositories/ConfigD2ConstantsRepository.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { apiToFuture, FutureData } from "$/data/api-futures";
import { configCodec } from "$/data/config-codec";
import { constants, errors } from "$/data/repositories/d2-metadata";
import { Config, defaultConfig } from "$/domain/entities/Config";
import { Future } from "$/domain/entities/generic/Future";
import { ConfigRepository } from "$/domain/repositories/ConfigRepository";
import { D2Api, MetadataPick } from "$/types/d2-api";
import i18n from "$/utils/i18n";

export class ConfigD2ConstantsRepository implements ConfigRepository {
constructor(private api: D2Api) {}
Expand All @@ -13,7 +13,7 @@ export class ConfigD2ConstantsRepository implements ConfigRepository {
const constants$ = apiToFuture(
this.api.models.constants.get({
fields: constantFields,
filter: { code: { eq: "TALLY_SHEETS_STORAGE" } },
filter: { code: { eq: constants.constantsStorageCode } },
paging: false,
})
);
Expand All @@ -31,7 +31,7 @@ export class ConfigD2ConstantsRepository implements ConfigRepository {
const constant$ = apiToFuture(
this.api.models.constants.get({
fields: constantFields,
filter: { code: { eq: "TALLY_SHEETS_STORAGE" } },
filter: { code: { eq: constants.constantsStorageCode } },
paging: false,
})
).map(res => res.objects.at(0));
Expand All @@ -55,9 +55,9 @@ export class ConfigD2ConstantsRepository implements ConfigRepository {

private create(payload?: D2Constant): FutureData<void> {
const defaultPayload = {
code: "TALLY_SHEETS_STORAGE",
name: "Tally Sheets Storage",
shortName: "Tally Sheets Storage",
code: constants.constantsStorageCode,
name: constants.constantsStorageName,
shortName: constants.constantsStorageName,
description: JSON.stringify(defaultConfig),
value: 1,
};
Expand All @@ -70,8 +70,9 @@ export class ConfigD2ConstantsRepository implements ConfigRepository {
private decodeConfig(description: Config): FutureData<Config> {
return configCodec.decode(description).caseOf({
Left: (err): FutureData<Config> => {
console.error(new Error(errorMessages.invalidJsonErrMsg + err));
return Future.error(new Error(errorMessages.invalidJsonErrMsg));
const errStr = errors.invalidJSON(constants.constantsStorageName, "description");
console.error(new Error(errStr + err));
return Future.error(new Error(errStr));
},
Right: (res): FutureData<Config> => {
if ("sheetName" in res) return Future.success(res);
Expand All @@ -84,11 +85,6 @@ export class ConfigD2ConstantsRepository implements ConfigRepository {
}
}

const errorMessages = {
constantsErrMsg: i18n.t("The TALLY_SHEETS_STORAGE constant is not defined."),
invalidJsonErrMsg: i18n.t("The TALLY_SHEETS_STORAGE description is not a valid JSON object."),
} as const;

const constantFields = {
id: true,
code: true,
Expand Down
18 changes: 7 additions & 11 deletions src/data/repositories/ConfigD2DataStoreRepository.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { DataStore } from "@eyeseetea/d2-api/api";
import { apiToFuture, FutureData } from "$/data/api-futures";
import { configCodec } from "$/data/config-codec";
import { Config, defaultConfig } from "$/domain/entities/Config";
import { Future } from "$/domain/entities/generic/Future";
import { ConfigRepository } from "$/domain/repositories/ConfigRepository";
import { D2Api } from "$/types/d2-api";
import i18n from "$/utils/i18n";
import { DataStore } from "@eyeseetea/d2-api/api";
import { constants, errors } from "$/data/repositories/d2-metadata";

export class ConfigD2DataStoreRepository implements ConfigRepository {
private namespace = "HMIS_Tally_Sheets";
private configKey = "config";
private namespace = constants.datastoreNamespace;
private configKey = constants.datastoreKey;
private dataStore: DataStore;

constructor(private api: D2Api) {
Expand All @@ -36,8 +36,9 @@ export class ConfigD2DataStoreRepository implements ConfigRepository {
private decodeConfig(config: Config): FutureData<Config> {
return configCodec.decode(config).caseOf({
Left: (err): FutureData<Config> => {
console.error(new Error(errorMessages.invalidJsonErrMsg + err));
return Future.error(new Error(errorMessages.invalidJsonErrMsg));
const errStr = errors.invalidJSON(this.namespace, this.configKey);
console.error(new Error(errStr + err));
return Future.error(new Error(errStr));
},
Right: (res): FutureData<Config> => {
if ("sheetName" in res) return Future.success(res);
Expand All @@ -49,8 +50,3 @@ export class ConfigD2DataStoreRepository implements ConfigRepository {
});
}
}

const errorMessages = {
constantsErrMsg: i18n.t("The TALLY_SHEETS_STORAGE constant is not defined."),
invalidJsonErrMsg: i18n.t("The TALLY_SHEETS_STORAGE description is not a valid JSON object."),
} as const;
17 changes: 17 additions & 0 deletions src/data/repositories/d2-metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import i18n from "$/utils/i18n";

export const constants = {
constantsStorageCode: "TALLY_SHEETS_STORAGE",
constantsStorageName: "Tally Sheets Storage",
datastoreNamespace: "HMIS_Tally_Sheets",
datastoreKey: "config",
hideAttributeName: "hideInTallySheet",
};

export const errors = {
invalidJSON: (storage: string, key: string) =>
i18n.t("The {{storage}} {{key}} is not a valid JSON object.", {
storage: storage,
key: key,
}),
};
7 changes: 4 additions & 3 deletions src/domain/entities/BasicDataSet.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { constants } from "$/data/repositories/d2-metadata";
import _c from "$/domain/entities/generic/Collection";
import { Struct } from "$/domain/entities/generic/Struct";
import { Id, NamedRef } from "$/domain/entities/Ref";
Expand Down Expand Up @@ -27,9 +28,9 @@ export class BasicDataSet extends Struct<BasicDataSetAttrs>() {
}
}

export function validateDataSetIsAllowed(basicDataSet: BasicDataSet): BasicDataSet {
function validateDataSetIsAllowed(basicDataSet: BasicDataSet): BasicDataSet {
const hasHideAttribute = basicDataSet.attributeValues.some(
av => av.attribute.name === "hideInTallySheet" && av.value === "true"
av => av.attribute.name === constants.hideAttributeName && av.value === "true"
);

const hasCustomFormType = basicDataSet.formType === "CUSTOM";
Expand All @@ -49,7 +50,7 @@ type AttributeValue = {
attribute: NamedRef;
};

export type DataSetFormType = "DEFAULT" | "CUSTOM" | "SECTION" | "SECTION_MULTIORG";
type DataSetFormType = "DEFAULT" | "CUSTOM" | "SECTION" | "SECTION_MULTIORG";

export type Translation = {
property: string;
Expand Down
2 changes: 1 addition & 1 deletion src/domain/entities/DataSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ type Category = {
categoryOptions: CategoryOption[];
};

export type CategoryOption = {
type CategoryOption = {
id: Id;
name: string;
formName?: string;
Expand Down
16 changes: 5 additions & 11 deletions src/webapp/pages/app/__tests__/App.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,17 @@ import { getTestContext } from "$/utils/tests";
import { Provider } from "@dhis2/app-runtime";

describe("App", () => {
// it("renders the feedback component", async () => {
// const view = getView();
// expect(await view.findByText("Send feedback")).toBeInTheDocument();
// });
// it("navigates to page", async () => {
// const view = getView();
// fireEvent.click(await view.findByText("John"));
// expect(await view.findByText("Hello John")).toBeInTheDocument();
// expect(view.asFragment()).toMatchSnapshot();
// });
it("renders the feedback component", async () => {
const view = getView();
expect(await view.findByText("Send feedback")).toBeInTheDocument();
});
it("with i18n translates correctly", () => {});
});

function getView() {
const { compositionRoot } = getTestContext();
return render(
<Provider config={{ baseUrl: "http://localhost:8080", apiVersion: 30 }}>
<Provider config={{ baseUrl: process.env["VITE_DHIS2_BASE_URL"] ?? "", apiVersion: 37 }}>
<App compositionRoot={compositionRoot} />
</Provider>
);
Expand Down
27 changes: 18 additions & 9 deletions src/webapp/pages/landing/LandingPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,10 @@ export const LandingPage: React.FC = React.memo(() => {

const selectedDataSets = dataSetSelectorProps.selectedItems;

const availableLocales = React.useMemo(() => {
const avail = _c(selectedDataSets.map(ds => ds.getAvailableLocaleCodes()))
.flatten()
.concat("en") // Add English because is not included in translations
.uniq()
.compact()
.value();
return _c(selectedDataSets).isEmpty() ? [] : avail;
}, [selectedDataSets]);
const availableLocales = React.useMemo(
() => getAvailableLocales(selectedDataSets),
[selectedDataSets]
);

const languageSelectorProps = useLanguagesSelector(
availableLocales,
Expand Down Expand Up @@ -162,8 +157,10 @@ export const LandingPage: React.FC = React.memo(() => {
selected={orgUnits}
disabled={loading}
/>

<MultipleSelector {...dataSetSelectorProps} />
<MultipleSelector {...languageSelectorProps} />

<DisableableTooltip
title={i18n.t("Restore changes")}
disabled={disabledRestore}
Expand Down Expand Up @@ -201,6 +198,7 @@ export const LandingPage: React.FC = React.memo(() => {
</Button>
</Tooltip>
)}

<Button
className={classes.actionButton}
variant="outlined"
Expand All @@ -210,6 +208,7 @@ export const LandingPage: React.FC = React.memo(() => {
>
{i18n.t("Print")}
</Button>

<Button
className={classes.actionButton}
variant="contained"
Expand Down Expand Up @@ -544,6 +543,16 @@ function getId(dataSet: BasicDataSet) {
return dataSet.id;
}

function getAvailableLocales(dataSets: BasicDataSet[]): string[] {
const avail = _c(dataSets.map(ds => ds.getAvailableLocaleCodes()))
.flatten()
.concat("en") // Add English because is not included in translations
.uniq()
.compact()
.value();
return _c(dataSets).isEmpty() ? [] : avail;
}

const useStyles = makeStyles((_theme: Theme) =>
createStyles({
iconButton: {
Expand Down

0 comments on commit 89d4935

Please sign in to comment.