Skip to content
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

Bug 2038898: Add valued update check for aws s3/s3 repo type #1436

Merged
merged 2 commits into from
Jun 6, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import utils from '../../../../../../common/duck/utils';
import storageUtils from '../../../../../../storage/duck/utils';
import commonUtils from '../../../../../../common/duck/utils';
import { validatedState } from '../../../../../../common/helpers';
import { IStorage } from '../../../../../../storage/duck/types';

interface IFormValues {
name: string;
Expand All @@ -33,8 +34,44 @@ interface IOtherProps {
initialStorageValues: any;
provider: string;
isAWS: boolean;
currentStorage?: any;
}

const valuesHaveUpdate = (values: any, currentStorage: IStorage, isAWS: boolean) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine because it's the pattern we've established, but it has me wondering if we could use Formik better for this kind of thing. Isn't this what the isDirty property in form state is supposed to be for? Might be worth looking into why we didn't use that in the past (I suspect something to do with prefilling asynchronously, that's why I ended up adding a prefill function to useFormState)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

100% agree. Would definitely be worth a revisit. With forms with multiple conditional fields, I wonder the best approach for properly using the isDirty prop. We might have to split out the s3 /aws s3 forms into their own forms.

if (!currentStorage) {
return true;
}

const existingMigStorageName = currentStorage.MigStorage.metadata.name;
const existingAWSBucketName = currentStorage.MigStorage.spec.backupStorageConfig.awsBucketName;
const existingAWSBucketRegion = currentStorage.MigStorage.spec.backupStorageConfig.awsRegion;
let existingAccessKey;
if (currentStorage.Secret.data['aws-access-key-id']) {
existingAccessKey = atob(currentStorage.Secret.data['aws-access-key-id']);
}

let existingSecretKey;
if (currentStorage.Secret.data['aws-secret-access-key']) {
existingSecretKey = atob(currentStorage.Secret.data['aws-secret-access-key']);
}

const existingS3URL = currentStorage.MigStorage.spec.backupStorageConfig.awsS3Url;
const existingRequireSSLValue = currentStorage.MigStorage.spec.backupStorageConfig.insecure;
const existingCABundleValue = currentStorage.MigStorage.spec.backupStorageConfig.s3CustomCABundle;

const valuesUpdatedObject =
values.name !== existingMigStorageName ||
values.awsBucketName !== existingAWSBucketName ||
values.awsBucketRegion !== existingAWSBucketRegion ||
values.accessKey !== existingAccessKey ||
values.secret !== existingSecretKey ||
(!isAWS && values.s3Url !== existingS3URL) ||
(!isAWS && values.requireSSL !== existingRequireSSLValue) ||
(!isAWS && values.caBundle !== existingCABundleValue);

return valuesUpdatedObject;
};

const componentTypeStr = 'Repository';
const currentStatusFn = addEditStatusText(componentTypeStr);
const addEditButtonTextFn = addEditButtonText(componentTypeStr);
Expand All @@ -51,6 +88,7 @@ const InnerS3Form: React.FunctionComponent<IOtherProps & FormikProps<IFormValues
handleSubmit,
handleBlur,
isAWS,
currentStorage,
}: IOtherProps & FormikProps<IFormValues>) => {
const nameKey = 'name';
const s3UrlKey = 's3Url';
Expand Down Expand Up @@ -254,7 +292,12 @@ const InnerS3Form: React.FunctionComponent<IOtherProps & FormikProps<IFormValues
aria-label="S3 Storage Submit Form"
variant="primary"
type="submit"
isDisabled={isAddEditButtonDisabled(currentStatus, errors, touched, true)}
isDisabled={isAddEditButtonDisabled(
currentStatus,
errors,
touched,
valuesHaveUpdate(values, currentStorage, isAWS)
)}
>
{addEditButtonTextFn(currentStatus)}
</Button>
Expand Down