Skip to content

Commit

Permalink
feat(app-hub): use the app-hub directly (#88)
Browse files Browse the repository at this point in the history
* feat: migrate to app hub

* feat(app-hub): use the app hub

* fix(d2): work around d2 having appHub hardcoded
  • Loading branch information
varl authored Feb 25, 2020
1 parent 8751261 commit 18d2154
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 285 deletions.
10 changes: 5 additions & 5 deletions i18n/en.pot
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ msgstr ""
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1)\n"
"POT-Creation-Date: 2019-03-08T14:42:33.797Z\n"
"PO-Revision-Date: 2019-03-08T14:42:33.797Z\n"
"POT-Creation-Date: 2020-02-21T13:01:54.731Z\n"
"PO-Revision-Date: 2020-02-21T13:01:54.731Z\n"

msgid "App installed successfully"
msgstr ""
Expand All @@ -17,10 +17,10 @@ msgstr ""
msgid "App removed successfully"
msgstr ""

msgid "Installing app from the app store..."
msgid "Installing app from the app hub..."
msgstr ""

msgid "Failed to install an app from the app store"
msgid "Failed to install an app from the app hub"
msgstr ""

msgid "Uploading..."
Expand All @@ -47,7 +47,7 @@ msgstr ""
msgid "Resource Apps"
msgstr ""

msgid "App Store"
msgid "App Hub"
msgstr ""

msgid "By"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"file-loader": "^3.0.1",
"html-webpack-plugin": "^3.2.0",
"loglevel": "^1.4.1",
"node-sass": "4.9.0",
"node-sass": "^4.13.1",
"sass-loader": "^6.0.6",
"style-loader": "^0.18.2",
"susy": "^3.0.0",
Expand Down
63 changes: 37 additions & 26 deletions src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import Action from 'd2-ui/lib/action/Action';
import { getInstance as getD2 } from 'd2/lib/d2';
import log from 'loglevel';

import appStoreStore from './stores/appStore.store';
import installedAppStore from './stores/installedApp.store';
import appHubStore from './stores/appHub.store';
import installedAppHub from './stores/installedApp.store';

import i18n from '@dhis2/d2-i18n'

