-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ability to create image from snapshot #1663
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9dae24d
Add create snapshot from image form
zephraph 781c057
Add toast on success
zephraph a5c8640
Fix failing snapshot test
zephraph 9edebc6
Add e2e test for creating image from snapshot
zephraph 38c5cad
Fix type failure, update snapshot test
zephraph a9e56e4
bytes to widdle bibbies
zephraph 52b8dc7
shared with to project
zephraph File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import fileSize from 'filesize' | ||
import { useForm } from 'react-hook-form' | ||
import type { LoaderFunctionArgs } from 'react-router-dom' | ||
import { useNavigate } from 'react-router-dom' | ||
import invariant from 'tiny-invariant' | ||
|
||
import { | ||
type ImageCreate, | ||
apiQueryClient, | ||
useApiMutation, | ||
useApiQuery, | ||
useApiQueryClient, | ||
} from '@oxide/api' | ||
import { PropertiesTable } from '@oxide/ui' | ||
|
||
import { DescriptionField, NameField, SideModalForm, TextField } from 'app/components/form' | ||
import { getProjectSnapshotSelector, useProjectSnapshotSelector } from 'app/hooks' | ||
import { addToast } from 'app/stores/toast' | ||
import { pb } from 'app/util/path-builder' | ||
|
||
const defaultValues: Omit<ImageCreate, 'source'> = { | ||
name: '', | ||
description: '', | ||
os: '', | ||
version: '', | ||
} | ||
|
||
CreateImageFromSnapshotSideModalForm.loader = async ({ params }: LoaderFunctionArgs) => { | ||
const { project, snapshot } = getProjectSnapshotSelector(params) | ||
await apiQueryClient.prefetchQuery('snapshotView', { | ||
path: { snapshot }, | ||
query: { project }, | ||
}) | ||
return null | ||
} | ||
|
||
export function CreateImageFromSnapshotSideModalForm() { | ||
const { snapshot, project } = useProjectSnapshotSelector() | ||
const { data } = useApiQuery('snapshotView', { path: { snapshot }, query: { project } }) | ||
invariant(data, 'Snapshot must be prefetched in loader') | ||
const navigate = useNavigate() | ||
const queryClient = useApiQueryClient() | ||
|
||
const onDismiss = () => navigate(pb.snapshots({ project })) | ||
|
||
const createImage = useApiMutation('imageCreate', { | ||
onSuccess() { | ||
queryClient.invalidateQueries('imageList', { query: { project } }) | ||
addToast({ | ||
content: 'Your image has been created', | ||
}) | ||
onDismiss() | ||
}, | ||
}) | ||
|
||
const form = useForm({ | ||
mode: 'all', | ||
defaultValues: { | ||
...defaultValues, | ||
name: data.name, | ||
}, | ||
}) | ||
|
||
return ( | ||
<SideModalForm | ||
id="create-image-from-snapshot-form" | ||
form={form} | ||
title={`Create image from snapshot`} | ||
submitLabel="Create image" | ||
onDismiss={onDismiss} | ||
onSubmit={(body) => | ||
createImage.mutate({ | ||
query: { project }, | ||
body: { ...body, source: { type: 'snapshot', id: data.id } }, | ||
}) | ||
} | ||
> | ||
<PropertiesTable> | ||
<PropertiesTable.Row label="Snapshot">{data.name}</PropertiesTable.Row> | ||
<PropertiesTable.Row label="Shared with">{project}</PropertiesTable.Row> | ||
<PropertiesTable.Row label="Size">{fileSize(data.size)}</PropertiesTable.Row> | ||
</PropertiesTable> | ||
|
||
<NameField name="name" control={form.control} required /> | ||
<DescriptionField name="description" control={form.control} required /> | ||
<TextField name="os" label="OS" control={form.control} required /> | ||
<TextField name="version" control={form.control} required /> | ||
</SideModalForm> | ||
) | ||
} |
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The path here is a little odd. I've patterned it after the other create endpoints though it doesn't necessarily need to be
image-new
. It could be something likecreate-image
. There's really just no precedent for that.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is the most reasonable choice.