Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default address country 🗺️ & Phone prefix ☎️ #8614

Merged
merged 31 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
6291ca5
Adding the DefaultCountry in the backend
guillim Nov 19, 2024
1352706
adding the frontend settings option to select countries by default
guillim Nov 19, 2024
03e0459
Merge branch 'main' of https://github.com/twentyhq/twenty into featur…
guillim Nov 19, 2024
47c79be
new design
guillim Nov 19, 2024
a454623
Default country in the input
guillim Nov 20, 2024
5ab4f71
fix
guillim Nov 20, 2024
c89a07a
lint
guillim Nov 20, 2024
a7d472e
Merge branch 'main' of https://github.com/twentyhq/twenty into featur…
guillim Nov 20, 2024
a7739d1
lint
guillim Nov 20, 2024
74691d3
moving towards defaultValue
guillim Nov 21, 2024
0420d3b
Merge branch 'main' of https://github.com/twentyhq/twenty into featur…
guillim Nov 25, 2024
18ccbd3
bugfix on quoting
guillim Nov 25, 2024
c6d01fc
request from thomas 2d
guillim Nov 25, 2024
47825a5
Address Field null in preview
guillim Nov 25, 2024
eaee2a0
end of coutrues
guillim Nov 26, 2024
c47f928
better typing
guillim Nov 26, 2024
dab6cf9
Charles feedback
guillim Nov 26, 2024
85fa685
Merge branch 'main' of https://github.com/twentyhq/twenty into featur…
guillim Nov 26, 2024
04d75ed
most fixes from charles feedback
guillim Nov 27, 2024
7ede48b
removing useless fct
guillim Nov 27, 2024
dc6f3d8
console log removal
guillim Nov 27, 2024
129223d
Fix typing
charlesBochet Nov 27, 2024
0d51f4c
Fix
charlesBochet Nov 27, 2024
6ba2fdf
Merge branch 'main' of https://github.com/twentyhq/twenty into featur…
guillim Nov 28, 2024
dc2a060
slamost done
guillim Nov 28, 2024
8b1f027
Merge branch 'main' of https://github.com/twentyhq/twenty into featur…
guillim Dec 2, 2024
daa8c1b
good phone default country
guillim Dec 2, 2024
8e006fb
fix display numbers
guillim Dec 2, 2024
c55d567
Merge branch 'main' of https://github.com/twentyhq/twenty into featur…
guillim Dec 2, 2024
a77b569
better typing 1
guillim Dec 2, 2024
cfd7eed
better typing usecountries
guillim Dec 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { FieldMetadataItem } from '@/object-metadata/types/FieldMetadataItem';
import { computeMetadataDefaultValue } from '~/pages/settings/data-model/utils/compute-metadata-defaultValue-utils';
import { computeMetadataNameFromLabel } from '~/pages/settings/data-model/utils/compute-metadata-name-from-label.utils';

