Skip to content

Commit

Permalink
feat: keep datastore ASWA version updated (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sharmyn28 authored Aug 18, 2023
1 parent d544558 commit 8863016
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/auth/AuthWall.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import { useAlert, useConfig, useDataMutation } from '@dhis2/app-runtime'
import i18n from '@dhis2/d2-i18n'
import isNil from 'lodash/isNil'
import PropTypes from 'prop-types'
import React, { useState } from 'react'
import React, { useEffect, useState } from 'react'
import DialogFirstLaunch from '../components/dialog/DialogFirstLaunch'
import { deletePrevDataStoreMutation, useCreateFirstSetup } from '../modules'
import {
updateInfoMutation,
useAndroidSettingsVersion,
} from './useAndroidSettingsVersion'
import { useIsAuthorized } from './useIsAuthorized'

const AuthWall = ({ children }) => {
const { hasAuthority, hasNamespace, hasOutDateNamespace } =
useIsAuthorized()
const { isInfoUpdated } = useAndroidSettingsVersion()
const { apiVersion } = useConfig()
const [hasDatastoreAccess, setDatastoreAccess] = useState(hasNamespace)
const [mutate] = useDataMutation(deletePrevDataStoreMutation)
const [mutateUpdateInfo] = useDataMutation(updateInfoMutation)
const { createSetup } = useCreateFirstSetup()
const { show } = useAlert(
({ success }) =>
Expand All @@ -25,6 +32,16 @@ const AuthWall = ({ children }) => {
({ success }) => (success ? { success: true } : { critical: true })
)

useEffect(() => {
if (!isNil(isInfoUpdated) && !isInfoUpdated) {
updateInfoVersion().catch((e) => console.error(e))
}
}, [isInfoUpdated])

const updateInfoVersion = async () => {
await mutateUpdateInfo()
}

const handleSave = async () => {
if (hasOutDateNamespace) {
await mutate()
Expand Down
37 changes: 37 additions & 0 deletions src/auth/useAndroidSettingsVersion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { useDataQuery } from '@dhis2/app-runtime'
import { INFO, NAMESPACE } from '../constants/data-store'
import { infoDefault } from '../constants/info'

export const getKeyInfo = {
info: {
resource: `dataStore/${NAMESPACE}/${INFO}`,
},
}

export const updateInfoMutation = {
resource: `dataStore/${NAMESPACE}/${INFO}`,
type: 'update',
data: { ...infoDefault },
}

const isUpdatedVersion = (currentVersion, updatedVersion) =>
currentVersion === updatedVersion

export const useAndroidSettingsVersion = () => {
const { data } = useDataQuery(getKeyInfo)

return {
isInfoUpdated:
data &&
isUpdatedVersion(
data.info?.androidSettingsVersion,
infoDefault.androidSettingsVersion
),
isDatastoreUpdated:
data &&
isUpdatedVersion(
data.info?.dataStoreVersion,
infoDefault.dataStoreVersion
),
}
}

0 comments on commit 8863016

Please sign in to comment.