Skip to content

Commit

Permalink
fix: show alert if custom logo upload fails (#825)
Browse files Browse the repository at this point in the history
  • Loading branch information
mediremi authored Apr 13, 2021
1 parent 64b3fad commit 4e90713
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 18 deletions.
16 changes: 11 additions & 5 deletions i18n/en.pot
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ msgstr ""
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1)\n"
"POT-Creation-Date: 2021-03-16T11:10:22.254Z\n"
"PO-Revision-Date: 2021-03-16T11:10:22.254Z\n"
"POT-Creation-Date: 2021-03-25T22:14:19.666Z\n"
"PO-Revision-Date: 2021-03-25T22:14:19.666Z\n"

msgid "Failed to load: {{error}}"
msgstr ""
Expand All @@ -29,6 +29,12 @@ msgstr ""
msgid "Upload image"
msgstr ""

msgid "File uploaded successfully"
msgstr ""

msgid "Error uploading file: {{error}}"
msgstr ""

msgid "Cancel upload"
msgstr ""

Expand Down Expand Up @@ -608,13 +614,13 @@ msgstr ""
msgid "Custom top menu logo"
msgstr ""

msgid "Allow users to switch dashboard favorite view type"
msgid "Allow users to switch dashboard items view type"
msgstr ""

msgid "Allow users to open dashboard favorite in relevant app"
msgid "Allow users to open dashboard items in relevant app"
msgstr ""

msgid "Allow users to show dashboard favorite interpretations and details"
msgid "Allow users to show dashboard items interpretations and details"
msgstr ""

msgid "Allow users to view dashboard items in fullscreen"
Expand Down
32 changes: 24 additions & 8 deletions src/form-fields/file-upload.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useConfig } from '@dhis2/app-runtime'
import { useConfig, useAlert } from '@dhis2/app-runtime'
import i18n from '@dhis2/d2-i18n'
import {
Button,
Expand Down Expand Up @@ -78,6 +78,7 @@ Upload.propTypes = {

class FileUpload extends React.Component {
static propTypes = {
alert: PropTypes.object.isRequired,
isEnabled: PropTypes.bool.isRequired,
label: PropTypes.string.isRequired,
name: PropTypes.oneOf(['logo_front', 'logo_banner']).isRequired,
Expand Down Expand Up @@ -112,7 +113,8 @@ class FileUpload extends React.Component {
}

onUpload = async e => {
if (e.target.files.length === 0) {
const { files } = e.target
if (files.length === 0) {
return
}

Expand All @@ -136,7 +138,7 @@ class FileUpload extends React.Component {
this.xhr = xhr

const data = new FormData()
data.append('file', e.target.files[0])
data.append('file', files[0])

try {
await api.post(['staticContent', this.props.name].join('/'), data)
Expand All @@ -146,12 +148,18 @@ class FileUpload extends React.Component {
progress: undefined,
isEnabled: true,
})
} catch {
} catch (error) {
this.props.alert.show({
message: i18n.t('Error uploading file: {{error}}', {
error: error.httpStatus || error.message,
nsSeparator: null,
}),
critical: true,
})
this.props.onChange({ target: { value: false } })
this.setState({
uploading: false,
progress: undefined,
isEnabled: false,
})
}
}
Expand Down Expand Up @@ -188,15 +196,13 @@ class FileUpload extends React.Component {
display: 'block',
whiteSpace: 'nowrap',
}

const checkStyle = {
display: 'inline-block',
whiteSpace: 'nowrap',
paddingRight: 8,
paddingTop: 8,
paddingBottom: 8,
}

const btnStyle = {
display: 'inline-block',
position: 'absolute',
Expand Down Expand Up @@ -244,4 +250,14 @@ class FileUpload extends React.Component {
}
}

export default FileUpload
const withAlerts = Component => {
return function ComponentWithAlerts(props) {
const alert = useAlert(
({ message }) => message,
options => options
)
return <Component {...props} alert={alert} />
}
}

export default withAlerts(FileUpload)
17 changes: 12 additions & 5 deletions src/settingsFields.component.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import i18n from '@dhis2/d2-i18n'
import { Button, Card } from '@dhis2/ui'
import { Button, Card, CenteredContent, CircularLoader } from '@dhis2/ui'
import FormBuilder from 'd2-ui/lib/forms/FormBuilder.component'
import { wordToValidatorMap } from 'd2-ui/lib/forms/Validators'
import IconButton from 'material-ui/IconButton'
Expand Down Expand Up @@ -204,9 +204,6 @@ class SettingsFields extends React.Component {
})

case 'staticContent':
if (!settingsStore.state) {
return null
}
return Object.assign({}, fieldBase, {
component: FileUpload,
props: {
Expand Down Expand Up @@ -264,7 +261,7 @@ class SettingsFields extends React.Component {
console.warn(
`Unknown control type "${mapping.type}" encountered for field "${key}"`
)
return {}
return null
}
}

Expand All @@ -278,6 +275,16 @@ class SettingsFields extends React.Component {
)
}

if (!settingsStore.state) {
return (
<Card className={classes.card} key={this.props.category}>
<CenteredContent>
<CircularLoader />
</CenteredContent>
</Card>
)
}

const fields = settings
.map(key => {
const mapping = settingsKeyMapping[key]
Expand Down

0 comments on commit 4e90713

Please sign in to comment.