From 80955b9065757a2a461a2b712603889ae1867690 Mon Sep 17 00:00:00 2001 From: KY Lau Date: Thu, 10 Jun 2021 10:20:22 +0800 Subject: [PATCH] Fix complexity issue --- .../AssetActions/Edit/FormAdvanceSettings.tsx | 2 +- src/models/FormEditCredential.ts | 44 +++++++++++-------- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/src/components/organisms/AssetActions/Edit/FormAdvanceSettings.tsx b/src/components/organisms/AssetActions/Edit/FormAdvanceSettings.tsx index 1690a759e..1e4a98178 100644 --- a/src/components/organisms/AssetActions/Edit/FormAdvanceSettings.tsx +++ b/src/components/organisms/AssetActions/Edit/FormAdvanceSettings.tsx @@ -1,5 +1,5 @@ import React, { ChangeEvent, ReactElement } from 'react' -import styles from './FormEditMetadata.module.css' // TODO +import styles from './FormEditMetadata.module.css' import { Field, Form, FormikContextType, useFormikContext } from 'formik' import Button from '../../../atoms/Button' import Input from '../../../atoms/Input' diff --git a/src/models/FormEditCredential.ts b/src/models/FormEditCredential.ts index f604efeed..50d876a13 100644 --- a/src/models/FormEditCredential.ts +++ b/src/models/FormEditCredential.ts @@ -1,5 +1,6 @@ import { CredentialAction, + Credential, Credentials, CredentialType, DDO @@ -19,48 +20,53 @@ export const validationSchema: Yup.SchemaOf = isOrderDisabled: Yup.boolean().nullable() }) +function getCredentialList( + credential: Credential[], + credentialType: CredentialType +): string[] { + const credentialByType = credential.find( + (credential) => credential.type === credentialType + ) + return credentialByType.value && credentialByType.value.length > 0 + ? credentialByType.value + : [] +} + function getAssetCredentials( credentials: Credentials, credentialType: CredentialType, credentialAction: CredentialAction ): string[] { - let values: string[] = [] + if (!credentials) return [] + if (credentialAction === 'allow') { - if (credentials && credentials.allow) { - const allowList = credentials.allow.find( - (credential) => credential.type === credentialType - ) - values = allowList && allowList.value.length > 0 ? allowList.value : [] - } - } else { - if (credentials && credentials.deny) { - const dennyList = credentials.deny.find( - (credential) => credential.type === credentialType - ) - values = dennyList && dennyList.value.length > 0 ? dennyList.value : [] - } + return credentials.allow + ? getCredentialList(credentials.allow, credentialType) + : [] } - return values + return credentials.deny + ? getCredentialList(credentials.deny, credentialType) + : [] } export function getInitialValues( ddo: DDO, credentailType: CredentialType ): AdvanceSettingsForm { - const allowCrendtail = getAssetCredentials( + const allowCredential = getAssetCredentials( ddo.credentials, credentailType, 'allow' ) - const denyCrendtail = getAssetCredentials( + const denyCredential = getAssetCredentials( ddo.credentials, credentailType, 'deny' ) const metadata = ddo.findServiceByType('metadata') return { - allow: allowCrendtail, - deny: denyCrendtail, + allow: allowCredential, + deny: denyCredential, isOrderDisabled: metadata.attributes?.status?.isOrderDisabled || false } }