From 88630163a0286a3d4c09c106133eb1819bfdda2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milagros=20Rodr=C3=ADguez?= Date: Fri, 18 Aug 2023 11:15:43 -0500 Subject: [PATCH] feat: keep datastore ASWA version updated (#175) --- src/auth/AuthWall.js | 19 +++++++++++++- src/auth/useAndroidSettingsVersion.js | 37 +++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 src/auth/useAndroidSettingsVersion.js diff --git a/src/auth/AuthWall.js b/src/auth/AuthWall.js index e191b1ed..2c51949d 100644 --- a/src/auth/AuthWall.js +++ b/src/auth/AuthWall.js @@ -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 }) => @@ -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() diff --git a/src/auth/useAndroidSettingsVersion.js b/src/auth/useAndroidSettingsVersion.js new file mode 100644 index 00000000..cf9617d6 --- /dev/null +++ b/src/auth/useAndroidSettingsVersion.js @@ -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 + ), + } +}