-
Notifications
You must be signed in to change notification settings - Fork 390
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[desk-tool] Fix lint errors and increase readability in actions
- Loading branch information
1 parent
aec5299
commit acc28f4
Showing
11 changed files
with
192 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 37 additions & 23 deletions
60
packages/@sanity/desk-tool/src/actions/HistoryRestoreAction.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,53 @@ | ||
import React from 'react' | ||
import {useDocumentOperation} from '@sanity/react-hooks' | ||
import {useRouter} from 'part:@sanity/base/router' | ||
import HistoryIcon from 'part:@sanity/base/history-icon' | ||
import React, {useCallback, useMemo} from 'react' | ||
|
||
export function HistoryRestoreAction({id, type, revision, onComplete}) { | ||
const {restore}: any = useDocumentOperation(id, type) | ||
const router = useRouter() | ||
const [isConfirmDialogOpen, setConfirmDialogOpen] = React.useState(false) | ||
const [error, setError] = React.useState<Error | null>(null) | ||
|
||
const handleConfirm = useCallback(() => { | ||
restore.execute(revision) | ||
router.navigateIntent('edit', {id, type}) | ||
onComplete() | ||
}, [revision, restore, router, onComplete, id, type]) | ||
|
||
const handle = useCallback(() => { | ||
setConfirmDialogOpen(true) | ||
}, []) | ||
|
||
const dialog = useMemo(() => { | ||
if (!error && isConfirmDialogOpen) { | ||
return { | ||
type: 'confirm', | ||
color: 'danger', | ||
onCancel: onComplete, | ||
onConfirm: handleConfirm, | ||
message: <>Are you sure you want to restore this document?</> | ||
} | ||
} | ||
|
||
if (!error) { | ||
return null | ||
} | ||
|
||
return { | ||
type: 'error', | ||
onClose: () => setError(null), | ||
title: 'An error occured', | ||
content: error.message | ||
} | ||
}, [error, handleConfirm, isConfirmDialogOpen, onComplete]) | ||
|
||
return { | ||
label: 'Restore', | ||
color: 'primary', | ||
onHandle: () => { | ||
setConfirmDialogOpen(true) | ||
}, | ||
onHandle: handle, | ||
title: 'Restore to this version', | ||
icon: HistoryIcon, | ||
dialog: | ||
(!error && | ||
isConfirmDialogOpen && { | ||
type: 'confirm', | ||
color: 'danger', | ||
onCancel: onComplete, | ||
onConfirm: () => { | ||
restore.execute(revision) | ||
router.navigateIntent('edit', {id, type}) | ||
onComplete() | ||
}, | ||
message: <>Are you sure you want to restore this document?</> | ||
}) || | ||
(error && { | ||
type: 'error', | ||
onClose: () => setError(null), | ||
title: 'An error occured', | ||
content: error.message | ||
}) | ||
dialog | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,17 @@ | ||
import {useDocumentOperation} from '@sanity/react-hooks' | ||
import {useCallback} from 'react' | ||
|
||
export function SaveAction({id, type, onComplete}) { | ||
const {commit}: any = useDocumentOperation(id, type) | ||
|
||
const handle = useCallback(async () => { | ||
await commit.execute() | ||
onComplete() | ||
}, [commit, onComplete]) | ||
|
||
return { | ||
disabled: Boolean(commit.disabled), | ||
label: 'Save', | ||
onHandle: async () => { | ||
await commit.execute() | ||
onComplete() | ||
} | ||
onHandle: handle | ||
} | ||
} |
Oops, something went wrong.