Skip to content

Commit

Permalink
Cleanup and invalidation
Browse files Browse the repository at this point in the history
  • Loading branch information
abailly-akamai committed Jan 26, 2024
1 parent 3d61628 commit bc6604c
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ interface Props {
regions: Region[];
selectedPlacementGroup?: PlacementGroup;
selectedRegionId?: string;
setIsFormDirty: React.Dispatch<React.SetStateAction<boolean>>;
}

export const PlacementGroupsDrawerContent = (props: Props) => {
Expand All @@ -43,6 +44,7 @@ export const PlacementGroupsDrawerContent = (props: Props) => {
regions,
selectedPlacementGroup,
selectedRegionId,
setIsFormDirty,
} = props;
const {
errors,
Expand All @@ -59,11 +61,15 @@ export const PlacementGroupsDrawerContent = (props: Props) => {
React.useEffect(() => {
if (open) {
resetForm();
// setGeneralSubnetErrorsFromAPI([]);
// setGeneralAPIError(undefined);
}
}, [open, resetForm]);

React.useEffect(() => {
if (isSubmitting) {
setIsFormDirty(true);
}
}, [isSubmitting]);

const generalError = status?.generalError;
const isRenameDrawer = mode === 'rename';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const PlacementGroupsCreateDrawer = (
const { data: regions } = useRegionsQuery();
const { mutateAsync } = useCreatePlacementGroup();
const { enqueueSnackbar } = useSnackbar();
const [isFormDirty, setIsFormDirty] = React.useState<boolean>(false);

const {
errors,
Expand All @@ -57,17 +58,16 @@ export const PlacementGroupsCreateDrawer = (
values: CreatePlacementGroupPayload,
{ setErrors, setStatus, setSubmitting }
) {
setIsFormDirty(false);
setStatus(undefined);
setErrors({});
const payload = { ...values };

mutateAsync(payload)
.then((response) => {
setSubmitting(false);
// Invalidate Placement Groups Queries
queryClient.invalidateQueries([placementGroupQueryKey]);

// Show snackbar notification
enqueueSnackbar(
`Placement Group ${payload.label} successfully created`,
{
Expand All @@ -94,18 +94,10 @@ export const PlacementGroupsCreateDrawer = (
});
},
validateOnBlur: false,
validateOnChange: false,
validateOnChange: isFormDirty,
validationSchema: createPlacementGroupSchema,
});

React.useEffect(() => {
if (open) {
resetForm();
// setGeneralSubnetErrorsFromAPI([]);
// setGeneralAPIError(undefined);
}
}, [open, resetForm]);

return (
<Drawer onClose={onClose} open={open} title="Create Placement Group">
<PlacementGroupsDrawerContent
Expand All @@ -128,6 +120,7 @@ export const PlacementGroupsCreateDrawer = (
open={open}
regions={regions ?? []}
selectedRegionId={selectedRegionId}
setIsFormDirty={setIsFormDirty}
/>
</Drawer>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ export const PlacementGroupsDetail = () => {
} = useMutatePlacementGroup(placementGroupId);
const errorText = getErrorStringOrDefault(updatePlacementGroupError ?? '');

if (isLoading) {
return <CircleProgress />;
}

if (!placementGroup) {
return <NotFound />;
}
Expand All @@ -47,10 +51,6 @@ export const PlacementGroupsDetail = () => {
);
}

if (isLoading) {
return <CircleProgress />;
}

const linodeCount = getPlacementGroupLinodeCount(placementGroup);
const tabs = [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export const PlacementGroupsRenameDrawer = (
selectedPlacementGroup?.id ?? -1
);
const { enqueueSnackbar } = useSnackbar();
const [isFormDirty, setIsFormDirty] = React.useState<boolean>(false);

const {
errors,
Expand All @@ -60,11 +61,13 @@ export const PlacementGroupsRenameDrawer = (
region: selectedPlacementGroup?.region ?? '',
},
onSubmit(values, { setErrors, setStatus, setSubmitting }) {
setIsFormDirty(false);
setStatus(undefined);
setErrors({});
const payload = { ...values };
const { label } = payload;

mutateAsync(payload)
mutateAsync({ label })
.then((response) => {
setSubmitting(false);
// Invalidate Placement Groups Queries
Expand Down Expand Up @@ -97,18 +100,10 @@ export const PlacementGroupsRenameDrawer = (
});
},
validateOnBlur: false,
validateOnChange: false,
validateOnChange: isFormDirty,
validationSchema: renamePlacementGroupSchema,
});

React.useEffect(() => {
if (open) {
resetForm();
// setGeneralSubnetErrorsFromAPI([]);
// setGeneralAPIError(undefined);
}
}, [open, resetForm]);

return (
<Drawer
onClose={onClose}
Expand All @@ -132,6 +127,7 @@ export const PlacementGroupsRenameDrawer = (
onClose={onClose}
open={open}
regions={regions ?? []}
setIsFormDirty={setIsFormDirty}
/>
</Drawer>
);
Expand Down
12 changes: 7 additions & 5 deletions packages/validation/src/placement-groups.schema.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { array, number, object, string } from 'yup';

const labelValidation = string()
.required('Label is required.')
.min(3, 'Label must be between 3 and 32 characters.')
.max(32, 'Label must be between 3 and 32 characters.');

export const createPlacementGroupSchema = object({
label: string()
.required('Label is required.')
.min(3, 'Label must be between 3 and 32 characters.')
.max(32, 'Label must be between 3 and 32 characters.'),
label: labelValidation,
affinity_type: string().required('Affinity type is required.'),
region: string().required('Region is required.'),
});

export const renamePlacementGroupSchema = object({
label: string().required('Label is required.'),
label: labelValidation,
});

/**
Expand Down

0 comments on commit bc6604c

Please sign in to comment.