2472 v2 settings workspace module (#2532)

* update findOneWorkspaceMember

* profile picture upload is working

* first name and last name working

* support almost working

* remove picture working

* removed unused code

* remove console logs and fix allowImpersonation in FIND_ONE_WORKSPACE_MEMBER_V2

* use useUpdateOneObjectRecord
This commit is contained in:
bosiraphael 2023-11-16 11:59:13 +01:00 committed by GitHub
parent 96661b5f56
commit bee986749d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 89 additions and 65 deletions

View File

@ -33,16 +33,12 @@ export const useMapFieldMetadataToGraphQLQuery = () => {
fieldType === 'RELATION' &&
field.toRelationMetadata?.relationType === 'ONE_TO_MANY'
) {
console.log({ objectMetadataItems, field });
const relationMetadataItem = objectMetadataItems.find(
(objectMetadataItem) =>
objectMetadataItem.id ===
(field.toRelationMetadata as any)?.fromObjectMetadata?.id,
);
console.log({ relationMetadataItem });
return `${field.name}
{
id

View File

@ -8,6 +8,10 @@ export const FIND_ONE_WORKSPACE_MEMBER_V2 = gql`
id
firstName
lastName
colorScheme
avatarUrl
locale
allowImpersonation
}
}
}

View File

@ -16,8 +16,6 @@ export const useUpdateOneObjectRecord = ({
objectNameSingular,
});
console.log('update one object');
// TODO: type this with a minimal type at least with Record<string, any>
const [mutate] = useMutation(updateOneMutation);

View File

