Skip to content

Commit

Permalink
fix remaining circular dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
AlessioGr committed Nov 19, 2024
1 parent 9494866 commit c98d392
Show file tree
Hide file tree
Showing 14 changed files with 187 additions and 205 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
"analyze-circular-dependencies-fields": "madge test/fields/config.ts --circular --warning",
"analyze-circular-dependencies-lexical": "madge packages/richtext-lexical/src/index.ts --circular --warning",
"analyze-circular-dependencies-lexical-client": "madge packages/richtext-lexical/src/exports/client/index.ts --circular --warning",
"analyze-circular-dependencies-lexical-migrate": "madge packages/richtext-lexical/src/exports/server/migrate.ts --circular --warning",
"analyze-circular-dependencies-mongodb": "madge packages/db-mongodb/src/index.ts --circular --warning",
"analyze-circular-dependencies-postgres": "madge packages/db-postgres/src/index.ts --circular --warning",
"analyze-circular-dependencies-drizzle": "madge packages/drizzle/src/index.ts --circular --warning",
"analyze-circular-dependencies-drizzle-postgres": "madge packages/drizzle/src/exports/postgres.ts --circular --warning",
"analyze-circular-dependencies-graphql": "madge packages/grapqhl/src/index.ts --circular --warning",
"analyze-circular-dependencies-fieldSchemasToFormState": "madge packages/ui/src/forms/fieldSchemasToFormState/index.tsx --circular --warning",
"analyze-circular-dependencies-buildFieldSchemaMap": "madge packages/ui/src/utilities/buildFieldSchemaMap/index.ts --circular --warning",
"bf": "pnpm run build:force",
"build": "pnpm run build:core",
"build:all": "turbo build",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use client'
import React from 'react'

import type { UnknownConvertedNodeData } from './index.js'

import './index.scss'

import type { UnknownConvertedNodeData } from './types.js'

type Props = {
data: UnknownConvertedNodeData
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import { addClassNamesToElement } from '@lexical/utils'
import { DecoratorNode } from 'lexical'
import * as React from 'react'

export type UnknownConvertedNodeData = {
nodeData: unknown
nodeType: string
}
import type { UnknownConvertedNodeData } from './types.js'

export type SerializedUnknownConvertedNode = Spread<
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type UnknownConvertedNodeData = {
nodeData: unknown
nodeType: string
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
'use client'
import React from 'react'

import type { UnknownConvertedNodeData } from './index.js'

import './index.scss'
import type { UnknownConvertedNodeData } from './types.js'

type Props = {
data: UnknownConvertedNodeData
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import { addClassNamesToElement } from '@lexical/utils'
import { DecoratorNode } from 'lexical'
import * as React from 'react'

export type UnknownConvertedNodeData = {
nodeData: unknown
nodeType: string
}
import type { UnknownConvertedNodeData } from './types.js'

export type SerializedUnknownConvertedNode = Spread<
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type UnknownConvertedNodeData = {
nodeData: unknown
nodeType: string
}
135 changes: 134 additions & 1 deletion packages/ui/src/forms/fieldSchemasToFormState/addFieldStatePromise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {
import type { RenderFieldMethod } from './types.js'

import { getFilterOptionsQuery } from './getFilterOptionsQuery.js'
import { iterateFields } from './iterateFields.js'

const ObjectId = (ObjectIdImport.default ||
ObjectIdImport) as unknown as typeof ObjectIdImport.default
Expand Down Expand Up @@ -736,3 +735,137 @@ export const addFieldStatePromise = async (args: AddFieldStatePromiseArgs): Prom
})
}
}

