-
Notifications
You must be signed in to change notification settings - Fork 451
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sanity): supporting editing bundle metadata (#7180)
* feat: supporting editing bundle metadata * test(sanity): updating tests for Bundle form * fix(releases): fixing issue with mocked imports on ReleasesOverview * refactor(core): bundle detail dialog form computes action from initial value rather than passing as a prop * refactor(core): bundle detail dialog form computes action from initial value rather than passing as a prop * refactor(core): only passing diff when editing bundle details
- Loading branch information
Showing
12 changed files
with
486 additions
and
260 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
120 changes: 120 additions & 0 deletions
120
packages/sanity/src/core/bundles/components/dialog/BundleDetailsDialog.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 |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import {ArrowRightIcon} from '@sanity/icons' | ||
import {Box, Button, Dialog, Flex} from '@sanity/ui' | ||
import {type FormEvent, useCallback, useState} from 'react' | ||
|
||
import {type BundleDocument} from '../../../store/bundles/types' | ||
import {useBundleOperations} from '../../../store/bundles/useBundleOperations' | ||
import {usePerspective} from '../../hooks/usePerspective' | ||
import {BundleForm} from './BundleForm' | ||
|
||
interface BundleDetailsDialogProps { | ||
onCancel: () => void | ||
onSubmit: () => void | ||
bundle?: BundleDocument | ||
} | ||
|
||
export function BundleDetailsDialog(props: BundleDetailsDialogProps): JSX.Element { | ||
const {onCancel, onSubmit, bundle} = props | ||
const {createBundle, updateBundle} = useBundleOperations() | ||
const [hasErrors, setHasErrors] = useState(false) | ||
const formAction = bundle ? 'edit' : 'create' | ||
|
||
const [value, setValue] = useState<Partial<BundleDocument>>(() => { | ||
if (bundle) { | ||
return { | ||
slug: bundle.slug, | ||
title: bundle.title, | ||
description: bundle.description, | ||
hue: bundle.hue, | ||
icon: bundle.icon, | ||
} | ||
} | ||
|
||
return { | ||
slug: '', | ||
title: '', | ||
hue: 'gray', | ||
icon: 'cube', | ||
//publishAt: undefined, | ||
} | ||
}) | ||
const [isSubmitting, setIsSubmitting] = useState(false) | ||
|
||
// TODO MAKE SURE THIS IS HOW WE WANT TO DO THIS | ||
const {setPerspective} = usePerspective() | ||
|
||
const bundleOperation = useCallback( | ||
(formValue: Partial<BundleDocument>) => { | ||
if (formAction === 'edit' && bundle?._id) { | ||
const updatedBundle: Partial<BundleDocument> = { | ||
...formValue, | ||
_id: bundle._id, | ||
} | ||
|
||
return updateBundle(updatedBundle) | ||
} | ||
return createBundle(formValue) | ||
}, | ||
[bundle?._id, createBundle, formAction, updateBundle], | ||
) | ||
|
||
const handleOnSubmit = useCallback( | ||
async (event: FormEvent<HTMLFormElement>) => { | ||
if (value.slug) { | ||
try { | ||
event.preventDefault() | ||
setIsSubmitting(true) | ||
await bundleOperation(value) | ||
setValue(value) | ||
} catch (err) { | ||
console.error(err) | ||
} finally { | ||
setIsSubmitting(false) | ||
if (formAction === 'create') { | ||
setPerspective(value.slug) | ||
} | ||
onSubmit() | ||
} | ||
} | ||
}, | ||
[bundleOperation, formAction, onSubmit, setPerspective, value], | ||
) | ||
|
||
const handleOnChange = useCallback((changedValue: Partial<BundleDocument>) => { | ||
setValue(changedValue) | ||
}, []) | ||
|
||
const handleOnError = useCallback((errorsExist: boolean) => { | ||
setHasErrors(errorsExist) | ||
}, []) | ||
|
||
const dialogTitle = formAction === 'edit' ? 'Edit release' : 'Create release' | ||
|
||
return ( | ||
<Dialog | ||
animate | ||
header={dialogTitle} | ||
id="create-bundle-dialog" | ||
onClose={onCancel} | ||
zOffset={5000} | ||
width={1} | ||
> | ||
<form onSubmit={handleOnSubmit}> | ||
<Box padding={6}> | ||
<BundleForm onChange={handleOnChange} onError={handleOnError} value={value} /> | ||
</Box> | ||
<Flex justify="flex-end" padding={3}> | ||
<Button | ||
disabled={!value.title || isSubmitting || hasErrors} | ||
iconRight={ArrowRightIcon} | ||
type="submit" | ||
// localize Text | ||
text={dialogTitle} | ||
loading={isSubmitting} | ||
data-testid="submit-release-button" | ||
/> | ||
</Flex> | ||
</form> | ||
</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
87 changes: 0 additions & 87 deletions
87
packages/sanity/src/core/bundles/components/dialog/CreateBundleDialog.tsx
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.