-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: default variables, create attribute dialog [DAT-1403]
- Loading branch information
Thomas Zemp
committed
Jul 29, 2022
1 parent
9e16649
commit c8c1261
Showing
5 changed files
with
170 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import { useAlert, useDataEngine } from '@dhis2/app-runtime' | ||
import i18n from '@dhis2/d2-i18n' | ||
import { | ||
Button, | ||
ButtonStrip, | ||
Modal, | ||
ModalActions, | ||
ModalContent, | ||
} from '@dhis2/ui' | ||
import PropTypes from 'prop-types' | ||
import React from 'react' | ||
|
||
const DEFAULT_ATTIBUTE_NAME = 'default-sql-values' | ||
|
||
const attributeCreate = { | ||
resource: 'attributes', | ||
type: 'create', | ||
data: ({ data }) => data, | ||
} | ||
|
||
const dataStoreCreate = { | ||
resource: 'dataStore/sqlViewer/config', | ||
type: 'create', | ||
data: ({ data }) => data, | ||
} | ||
|
||
const SettingsModal = ({ setSettingsModalOpen }) => { | ||
const engine = useDataEngine() | ||
const { show: showAlert } = useAlert( | ||
({ msg }) => msg, | ||
({ options }) => ({ ...options, duration: 3000 }) | ||
) | ||
|
||
const createAttribute = async () => { | ||
try { | ||
// post to create attribute | ||
const defaultsAttribute = { | ||
name: DEFAULT_ATTIBUTE_NAME, | ||
valueType: 'LONG_TEXT', | ||
sqlViewAttribute: true, | ||
} | ||
const attributeResponse = await engine.mutate(attributeCreate, { | ||
variables: { data: defaultsAttribute }, | ||
}) | ||
|
||
// if successful, post to create datastore (note: assumes datastore does not exist) | ||
const datastoreContent = { | ||
defaultsAttributeId: attributeResponse?.response?.uid, | ||
} | ||
await engine.mutate(dataStoreCreate, { | ||
variables: { data: datastoreContent }, | ||
}) | ||
|
||
showAlert({ | ||
msg: i18n.t('Attribute has been created. App will reload shortly.'), | ||
options: { success: true }, | ||
}) | ||
|
||
// here the context should be created, but reloading page is a simpler approach | ||
setTimeout(() => { | ||
location.reload() | ||
}, 3000) | ||
} catch (e) { | ||
showAlert({ | ||
msg: i18n.t('Attribute could not be created.'), | ||
options: { warning: true }, | ||
}) | ||
} | ||
setSettingsModalOpen(false) | ||
} | ||
|
||
return ( | ||
<Modal | ||
onClose={() => { | ||
setSettingsModalOpen(false) | ||
}} | ||
position="middle" | ||
> | ||
<ModalContent> | ||
{i18n.t( | ||
'Do you want to create an attribute that allows saving default variable values?' | ||
)} | ||
</ModalContent> | ||
<ModalActions> | ||
<ButtonStrip> | ||
<Button | ||
onClick={() => { | ||
setSettingsModalOpen(false) | ||
}} | ||
> | ||
{i18n.t('Cancel')} | ||
</Button> | ||
<Button primary onClick={createAttribute}> | ||
{i18n.t('Create')} | ||
</Button> | ||
</ButtonStrip> | ||
</ModalActions> | ||
</Modal> | ||
) | ||
} | ||
|
||
SettingsModal.propTypes = { | ||
setSettingsModalOpen: PropTypes.func, | ||
} | ||
|
||
export default SettingsModal |