-
Notifications
You must be signed in to change notification settings - Fork 364
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: [M3-7806] - Linode Create Refactor - Part 1 (#10268)
* linode create v2 initial commit * fix old linode create flow spacing * Added changeset: Linode Create Refactor - Part 1 * clean up comments * use `useRestrictedGlobalGrantCheck` helpers * Added changeset: Make `type` and `region` required in `CreateLinodeRequest` --------- Co-authored-by: Banks Nussman <banks@nussman.us>
- Loading branch information
1 parent
74695aa
commit aa59941
Showing
19 changed files
with
383 additions
and
176 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
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)) |
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
5 changes: 5 additions & 0 deletions
5
packages/manager/.changeset/pr-10268-tech-stories-1709929859267.md
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,5 @@ | ||
--- | ||
"@linode/manager": Tech Stories | ||
--- | ||
|
||
Linode Create Refactor - Part 1 ([#10268](https://github.com/linode/manager/pull/10268)) |
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
24 changes: 24 additions & 0 deletions
24
packages/manager/src/features/Linodes/LinodeCreatev2/Error.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,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> | ||
); | ||
}; |
53 changes: 53 additions & 0 deletions
53
packages/manager/src/features/Linodes/LinodeCreatev2/Plan.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,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', | ||
}); | ||
|
||
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) ?? []} | ||
/> | ||
); | ||
}; |
27 changes: 27 additions & 0 deletions
27
packages/manager/src/features/Linodes/LinodeCreatev2/Region.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,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} | ||
/> | ||
); | ||
}; |
22 changes: 22 additions & 0 deletions
22
packages/manager/src/features/Linodes/LinodeCreatev2/Summary.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,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> | ||
); | ||
}; |
52 changes: 52 additions & 0 deletions
52
packages/manager/src/features/Linodes/LinodeCreatev2/index.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,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)}> | ||
<Stack gap={2}> | ||
<Error /> | ||
<Region /> | ||
<Plan /> | ||
<Summary /> | ||
</Stack> | ||
</form> | ||
</FormProvider> | ||
); | ||
}; |
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
Oops, something went wrong.