From 911229ab7341dec4ce4f76d6b404acbc19d9b5b6 Mon Sep 17 00:00:00 2001 From: aliel Date: Tue, 2 Jan 2024 02:20:51 +0100 Subject: [PATCH] rebase + refactor domain type/target --- .../pages/dashboard/NewDomainPage/cmp.tsx | 25 +++++++---- src/domain/domain.ts | 25 +++-------- src/helpers/schemas/base.ts | 5 --- src/helpers/schemas/domain.ts | 1 - src/hooks/form/useAddDomains.ts | 8 ++-- src/hooks/pages/dashboard/useNewDomainPage.ts | 44 +++++++------------ 6 files changed, 41 insertions(+), 67 deletions(-) diff --git a/src/components/pages/dashboard/NewDomainPage/cmp.tsx b/src/components/pages/dashboard/NewDomainPage/cmp.tsx index c6472aa4..17e71606 100644 --- a/src/components/pages/dashboard/NewDomainPage/cmp.tsx +++ b/src/components/pages/dashboard/NewDomainPage/cmp.tsx @@ -4,7 +4,7 @@ import Container from '@/components/common/CenteredContainer' import ExternalLinkButton from '@/components/common/ExternalLinkButton' import { NoisyContainer } from '@aleph-front/aleph-core' import Form from '@/components/form/Form' -import { EntityType, EntityTypeName } from '@/helpers/constants' +import { EntityType, EntityTypeName, AddDomainTarget } from '@/helpers/constants' import { useNewDomainPage } from '@/hooks/pages/dashboard/useNewDomainPage' import { Button, @@ -24,15 +24,24 @@ export default function NewDomain() { hasFunctions, hasInstances, nameCtrl, - programTypeCtrl, + targetCtrl, refCtrl, - ipfsRefCtrl, errors, handleSubmit, + setTarget } = useNewDomainPage() const [tabId, setTabId] = useState('compute') + const onTabChange = (tabId) => { + setTabId(tabId) + if (tabId == "ipfs") { + setTarget(AddDomainTarget.IPFS) + } else { + setTarget(AddDomainTarget.INSTANCE) + } + } + return ( <>
@@ -95,7 +104,7 @@ export default function NewDomain() { export type AddDomain = { name: string - target: AddDomainTarget | EntityType.Program | EntityType.Instance - programType: EntityType.Instance | EntityType.Program + target: AddDomainTarget ref: string } -export type Domain = Omit & { +export type Domain = Omit & { type: EntityType.Domain id: string confirmed?: boolean - programType?: EntityType.Instance | EntityType.Program updated_at: string } @@ -81,7 +78,6 @@ export class DomainManager implements EntityManager { const content = { message_id: domain.ref, type: domain.target, - programType: domain.programType, updated_at: new Date().toISOString(), } @@ -110,20 +106,14 @@ export class DomainManager implements EntityManager { if (!domains.length) return [] const content: DomainAggregate = domains.reduce((ac, cv) => { - const { name, ref, target, programType } = cv + const { name, ref, target } = cv const domain = { message_id: ref, - programType, - type: target === AddDomainTarget.IPFS ? target : programType, + type: target, updated_at: new Date().toISOString(), } - // @note: legacy domains don't include programType (default to Instance) - if (target === AddDomainTarget.Program) { - domain.programType = cv.programType || EntityType.Instance - } - ac[name] = domain return ac @@ -241,11 +231,6 @@ export class DomainManager implements EntityManager { updated_at: updated_at, } - // @note: legacy domains don't include programType (default to Instance) - if (type === AddDomainTarget.Program) { - domain.programType = content.programType || EntityType.Instance - } - return domain } } diff --git a/src/helpers/schemas/base.ts b/src/helpers/schemas/base.ts index 274bfd15..b23f765a 100644 --- a/src/helpers/schemas/base.ts +++ b/src/helpers/schemas/base.ts @@ -83,11 +83,6 @@ export const ipfsCIDSchema = requiredStringSchema.regex( { message: 'Invalid IPFS CID hash' }, ) -export const programTypeSchema = z.enum([ - EntityType.Instance, - EntityType.Program, -]) - export const paymentMethodSchema = z.enum([ PaymentMethod.Hold, PaymentMethod.Stream, diff --git a/src/helpers/schemas/domain.ts b/src/helpers/schemas/domain.ts index b9b4600f..9946d35f 100644 --- a/src/helpers/schemas/domain.ts +++ b/src/helpers/schemas/domain.ts @@ -11,7 +11,6 @@ export const domainSchema = z.object({ AddDomainTarget.Program, AddDomainTarget.Instance, ]), - programType: programTypeSchema, ref: messageHashSchema, }) diff --git a/src/hooks/form/useAddDomains.ts b/src/hooks/form/useAddDomains.ts index 214efac5..127e77e7 100644 --- a/src/hooks/form/useAddDomains.ts +++ b/src/hooks/form/useAddDomains.ts @@ -12,14 +12,12 @@ export type DomainField = { name: string ref: string target: AddDomainTarget - programType: EntityType.Instance | EntityType.Program } export const defaultValues: DomainField = { name: '', ref: '', target: AddDomainTarget.Instance, - programType: EntityType.Instance, } export type UseDomainItemProps = { @@ -77,7 +75,7 @@ export type UseDomainsReturn = { export function useAddDomains({ name = 'domains', control, - entityType: programType, + entityType: target, }: UseDomainsProps): UseDomainsReturn { const domainsCtrl = useFieldArray({ control, @@ -87,8 +85,8 @@ export function useAddDomains({ const { fields, remove: handleRemove, append } = domainsCtrl const handleAdd = useCallback(() => { - append({ ...defaultValues, programType }) - }, [append, programType]) + append({ ...defaultValues, target }) + }, [append, target]) return { name, diff --git a/src/hooks/pages/dashboard/useNewDomainPage.ts b/src/hooks/pages/dashboard/useNewDomainPage.ts index 8ce95094..a8004706 100644 --- a/src/hooks/pages/dashboard/useNewDomainPage.ts +++ b/src/hooks/pages/dashboard/useNewDomainPage.ts @@ -27,7 +27,7 @@ export const defaultValues: NewDomainFormState = { export type DomainRefOptions = { label: string value: string - type: EntityType + type: AddDomainTarget } export type UseNewDomainPageReturn = { @@ -36,9 +36,8 @@ export type UseNewDomainPageReturn = { hasFunctions: boolean hasEntities: boolean nameCtrl: UseControllerReturn - programTypeCtrl: UseControllerReturn + targetCtrl: UseControllerReturn refCtrl: UseControllerReturn - ipfsRefCtrl: UseControllerReturn errors: FieldErrors handleSubmit: (e: FormEvent) => Promise } @@ -82,18 +81,12 @@ export function useNewDomainPage(): UseNewDomainPageReturn { name: 'name', }) - const programTypeCtrl = useController({ + const targetCtrl = useController({ control, - name: 'programType', + name: 'target', rules: { onChange(state) { setValue('ref', '') - - if (state.target.value === EntityType.Program) { - setValue('target', AddDomainTarget.Program) - } else if (state.target.value === EntityType.Instance) { - setValue('target', AddDomainTarget.Instance) - } }, }, }) @@ -103,17 +96,7 @@ export function useNewDomainPage(): UseNewDomainPageReturn { name: 'ref', }) - const ipfsRefCtrl = useController({ - control, - name: 'ref', - rules: { - onChange() { - setValue('target', AddDomainTarget.IPFS) - }, - }, - }) - - const entityType = programTypeCtrl.field.value + const entityType = targetCtrl.field.value const entities = useMemo(() => { const entities = !entityType @@ -147,25 +130,30 @@ export function useNewDomainPage(): UseNewDomainPageReturn { ) useEffect(() => { - if (entityType === EntityType.Instance && !hasInstances) { - setValue('programType', EntityType.Program) + if (entityType === EntityType.Instance && hasInstances) { + setValue('target', EntityType.Instance) } - if (entityType === EntityType.Program && !hasFunctions) { - setValue('programType', EntityType.Instance) + if (entityType === EntityType.Program && hasFunctions) { + setValue('target', EntityType.Program) } + }, [entityType, hasFunctions, hasInstances, setValue]) + const setTarget = (target) => { + setValue('target', target) + } + return { entities, hasInstances, hasFunctions, hasEntities, nameCtrl, - programTypeCtrl, + targetCtrl, refCtrl, - ipfsRefCtrl, errors, handleSubmit, + setTarget } }