Skip to content

Commit

Permalink
Fix select field options update (twentyhq#5736)
Browse files Browse the repository at this point in the history
Update of select fields options was failing if we deleted an option that
was used for at least one row: former code would not update the value to
null but leave it to the no-longer-allowed value.
  • Loading branch information
ijreilly authored Jun 5, 2024
1 parent a483d19 commit 40b81ce
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useEffect } from 'react';
import { FormProvider, useForm } from 'react-hook-form';
import { useNavigate, useParams } from 'react-router-dom';
import { useApolloClient } from '@apollo/client';
import styled from '@emotion/styled';
import { zodResolver } from '@hookform/resolvers/zod';
import omit from 'lodash.omit';
Expand All @@ -16,6 +17,7 @@ import { FieldMetadataItem } from '@/object-metadata/types/FieldMetadataItem';
import { formatFieldMetadataItemInput } from '@/object-metadata/utils/formatFieldMetadataItemInput';
import { getFieldSlug } from '@/object-metadata/utils/getFieldSlug';
import { isLabelIdentifierField } from '@/object-metadata/utils/isLabelIdentifierField';
import { useFindManyRecordsQuery } from '@/object-record/hooks/useFindManyRecordsQuery';
import { SaveAndCancelButtons } from '@/settings/components/SaveAndCancelButtons/SaveAndCancelButtons';
import { SettingsHeaderContainer } from '@/settings/components/SettingsHeaderContainer';
import { SettingsPageContainer } from '@/settings/components/SettingsPageContainer';
Expand Down Expand Up @@ -73,6 +75,20 @@ export const SettingsObjectFieldEdit = () => {
const getRelationMetadata = useGetRelationMetadata();
const { updateOneFieldMetadataItem } = useUpdateOneFieldMetadataItem();

const apolloClient = useApolloClient();

const { findManyRecordsQuery } = useFindManyRecordsQuery({
objectNameSingular: activeObjectMetadataItem?.nameSingular || '',
});

const refetchRecords = async () => {
if (!activeObjectMetadataItem) return;
await apolloClient.query({
query: findManyRecordsQuery,
fetchPolicy: 'network-only',
});
};

const formConfig = useForm<SettingsDataModelFieldEditFormValues>({
mode: 'onTouched',
resolver: zodResolver(settingsFieldFormSchema),
Expand Down Expand Up @@ -135,6 +151,8 @@ export const SettingsObjectFieldEdit = () => {
}

navigate(`/settings/objects/${objectSlug}`);

refetchRecords();
} catch (error) {
enqueueSnackBar((error as Error).message, {
variant: SnackBarVariant.Error,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Injectable } from '@nestjs/common';

import { QueryRunner, TableColumn } from 'typeorm';
import { v4 } from 'uuid';
import { isDefined } from 'class-validator';

import {
WorkspaceMigrationColumnAlter,
Expand Down Expand Up @@ -117,10 +118,17 @@ export class WorkspaceMigrationEnumService {
private migrateEnumValue(
value: string,
renamedEnumValues?: WorkspaceMigrationRenamedEnum[],
allEnumValues?: string[],
) {
return (
renamedEnumValues?.find((enumVal) => enumVal?.from === value)?.to || value
);
if (renamedEnumValues?.find((enumVal) => enumVal?.from === value)?.to) {
return renamedEnumValues?.find((enumVal) => enumVal?.from === value)?.to;
}

if (allEnumValues?.includes(value)) {
return value;
}

return null;
}

private async migrateEnumValues(
Expand All @@ -147,11 +155,19 @@ export class WorkspaceMigrationEnumService {
.slice(1, -1)
.split(',')
.map((v: string) => v.trim())
.map((v: string) => this.migrateEnumValue(v, renamedEnumValues))
.filter((v: string) => enumValues.includes(v)),
.map((v: string) =>
this.migrateEnumValue(v, renamedEnumValues, enumValues),
)
.filter((v: string | null) => isDefined(v)),
);
} else if (typeof val === 'string') {
val = `'${this.migrateEnumValue(val, renamedEnumValues)}'`;
const migratedValue = this.migrateEnumValue(
val,
renamedEnumValues,
enumValues,
);

val = isDefined(migratedValue) ? `'${migratedValue}'` : null;
}

await queryRunner.query(`
Expand Down

0 comments on commit 40b81ce

Please sign in to comment.