Skip to content

Commit

Permalink
new useMutationsHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
hervedombya committed Oct 5, 2023
1 parent e5e03b0 commit ec540c8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 48 deletions.
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
"@hookform/resolvers": "^2.8.8",
"@js-temporal/polyfill": "^0.4.3",
"@monaco-editor/react": "^4.4.5",
"@scality/core-ui": "github:scality/core-ui#0b877541d7a1c867a399ad5d5e76b853de97b669",
"@scality/core-ui": "github:scality/core-ui#b4d68882cc21e02f4da7318773cb316ac1b0f8a2",
"@scality/module-federation": "github:scality/module-federation#1.1.0",
"@types/react-table": "^7.7.10",
"@types/react-virtualized": "^9.21.20",
Expand Down
14 changes: 4 additions & 10 deletions src/react/ToastProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,26 @@ import { UseMutationResult } from 'react-query';

const toastGlobal = {};

type MainMutationConfig<Data, Variables> = {
export type MutationConfig<Data, Variables> = {
mutation: UseMutationResult<Data, unknown, Variables, unknown>;
name: string;
isPrimary: boolean;
};

type DependantMutationConfig<Data, Variables> = Omit<
MainMutationConfig<Data, Variables>,
'isPrimary'
>;

type DescriptionBuilder<Data> = {
data?: Data;
error?: unknown;
name: string;
};

type MutationsHandlerProps<Data, Variables> = {
mainMutation: MainMutationConfig<Data, Variables>;
dependantMutations?: DependantMutationConfig<Data, Variables>[];
mainMutation: MutationConfig<Data, Variables>;
dependantMutations?: MutationConfig<Data, Variables>[];
messageDescriptionBuilder: (
successMutations: DescriptionBuilder<Data>[],
errorMutations: DescriptionBuilder<Data>[],
) => ReactNode;
toastStyles?: React.CSSProperties;
onPrimarySuccess?: () => void;
onMainMutationSuccess?: () => void;
};

export function useToast() {
Expand Down
50 changes: 19 additions & 31 deletions src/react/databrowser/buckets/BucketCreate.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import {
Banner,
Checkbox,
Form,
FormGroup,
FormSection,
Icon,
Stack,
} from '@scality/core-ui';
import { Controller, FormProvider, useForm } from 'react-hook-form';
Expand All @@ -24,12 +22,14 @@ import { XDM_FEATURE } from '../../../js/config';
import { renderLocation } from '../../locations/utils';
import { convertRemToPixels } from '@scality/core-ui/dist/utils';
import { useHistory, useParams } from 'react-router-dom';
import { useMutationsHandler } from '../../ToastProvider';
import { MutationConfig, useMutationsHandler } from '../../ToastProvider';
import {
useChangeBucketDefaultRetention,
useChangeBucketVersionning,
useCreateBucket,
} from '../../next-architecture/domain/business/buckets';
import { PromiseResult } from 'aws-sdk/lib/request';
import { AWSError, S3 } from 'aws-sdk';

const helpNonAsyncLocation =
'Selected Storage Location does not support Async Metadata updates.';
Expand Down Expand Up @@ -173,7 +173,7 @@ function BucketCreate() {
ObjectLockEnabledForBucket: isObjectLockEnabled,
},
{
onSuccess: (data) => {
onSuccess: () => {
if (isVersioning) {
changeBucketVersionning({
Bucket: name,
Expand Down Expand Up @@ -205,7 +205,6 @@ function BucketCreate() {
},
});
}
// history.push(`/accounts/${accountName}/buckets/${name}`);
},
},
);
Expand All @@ -225,8 +224,7 @@ function BucketCreate() {
const mainMutation = {
mutation: createBucketMutation,
name: 'createBucket',
isPrimary: true,
};
} as MutationConfig<PromiseResult<S3.CreateBucketOutput, AWSError>, unknown>;

const dependantMutations = [
{
Expand All @@ -237,9 +235,20 @@ function BucketCreate() {
mutation: changeBucketDefaultRetentionMutation,
name: 'changeBucketDefaultRetention',
},
];
] as MutationConfig<unknown, unknown>[];

const messageDescriptionBuilder = (successMutations, errorMutations) => {
const messageDescriptionBuilder = (
successMutations: {
data?: unknown;
error?: unknown;
name: string;
}[],
errorMutations: {
data?: unknown;
error?: unknown;
name: string;
}[],
) => {
const bucketCreateMessage = 'Bucket successfully created.';
const bucketVersionningMessage = 'Bucket version enabled.';
const retentionMessage = 'Default retention enabled.';
Expand Down Expand Up @@ -290,11 +299,7 @@ function BucketCreate() {
mainMutation,
dependantMutations,
messageDescriptionBuilder,
toastStyles: {
top: '3rem',
right: '1rem',
},
onPrimarySuccess: () =>
onMainMutationSuccess: () =>
history.push(`/accounts/${accountName}/buckets/${name}`),
});

Expand Down Expand Up @@ -328,23 +333,6 @@ function BucketCreate() {
/>
</Stack>
}
// banner={
// (errorMessage || error) && (
// <Banner
// id="zk-error-banner"
// variant="danger"
// icon={<Icon name="Exclamation-triangle" />}
// title={'Error'}
// >
// {errorMessage ||
// (error &&
// typeof error === 'object' &&
// 'message' in error &&
// error?.message) ||
// 'An unexpected error occurred.'}
// </Banner>
// )
// }
>
<FormSection forceLabelWidth={convertRemToPixels(17.5)}>
<FormGroup
Expand Down

0 comments on commit ec540c8

Please sign in to comment.