Skip to content

Commit 1befb7f

Browse files
committed
feat: implement router
1 parent 469c9fe commit 1befb7f

File tree

10 files changed

+171
-137
lines changed

10 files changed

+171
-137
lines changed

d2.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const config = {
55
coreApp: true,
66

77
entryPoints: {
8-
app: './src/AppWrapper',
8+
app: './src/App',
99
},
1010
}
1111

i18n/en.pot

+20-8
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ msgstr ""
55
"Content-Type: text/plain; charset=utf-8\n"
66
"Content-Transfer-Encoding: 8bit\n"
77
"Plural-Forms: nplurals=2; plural=(n != 1)\n"
8-
"POT-Creation-Date: 2020-08-28T10:39:30.969Z\n"
9-
"PO-Revision-Date: 2020-08-28T10:39:30.969Z\n"
8+
"POT-Creation-Date: 2021-02-15T13:13:41.656Z\n"
9+
"PO-Revision-Date: 2021-02-15T13:13:41.656Z\n"
1010

1111
msgid "App installed successfully"
1212
msgstr ""
@@ -23,6 +23,18 @@ msgstr ""
2323
msgid "Failed to install an app from the app hub"
2424
msgstr ""
2525

26+
msgid "By"
27+
msgstr ""
28+
29+
msgid "Install"
30+
msgstr ""
31+
32+
msgid "App Hub"
33+
msgstr ""
34+
35+
msgid "CORE APP"
36+
msgstr ""
37+
2638
msgid "Uploading..."
2739
msgstr ""
2840

@@ -47,20 +59,20 @@ msgstr ""
4759
msgid "Resource Apps"
4860
msgstr ""
4961

50-
msgid "App Hub"
62+
msgid "A new version of App Management is available on the App Hub"
5163
msgstr ""
5264

53-
msgid "By"
65+
msgid "Update and reload app"
5466
msgstr ""
5567

56-
msgid "Install"
68+
msgid "Preinstalled core apps"
5769
msgstr ""
5870

59-
msgid "CORE APP"
71+
msgid "Custom apps"
6072
msgstr ""
6173

62-
msgid "A new version of App Management is available on the App Hub"
74+
msgid "Discover more apps"
6375
msgstr ""
6476

65-
msgid "Update and reload app"
77+
msgid "Manual install"
6678
msgstr ""

src/App.js

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { useD2 } from '@dhis2/app-runtime-adapter-d2'
2+
import { CssVariables } from '@dhis2/ui'
3+
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider'
4+
import React from 'react'
5+
import { HashRouter, Route, Switch, Redirect } from 'react-router-dom'
6+
import styles from './App.module.css'
7+
import CoreApps from './components/CoreApps'
8+
import CustomApps from './components/CustomApps'
9+
import Sidebar from './components/Sidebar'
10+
import './locales'
11+
12+
import installedAppHub from './stores/installedApp.store'
13+
import 'material-design-icons-iconfont/dist/material-design-icons.css'
14+
import './scss/style.scss'
15+
import theme from './theme'
16+
17+
// TODO
18+
const AppHub = () => null
19+
const ManualInstall = () => null
20+
21+
const App = () => {
22+
const { d2 } = useD2({
23+
onInitialized: d2 => {
24+
installedAppHub.setState(d2.system.installedApps)
25+
},
26+
})
27+
28+
if (!d2) {
29+
return null
30+
}
31+
32+
return (
33+
<MuiThemeProvider muiTheme={theme}>
34+
<CssVariables spacers colors />
35+
<HashRouter>
36+
<div className={styles.container}>
37+
<div className={styles.sidebar}>
38+
<Sidebar />
39+
</div>
40+
41+
<main className={styles.content}>
42+
<Switch>
43+
<Route exact path="/" component={CoreApps} />
44+
<Route path="/custom-apps" component={CustomApps} />
45+
<Route path="/discover" component={AppHub} />
46+
<Route
47+
path="/manual-install"
48+
component={ManualInstall}
49+
/>
50+
<Redirect from="*" to="/" />
51+
</Switch>
52+
</main>
53+
</div>
54+
</HashRouter>
55+
</MuiThemeProvider>
56+
)
57+
}
58+
59+
export default App

src/App.module.css

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
.container {
2+
display: grid;
3+
height: 100%;
4+
grid-template-areas: "sidebar main";
5+
grid-template-columns: 224px auto;
6+
color: var(--colors-grey900);
7+
}
8+
9+
.sidebar {
10+
grid-area: sidebar;
11+
padding-top: var(--spacers-dp4);
12+
height: 100%;
13+
background: var(--colors-grey200);
14+
border-right: 1px solid var(--colors-grey400);
15+
}
16+
17+
.content {
18+
position: relative;
19+
grid-area: main;
20+
padding: var(--spacers-dp16);
21+
background: var(--colors-grey050);
22+
}

src/AppWrapper.js

-31
This file was deleted.

src/components/CoreApps.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import React from 'react'
2+
3+
export default () => <p>Core apps</p>

src/components/App.component.js renamed to src/components/CustomApps.js

