Skip to content

Commit

Permalink
rebase + refactor domain type/target
Browse files Browse the repository at this point in the history
  • Loading branch information
aliel committed Jan 2, 2024
1 parent 7188eb5 commit 911229a
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 67 deletions.
25 changes: 17 additions & 8 deletions src/components/pages/dashboard/NewDomainPage/cmp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 (
<>
<Form onSubmit={handleSubmit} errors={errors}>
Expand Down Expand Up @@ -95,7 +104,7 @@ export default function NewDomain() {
<Tabs
align="left"
selected={tabId}
onTabChange={setTabId}
onTabChange={onTabChange}
tabs={[
{
id: 'compute',
Expand All @@ -112,8 +121,8 @@ export default function NewDomain() {
{tabId === 'compute' ? (
<NoisyContainer tw="z-10!">
<RadioGroup
{...programTypeCtrl.field}
{...programTypeCtrl.fieldState}
{...targetCtrl.field}
{...targetCtrl.fieldState}
required
label="Choose resource type"
direction="row"
Expand Down Expand Up @@ -154,8 +163,8 @@ export default function NewDomain() {
</p>
<NoisyContainer>
<TextInput
{...ipfsRefCtrl.field}
{...ipfsRefCtrl.fieldState}
{...refCtrl.field}
{...refCtrl.fieldState}
required
label="Link your custom domain to an Aleph Message ID"
placeholder="Paste your IPFS Aleph Message ID"
Expand Down
25 changes: 5 additions & 20 deletions src/domain/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,23 @@ import { domainSchema, domainsSchema } from '@/helpers/schemas/domain'
export { AddDomainTarget }

export type DomainAggregateItem = {
type: AddDomainTarget | EntityType.Program | EntityType.Instance
type: AddDomainTarget
message_id: string
programType?: EntityType.Instance | EntityType.Program
updated_at: string
}

export type DomainAggregate = Record<string, DomainAggregateItem | null>

export type AddDomain = {
name: string
target: AddDomainTarget | EntityType.Program | EntityType.Instance
programType: EntityType.Instance | EntityType.Program
target: AddDomainTarget
ref: string
}

export type Domain = Omit<AddDomain, 'programType'> & {
export type Domain = Omit<AddDomain, 'target'> & {
type: EntityType.Domain
id: string
confirmed?: boolean
programType?: EntityType.Instance | EntityType.Program
updated_at: string
}

Expand Down Expand Up @@ -81,7 +78,6 @@ export class DomainManager implements EntityManager<Domain, AddDomain> {
const content = {
message_id: domain.ref,
type: domain.target,
programType: domain.programType,
updated_at: new Date().toISOString(),
}

Expand Down Expand Up @@ -110,20 +106,14 @@ export class DomainManager implements EntityManager<Domain, AddDomain> {
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
Expand Down Expand Up @@ -241,11 +231,6 @@ export class DomainManager implements EntityManager<Domain, AddDomain> {
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
}
}
5 changes: 0 additions & 5 deletions src/helpers/schemas/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion src/helpers/schemas/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export const domainSchema = z.object({
AddDomainTarget.Program,
AddDomainTarget.Instance,
]),
programType: programTypeSchema,
ref: messageHashSchema,
})

Expand Down
8 changes: 3 additions & 5 deletions src/hooks/form/useAddDomains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -77,7 +75,7 @@ export type UseDomainsReturn = {
export function useAddDomains({
name = 'domains',
control,
entityType: programType,
entityType: target,
}: UseDomainsProps): UseDomainsReturn {
const domainsCtrl = useFieldArray({
control,
Expand All @@ -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,
Expand Down
44 changes: 16 additions & 28 deletions src/hooks/pages/dashboard/useNewDomainPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const defaultValues: NewDomainFormState = {
export type DomainRefOptions = {
label: string
value: string
type: EntityType
type: AddDomainTarget
}

export type UseNewDomainPageReturn = {
Expand All @@ -36,9 +36,8 @@ export type UseNewDomainPageReturn = {
hasFunctions: boolean
hasEntities: boolean
nameCtrl: UseControllerReturn<NewDomainFormState, 'name'>
programTypeCtrl: UseControllerReturn<NewDomainFormState, 'programType'>
targetCtrl: UseControllerReturn<NewDomainFormState, 'target'>
refCtrl: UseControllerReturn<NewDomainFormState, 'ref'>
ipfsRefCtrl: UseControllerReturn<NewDomainFormState, 'ref'>
errors: FieldErrors<NewDomainFormState>
handleSubmit: (e: FormEvent) => Promise<void>
}
Expand Down Expand Up @@ -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)
}
},
},
})
Expand All @@ -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
Expand Down Expand Up @@ -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
}
}

0 comments on commit 911229a

Please sign in to comment.