Skip to content

Commit

Permalink
feat: correctly defined base inputLevels
Browse files Browse the repository at this point in the history
  • Loading branch information
g-saracca committed Jul 30, 2024
1 parent 68c858c commit 3dc04f8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 39 deletions.
40 changes: 11 additions & 29 deletions src/sections/create-collection/CreateCollection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { SeparationLine } from '../shared/layout/SeparationLine/SeparationLine'
import { RequiredFieldText } from '../shared/form/RequiredFieldText/RequiredFieldText'
import { PageNotFound } from '../page-not-found/PageNotFound'
import { CreateCollectionSkeleton } from './CreateCollectionSkeleton'
import { CollectionInputLevel } from '../../collection/domain/models/Collection'
import { useGetAllMetadataBlocksInfoByName } from './useGetAllMetadataBlocksInfoByName'
import { CollectionFormHelper } from './collection-form/CollectionFormHelper'

Expand All @@ -44,8 +43,8 @@ export function CreateCollection({
ownerCollectionId
)

// TODO:ME Quizas en modo edicion collection id no deberia ser sobre el owner sino sobre la collection en si, pero esta quizas se puede diferenciar por pagina.
// Es decir, en esta pagina create, esta bien obtener sobre el padre, en la pagina edit sobre el mismo collection.
// TODO:ME Maybe in edit mode collection id should not be on the owner but on the collection itself, but this can perhaps be differentiated by page.
// That is to say, in this create page, it is good to get on the parent, in the edit page on the same collection.
const { metadataBlocksNamesInfo, isLoading: isLoadingMetadataBlocksNamesInfo } =
useGetCollectionMetadataBlocksNamesInfo({
collectionId: ownerCollectionId,
Expand All @@ -55,10 +54,14 @@ export function CreateCollection({
const { allMetadataBlocksInfo, isLoading: isLoadingAllMetadataBlocksInfo } =
useGetAllMetadataBlocksInfoByName({ metadataBlockInfoRepository })

const baseInputLevels = useDeepCompareMemo(() => {
return CollectionFormHelper.getFormBaseInputLevels(allMetadataBlocksInfo)
const formBaseInputLevels: FormattedCollectionInputLevels = useDeepCompareMemo(() => {
return CollectionFormHelper.defineBaseInputLevels(allMetadataBlocksInfo)
}, [allMetadataBlocksInfo])

const formDefaultInputLevels: FormattedCollectionInputLevels = useDeepCompareMemo(() => {
return CollectionFormHelper.formatCollectiontInputLevels(collection?.inputLevels)
}, [collection?.inputLevels])

const defaultBlocksNames = useDeepCompareMemo(
() =>
metadataBlocksNamesInfo.reduce(
Expand Down Expand Up @@ -94,6 +97,7 @@ export function CreateCollection({
setIsLoading
])

// TODO:ME Instead show error of parent collection not found.
if (!isLoadingCollection && !collection) {
return <PageNotFound />
}
Expand All @@ -107,28 +111,6 @@ export function CreateCollection({
return <CreateCollectionSkeleton />
}

console.log({ baseInputLevels })

// TODO:ME Move to another place?
const transformInputLevels = (levels: CollectionInputLevel[]): FormattedCollectionInputLevels => {
const result: FormattedCollectionInputLevels = {}
levels.forEach((level) => {
const { datasetFieldName, include, required } = level
const replaceDotWithSlash = (str: string) => str.replace(/\./g, '/')
const normalizedFieldName = replaceDotWithSlash(datasetFieldName)

result[normalizedFieldName] = {
include,
optionalOrRequired: required ? 'required' : 'optional'
}
})
return result
}

const defaultInputLevels: FormattedCollectionInputLevels | undefined = collection.inputLevels
? transformInputLevels(collection.inputLevels)
: undefined

const formDefaultValues: CollectionFormData = {
hostCollection: collection.name,
name: user?.displayName ? `${user?.displayName} Collection` : '',
Expand All @@ -141,8 +123,8 @@ export function CreateCollection({
[USE_FIELDS_FROM_PARENT]: true,
[METADATA_BLOCKS_NAMES_GROUPER]: defaultBlocksNames,
inputLevels: {
...baseInputLevels,
...defaultInputLevels
...formBaseInputLevels,
...formDefaultInputLevels
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,6 @@ export const CollectionFormInputLevelOptions = {
export type CollectionFormInputLevelValue =
(typeof CollectionFormInputLevelOptions)[keyof typeof CollectionFormInputLevelOptions]

// export type FormattedCollectionInputLevels = {
// [key: string]: {
// include: boolean
// required: boolean
// }
// }

// On the submit function callback, type is CollectionType as type field is required and wont never be ""
export type CollectionFormValuesOnSubmit = Omit<CollectionFormData, 'type'> & {
type: CollectionType
Expand All @@ -95,9 +88,10 @@ export const CollectionForm = ({
defaultValues
})

const { formState, watch } = form
const { formState } = form

console.log(watch('inputLevels'))
console.log({ defaultValues })
// console.log(watch('inputLevels'))

const preventEnterSubmit = (e: React.KeyboardEvent<HTMLFormElement | HTMLButtonElement>) => {
// When pressing Enter, only submit the form if the user is focused on the submit button itself
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CollectionInputLevel } from '../../../collection/domain/models/Collection'
import { ReducedMetadataBlockInfo } from '../useGetAllMetadataBlocksInfoByName'
import { FormattedCollectionInputLevels } from './CollectionForm'

Expand All @@ -6,7 +7,7 @@ export class CollectionFormHelper {

public static replaceSlashWithDot = (str: string) => str.replace(/\//g, '.')

public static getFormBaseInputLevels(
public static defineBaseInputLevels(
allMetadataBlocksInfoReduced: ReducedMetadataBlockInfo[]
): FormattedCollectionInputLevels {
const fields: FormattedCollectionInputLevels = {}
Expand Down Expand Up @@ -38,4 +39,26 @@ export class CollectionFormHelper {
...childFields
}
}

public static formatCollectiontInputLevels(
collectionInputLevels: CollectionInputLevel[] | undefined
): FormattedCollectionInputLevels {
const result: FormattedCollectionInputLevels = {}

if (!collectionInputLevels) {
return result
}

collectionInputLevels.forEach((level) => {
const { datasetFieldName, include, required } = level
const replaceDotWithSlash = (str: string) => str.replace(/\./g, '/')
const normalizedFieldName = replaceDotWithSlash(datasetFieldName)

result[normalizedFieldName] = {
include,
optionalOrRequired: required ? 'required' : 'optional'
}
})
return result
}
}

0 comments on commit 3dc04f8

Please sign in to comment.