Skip to content

Commit

Permalink
More concise differential. Add BucketForm Type.
Browse files Browse the repository at this point in the history
  • Loading branch information
kamorel committed Apr 5, 2023
1 parent d8c5a18 commit 5160e20
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 26 deletions.
33 changes: 21 additions & 12 deletions frontend/src/components/bucket/BucketConfigForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ import { differential } from '@/utils/utils';
import type { Bucket } from '@/types';
export type BucketForm = {
accessKeyId?: string;
bucket?: string;
bucketName?: string;
endpoint?: string;
key?: string;
secretAccessKey?: string;
};
// Props
type Props = {
bucket?: Bucket;
Expand All @@ -28,23 +37,23 @@ const bucketStore = useBucketStore();
const { getUserId } = storeToRefs(useAuthStore());
// Default form values
const initialValues: any = {
bucketName: props.bucket?.bucketName,
const initialValues: BucketForm = {
accessKeyId: props.bucket?.accessKeyId,
bucket: props.bucket?.bucket,
bucketName: props.bucket?.bucketName,
endpoint: props.bucket?.endpoint,
accessKeyId: props.bucket?.accessKeyId,
secretAccessKey: props.bucket?.secretAccessKey,
key: props.bucket?.key
key: props.bucket?.key,
secretAccessKey: props.bucket?.secretAccessKey
};
// Form validation schema
const schema = object({
bucketName: string().max(255).required().label('Bucket name'),
accessKeyId: string().max(255).required().label('Access Key ID'),
bucket: string().max(255).required().label('Bucket'),
bucketName: string().max(255).required().label('Bucket name'),
endpoint: string().max(255).required().label('Endpoint'),
accessKeyId: string().max(255).required().label('Access Key ID'),
secretAccessKey: string().max(255).required().label('Secret Access Key'),
key: string().max(255).label('Key')
key: string().max(255).label('Key'),
secretAccessKey: string().max(255).required().label('Secret Access Key')
});
// Actions
Expand All @@ -53,12 +62,12 @@ const toast = useToast();
const onSubmit = async (values: any) => {
try {
const formBucket = {
bucketName: values.bucketName,
accessKeyId: values.accessKeyId,
bucket: values.bucket,
bucketName: values.bucketName,
endpoint: values.endpoint,
accessKeyId: values.accessKeyId,
secretAccessKey: values.secretAccessKey,
key: values.key ? values.key : '/',
secretAccessKey: values.secretAccessKey,
} as Bucket;
props.bucket ?
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/types/Bucket.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { IAudit } from '@/interfaces';

export type Bucket = {
bucketId: string;
bucketName: string;
active: boolean;
accessKeyId: string;
bucket: string;
bucketId: string;
bucketName: string;
endpoint: string;
key: string;
secretAccessKey: string;
region: string;
active: boolean;
secretAccessKey: string;
} & IAudit;
14 changes: 4 additions & 10 deletions frontend/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,10 @@
* @param {object} comparer The object to be compared against
* @returns {object} Object containing the non-matching key/value pairs in the source object
*/
export function differential(source: any, comparer: any) {
const diff: any = {};
for (const [key, value] of Object.entries(source)) {
if (!Object.prototype.hasOwnProperty.call(comparer, key) ||
(Object.prototype.hasOwnProperty.call(comparer, key) && comparer[key] !== value)) {
diff[key] = value;
}
}

return diff;
export function differential(source: any, comparer: any): any {
return Object.fromEntries(Object.entries(source)
.filter(([key, value]) => comparer[key] !== value)
);
}

/**
Expand Down

0 comments on commit 5160e20

Please sign in to comment.