type IterateFieldsArgs = {
addErrorPathToParent: (fieldPath: string) => void
/**
* if any parents is localized, then the field is localized. @default false
*/
anyParentLocalized?: boolean
collectionSlug?: string
data: Data
fields: Field[]
fieldSchemaMap: FieldSchemaMap
filter?: (args: AddFieldStatePromiseArgs) => boolean
/**
* Force the value of fields like arrays or blocks to be the full value instead of the length @default false
*/
forceFullValue?: boolean
fullData: Data
id?: number | string
/**
* Whether the field schema should be included in the state. @default false
*/
includeSchema?: boolean
/**
* Whether to omit parent fields in the state. @default false
*/
omitParents?: boolean
/**
* operation is only needed for validation
*/
operation: 'create' | 'update'
parentIndexPath: string
parentPassesCondition?: boolean
parentPath: string
parentSchemaPath: string
permissions:
| {
[fieldName: string]: SanitizedFieldPermissions
}
| null
| SanitizedFieldPermissions
preferences?: DocumentPreferences
previousFormState: FormState
renderAllFields: boolean
renderFieldFn: RenderFieldMethod
req: PayloadRequest
/**
* Whether to skip checking the field's condition. @default false
*/
skipConditionChecks?: boolean
/**
* Whether to skip validating the field. @default false
*/
skipValidation?: boolean
state?: FormStateWithoutComponents
}

/**
* Flattens the fields schema and fields data
*/
export const iterateFields = async ({
id,
addErrorPathToParent: addErrorPathToParentArg,
anyParentLocalized = false,
collectionSlug,
data,
fields,
fieldSchemaMap,
filter,
forceFullValue = false,
fullData,
includeSchema = false,
omitParents = false,
operation,
parentIndexPath,
parentPassesCondition = true,
parentPath,
parentSchemaPath,
permissions,
preferences,
previousFormState,
renderAllFields,
renderFieldFn: renderFieldFn,
req,
skipConditionChecks = false,
skipValidation = false,
state = {},
}: IterateFieldsArgs): Promise<void> => {
const promises = []

fields.forEach((field, fieldIndex) => {
let passesCondition = true
if (!skipConditionChecks) {
passesCondition = Boolean(
(field?.admin?.condition
? Boolean(field.admin.condition(fullData || {}, data || {}, { user: req.user }))
: true) && parentPassesCondition,
)
}

promises.push(
addFieldStatePromise({
id,
addErrorPathToParent: addErrorPathToParentArg,
anyParentLocalized,
collectionSlug,
data,
field,
fieldIndex,
fieldSchemaMap,
filter,
forceFullValue,
fullData,
includeSchema,
omitParents,
operation,
parentIndexPath,
parentPath,
parentSchemaPath,
passesCondition,
permissions,
preferences,
previousFormState,
renderAllFields,
renderFieldFn,
req,
skipConditionChecks,
skipValidation,
state,
}),
)
})

await Promise.all(promises)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Data, Field as FieldSchema, User } from 'payload'

import { iterateFields } from './iterateFields.js'
import { iterateFields } from './promise.js'

type Args = {
data: Data
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import type { Data, Field, TabAsField, User } from 'payload'
import { getDefaultValue } from 'payload'
import { fieldAffectsData, tabHasName } from 'payload/shared'

import { iterateFields } from './iterateFields.js'

type Args<T> = {
data: T
field: Field | TabAsField
Expand Down Expand Up @@ -168,3 +166,38 @@ export const defaultValuePromise = async <T>({
}
}
}

type IterateFieldsArgs<T> = {
data: T
fields: (Field | TabAsField)[]
id?: number | string
locale: string | undefined
siblingData: Data
user: User
}

export const iterateFields = async <T>({
id,
data,
fields,
locale,
siblingData,
user,
}: IterateFieldsArgs<T>): Promise<void> => {
const promises = []

fields.forEach((field) => {
promises.push(
defaultValuePromise({
id,
data,
field,
locale,
siblingData,
user,
}),
)
})

await Promise.all(promises)
}
2 changes: 1 addition & 1 deletion packages/ui/src/forms/fieldSchemasToFormState/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import type {

import type { RenderFieldMethod } from './types.js'

import { iterateFields } from './addFieldStatePromise.js'
import { calculateDefaultValues } from './calculateDefaultValues/index.js'
import { iterateFields } from './iterateFields.js'

type Args = {
collectionSlug?: string
Expand Down
Loading

0 comments on commit c98d392

Please sign in to comment.