Skip to content

Commit

Permalink
Solve the issue of creating a Metadata field without entities
Browse files Browse the repository at this point in the history
  • Loading branch information
novakzaballa committed Jun 5, 2024
1 parent f15418e commit 18ae518
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 57 deletions.
135 changes: 79 additions & 56 deletions frontend/web/components/modals/CreateMetadataField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
MetadataModelField,
isRequiredFor,
} from 'common/types/responses'
import ErrorMessage from 'components/ErrorMessage'

type CreateMetadataFieldType = {
id?: string
Expand Down Expand Up @@ -76,8 +77,10 @@ const CreateMetadataField: FC<CreateMetadataFieldType> = ({
const { data: supportedContentTypes } = useGetSupportedContentTypeQuery({
organisation_id: `${organisationId}`,
})
const [createMetadataField, { isLoading: creating, isSuccess: created }] =
useCreateMetadataFieldMutation()
const [
createMetadataField,
{ error: errorCreating, isLoading: creating, isSuccess: created },
] = useCreateMetadataFieldMutation()
const [updateMetadataField, { isLoading: updating, isSuccess: updated }] =
useUpdateMetadataFieldMutation()

Expand Down Expand Up @@ -167,31 +170,47 @@ const CreateMetadataField: FC<CreateMetadataFieldType> = ({
},
id: id!,
}).then(() => {
Promise.all(
metadataUpdatedSelectList?.map(
async (m: metadataFieldUpdatedSelectListType) => {
if (metadataFieldSelectList.length) {
Promise.all(
metadataFieldSelectList.map(async (m) => {
const query = generateDataQuery(
m.content_type,
m.field,
m.is_required_for,
m.id,
m.new,
m.value,
parseInt(id!),
!!m?.isRequired,
0,
true,
)
if (!m.removed && !m.new) {
await updateMetadataModelField(query)
} else if (m.removed) {
await deleteMetadataModelField({
id: m.id,
organisation_id: organisationId,
})
} else if (m.new) {
const newQuery = { ...query }
delete newQuery.id
await createMetadataModelField(newQuery)
}
},
),
)
await createMetadataModelField(query)
}),
)
}
if (metadataUpdatedSelectList.length) {
Promise.all(
metadataUpdatedSelectList?.map(
async (m: metadataFieldUpdatedSelectListType) => {
const query = generateDataQuery(
m.content_type,
m.field,
m.is_required_for,
m.id,
m.new,
)
if (!m.removed && !m.new) {
await updateMetadataModelField(query)
} else if (m.removed) {
await deleteMetadataModelField({
id: m.id,
organisation_id: organisationId,
})
} else if (m.new) {
const newQuery = { ...query }
delete newQuery.id
await createMetadataModelField(newQuery)
}
},
),
)
}
closeModal()
})
} else {
Expand Down Expand Up @@ -272,50 +291,54 @@ const CreateMetadataField: FC<CreateMetadataFieldType> = ({
if (isEdit) {
const newMetadataFieldArray: metadataFieldUpdatedSelectListType[] =
[]
if (!metadataModelFieldList?.length) {
setMetadataFieldSelectList(m)
} else {
metadataModelFieldList?.forEach((item1) => {
const match = m.find(
(item2) => item2.value === item1.content_type.toString(),
)

metadataModelFieldList?.forEach((item1) => {
const match = m.find(
(item2) => item2.value === item1.content_type.toString(),
)

if (match) {
const isRequiredLength = !!item1.is_required_for.length
const isRequired = match.isRequired
if (isRequiredLength !== isRequired) {
if (match) {
const isRequiredLength = !!item1.is_required_for.length
const isRequired = match.isRequired
if (isRequiredLength !== isRequired) {
newMetadataFieldArray.push({
...item1,
is_required_for: isRequired,
})
}
} else {
newMetadataFieldArray.push({
...item1,
is_required_for: isRequired,
new: false,
removed: true,
})
}
} else {
newMetadataFieldArray.push({
...item1,
new: false,
removed: true,
m.forEach((item) => {
const match = metadataModelFieldList.find(
(item2) => item2.content_type.toString() === item.value,
)
if (!match) {
newMetadataFieldArray.push({
...item1,
content_type: item.value,
is_required_for: m?.isRequired,
new: true,
removed: false,
})
}
})
}
m.forEach((item) => {
const match = metadataModelFieldList.find(
(item2) => item2.content_type.toString() === item.value,
)
if (!match) {
newMetadataFieldArray.push({
...item1,
content_type: item.value,
is_required_for: m?.isRequired,
new: true,
removed: false,
})
}
})
})
setMetadataFieldUpdatedSelectList(newMetadataFieldArray)
setMetadataFieldUpdatedSelectList(newMetadataFieldArray)
}
} else {
setMetadataFieldSelectList(m)
}
}}
metadataModelFieldList={metadataModelFieldList!}
/>
{errorCreating && <ErrorMessage error={errorCreating} />}
<Button
disabled={!name || !typeValue || !metadataFieldSelectList}
onClick={save}
Expand Down
1 change: 0 additions & 1 deletion frontend/web/components/pages/EnvironmentSettingsPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import { enableFeatureVersioning } from 'common/services/useEnableFeatureVersion
import AddMetadataToEntity from 'components/metadata/AddMetadataToEntity'
import { getSupportedContentType } from 'common/services/useSupportedContentType'
import EnvironmentVersioningListener from 'components/EnvironmentVersioningListener'
import MetadataTitle from 'components/metadata/MetadataTitle'

const showDisabledFlagOptions = [
{ label: 'Inherit from Project', value: null },
Expand Down

0 comments on commit 18ae518

Please sign in to comment.