Skip to content

Commit

Permalink
feat(neuron-ui): add clear cache button on the general settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Keith-CY committed Nov 17, 2019
1 parent 429be9c commit 7419d37
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 8 deletions.
34 changes: 27 additions & 7 deletions packages/neuron-ui/src/components/GeneralSetting/index.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,48 @@
import React, { useCallback, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { Stack, PrimaryButton, Spinner } from 'office-ui-fabric-react'
import { StateWithDispatch, AppActions } from 'states/stateProvider/reducer'
import { clearCellCache } from 'services/remote'

const GeneralSetting = () => {
const GeneralSetting = ({ dispatch }: React.PropsWithoutRef<StateWithDispatch>) => {
const [t] = useTranslation()
const [clearing, setClearing] = useState(false)

const clearCache = useCallback(() => {
// TODO: real clear action
setClearing(true)
setTimeout(() => {
setClearing(false)
}, 2000)
}, [])
clearCellCache()
.catch(err => {
dispatch({
type: AppActions.AddNotification,
payload: {
type: 'alert',
timestamp: +new Date(),
content: err.message,
},
})
})
.finally(() => {
setClearing(false)
})
}, 1000)
}, [dispatch])

return (
<Stack tokens={{ childrenGap: 15 }} horizontal horizontalAlign="start">
<PrimaryButton
text={t('settings.general.clear-cache')}
onClick={clearCache}
disabled={clearing}
ariaDescription="Create new network configuration"
/>
{clearing ? <Spinner /> : null}
styles={{
root: {
minWidth: 150,
},
}}
>
{clearing ? <Spinner /> : t('settings.general.clear-cache')}
</PrimaryButton>
</Stack>
)
}
Expand Down
3 changes: 3 additions & 0 deletions packages/neuron-ui/src/services/remote/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ export const getNeuronWalletState = apiMethodWrapper<void>(api => () => api.load
export const handleViewError = apiMethodWrapper<string>(api => errorMessage => api.handleViewError(errorMessage))
export const contextMenu = apiMethodWrapper<{ type: string; id: string }>(api => params => api.contextMenu(params))

export const clearCellCache = apiMethodWrapper<void>(api => () => api.clearCellCache())

export default {
getNeuronWalletState,
handleViewError,
contextMenu,
clearCellCache,
}
16 changes: 15 additions & 1 deletion packages/neuron-ui/src/stories/GeneralSetting.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,23 @@ import React from 'react'
import { storiesOf } from '@storybook/react'
import { withKnobs } from '@storybook/addon-knobs'
import GeneralSetting from 'components/GeneralSetting'
import initStates from 'states/initStates'

const states: { [title: string]: boolean } = {
'Clear cell cache on': true,
'Clear cell cache off': false,
}

const stories = storiesOf('GeneralSettings', module)

Object.entries(states).forEach(([title]) => {
const props = { ...initStates, settings: { ...initStates.settings }, dispatch: () => {} }
stories.add(title, () => <GeneralSetting {...props} />)
})

stories.addDecorator(withKnobs).add('With knobs', () => {
return <GeneralSetting />
const props = {
...initStates,
}
return <GeneralSetting {...props} dispatch={() => {}} />
})
6 changes: 6 additions & 0 deletions packages/neuron-wallet/src/controllers/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,4 +297,10 @@ export default class ApiController {
) {
return DaoController.getDaoCells(params)
}

// settings
@MapApiResponse
public static async clearCellCache () {
return Promise.resolve()
}
}

0 comments on commit 7419d37

Please sign in to comment.