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
9 changed files with 89 additions and 65 deletions

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 (