+9-87
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import i18n from '@dhis2/d2-i18n'
22
import { PropTypes } from '@dhis2/prop-types'
3-
import Sidebar from 'd2-ui/lib/sidebar/Sidebar.component'
43
import { Card, CardText } from 'material-ui/Card'
54
import CircularProgress from 'material-ui/CircularProgress'
65
import FontIcon from 'material-ui/FontIcon'
76
import LinearProgress from 'material-ui/LinearProgress'
8-
import Snackbar from 'material-ui/Snackbar'
97
import React from 'react'
108
import actions from '../actions'
119
import appHubStore from '../stores/appHub.store'
@@ -43,20 +41,16 @@ const styles = {
4341
fontWeight: 300,
4442
maxWidth: 734,
4543
},
46-
snackbar: {
47-
// left: '2rem',
48-
right: 'initial',
49-
},
5044
menuLabel: {
5145
position: 'relative',
5246
top: -6,
5347
marginLeft: 16,
5448
},
5549
}
5650

57-
class App extends React.Component {
58-
constructor(props, context) {
59-
super(props, context)
51+
class CustomApps extends React.Component {
52+
constructor(props) {
53+
super(props)
6054

6155
this.state = {
6256
installedApps: [],
@@ -66,24 +60,6 @@ class App extends React.Component {
6660
appHub: {},
6761
lastUpdate: null,
6862
}
69-
70-
// Bind 'this' for functions that need it
71-
;[
72-
'setSection',
73-
'progress',
74-
'closeSnackbar',
75-
'showSnackbar',
76-
'search',
77-
].forEach(fn => {
78-
this[fn] = this[fn].bind(this)
79-
})
80-
}
81-
82-
getChildContext() {
83-
return {
84-
d2: this.props.d2,
85-
muiTheme: AppTheme,
86-
}
8763
}
8864

8965
componentDidMount() {
@@ -113,16 +89,6 @@ class App extends React.Component {
11389
actions.refreshApps.subscribe(() => {
11490
this.setState({ uploading: false })
11591
}),
116-
actions.showSnackbarMessage.subscribe(params => {
117-
if (this.state.snackbar) {
118-
this.setState({ snackbar: undefined })
119-
setTimeout(() => {
120-
this.setState({ snackbar: params.data })
121-
}, 150)
122-
} else {
123-
this.setState({ snackbar: params.data })
124-
}
125-
}),
12692
actions.installAppVersion.subscribe(({ data }) => {
12793
const app = appHubStore.getAppFromVersionId(data[0])
12894
this.setSection(
@@ -136,18 +102,11 @@ class App extends React.Component {
136102

137103
componentWillUnmount() {
138104
this.subscriptions.forEach(subscription => {
139-
subscription.dispose()
105+
subscription.remove()
140106
})
141107
}
142108

143-
setSection(key) {
144-
if (this.sidebar) {
145-
this.sidebar.clearSearchBox()
146-
}
147-
this.setState({ section: key, appSearch: undefined })
148-
}
149-
150-
progress(p) {
109+
progress = p => {
151110
if (p) {
152111
if (p === 1) {
153112
this.setState({ uploading: false, progress: undefined })
@@ -159,15 +118,7 @@ class App extends React.Component {
159118
}
160119
}
161120

162-
closeSnackbar() {
163-
this.setState({ snackbar: undefined })
164-
}
165-
166-
showSnackbar(message) {
167-
this.setState({ snackbar: message })
168-
}
169-
170-
search(text) {
121+
search = text => {
171122
if (text.length > 0) {
172123
this.setState({
173124
appSearch: this.state.installedApps
@@ -219,7 +170,6 @@ class App extends React.Component {
219170
<CircularProgress
220171
mode="indeterminate"
221172
color="#6688AA"
222-
// size={0.75}
223173
value={this.state.progress}
224174
/>
225175
<br />
@@ -234,14 +184,13 @@ class App extends React.Component {
234184

235185
renderSection(key, apps, showUpload) {
236186
if (key === 'store') {
237-
return <AppHub appHub={this.state.appHub} d2={this.props.d2} />
187+
return <AppHub appHub={this.state.appHub} />
238188
}
239189

240190
const filter = (key && key.toString().toUpperCase()) || 'APP'
241191

242192
return (
243193
<AppList
244-
d2={this.props.d2}
245194
installedApps={apps}
246195
uploadProgress={this.progress}
247196
transitionUnmount={this.state.unmountSection}
@@ -317,29 +266,10 @@ class App extends React.Component {
317266
),
318267
}))
319268

320-
const reffer = r => {
321-
this.sidebar = r
322-
}
323-
324269
return (
325270
<div className="app">
326271
<SelfUpdateNoticeBox appHub={this.state.appHub} />
327-
<Sidebar
328-
sections={sections}
329-
currentSection={this.state.section}
330-
onChangeSection={this.setSection}
331-
showSearchField
332-
onChangeSearchText={this.search}
333-
ref={reffer}
334-
/>
335-
<Snackbar
336-
message={this.state.snackbar || ''}
337-
autoHideDuration={2500}
338-
onRequestClose={this.closeSnackbar}
339-
open={!!this.state.snackbar}
340-
style={styles.snackbar}
341-
/>
342-
<div className="content-area">
272+
<div>
343273
{this.state.appSearch
344274
? this.renderSearchResults()
345275
: this.renderSection(
@@ -354,13 +284,5 @@ class App extends React.Component {
354284
)
355285
}
356286
}
357-
App.propTypes = {
358-
d2: PropTypes.object.isRequired,
359-
}
360-
361-
App.childContextTypes = {
362-
d2: PropTypes.object,
363-
muiTheme: PropTypes.object,
364-
}
365287

366-
export default App
288+
export default CustomApps

0 commit comments

Comments
 (0)