Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
nl0 committed Dec 12, 2024
1 parent af0f98c commit cdc31dd
Showing 1 changed file with 7 additions and 16 deletions.
23 changes: 7 additions & 16 deletions catalog/app/containers/Admin/Settings/TabulatorSettings.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as Eff from 'effect'
import * as React from 'react'
import * as M from '@material-ui/core'
import * as Lab from '@material-ui/lab'
Expand All @@ -17,43 +16,35 @@ interface ToggleProps {
checked: boolean
}

const NONE = Eff.Option.none<{ value: boolean }>()

function Toggle({ checked }: ToggleProps) {
const { push: notify } = Notifications.use()
const mutate = GQL.useMutation(SET_UNRESTRICTED_MUTATION)
const [mutationState, setMutationState] = React.useState(NONE)
const [mutation, setMutation] = React.useState<{ value: boolean } | null>(null)

Check warning on line 22 in catalog/app/containers/Admin/Settings/TabulatorSettings.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/Settings/TabulatorSettings.tsx#L19-L22

Added lines #L19 - L22 were not covered by tests

const handleChange = React.useCallback(
async (_event, value: boolean) => {

Check warning on line 25 in catalog/app/containers/Admin/Settings/TabulatorSettings.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/Settings/TabulatorSettings.tsx#L24-L25

Added lines #L24 - L25 were not covered by tests
if (Eff.Option.isSome(mutationState)) return
setMutationState(Eff.Option.some({ value }))
if (mutation) return
setMutation({ value })

Check warning on line 27 in catalog/app/containers/Admin/Settings/TabulatorSettings.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/Settings/TabulatorSettings.tsx#L27

Added line #L27 was not covered by tests
try {
await mutate({ value })

Check warning on line 29 in catalog/app/containers/Admin/Settings/TabulatorSettings.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/Settings/TabulatorSettings.tsx#L29

Added line #L29 was not covered by tests
} catch (e) {
Sentry.captureException(e)
notify(`Failed to update tabulator settings: ${e}`)

Check warning on line 32 in catalog/app/containers/Admin/Settings/TabulatorSettings.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/Settings/TabulatorSettings.tsx#L31-L32

Added lines #L31 - L32 were not covered by tests
} finally {
setMutationState(NONE)
setMutation(null)

Check warning on line 34 in catalog/app/containers/Admin/Settings/TabulatorSettings.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/Settings/TabulatorSettings.tsx#L34

Added line #L34 was not covered by tests
}
},
[mutate, notify, mutationState, setMutationState],
)

const value = Eff.pipe(
mutationState,
Eff.Option.map((x) => x.value),
Eff.Option.getOrElse(() => checked),
[mutate, notify, mutation],
)

return (

Check warning on line 40 in catalog/app/containers/Admin/Settings/TabulatorSettings.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/Settings/TabulatorSettings.tsx#L40

Added line #L40 was not covered by tests
<>
<M.FormControlLabel
control={
<M.Switch
checked={value}
checked={mutation?.value ?? checked}
onChange={handleChange}
disabled={Eff.Option.isSome(mutationState)}
disabled={!!mutation}
/>
}
label="Enable unrestricted access"
Expand Down

0 comments on commit cdc31dd

Please sign in to comment.