profile edit

This commit is contained in:
2026-04-03 13:47:14 +05:30
parent 399b2656b8
commit 0f44a8e1b6
3 changed files with 49 additions and 7 deletions

View File

@@ -10,18 +10,43 @@ export default function ProfileView() {
const resourceConfig = appConfig?.resources.find(r => r.name === profileConfig?.resource); const resourceConfig = appConfig?.resources.find(r => r.name === profileConfig?.resource);
if (!profileConfig || !resourceConfig) { if (!profileConfig || !resourceConfig) {
debugger;
return <Alert severity="error">Profile configuration not found.</Alert>; return <Alert severity="error">Profile configuration not found.</Alert>;
} }
const { useMe, useUpdate } = useResource(resourceConfig); // Create a modified config where only extraFields are editable
const editableConfig = React.useMemo(() => {
const newFields = { ...resourceConfig.fields };
const extraFields = profileConfig.extraFields || [];
Object.keys(newFields).forEach(key => {
newFields[key] = {
...newFields[key],
readOnly: !extraFields.includes(key),
};
});
return {
...resourceConfig,
fields: newFields,
};
}, [resourceConfig, profileConfig.extraFields]);
const { useMe, useUpdateMe } = useResource(resourceConfig);
const { data: profile, isLoading, error } = useMe(); const { data: profile, isLoading, error } = useMe();
const updateMutation = useUpdate(); const updateMutation = useUpdateMe();
const handleSave = async (formData: any) => { const handleSave = async (formData: any) => {
try { try {
const id = profile[resourceConfig.primaryKey]; // Only send editable fields to prevent accidental overwrites of read-only data
await updateMutation.mutateAsync({ id, data: formData }); const extraFields = profileConfig.extraFields || [];
const dataToSave = Object.keys(formData)
.filter(key => extraFields.includes(key))
.reduce((obj: any, key) => {
obj[key] = formData[key];
return obj;
}, {});
await updateMutation.mutateAsync(dataToSave);
} catch (err) { } catch (err) {
console.error('Profile update failed:', err); console.error('Profile update failed:', err);
} }
@@ -46,7 +71,7 @@ export default function ProfileView() {
</Typography> </Typography>
<Paper sx={{ p: 4, mt: 2 }}> <Paper sx={{ p: 4, mt: 2 }}>
<GenericForm <GenericForm
config={resourceConfig} config={editableConfig}
initialData={profile} initialData={profile}
onSave={handleSave} onSave={handleSave}
onCancel={() => window.history.back()} onCancel={() => window.history.back()}

View File

@@ -43,5 +43,7 @@ export const configuration: Record<string, ResourceOverride> = {
export const profileConfiguration = { export const profileConfiguration = {
"extraFields": ['name'], "extraFields": ['name'],
"resource": "payors" "resource": "payors",
// not in use
"hidden": true,
}; };

View File

@@ -92,12 +92,27 @@ export function useResource<T = any>(config: ResourceConfig) {
}, },
}); });
// --- UPDATE ME ---
const useUpdateMe = () =>
useMutation({
mutationFn: async (data: Partial<T>) => {
// @ts-ignore
const res = await api.put<T>(`${endpoint}/me`, data);
return res.data;
},
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: [name, "me"] });
queryClient.invalidateQueries({ queryKey: [name, "list"] });
},
});
return { return {
useList, useList,
useRead, useRead,
useMe, useMe,
useCreate, useCreate,
useUpdate, useUpdate,
useUpdateMe,
useDelete, useDelete,
getListQueryOptions, getListQueryOptions,
}; };