Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: O3-310: Add an application-writable config.json file in frontends/ (attempt #2) #865

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { isVersionSatisfied, openmrsFetch } from '@openmrs/esm-framework';
import difference from 'lodash-es/difference';
import { useMemo, useState } from 'react';

export type ResolvedBackendModuleType = 'missing' | 'version-mismatch' | 'okay';

Expand Down Expand Up @@ -155,3 +156,21 @@ export async function checkModules(): Promise<Array<ResolvedDependenciesModule>>
export function hasInvalidDependencies(frontendModules: Array<ResolvedDependenciesModule>) {
return frontendModules.some((m) => m.dependencies.some((n) => n.type !== 'okay'));
}

export function useBackendDependencyCheck(moduleName: string) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rename the hook to something like: useBackendModuleExists(moduleName: string). What do you think of adding an optional parameter that validates the module versions:

export function useBackendModuleExists(moduleName: string, moduleVersion?: string) {}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with the rename, but don't complicate code for no reason. You can add that parameter when you need it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually think we are going to need a version check for this to function properly. Regardless of which module the backend code for this goes into, it's only going to be available from the next version on, so it would be nice if isSpaModulePresent was actually based on not just the presence of the module, but the presence of at least a minimal version...

const [backendDependencies, setBackendDependencies] = useState<Array<BackendModule>>([]);

useMemo(async () => {
const dependencies = await initInstalledBackendModules();
setBackendDependencies(dependencies);
}, []);
Comment on lines +163 to +166
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use a useEffect instead?


const isPresent = useMemo(() => {
if (backendDependencies) {
return backendDependencies.some((module) => module.uuid === moduleName);
}
return false;
}, [backendDependencies, moduleName]);

return isPresent;
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import React, { useMemo, useState } from 'react';
import { Button, Column, FlexGrid, Row, TextInput, Toggle } from '@carbon/react';
import { useTranslation } from 'react-i18next';
import { ChevronDown, ChevronUp, Download, TrashCan } from '@carbon/react/icons';
import { ChevronDown, ChevronUp, Download, TrashCan, Upload } from '@carbon/react/icons';
import cloneDeep from 'lodash-es/cloneDeep';
import isEmpty from 'lodash-es/isEmpty';
import type { Config } from '@openmrs/esm-framework/src/internal';
import {
getExtensionInternalStore,
implementerToolsConfigStore,
navigate,
showNotification,
showToast,
temporaryConfigStore,
useStore,
useStoreWithActions,
Expand All @@ -17,6 +20,8 @@ import { Description } from './interactive-editor/description.component';
import type { ImplementerToolsStore } from '../store';
import { implementerToolsStore } from '../store';
import styles from './configuration.styles.scss';
import { saveConfig } from './configuration.resource';
import { useBackendDependencyCheck } from '../backend-dependencies/openmrs-backend-dependencies';

const JsonEditor = React.lazy(() => import('./json-editor/json-editor.component'));

Expand Down Expand Up @@ -62,6 +67,7 @@ export interface ConfigurationProps {}

export const Configuration: React.FC<ConfigurationProps> = () => {
const { t } = useTranslation();
const isSpaModulePresent = useBackendDependencyCheck('spa');
const {
isUIEditorEnabled,
toggleIsUIEditorEnabled,
Expand Down Expand Up @@ -158,6 +164,43 @@ export const Configuration: React.FC<ConfigurationProps> = () => {
>
{t('clearConfig', 'Clear Local Config')}
</Button>
{isSpaModulePresent ? (
<Button
kind="primary"
iconDescription="Publish config to server"
renderIcon={(props) => <Upload size={16} {...props} />}
onClick={() => {
saveConfig(tempConfig).then(
(response) => {
if (response.ok && !response.redirected) {
temporaryConfigStore.setState({ config: {} });
showToast({
critical: true,
title: t('savedConfiguration', 'Configuration published'),
kind: 'success',
description: t(
'successfullySavedConfiguration',
'Configuration has been pusblished to the server. It is now live for all users. Your local configuration overrides have been cleared.',
),
});
} else if (response.ok && response.redirected) {
navigate({ to: response.url });
}
},
(error) => {
showNotification({
title: t('errorSavingConfiguration', 'Error saving configuration'),
kind: 'error',
critical: true,
description: error?.message,
});
},
);
}}
>
{t('publishConfig', 'Publish Config')}
</Button>
) : null}
<Button
kind="secondary"
iconDescription="Download config"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { type Config, openmrsFetch } from '@openmrs/esm-framework';

export async function saveConfig(config: Config) {
return openmrsFetch(`/ws/frontend/config.json`, {
headers: {
'Content-Type': 'application/json',
},
method: 'POST',
body: config,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
bottom: 0;
left: 0;
width: 100%;
z-index: 100000;
z-index: 99000;
background-color: $ui-03;
border-top: 2px solid $ui-05;
color: $color-gray-100;
Expand Down
4 changes: 4 additions & 0 deletions packages/apps/esm-implementer-tools-app/translations/am.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"edit": "Edit",
"editValueButtonText": "Edit",
"enabled": "Enabled",
"errorSavingConfiguration": "Error saving configuration",
"extensions": "Extensions",
"featureFlag": "Feature Flag",
"featureFlags": "Feature Flags",
Expand All @@ -19,8 +20,11 @@
"missing": "Missing",
"moduleName": "Module Name",
"modulesWithMissingDependenciesWarning": "Some modules have unresolved backend dependencies",
"publishConfig": "Publish Config",
"requiredVersion": "Required Version",
"resetToDefaultValueButtonText": "Reset to default",
"savedConfiguration": "Configuration published",
"successfullySavedConfiguration": "Configuration has been pusblished to the server. It is now live for all users. Your local configuration overrides have been cleared.",
"uiEditor": "UI Editor",
"unknownVersion": "unknown",
"value": "Value",
Expand Down
4 changes: 4 additions & 0 deletions packages/apps/esm-implementer-tools-app/translations/ar.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"edit": "تعديل",
"editValueButtonText": "تعديل",
"enabled": "مفعل",
"errorSavingConfiguration": "Error saving configuration",
"extensions": "الملحقات",
"featureFlag": "علامة الميزة",
"featureFlags": "علامات الميزة",
Expand All @@ -19,8 +20,11 @@
"missing": "مفقود",
"moduleName": "اسم الوحدة",
"modulesWithMissingDependenciesWarning": "بعض الوحدات لديها اعتمادات خلفية غير محلولة",
"publishConfig": "Publish Config",
"requiredVersion": "الإصدار المطلوب",
"resetToDefaultValueButtonText": "إعادة تعيين إلى القيمة الافتراضية",
"savedConfiguration": "Configuration published",
"successfullySavedConfiguration": "Configuration has been pusblished to the server. It is now live for all users. Your local configuration overrides have been cleared.",
"uiEditor": "محرر الواجهة",
"unknownVersion": "غير معروف",
"value": "قيمة",
Expand Down
4 changes: 4 additions & 0 deletions packages/apps/esm-implementer-tools-app/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"edit": "Edit",
"editValueButtonText": "Edit",
"enabled": "Enabled",
"errorSavingConfiguration": "Error saving configuration",
"extensions": "Extensions",
"featureFlag": "Feature Flag",
"featureFlags": "Feature Flags",
Expand All @@ -19,8 +20,11 @@
"missing": "Missing",
"moduleName": "Module Name",
"modulesWithMissingDependenciesWarning": "Some modules have unresolved backend dependencies",
"publishConfig": "Publish Config",
"requiredVersion": "Required Version",
"resetToDefaultValueButtonText": "Reset to default",
"savedConfiguration": "Saved configuration",
"successfullySavedConfiguration": "Successfully saved configuration",
"uiEditor": "UI Editor",
"unknownVersion": "unknown",
"value": "Value",
Expand Down
4 changes: 4 additions & 0 deletions packages/apps/esm-implementer-tools-app/translations/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"edit": "Editar",
"editValueButtonText": "Editar",
"enabled": "Habilitado",
"errorSavingConfiguration": "Error saving configuration",
"extensions": "Extensiones",
"featureFlag": "Feature Flag",
"featureFlags": "Feature Flags",
Expand All @@ -19,8 +20,11 @@
"missing": "Falta",
"moduleName": "Nombre de modulo",
"modulesWithMissingDependenciesWarning": "Some modules have unresolved backend dependencies",
"publishConfig": "Publish Config",
"requiredVersion": "Versión requerida",
"resetToDefaultValueButtonText": "Volver al valor predeterminado",
"savedConfiguration": "Configuration published",
"successfullySavedConfiguration": "Configuration has been pusblished to the server. It is now live for all users. Your local configuration overrides have been cleared.",
"uiEditor": "Editór visual",
"unknownVersion": "desconocida",
"value": "Valor",
Expand Down
4 changes: 4 additions & 0 deletions packages/apps/esm-implementer-tools-app/translations/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"edit": "Edit",
"editValueButtonText": "Edit",
"enabled": "Enabled",
"errorSavingConfiguration": "Error saving configuration",
"extensions": "Extensions",
"featureFlag": "Feature Flag",
"featureFlags": "Feature Flags",
Expand All @@ -19,8 +20,11 @@
"missing": "Missing",
"moduleName": "Module Name",
"modulesWithMissingDependenciesWarning": "Some modules have unresolved backend dependencies",
"publishConfig": "Publish Config",
"requiredVersion": "Required Version",
"resetToDefaultValueButtonText": "Reset to default",
"savedConfiguration": "Configuration published",
"successfullySavedConfiguration": "Configuration has been pusblished to the server. It is now live for all users. Your local configuration overrides have been cleared.",
"uiEditor": "UI Editor",
"unknownVersion": "unknown",
"value": "Value",
Expand Down
4 changes: 4 additions & 0 deletions packages/apps/esm-implementer-tools-app/translations/he.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"edit": "ערוך",
"editValueButtonText": "ערוך",
"enabled": "Enabled",
"errorSavingConfiguration": "Error saving configuration",
"extensions": "Extensions",
"featureFlag": "Feature Flag",
"featureFlags": "Feature Flags",
Expand All @@ -19,8 +20,11 @@
"missing": "חסר",
"moduleName": "שם המודול",
"modulesWithMissingDependenciesWarning": "Some modules have unresolved backend dependencies",
"publishConfig": "Publish Config",
"requiredVersion": "גרסה נדרשת",
"resetToDefaultValueButtonText": "איפוס לברירת מחדל",
"savedConfiguration": "Configuration published",
"successfullySavedConfiguration": "Configuration has been pusblished to the server. It is now live for all users. Your local configuration overrides have been cleared.",
"uiEditor": "עורך ממשק משתמש",
"unknownVersion": "לא ידוע",
"value": "ערך",
Expand Down
4 changes: 4 additions & 0 deletions packages/apps/esm-implementer-tools-app/translations/km.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"edit": "Edit",
"editValueButtonText": "Edit",
"enabled": "Enabled",
"errorSavingConfiguration": "Error saving configuration",
"extensions": "Extensions",
"featureFlag": "Feature Flag",
"featureFlags": "Feature Flags",
Expand All @@ -19,8 +20,11 @@
"missing": "Missing",
"moduleName": "Module Name",
"modulesWithMissingDependenciesWarning": "Some modules have unresolved backend dependencies",
"publishConfig": "Publish Config",
"requiredVersion": "Required Version",
"resetToDefaultValueButtonText": "Reset to default",
"savedConfiguration": "Configuration published",
"successfullySavedConfiguration": "Configuration has been pusblished to the server. It is now live for all users. Your local configuration overrides have been cleared.",
"uiEditor": "UI Editor",
"unknownVersion": "unknown",
"value": "Value",
Expand Down
Loading