Expand All @@ -19,8 +19,8 @@ const actions = {
appInstalled: Action.create('An app was installed'),

// App store actions
loadAppStore: Action.create('Load DHIS2 App Store'),
installAppVersion: Action.create('Install App Version from DHIS2 App Store'),
loadAppHub: Action.create('Load DHIS2 App Hub'),
installAppVersion: Action.create('Install App Version from DHIS2 App Hub'),

// Snackbar
showSnackbarMessage: Action.create('Show Snackbar message'),
Expand All @@ -37,7 +37,7 @@ actions.installApp.subscribe((params) => {
d2.system.uploadApp(zipFile, progressCallback)
.then(() => d2.system.reloadApps())
.then((apps) => {
installedAppStore.setState(apps);
installedAppHub.setState(apps);

actions.showSnackbarMessage(i18n.t('App installed successfully'));
actions.appInstalled(zipFile.name.substring(0, zipFile.name.lastIndexOf('.')));
Expand Down Expand Up @@ -77,16 +77,16 @@ actions.uninstallApp.subscribe((params) => {
actions.refreshApps.subscribe(() => {
getD2().then((d2) => {
d2.system.reloadApps().then((apps) => {
installedAppStore.setState(apps);
installedAppHub.setState(apps);
});
});
});


/*
* Load the app store
* Load the app hub
*/
actions.loadAppStore.subscribe(async () => {
actions.loadAppHub.subscribe(async () => {

const d2 = await getD2();
const baseUrl = d2.Api.getApi().baseUrl;
Expand All @@ -99,10 +99,10 @@ actions.loadAppStore.subscribe(async () => {
}
}

const getAppstoreUrl = async () => {
const response = await fetch(`${baseUrl}/configuration/settings/filter.json?type=CONFIGURATION`, fetchOptions)
const getAppHubUrl = async () => {
const response = await fetch(`${baseUrl}/configuration/appHubUrl`, fetchOptions)
const dhis2Configuration = await response.json();
return dhis2Configuration['dhis-configurations']['appstore.api.url'];
return dhis2Configuration.apiUrl;
}

const getDhisVersion = async () => {
Expand All @@ -112,8 +112,8 @@ actions.loadAppStore.subscribe(async () => {
return json.version.replace('-SNAPSHOT', '');
}

const url = await getAppstoreUrl();
debug(`Got appstore url: ${url}`)
const url = await getAppHubUrl();
debug(`Got apphub url: ${url}`)

const version = await getDhisVersion();
debug(`Got dhis2 version: ${version}`)
Expand All @@ -123,41 +123,52 @@ actions.loadAppStore.subscribe(async () => {

const response = await fetch(`${url}/apps?dhis_version=${version}`, corsOptions)
const apps = await response.json();
appStoreStore.setState(Object.assign(appStoreStore.getState() || {}, { apps }));
appHubStore.setState(Object.assign(appHubStore.getState() || {}, { apps }));
});


/*
* Install app version from the app store
* Install app version from the app hub
*/
actions.installAppVersion.subscribe((params) => {
const versionId = params.data[0];
const appStoreState = appStoreStore.getState();
appStoreStore.setState(Object.assign(appStoreState, {
installing: appStoreState.installing ? appStoreState.installing + 1 : 1,
const appHubState = appHubStore.getState();
appHubStore.setState(Object.assign(appHubState, {
installing: appHubState.installing ? appHubState.installing + 1 : 1,
}));

getD2().then((d2) => {
actions.showSnackbarMessage(i18n.t('Installing app from the app store...'));
d2.system.installAppVersion(versionId)
actions.showSnackbarMessage(i18n.t('Installing app from the app hub...'));
installAppVersion(versionId, d2)
.then(() => d2.system.reloadApps())
.then((apps) => {
actions.showSnackbarMessage(i18n.t('App installed successfully'));
const appStoreState2 = appStoreStore.getState();
appStoreStore.setState(Object.assign(appStoreState2, { installing: appStoreState2.installing - 1 }));
installedAppStore.setState(apps);
const appHubState2 = appHubStore.getState();
appHubStore.setState(Object.assign(appHubState2, { installing: appHubState2.installing - 1 }));
installedAppHub.setState(apps);
params.complete(apps);
})
.catch((err) => {
actions.showSnackbarMessage(
`${i18n.t('Failed to install an app from the app store')}: ${err.message}`,
`${i18n.t('Failed to install an app from the app hub')}: ${err.message}`,
);
appStoreStore.setState(Object.assign(appStoreStore.getState(), {
installing: appStoreStore.getState().installing - 1,
appHubStore.setState(Object.assign(appHubStore.getState(), {
installing: appHubStore.getState().installing - 1,
}));
log.error(err);
});
});
});

function installAppVersion(uid, d2) {
const api = d2.Api.getApi();
return new Promise((resolve, reject) => {
api.post(['appHub', uid].join('/'), '', { dataType: 'text' }).then(() => {
resolve();
}).catch((err) => {
reject(err);
});
});
}

export default actions;
26 changes: 13 additions & 13 deletions src/components/App.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import Snackbar from 'material-ui/Snackbar';
import FontIcon from 'material-ui/FontIcon';

import AppList from './AppList.component';
import AppStore from './AppStore.component';
import AppHub from './AppHub.component';
import AppTheme from '../theme';

import actions from '../actions';
import appStoreStore from '../stores/appStore.store';
import installedAppStore from '../stores/installedApp.store';
import appHubStore from '../stores/appHub.store';
import installedAppHub from '../stores/installedApp.store';

import i18n from '@dhis2/d2-i18n';

Expand Down Expand Up @@ -73,7 +73,7 @@ class App extends React.Component {
installing: false,
uploading: false,
progress: undefined,
appStore: {},
appHub: {},
lastUpdate: null,
};

Expand All @@ -91,19 +91,19 @@ class App extends React.Component {

componentDidMount() {
this.subscriptions = [
installedAppStore.subscribe((installedApps) => {
installedAppHub.subscribe((installedApps) => {
this.setState({ installedApps, lastUpdate: new Date() });
}),
appStoreStore.subscribe((appStore) => {
this.setState({ appStore, installing: appStore.installing !== undefined && appStore.installing > 0 });
appHubStore.subscribe((appHub) => {
this.setState({ appHub, installing: appHub.installing !== undefined && appHub.installing > 0 });
}),

actions.installApp.subscribe(() => {
this.setState({ uploading: true });
}),
actions.appInstalled.subscribe(({ data }) => {
this.setState({ uploading: false });
this.setSection(installedAppStore.getAppFromKey(data).appType.toLowerCase() || 'app');
this.setSection(installedAppHub.getAppFromKey(data).appType.toLowerCase() || 'app');
}),
actions.refreshApps.subscribe(() => {
this.setState({ uploading: false });
Expand All @@ -119,12 +119,12 @@ class App extends React.Component {
}
}),
actions.installAppVersion.subscribe(({ data }) => {
const app = appStoreStore.getAppFromVersionId(data[0]);
const app = appHubStore.getAppFromVersionId(data[0]);
this.setSection((app.appType && app.appType.toLowerCase()) || 'app');
}),
];

actions.loadAppStore();
actions.loadAppHub();
}

componentWillUnmount() {
Expand Down Expand Up @@ -215,7 +215,7 @@ class App extends React.Component {

renderSection(key, apps, showUpload) {
if (key === 'store') {
return <AppStore appStore={this.state.appStore} />;
return <AppHub appHub={this.state.appHub} />;
}

const filter = (key && key.toString().toUpperCase()) || 'APP';
Expand All @@ -226,7 +226,7 @@ class App extends React.Component {
uploadProgress={this.progress}
transitionUnmount={this.state.unmountSection}
showUpload={showUpload && !this.state.uploading}
appStore={this.state.appStore}
appHub={this.state.appHub}
appTypeFilter={filter}
/>
);
Expand Down Expand Up @@ -284,7 +284,7 @@ class App extends React.Component {
}, {
key: 'store',
icon: 'store',
label: i18n.t('App Store'),
label: i18n.t('App Hub'),
},
].map(section => ({
key: section.key,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function parseDescription(description) {
*/


class AppStore extends React.Component {
class AppHub extends React.Component {
constructor(props) {
super(props);

Expand All @@ -41,8 +41,8 @@ class AppStore extends React.Component {
}

componentWillMount() {
if (!Array.isArray(this.props.appStore.apps)) {
actions.loadAppStore();
if (!Array.isArray(this.props.appHub.apps)) {
actions.loadAppHub();
}
}

Expand Down Expand Up @@ -96,7 +96,7 @@ class AppStore extends React.Component {

return (
<div style={styles.flex}>
{this.props.appStore.apps.map(app => (
{this.props.appHub.apps.map(app => (
<Card style={styles.card} key={app.name}>
<CardHeader
title={app.name}
Expand Down Expand Up @@ -168,9 +168,9 @@ class AppStore extends React.Component {
};

/* eslint-disable react/no-danger */
return Array.isArray(this.props.appStore.apps) ? (
return Array.isArray(this.props.appHub.apps) ? (
<div>
<div style={styles.header}>{i18n.t('App Store')}</div>
<div style={styles.header}>{i18n.t('App Hub')}</div>
<div style={styles.apps}>{this.renderApps()}</div>
</div>
) : (
Expand All @@ -180,15 +180,15 @@ class AppStore extends React.Component {
);
}
}
AppStore.propTypes = {
appStore: React.PropTypes.object.isRequired,
AppHub.propTypes = {
appHub: React.PropTypes.object.isRequired,
};
AppStore.defaultProps = {
appStore: {},
AppHub.defaultProps = {
appHub: {},
};

AppStore.contextTypes = {
AppHub.contextTypes = {
d2: React.PropTypes.object,
};

export default AppStore;
export default AppHub;
4 changes: 2 additions & 2 deletions src/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import injectTapEventPlugin from 'react-tap-event-plugin';
import D2Library from 'd2/lib/d2';
import LoadingMask from 'd2-ui/lib/loading-mask/LoadingMask.component';

import installedAppStore from './stores/installedApp.store';
import installedAppHub from './stores/installedApp.store';
import App from './components/App.component';
import theme from './theme';

Expand Down Expand Up @@ -37,7 +37,7 @@ D2Library.getManifest('manifest.webapp')
.then(D2Library.init)
.then((d2) => {
log.debug('D2 initialized', d2);
installedAppStore.setState(d2.system.installedApps);
installedAppHub.setState(d2.system.installedApps);
ReactDOM.render(
<MuiThemeProvider muiTheme={theme}><App d2={d2} /></MuiThemeProvider>,
document.getElementById('app'),
Expand Down
6 changes: 3 additions & 3 deletions src/locales/en/translations.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"App installed successfully": "",
"Failed to install app": "",
"App removed successfully": "",
"Installing app from the app store...": "",
"Failed to install an app from the app store": "",
"Installing app from the app hub...": "",
"Failed to install an app from the app hub": "",
"Uploading...": "",
"Installing...": "",
"No apps found": "",
Expand All @@ -12,7 +12,7 @@
"Dashboard Apps": "",
"Tracker Dashboard Apps": "",
"Resource Apps": "",
"App Store": "",
"App Hub": "",
"By": "",
"Install": ""
}
2 changes: 1 addition & 1 deletion src/manifest.webapp
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
"icons": {
"48": "icon.png"
},
"manifest_generated_at": "Fri Mar 08 2019 16:04:51 GMT+0100 (GMT+01:00)"
"manifest_generated_at": "Fri Feb 21 2020 19:59:02 GMT+0100 (Central European Standard Time)"
}
File renamed without changes.
Loading

0 comments on commit 18d2154

Please sign in to comment.