Files
twenty/packages/twenty-front/src/modules/settings/profile/components/NameFields.tsx
Hinson Chan e1d0b26cf9 5180 - does not call debounced update for invalid names (#5181)
fix: #5180 

Previously, clearing your name would kick you to the profile creation
page.


https://github.com/twentyhq/twenty/assets/68029599/8c0087da-6b03-4b6e-b202-eabe8ebcee18


Fixed so it checks your name is valid before calling the debounced
update


https://github.com/twentyhq/twenty/assets/68029599/4bc71d8f-e4f4-49ae-9cb8-497bd971be94

---------

Co-authored-by: Weiko <corentin@twenty.com>
2024-04-26 15:23:03 +02:00

129 lines
3.2 KiB
TypeScript

import { useEffect, useState } from 'react';
import styled from '@emotion/styled';
import { useRecoilState, useRecoilValue } from 'recoil';
import { useDebouncedCallback } from 'use-debounce';
import { currentUserState } from '@/auth/states/currentUserState';
import { currentWorkspaceMemberState } from '@/auth/states/currentWorkspaceMemberState';
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
import { useUpdateOneRecord } from '@/object-record/hooks/useUpdateOneRecord';
import { TextInput } from '@/ui/input/components/TextInput';
import { logError } from '~/utils/logError';
const StyledComboInputContainer = styled.div`
display: flex;
flex-direction: row;
> * + * {
margin-left: ${({ theme }) => theme.spacing(4)};
}
`;
type NameFieldsProps = {
autoSave?: boolean;
onFirstNameUpdate?: (firstName: string) => void;
onLastNameUpdate?: (lastName: string) => void;
};
export const NameFields = ({
autoSave = true,
onFirstNameUpdate,
onLastNameUpdate,
}: NameFieldsProps) => {
const currentUser = useRecoilValue(currentUserState);
const [currentWorkspaceMember, setCurrentWorkspaceMember] = useRecoilState(
currentWorkspaceMemberState,
);
const [firstName, setFirstName] = useState(
currentWorkspaceMember?.name?.firstName ?? '',
);
const [lastName, setLastName] = useState(
currentWorkspaceMember?.name?.lastName ?? '',
);
const { updateOneRecord } = useUpdateOneRecord({
objectNameSingular: CoreObjectNameSingular.WorkspaceMember,
});
// TODO: Enhance this with react-web-hook-form (https://www.react-hook-form.com)
const debouncedUpdate = useDebouncedCallback(async () => {
onFirstNameUpdate?.(firstName);
onLastNameUpdate?.(lastName);
try {
if (!currentWorkspaceMember?.id) {
throw new Error('User is not logged in');
}
if (autoSave) {
await updateOneRecord({
idToUpdate: currentWorkspaceMember?.id,
updateOneRecordInput: {
name: {
firstName: firstName,
lastName: lastName,
},
},
});
setCurrentWorkspaceMember({
...currentWorkspaceMember,
name: {
firstName,
lastName,
},
});
}
} catch (error) {
logError(error);
}
}, 500);
useEffect(() => {
if (!currentWorkspaceMember) {
return;
}
const { firstName: currentFirstName, lastName: currentLastName } =
currentWorkspaceMember.name;
if (
(currentFirstName !== firstName || currentLastName !== lastName) &&
firstName !== '' &&
lastName !== ''
) {
debouncedUpdate();
}
return () => {
debouncedUpdate.cancel();
};
}, [
firstName,
lastName,
currentUser,
debouncedUpdate,
autoSave,
currentWorkspaceMember,
]);
return (
<StyledComboInputContainer>
<TextInput
label="First Name"
value={firstName}
onChange={setFirstName}
placeholder="Tim"
fullWidth
/>
<TextInput
label="Last Name"
value={lastName}
onChange={setLastName}
placeholder="Cook"
fullWidth
/>
</StyledComboInputContainer>
);
};