-
Notifications
You must be signed in to change notification settings - Fork 364
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
feat: [M3-7806] - Linode Create Refactor - Part 1 #10268
Changes from all commits
e441cc7
29a1483
175cc34
f6840cf
10256b8
b7c35c9
f090e5c
f1f7e8a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@linode/api-v4": Changed | ||
--- | ||
|
||
Make `type` and `region` required in `CreateLinodeRequest` ([#10268](https://github.com/linode/manager/pull/10268)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@linode/manager": Tech Stories | ||
--- | ||
|
||
Linode Create Refactor - Part 1 ([#10268](https://github.com/linode/manager/pull/10268)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Typography } from '@mui/material'; | ||
import React from 'react'; | ||
import { useFormContext } from 'react-hook-form'; | ||
|
||
import { Notice } from 'src/components/Notice/Notice'; | ||
import { Paper } from 'src/components/Paper'; | ||
|
||
import type { CreateLinodeRequest } from '@linode/api-v4'; | ||
|
||
export const Error = () => { | ||
const { formState } = useFormContext<CreateLinodeRequest>(); | ||
|
||
if (!formState.errors.root?.message) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<Paper sx={{ p: 0 }}> | ||
<Notice spacingBottom={0} spacingTop={0} variant="error"> | ||
<Typography py={2}>{formState.errors.root.message}</Typography> | ||
</Notice> | ||
</Paper> | ||
); | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Love it - can There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think For example |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import React from 'react'; | ||
import { useController, useFormContext } from 'react-hook-form'; | ||
|
||
import { DocsLink } from 'src/components/DocsLink/DocsLink'; | ||
import { PlansPanel } from 'src/features/components/PlansPanel/PlansPanel'; | ||
import { useRestrictedGlobalGrantCheck } from 'src/hooks/useRestrictedGlobalGrantCheck'; | ||
import { useRegionsQuery } from 'src/queries/regions'; | ||
import { useAllTypes } from 'src/queries/types'; | ||
import { sendLinodeCreateFlowDocsClickEvent } from 'src/utilities/analytics'; | ||
import { extendType } from 'src/utilities/extendType'; | ||
|
||
import type { CreateLinodeRequest } from '@linode/api-v4'; | ||
|
||
export const Plan = () => { | ||
const { watch } = useFormContext<CreateLinodeRequest>(); | ||
const { field, fieldState } = useController<CreateLinodeRequest>({ | ||
name: 'type', | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this looks like a very useful API - will learn about |
||
|
||
const { data: regions } = useRegionsQuery(); | ||
const { data: types } = useAllTypes(); | ||
|
||
const regionId = watch('region'); | ||
|
||
const isLinodeCreateRestricted = useRestrictedGlobalGrantCheck({ | ||
globalGrantType: 'add_linodes', | ||
}); | ||
|
||
return ( | ||
<PlansPanel | ||
docsLink={ | ||
<DocsLink | ||
onClick={() => { | ||
sendLinodeCreateFlowDocsClickEvent('Choosing a Plan'); | ||
}} | ||
href="https://www.linode.com/docs/guides/choosing-a-compute-instance-plan/" | ||
label="Choosing a Plan" | ||
/> | ||
} | ||
data-qa-select-plan | ||
disabled={isLinodeCreateRestricted} | ||
error={fieldState.error?.message} | ||
isCreate | ||
linodeID={undefined} // @todo add cloning support | ||
onSelect={field.onChange} | ||
regionsData={regions} // @todo move this query deeper if possible | ||
selectedId={field.value} | ||
selectedRegionID={regionId} | ||
showTransfer | ||
types={types?.map(extendType) ?? []} | ||
/> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import React from 'react'; | ||
import { useController } from 'react-hook-form'; | ||
|
||
import { SelectRegionPanel } from 'src/components/SelectRegionPanel/SelectRegionPanel'; | ||
import { useRestrictedGlobalGrantCheck } from 'src/hooks/useRestrictedGlobalGrantCheck'; | ||
|
||
import type { CreateLinodeRequest } from '@linode/api-v4'; | ||
|
||
export const Region = () => { | ||
const { field, formState } = useController<CreateLinodeRequest>({ | ||
name: 'region', | ||
}); | ||
|
||
const isLinodeCreateRestricted = useRestrictedGlobalGrantCheck({ | ||
globalGrantType: 'add_linodes', | ||
}); | ||
|
||
return ( | ||
<SelectRegionPanel | ||
currentCapability="Linodes" | ||
disabled={isLinodeCreateRestricted} | ||
error={formState.errors.region?.message} | ||
handleSelection={field.onChange} | ||
selectedId={field.value} | ||
/> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { CreateLinodeRequest } from '@linode/api-v4'; | ||
import React from 'react'; | ||
import { useFormContext } from 'react-hook-form'; | ||
|
||
import { Button } from 'src/components/Button/Button'; | ||
import { Paper } from 'src/components/Paper'; | ||
|
||
export const Summary = () => { | ||
const { formState } = useFormContext<CreateLinodeRequest>(); | ||
|
||
return ( | ||
<Paper sx={{ display: 'flex', justifyContent: 'flex-end' }}> | ||
<Button | ||
buttonType="primary" | ||
loading={formState.isSubmitting} | ||
type="submit" | ||
> | ||
Create Linode | ||
</Button> | ||
</Paper> | ||
); | ||
}; |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the root of the new create flow! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import React from 'react'; | ||
import { FormProvider, useForm } from 'react-hook-form'; | ||
import { useHistory } from 'react-router-dom'; | ||
|
||
import { Stack } from 'src/components/Stack'; | ||
import { useCreateLinodeMutation } from 'src/queries/linodes/linodes'; | ||
|
||
import { Error } from './Error'; | ||
import { Plan } from './Plan'; | ||
import { Region } from './Region'; | ||
import { Summary } from './Summary'; | ||
|
||
import type { CreateLinodeRequest } from '@linode/api-v4'; | ||
import type { SubmitHandler } from 'react-hook-form'; | ||
|
||
export const LinodeCreatev2 = () => { | ||
const methods = useForm<CreateLinodeRequest>(); | ||
const history = useHistory(); | ||
|
||
const { mutateAsync: createLinode } = useCreateLinodeMutation(); | ||
|
||
const onSubmit: SubmitHandler<CreateLinodeRequest> = async (data) => { | ||
try { | ||
const linode = await createLinode(data); | ||
|
||
history.push(`/linodes/${linode.id}`); | ||
} catch (errors) { | ||
// @todo this is temporary API error handling. We will develop a more | ||
// robust helper that can convert API errors to react-hook-form errors | ||
for (const error of errors) { | ||
if (error.field) { | ||
methods.setError(error.field, { message: error.reason }); | ||
} else { | ||
methods.setError('root', { message: error.reason }); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
return ( | ||
<FormProvider {...methods}> | ||
<form onSubmit={methods.handleSubmit(onSubmit)}> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I get why you named it |
||
<Stack gap={2}> | ||
<Error /> | ||
<Region /> | ||
<Plan /> | ||
<Summary /> | ||
Comment on lines
+44
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure how well these component names will scale, but for now, I'm just trying to keep things dead simple. We can adjust as we build this out |
||
</Stack> | ||
</form> | ||
</FormProvider> | ||
); | ||
}; |
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.
This was a major oversight.
type
andregion
are the two required values to create a LinodeThere 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.
😮 thx for catching
Did you consider creating a v2 type and not touch at all the existing flow?
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.
We could do that, but I think it's fair to fix the primary type.