Skip to content

A full-featured CRM built with React, shadcn/ui, and Supabase.

Restore sales avatar update

React-Admin CI2c2ed2662707
diff --git a/src/providers/supabase/dataProvider.ts b/src/providers/supabase/dataProvider.ts
index ece14e41..79a6bca5 100644
--- a/src/providers/supabase/dataProvider.ts
+++ b/src/providers/supabase/dataProvider.ts
@@ -147,7 +147,14 @@ const dataProviderWithCustomMethods = {
id: Identifier,
data: Partial<Omit<SalesFormData, 'password'>>
) {
- const { email, first_name, last_name, administrator, disabled } = data;
+ const {
+ email,
+ first_name,
+ last_name,
+ administrator,
+ avatar,
+ disabled,
+ } = data;
const { data: sale, error } = await supabase.functions.invoke<Sale>(
'users',
@@ -160,6 +167,7 @@ const dataProviderWithCustomMethods = {
last_name,
administrator,
disabled,
+ avatar,
},
}
);
diff --git a/src/settings/SettingsPage.tsx b/src/settings/SettingsPage.tsx
index e11a4f8c..9a037b2c 100644
--- a/src/settings/SettingsPage.tsx
+++ b/src/settings/SettingsPage.tsx
@@ -20,12 +20,12 @@ import {
useGetIdentity,
useGetOne,
useNotify,
- useUpdate,
+ useRecordContext,
} from 'react-admin';
import { useFormState } from 'react-hook-form';
import ImageEditorField from '../misc/ImageEditorField';
import { CrmDataProvider } from '../providers/types';
-import { SalesFormData } from '../types';
+import { Sale, SalesFormData } from '../types';
import { useMutation } from '@tanstack/react-query';
import ContentCopyIcon from '@mui/icons-material/ContentCopy';
@@ -84,13 +84,13 @@ const SettingsForm = ({
isEditMode: boolean;
setEditMode: (value: boolean) => void;
}) => {
- const [update] = useUpdate();
const notify = useNotify();
+ const record = useRecordContext<Sale>();
const { identity, refetch } = useGetIdentity();
const { isDirty } = useFormState();
const dataProvider = useDataProvider<CrmDataProvider>();
- const { mutate } = useMutation({
+ const { mutate: updatePassword } = useMutation({
mutationKey: ['updatePassword'],
mutationFn: async () => {
if (!identity) {
@@ -110,33 +110,30 @@ const SettingsForm = ({
},
});
+ const { mutate: mutateSale } = useMutation({
+ mutationKey: ['signup'],
+ mutationFn: async (data: SalesFormData) => {
+ if (!record) {
+ throw new Error('Record not found');
+ }
+ return dataProvider.salesUpdate(record.id, data);
+ },
+ onSuccess: () => {
+ refetch();
+ notify('Your profile has been updated');
+ },
+ onError: () => {
+ notify('An error occurred. Please try again.');
+ },
+ });
if (!identity) return null;
const handleClickOpenPasswordChange = () => {
- mutate();
+ updatePassword();
};
const handleAvatarUpdate = async (values: any) => {
- await update(
- 'sales',
- {
- id: identity.id,
- data: values,
- previousData: identity,
- },
- {
- onSuccess: () => {
- refetch();
- setEditMode(false);
- notify('Your profile has been updated');
- },
- onError: _ => {
- notify('An error occurred. Please try again', {
- type: 'error',
- });
- },
- }
- );
+ mutateSale(values);
};
return (
diff --git a/src/types.ts b/src/types.ts
index 70191d1e..c2c88eea 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -16,6 +16,7 @@ export type SignUpData = {
};
export type SalesFormData = {
+ avatar: string;
email: string;
password: string;
first_name: string;
diff --git a/supabase/functions/users/index.ts b/supabase/functions/users/index.ts
index 8000c474..e31601b1 100644
--- a/supabase/functions/users/index.ts
+++ b/supabase/functions/users/index.ts
@@ -21,7 +21,21 @@ async function updateSaleAdministrator(
.select('*');
if (!sales?.length || salesError) {
- console.error('Error inviting user:', salesError);
+ console.error('Error updating user:', salesError);
+ throw salesError ?? new Error('Failed to update sale');
+ }
+ return sales.at(0);
+}
+
+async function updateSaleAvatar(user_id: string, avatar: string) {
+ const { data: sales, error: salesError } = await supabaseAdmin
+ .from('sales')
+ .update({ avatar })
+ .eq('user_id', user_id)
+ .select('*');
+
+ if (!sales?.length || salesError) {
+ console.error('Error updating user:', salesError);
throw salesError ?? new Error('Failed to update sale');
}
return sales.at(0);
@@ -74,8 +88,15 @@ async function inviteUser(req: Request, currentUserSale: any) {
}
async function patchUser(req: Request, currentUserSale: any) {
- const { sales_id, email, first_name, last_name, administrator, disabled } =
- await req.json();
+ const {
+ sales_id,
+ email,
+ first_name,
+ last_name,
+ avatar,
+ administrator,
+ disabled,
+ } = await req.json();
const { data: sale } = await supabaseAdmin
.from('sales')
.select('*')
@@ -103,6 +124,10 @@ async function patchUser(req: Request, currentUserSale: any) {
return createErrorResponse(500, 'Internal Server Error');
}
+ if (avatar) {
+ await updateSaleAvatar(data.user.id, avatar);
+ }
+
// Only administrators can update the administrator and disabled status
if (!currentUserSale.administrator) {
const { data: new_sale } = await supabaseAdmin