Skip to content

Commit 726d1ee

Browse files
author
Matthias Rütten
committed
implement "remove config" and fix bug when saving a new config
1 parent 561152e commit 726d1ee

File tree

4 files changed

+42
-17
lines changed

4 files changed

+42
-17
lines changed

src/renderer/components/pages/SettingsPage.tsx

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { FormHelperText } from '@material-ui/core'
1111

1212
import { useBackend } from '../../hooks/backend'
1313
import { useConfig } from '../../hooks/config'
14+
import sleep from '../../util/sleep'
1415

1516
const useStyles = makeStyles(theme => ({
1617
button: {
@@ -32,12 +33,14 @@ interface FormData {
3233
token: string
3334
}
3435

36+
// tslint:disable-next-line:cyclomatic-complexity
3537
export const SettingsPage = () => {
3638
const classes = useStyles()
3739
const { history } = useReactRouter()
38-
const { reset, testConfig } = useBackend()
39-
const { config, updateConfig } = useConfig()
40+
const { testConfig } = useBackend()
41+
const { config, updateConfig, removeConfig } = useConfig()
4042

43+
const [confirmDelete, setConfirmDelete] = React.useState(false)
4144
const [submitting, setSubmitting] = React.useState(false)
4245
const [errors, setErrors] = React.useState<FormErrorData>({
4346
url: '',
@@ -59,6 +62,16 @@ export const SettingsPage = () => {
5962
setValues({ ...values, [name]: event.target.value })
6063
}
6164

65+
const confirmRemove = async () => {
66+
setConfirmDelete(true)
67+
}
68+
69+
const remove = async () => {
70+
removeConfig()
71+
setErrors({ url: '', token: '', group: '', invalidSettings: false })
72+
setValues({ url: '', token: '', group: '' })
73+
}
74+
6275
const save = async () => {
6376
setSubmitting(true)
6477
setErrors({ url: '', token: '', group: '', invalidSettings: false })
@@ -82,10 +95,9 @@ export const SettingsPage = () => {
8295

8396
try {
8497
await testConfig(newConfig)
85-
8698
updateConfig(newConfig)
8799

88-
reset()
100+
await sleep(1000)
89101
history.push('/')
90102
} catch (_) {
91103
setError('invalidSettings', true)
@@ -95,6 +107,18 @@ export const SettingsPage = () => {
95107
}
96108
}
97109

110+
const renderRemoveButton = () => {
111+
return confirmDelete ? (
112+
<Button variant='contained' color='secondary' fullWidth className={classes.button} onClick={remove}>
113+
Are you sure?
114+
</Button>
115+
) : (
116+
<Button fullWidth className={classes.button} onClick={confirmRemove}>
117+
remove config
118+
</Button>
119+
)
120+
}
121+
98122
return (
99123
<Container>
100124
<Typography variant='h6' className={classes.headline}>
@@ -143,6 +167,7 @@ export const SettingsPage = () => {
143167
<Button variant='contained' color='primary' aria-label='add' fullWidth onClick={save} className={classes.button} disabled={submitting}>
144168
Save
145169
</Button>
170+
{config && !submitting && renderRemoveButton()}
146171
{config && !submitting && (
147172
<Link to='/' className={classes.goBackLink}>
148173
<Button fullWidth className={classes.button}>

src/renderer/hooks/backend.tsx

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ export interface MergeRequest {
4646
export interface BackendContext {
4747
mergeRequests: MergeRequest[] | undefined
4848
testConfig: (config: Config) => Promise<void>
49-
reset: () => void
5049
}
5150

5251
const Context = React.createContext<BackendContext | null>(null)
@@ -94,26 +93,17 @@ export const BackendProvider = ({ ...props }) => {
9493
}
9594

9695
React.useEffect(() => {
97-
console.log('useEffect')
9896
updateData()
9997
const interval = setInterval(updateData, 30000)
10098

10199
return () => {
102100
clearInterval(interval)
103101
}
104-
}, [])
102+
}, [config])
105103

106104
const testConfig = async (newConfig: Config) => {
107-
console.log('testConfig', newConfig)
108-
109105
return doRequest(newConfig)
110106
}
111107

112-
const reset = () => {
113-
console.log('reset backend')
114-
setLoadErrors(0)
115-
setMergeRequests(undefined)
116-
}
117-
118-
return <Context.Provider value={{ mergeRequests, reset, testConfig }} {...props} />
108+
return <Context.Provider value={{ mergeRequests, testConfig }} {...props} />
119109
}

src/renderer/hooks/config.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface Config {
88

99
interface ConfigContext {
1010
config: Config | null
11+
removeConfig: () => void
1112
updateConfig: (newConfig: Config) => void
1213
}
1314

@@ -25,10 +26,14 @@ export const ConfigProvider = ({ ...props }) => {
2526
const localStorageValue = window.localStorage.getItem('config')
2627
const [config, setConfig] = React.useState<Config | null>(localStorageValue ? JSON.parse(localStorageValue) : null)
2728

29+
const removeConfig = () => {
30+
setConfig(null)
31+
window.localStorage.removeItem('config')
32+
}
2833
const updateConfig = (newConfig: Config) => {
2934
setConfig(newConfig)
3035
window.localStorage.setItem('config', JSON.stringify(newConfig))
3136
}
3237

33-
return <Context.Provider value={{ config, updateConfig }} {...props} />
38+
return <Context.Provider value={{ config, updateConfig, removeConfig }} {...props} />
3439
}

src/renderer/util/sleep.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default function sleep(ms: number) {
2+
return new Promise(resolve => {
3+
setTimeout(resolve, ms)
4+
})
5+
}

0 commit comments

Comments
 (0)