Skip to content

Commit

Permalink
feat: fixed authorizing access dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
mbret committed Feb 23, 2024
1 parent 04d665d commit 91a2264
Show file tree
Hide file tree
Showing 38 changed files with 332 additions and 354 deletions.
15 changes: 6 additions & 9 deletions packages/web/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import { FC, Suspense, useEffect, useState } from "react"
import { AppNavigator } from "./navigation/AppNavigator"
import {
ThemeProvider,
Theme,
StyledEngineProvider,
Fade,
Box
} from "@mui/material"
import { theme } from "./theme"
import { Theme, StyledEngineProvider, Fade, Box } from "@mui/material"
import { theme } from "./theme/theme"
import { BlockingBackdrop } from "./common/BlockingBackdrop"
import { TourProvider } from "./app-tour/TourProvider"
import { ManageBookCollectionsDialog } from "./books/ManageBookCollectionsDialog"
Expand Down Expand Up @@ -36,6 +30,8 @@ import {
import localforage from "localforage"
import { signalEntriesToPersist } from "./storage"
import { queryClient } from "./queries/client"
import { ThemeProvider } from "./theme/ThemeProvider"
import { AuthorizeActionDialog } from "./auth/AuthorizeActionDialog"

declare module "@mui/styles/defaultTheme" {
// eslint-disable-next-line @typescript-eslint/no-empty-interface
Expand Down Expand Up @@ -70,7 +66,7 @@ export function App() {
}}
>
<StyledEngineProvider injectFirst>
<ThemeProvider theme={theme}>
<ThemeProvider>
<QueryClientProvider client={queryClient}>
<Suspense fallback={<SplashScreen show />}>
{/* <SplashScreen show={!isAppReady} /> */}
Expand All @@ -93,6 +89,7 @@ export function App() {
<ManageBookCollectionsDialog />
<ManageBookTagsDialog />
<ManageTagBooksDialog />
<AuthorizeActionDialog />
</TourProvider>
<UpdateAvailableDialog
serviceWorker={newServiceWorker}
Expand Down
4 changes: 2 additions & 2 deletions packages/web/src/PreloadQueries.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { memo, useEffect, useState } from "react"
import { useAccountSettings } from "./settings/helpers"
import { useSettings } from "./settings/helpers"
import { useLiveRef } from "reactjrx"

const usePrefetchAccountSettings = () => {
const [prefetched, setPrefetched] = useState(false)

const { data, status } = useAccountSettings({
const { data, status } = useSettings({
enabled: !prefetched
})

Expand Down
113 changes: 113 additions & 0 deletions packages/web/src/auth/AuthorizeActionDialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import {
Button,
Dialog,
DialogActions,
DialogContent,
DialogContentText,
DialogTitle,
TextField
} from "@mui/material"
import { FC, useEffect } from "react"
import { useValidateAppPassword } from "../settings/helpers"
import { Controller, useForm } from "react-hook-form"
import { errorToHelperText } from "../common/forms/errorToHelperText"
import { signal, useSignalValue } from "reactjrx"

const FORM_ID = "LockActionBehindUserPasswordDialog"

type Inputs = {
password: string
}

const actionSignal = signal<(() => void) | undefined>({})

export const authorizeAction = (action: () => void) =>
actionSignal.setValue(() => action)

export const AuthorizeActionDialog: FC<{}> = () => {
const action = useSignalValue(actionSignal)
const open = !!action
const { control, handleSubmit, setFocus, setError, reset } = useForm<Inputs>({
defaultValues: {
password: ""
}
})
const {
mutate: validatePassword,
reset: resetValidatePasswordMutation,
error
} = useValidateAppPassword({
onSuccess: () => {
onClose()
action && action()
},
onError: () => {
setError("password", {
message: "Invalid"
})
}
})

const onClose = () => {
actionSignal.setValue(undefined)
}

useEffect(() => {
reset()
resetValidatePasswordMutation()

if (open) {
setTimeout(() => {
setFocus("password")
})
}
}, [open, resetValidatePasswordMutation, reset, setFocus])

return (
<Dialog onClose={onClose} open={open}>
<DialogTitle>Authorization required</DialogTitle>
<DialogContent>
<DialogContentText>
This action requires explicit authorization. Please enter your app
password to continue.
</DialogContentText>
<form
noValidate
id={FORM_ID}
onSubmit={handleSubmit((data) => {
validatePassword(data.password)
})}
>
<Controller
name="password"
control={control}
rules={{ required: true }}
render={({ field: { ref, ...rest }, fieldState }) => {
return (
<TextField
{...rest}
label="Password"
type="password"
fullWidth
margin="normal"
inputRef={ref}
autoComplete="current-password"
error={fieldState.invalid}
helperText={errorToHelperText(fieldState.error)}
/>
)
}}
/>
</form>
</DialogContent>
<DialogActions>
<Button onClick={onClose} color="primary">
Cancel
</Button>
<Button color="primary" type="submit" form={FORM_ID}>
Authorize
</Button>
</DialogActions>
</Dialog>
)
}
101 changes: 0 additions & 101 deletions packages/web/src/auth/LockActionBehindUserPasswordDialog.tsx

This file was deleted.

71 changes: 0 additions & 71 deletions packages/web/src/auth/LockActionDialog.tsx

This file was deleted.

4 changes: 2 additions & 2 deletions packages/web/src/auth/UnlockLibraryDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { PreventAutocompleteFields } from "../common/forms/PreventAutocompleteFi
import { useModalNavigationControl } from "../navigation/useModalNavigationControl"
import { libraryStateSignal } from "../library/states"
import { signal, useSignalValue } from "reactjrx"
import { useAccountSettings } from "../settings/helpers"
import { useSettings } from "../settings/helpers"

export const unlockLibraryDialogSignal = signal({
key: "unlockLibraryDialog",
Expand All @@ -34,7 +34,7 @@ export const UnlockLibraryDialog: FC<{}> = () => {
unlockPassword: ""
}
})
const { data: accountSettings } = useAccountSettings()
const { data: accountSettings } = useSettings()
const isOpened = useSignalValue(unlockLibraryDialogSignal)
const contentPassword = accountSettings?.contentPassword
const { closeModalWithNavigation } = useModalNavigationControl(
Expand Down
Loading

0 comments on commit 91a2264

Please sign in to comment.