Skip to content

Commit

Permalink
refactor(headerbar use instance info): simplify hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammer5 committed Oct 7, 2021
1 parent 64810d8 commit 020309f
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 52 deletions.
35 changes: 20 additions & 15 deletions components/header-bar/src/header-bar.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ const mockConfig = {
const customData = {
'system/info': {
contextPath: 'https://debug.dhis2.org/dev',
userAgent: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36',
userAgent:
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36',
calendar: 'iso8601',
dateFormat: 'yyyy-mm-dd',
serverDate: '2021-10-06T08:06:15.256',
Expand All @@ -42,7 +43,7 @@ const customData = {
jasperReportsVersion: '6.3.1',
environmentVariable: 'DHIS2_HOME',
databaseInfo: {
spatialSupport: true
spatialSupport: true,
},
encryption: false,
emailConfigured: false,
Expand All @@ -52,7 +53,7 @@ const customData = {
instanceBaseUrl: 'https://debug.dhis2.org/dev',
clusterHostname: '',
isMetadataVersionEnabled: true,
metadataSyncEnabled: false
metadataSyncEnabled: false,
},
'systemSettings/applicationTitle': {
applicationTitle: 'Foobar',
Expand Down Expand Up @@ -260,10 +261,12 @@ export const WithOnlineStatus = () => (
</CustomDataProvider>
)

WithOnlineStatus.decorators = [fn => {
const config = { ...mockConfig, pwaEnabled: true }
return <Provider config={config}>{fn()}</Provider>
}]
WithOnlineStatus.decorators = [
fn => {
const config = { ...mockConfig, pwaEnabled: true }
return <Provider config={config}>{fn()}</Provider>
},
]

WithOnlineStatus.parameters = {
docs: {
Expand All @@ -282,14 +285,16 @@ export const WithLastOnlineInfo = () => (
</CustomDataProvider>
)

WithLastOnlineInfo.decorators = [fn => {
const config = {
...mockConfig,
pwaEnabled: true,
headerbar: { onlineStatusInfo: 'LAST_ONLINE' },
}
return <Provider config={config}>{fn()}</Provider>
}]
WithLastOnlineInfo.decorators = [
fn => {
const config = {
...mockConfig,
pwaEnabled: true,
headerbar: { onlineStatusInfo: 'LAST_ONLINE' },
}
return <Provider config={config}>{fn()}</Provider>
},
]

WithLastOnlineInfo.parameters = {
docs: {
Expand Down
19 changes: 8 additions & 11 deletions components/header-bar/src/profile-menu/instance-and-app-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ const InstanceInfo = ({ instanceData }) => {

{', '}

<span className="build">
{`${i18n.t('Build')} ${revision}`}
</span>
<span className="build">{`${i18n.t('Build')} ${revision}`}</span>

<style jsx>{`
.version, .build {
.version,
.build {
white-space: nowrap;
}
`}</style>
Expand Down Expand Up @@ -67,16 +66,14 @@ export const InstanceAndAppInfo = ({ appName, appVersion }) => {
// other instance info when an error occurs
return (
<MenuItemWithBorderTop>
{loading && (
<div>{i18n.t('Loading instance information..')}</div>
)}
{loading && <div>{i18n.t('Loading instance information..')}</div>}

{!loading && !error && <InstanceInfo instanceData={data} />}
{!loading && !error && (
<InstanceInfo instanceData={data.systemInfo} />
)}

{appName && appVersion && (
<div className="app-info">
{`${appName}, ${appVersion}`}
</div>
<div className="app-info">{`${appName}, ${appVersion}`}</div>
)}
</MenuItemWithBorderTop>
)
Expand Down
23 changes: 20 additions & 3 deletions components/header-bar/src/profile-menu/profile-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@ const LoadingMask = () => (
</Layer>
)

const ProfileContents = ({ appName, appVersion, name, email, avatarUrl, helpUrl }) => {
const ProfileContents = ({
appName,
appVersion,
name,
email,
avatarUrl,
helpUrl,
}) => {
const { baseUrl } = useConfig()
const [loading, setLoading] = useState(false)

Expand Down Expand Up @@ -104,7 +111,10 @@ const ProfileContents = ({ appName, appVersion, name, email, avatarUrl, helpUrl
value="logout"
icon={<IconLogOut24 color={colors.grey700} />}
/>
<InstanceAndAppInfo appName={appName} appVersion={appVersion} />
<InstanceAndAppInfo
appName={appName}
appVersion={appVersion}
/>
</ul>
</div>

Expand Down Expand Up @@ -143,7 +153,14 @@ ProfileContents.propTypes = {
name: PropTypes.string,
}

export const ProfileMenu = ({ appName, appVersion, avatarUrl, name, email, helpUrl }) => (
export const ProfileMenu = ({
appName,
appVersion,
avatarUrl,
name,
email,
helpUrl,
}) => (
<div data-test="headerbar-profile-menu">
<ProfileContents
appName={appName}
Expand Down
24 changes: 5 additions & 19 deletions components/header-bar/src/profile-menu/use-instance-info.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,12 @@
import { useConfig, useDataQuery } from '@dhis2/app-runtime'
import { useMemo } from 'react'
import { useDataQuery } from '@dhis2/app-runtime'

const QUERY_INSTANCE_INFO = {
systemInfo: {
resource: 'system/info',
params: {
fields: ['version', 'revision']
}
}
fields: ['version', 'revision'],
},
},
}

export const useInstanceInfo = () => {
const config = useConfig()
const result = useDataQuery(QUERY_INSTANCE_INFO)
const data = useMemo(() => {
if (!result.data) {
return result.data
}

const { systemInfo } = result.data
return { ...systemInfo, ...config }
}, [result.data])

return { ...result, data }
}
export const useInstanceInfo = () => useDataQuery(QUERY_INSTANCE_INFO)
5 changes: 1 addition & 4 deletions components/header-bar/src/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ const Profile = ({ name, email, avatarUrl, helpUrl, appName, appVersion }) => {
data-test="headerbar-profile"
className="headerbar-profile"
>
<button
className="headerbar-profile-btn"
onClick={toggle}
>
<button className="headerbar-profile-btn" onClick={toggle}>
<UserIcon
avatarUrl={avatarUrl}
name={name}
Expand Down

0 comments on commit 020309f

Please sign in to comment.