export const formatFieldMetadataItemInput = (
Expand All @@ -18,7 +19,7 @@ export const formatFieldMetadataItemInput = (
const label = input.label?.trim();

return {
defaultValue: input.defaultValue,
defaultValue: computeMetadataDefaultValue(input.defaultValue),
guillim marked this conversation as resolved.
Show resolved Hide resolved
description: input.description?.trim() ?? null,
icon: input.icon,
label,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ export const useNumberField = () => {

const persistNumberField = (newValue: string) => {
if (fieldDefinition?.metadata?.settings?.type === 'percentage') {
newValue = newValue.replaceAll('%', '');
if (!canBeCastAsNumberOrNull(newValue)) {
const newValueEscaped = newValue.replaceAll('%', '');
if (!canBeCastAsNumberOrNull(newValueEscaped)) {
return;
}
const castedValue = castAsNumberOrNull(newValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import { CurrencyCode } from './CurrencyCode';
export type FieldUuidMetadata = {
objectMetadataNameSingular?: string;
fieldName: string;
settings?: Record<string, never>;
settings?: null;
};

export type FieldBooleanMetadata = {
objectMetadataNameSingular?: string;
fieldName: string;
settings?: Record<string, never>;
settings?: null;
};

export type FieldTextMetadata = {
Expand Down Expand Up @@ -61,80 +61,80 @@ export type FieldLinkMetadata = {
objectMetadataNameSingular?: string;
placeHolder: string;
fieldName: string;
settings?: Record<string, never>;
settings?: null;
};

export type FieldLinksMetadata = {
objectMetadataNameSingular?: string;
fieldName: string;
settings?: Record<string, never>;
settings?: null;
};

export type FieldCurrencyMetadata = {
objectMetadataNameSingular?: string;
fieldName: string;
placeHolder: string;
isPositive?: boolean;
settings?: Record<string, never>;
settings?: null;
};

export type FieldFullNameMetadata = {
objectMetadataNameSingular?: string;
placeHolder: string;
fieldName: string;
settings?: Record<string, never>;
settings?: null;
};

export type FieldEmailMetadata = {
objectMetadataNameSingular?: string;
placeHolder: string;
fieldName: string;
settings?: Record<string, never>;
settings?: null;
};

export type FieldEmailsMetadata = {
objectMetadataNameSingular?: string;
fieldName: string;
settings?: Record<string, never>;
settings?: null;
};

export type FieldPhoneMetadata = {
objectMetadataNameSingular?: string;
placeHolder: string;
fieldName: string;
settings?: Record<string, never>;
settings?: null;
};

export type FieldRatingMetadata = {
objectMetadataNameSingular?: string;
fieldName: string;
settings?: Record<string, never>;
settings?: null;
};

export type FieldAddressMetadata = {
objectMetadataNameSingular?: string;
placeHolder: string;
fieldName: string;
settings?: Record<string, never>;
settings?: null;
};

export type FieldRawJsonMetadata = {
objectMetadataNameSingular?: string;
fieldName: string;
placeHolder: string;
settings?: Record<string, never>;
settings?: null;
};

export type FieldRichTextMetadata = {
objectMetadataNameSingular?: string;
fieldName: string;
settings?: Record<string, never>;
settings?: null;
};

export type FieldPositionMetadata = {
objectMetadataNameSingular?: string;
fieldName: string;
settings?: Record<string, never>;
settings?: null;
};

export type FieldRelationMetadata = {
Expand All @@ -146,47 +146,47 @@ export type FieldRelationMetadata = {
relationType?: RelationDefinitionType;
targetFieldMetadataName?: string;
useEditButton?: boolean;
settings?: Record<string, never>;
settings?: null;
};

export type FieldSelectMetadata = {
objectMetadataNameSingular?: string;
fieldName: string;
options: { label: string; color: ThemeColor; value: string }[];
isNullable: boolean;
settings?: Record<string, never>;
settings?: null;
};

export type FieldMultiSelectMetadata = {
objectMetadataNameSingular?: string;
fieldName: string;
options: { label: string; color: ThemeColor; value: string }[];
settings?: Record<string, never>;
settings?: null;
};

export type FieldActorMetadata = {
objectMetadataNameSingular?: string;
fieldName: string;
settings?: Record<string, never>;
settings?: null;
};

export type FieldArrayMetadata = {
objectMetadataNameSingular?: string;
fieldName: string;
values: { label: string; value: string }[];
settings?: Record<string, never>;
settings?: null;
};

export type FieldPhonesMetadata = {
objectMetadataNameSingular?: string;
fieldName: string;
settings?: Record<string, never>;
settings?: null;
};

export type FieldTsVectorMetadata = {
objectMetadataNameSingular?: string;
fieldName: string;
settings?: Record<string, never>;
settings?: null;
};

export type FieldMetadata =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { z } from 'zod';

import { FieldAddressValue } from '../FieldMetadata';

const addressSchema = z.object({
export const addressSchema = z.object({
addressStreet1: z.string(),
addressStreet2: z.string().nullable(),
addressCity: z.string().nullable(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FieldDefinition } from '@/object-record/record-field/types/FieldDefinition';
import { FieldInputDraftValue } from '@/object-record/record-field/types/FieldInputDraftValue';
import { FieldMetadata } from '@/object-record/record-field/types/FieldMetadata';
import { isFieldAddress } from '@/object-record/record-field/types/guards/isFieldAddress';
import { isFieldCurrency } from '@/object-record/record-field/types/guards/isFieldCurrency';
import { isFieldCurrencyValue } from '@/object-record/record-field/types/guards/isFieldCurrencyValue';
import { isFieldNumber } from '@/object-record/record-field/types/guards/isFieldNumber';
Expand Down Expand Up @@ -42,6 +43,20 @@ export const computeDraftValueFromFieldValue = <FieldValue>({
} as unknown as FieldInputDraftValue<FieldValue>;
}

if (isFieldAddress(fieldDefinition)) {
guillim marked this conversation as resolved.
Show resolved Hide resolved
if (
isFieldValueEmpty({ fieldValue, fieldDefinition }) &&
!!fieldDefinition?.defaultValue?.addressCountry
) {
return {
...fieldValue,
addressCountry: fieldDefinition?.defaultValue?.addressCountry,
} as unknown as FieldInputDraftValue<FieldValue>;
}

return fieldValue as FieldInputDraftValue<FieldValue>;
}

if (
isFieldNumber(fieldDefinition) &&
isFieldNumberValue(fieldValue) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export const SETTINGS_COMPOSITE_FIELD_TYPE_CONFIGS = {
},
exampleValue: {
addressStreet1: '456 Oak Street',
addressStreet2: 'Unit 3B',
addressStreet2: '',
addressCity: 'Springfield',
addressState: 'California',
addressCountry: 'United States',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { Controller, useFormContext } from 'react-hook-form';

import { FieldMetadataItem } from '@/object-metadata/types/FieldMetadataItem';
import { addressSchema as addressFieldDefaultValueSchema } from '@/object-record/record-field/types/guards/isFieldAddressValue';
import { SettingsOptionCardContentSelect } from '@/settings/components/SettingsOptions/SettingsOptionCardContentSelect';
import { useCountries } from '@/ui/input/components/internal/hooks/useCountries';
import { IconMap } from 'twenty-ui';
import { z } from 'zod';
import { stripSimpleQuotesFromStringRecursive } from '~/utils/string/stripSimpleQuotesFromString';

type SettingsDataModelFieldAddressFormProps = {
disabled?: boolean;
defaultCountry?: string;
fieldMetadataItem: Pick<
FieldMetadataItem,
'icon' | 'label' | 'type' | 'defaultValue' | 'settings'
>;
};

export const settingsDataModelFieldAddressFormSchema = z.object({
defaultValue: addressFieldDefaultValueSchema,
});

export type SettingsDataModelFieldTextFormValues = z.infer<
typeof settingsDataModelFieldAddressFormSchema
>;

export const SettingsDataModelFieldAddressForm = ({
disabled,
fieldMetadataItem,
}: SettingsDataModelFieldAddressFormProps) => {
const { control } = useFormContext<SettingsDataModelFieldTextFormValues>();
const countries = useCountries()
.sort((a, b) => a.countryName.localeCompare(b.countryName))
.map((country) => ({
label: country.countryName,
value: country.countryName,
}));
countries.unshift({ label: 'No country', value: '' });
const defaultValueInstance = {
addressStreet1: '',
addressStreet2: null,
addressCity: null,
addressState: null,
addressPostcode: null,
addressCountry: null,
addressLat: null,
addressLng: null,
};
const fieldMetadataItemDefaultValue = fieldMetadataItem?.defaultValue
? stripSimpleQuotesFromStringRecursive(fieldMetadataItem?.defaultValue)
: fieldMetadataItem?.defaultValue;

return (
<Controller
name="defaultValue"
defaultValue={{
...defaultValueInstance,
...fieldMetadataItemDefaultValue,
}}
control={control}
render={({ field: { onChange, value } }) => {
const defaultCountry = value?.addressCountry || '';
return (
<>
guillim marked this conversation as resolved.
Show resolved Hide resolved
<SettingsOptionCardContentSelect
Icon={IconMap}
dropdownId="selectDefaultCountry"
title="Default Country"
description="The default country for new addresses"
value={defaultCountry}
onChange={(newCountry) =>
onChange({ ...value, addressCountry: newCountry })
}
disabled={disabled}
options={countries}
fullWidth={true}
/>
</>
);
}}
/>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import styled from '@emotion/styled';

import { FieldMetadataItem } from '@/object-metadata/types/FieldMetadataItem';

import { SettingsDataModelPreviewFormCard } from '@/settings/data-model/components/SettingsDataModelPreviewFormCard';
import { SettingsDataModelFieldAddressForm } from '@/settings/data-model/fields/forms/address/components/SettingsDataModelFieldAddressForm';
import {
SettingsDataModelFieldPreviewCard,
SettingsDataModelFieldPreviewCardProps,
} from '@/settings/data-model/fields/preview/components/SettingsDataModelFieldPreviewCard';

type SettingsDataModelFieldAddressSettingsFormCardProps = {
disabled?: boolean;
fieldMetadataItem: Pick<
FieldMetadataItem,
'icon' | 'label' | 'type' | 'defaultValue'
>;
} & Pick<SettingsDataModelFieldPreviewCardProps, 'objectMetadataItem'>;

const StyledFieldPreviewCard = styled(SettingsDataModelFieldPreviewCard)`
flex: 1 1 100%;
`;

export const SettingsDataModelFieldAddressSettingsFormCard = ({
disabled,
fieldMetadataItem,
objectMetadataItem,
}: SettingsDataModelFieldAddressSettingsFormCardProps) => {
return (
<SettingsDataModelPreviewFormCard
preview={
<StyledFieldPreviewCard
fieldMetadataItem={fieldMetadataItem}
objectMetadataItem={objectMetadataItem}
/>
}
form={
<SettingsDataModelFieldAddressForm
disabled={disabled}
fieldMetadataItem={fieldMetadataItem}
/>
}
/>
);
};
Loading
Loading