@ -1,14 +1,12 @@
import { useEffect, useState } from 'react';
import { getOperationName } from '@apollo/client/utilities';
import styled from '@emotion/styled';
import debounce from 'lodash.debounce';
import { useRecoilValue } from 'recoil';
import { useRecoilState, useRecoilValue } from 'recoil';
import { currentUserState } from '@/auth/states/currentUserState';
import { currentWorkspaceMemberState } from '@/auth/states/currentWorkspaceMemberState';
import { useUpdateOneObjectRecord } from '@/object-record/hooks/useUpdateOneObjectRecord';
import { TextInput } from '@/ui/input/components/TextInput';
import { GET_CURRENT_USER } from '@/users/graphql/queries/getCurrentUser';
import { useUpdateUserMutation } from '~/generated/graphql';
import { logError } from '~/utils/logError';
const StyledComboInputContainer = styled.div`
@ -31,7 +29,9 @@ export const NameFields = ({
onLastNameUpdate,
}: NameFieldsProps) => {
const currentUser = useRecoilValue(currentUserState);
const currentWorkspaceMember = useRecoilValue(currentWorkspaceMemberState);
const [currentWorkspaceMember, setCurrentWorkspaceMember] = useRecoilState(
currentWorkspaceMemberState,
);
const [firstName, setFirstName] = useState(
currentWorkspaceMember?.firstName ?? '',
@ -40,7 +40,10 @@ export const NameFields = ({
currentWorkspaceMember?.lastName ?? '',
);
const [updateUser] = useUpdateUserMutation();
const { updateOneObject, objectNotFoundInMetadata } =
useUpdateOneObjectRecord({
objectNameSingular: 'workspaceMemberV2',
});
// TODO: Enhance this with react-web-hook-form (https://www.react-hook-form.com)
const debouncedUpdate = debounce(async () => {
@ -52,22 +55,20 @@ export const NameFields = ({
}
try {
if (autoSave) {
const { data, errors } = await updateUser({
variables: {
where: {
id: currentUser?.id,
},
data: {
firstName,
lastName,
},
if (!updateOneObject || objectNotFoundInMetadata) {
return;
}
await updateOneObject({
idToUpdate: currentWorkspaceMember?.id ?? '',
input: {
firstName,
lastName,
},
refetchQueries: [getOperationName(GET_CURRENT_USER) ?? ''],
});
if (errors || !data?.updateUser) {
throw errors;
}
setCurrentWorkspaceMember(
(current) => ({ ...current, firstName, lastName } as any),
);
}
} catch (error) {
logError(error);

View File

@ -1,28 +1,31 @@
import { useState } from 'react';
import { getOperationName } from '@apollo/client/utilities';
import { useRecoilState, useRecoilValue } from 'recoil';
import { useRecoilState } from 'recoil';
import { currentUserState } from '@/auth/states/currentUserState';
import { currentWorkspaceMemberState } from '@/auth/states/currentWorkspaceMemberState';
import { useUpdateOneObjectRecord } from '@/object-record/hooks/useUpdateOneObjectRecord';
import { ImageInput } from '@/ui/input/components/ImageInput';
import { GET_CURRENT_USER } from '@/users/graphql/queries/getCurrentUser';
import { getImageAbsoluteURIOrBase64 } from '@/users/utils/getProfilePictureAbsoluteURI';
import {
useRemoveProfilePictureMutation,
useUploadProfilePictureMutation,
} from '~/generated/graphql';
import { useUploadProfilePictureMutation } from '~/generated/graphql';
export const ProfilePictureUploader = () => {
const [uploadPicture, { loading: isUploading }] =
useUploadProfilePictureMutation();
const [removePicture] = useRemoveProfilePictureMutation();
const [currentUser] = useRecoilState(currentUserState);
const currentWorkspaceMember = useRecoilValue(currentWorkspaceMemberState);
const [currentWorkspaceMember, setCurrentWorkspaceMember] = useRecoilState(
currentWorkspaceMemberState,
);
const [uploadController, setUploadController] =
useState<AbortController | null>(null);
const [errorMessage, setErrorMessage] = useState<string | null>(null);
const { updateOneObject, objectNotFoundInMetadata } =
useUpdateOneObjectRecord({
objectNameSingular: 'workspaceMemberV2',
});
const handleUpload = async (file: File) => {
if (!file) {
return;
@ -46,6 +49,26 @@ export const ProfilePictureUploader = () => {
setUploadController(null);
setErrorMessage(null);
const avatarUrl = result?.data?.uploadProfilePicture;
if (!avatarUrl) {
return;
}
if (!updateOneObject || objectNotFoundInMetadata) {
return;
}
await updateOneObject({
idToUpdate: currentWorkspaceMember?.id ?? '',
input: {
avatarUrl,
},
});
setCurrentWorkspaceMember(
(current) => ({ ...current, avatarUrl } as any),
);
return result;
} catch (error) {
setErrorMessage('An error occured while uploading the picture.');
@ -60,14 +83,19 @@ export const ProfilePictureUploader = () => {
};
const handleRemove = async () => {
await removePicture({
variables: {
where: {
id: currentUser?.id,
},
if (!updateOneObject || objectNotFoundInMetadata) {
return;
}
await updateOneObject({
idToUpdate: currentWorkspaceMember?.id ?? '',
input: {
avatarUrl: null,
},
refetchQueries: [getOperationName(GET_CURRENT_USER) ?? ''],
});
setCurrentWorkspaceMember(
(current) => ({ ...current, avatarUrl: null } as any),
);
};
return (

View File

@ -1,28 +1,40 @@
import { useRecoilValue } from 'recoil';
import { useRecoilState } from 'recoil';
import { currentWorkspaceMemberState } from '@/auth/states/currentWorkspaceMemberState';
import { useUpdateOneObjectRecord } from '@/object-record/hooks/useUpdateOneObjectRecord';
import { useSnackBar } from '@/ui/feedback/snack-bar/hooks/useSnackBar';
import { Toggle } from '@/ui/input/components/Toggle';
import { useUpdateAllowImpersonationMutation } from '~/generated/graphql';
export const ToggleField = () => {
const { enqueueSnackBar } = useSnackBar();
const currentWorkspaceMember = useRecoilValue(currentWorkspaceMemberState);
const [currentWorkspaceMember, setCurrentWorkspaceMember] = useRecoilState(
currentWorkspaceMemberState,
);
const [updateAllowImpersonation] = useUpdateAllowImpersonationMutation();
const { updateOneObject, objectNotFoundInMetadata } =
useUpdateOneObjectRecord({
objectNameSingular: 'workspaceMemberV2',
});
const handleChange = async (value: boolean) => {
try {
const { data, errors } = await updateAllowImpersonation({
variables: {
if (!updateOneObject || objectNotFoundInMetadata) {
return;
}
await updateOneObject({
idToUpdate: currentWorkspaceMember?.id ?? '',
input: {
allowImpersonation: value,
},
});
if (errors || !data?.allowImpersonation) {
throw new Error('Error while updating user');
}
setCurrentWorkspaceMember(
(current) =>
({
...current,
allowImpersonation: value,
} as any),
);
} catch (err: any) {
enqueueSnackBar(err?.message, {
variant: 'error',

View File

@ -6,11 +6,6 @@ import { useRelationField } from '../../hooks/useRelationField';
export const RelationFieldDisplay = () => {
const { fieldValue, fieldDefinition } = useRelationField();
console.log({
fieldDefinition,
fieldValue,
});
const entityChipProps = getEntityChipFromFieldMetadata(
fieldDefinition,
fieldValue,

View File

@ -18,11 +18,6 @@ export const getEntityChipFromFieldMetadata = (
entityId: fieldValue?.id,
};
console.log({
fieldName,
fieldValue,
});
// TODO: use every
if (fieldName === 'accountOwner' && fieldValue) {
chipValue.name = fieldValue.firstName + ' ' + fieldValue.lastName;

View File

@ -22,11 +22,6 @@ export const useRelationField = () => {
}),
);
console.log({
fieldDefinition,
fieldValue,
});
const fieldInitialValue = useFieldInitialValue();
const initialSearchValue = fieldInitialValue?.isEmpty