Skip to content

Commit

Permalink
feat: load apps directly from app store backend
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Arenhill committed Dec 4, 2019
1 parent 5d953f2 commit 74192b5
Showing 1 changed file with 41 additions and 6 deletions.
47 changes: 41 additions & 6 deletions src/actions.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Debug from 'debug'

import Action from 'd2-ui/lib/action/Action';
import { getInstance as getD2 } from 'd2/lib/d2';
import log from 'loglevel';
Expand All @@ -7,6 +9,8 @@ import installedAppStore from './stores/installedApp.store';

import i18n from '@dhis2/d2-i18n'

const debug = Debug('app-management-app:frontend:client')

const actions = {
// App management actions
installApp: Action.create('Install App'),
Expand Down Expand Up @@ -82,12 +86,43 @@ actions.refreshApps.subscribe(() => {
/*
* Load the app store
*/
actions.loadAppStore.subscribe(() => {
getD2().then((d2) => {
d2.system.loadAppStore().then((apps) => {
appStoreStore.setState(Object.assign(appStoreStore.getState() || {}, { apps }));
});
});
actions.loadAppStore.subscribe(async () => {

const d2 = await getD2();
const baseUrl = d2.Api.getApi().baseUrl;

const fetchOptions = {
credentials: 'include',
headers: {
'Content-Type': 'application/json'
}
}

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

const getDhisVersion = async () => {
const response = await fetch(`${baseUrl}/system/info`, fetchOptions)
const json = await response.json();
//if we're running a dev version remove the snapshot suffix to just keep the dhis version
return json.version.replace('-SNAPSHOT', '');
}

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

const version = await getDhisVersion();
debug(`Got dhis2 version: ${version}`)

const corsOptions = { ...fetchOptions, mode: 'cors', credentials: undefined }
debug('Using fetch/cors options:', corsOptions)

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


Expand Down

0 comments on commit 74192b5

Please sign in to comment.