2024-06-07 20:17:03 +03:00
|
|
|
'use client';
|
|
|
|
|
|
|
|
import { createContext, useCallback, useContext, useEffect, useState } from 'react';
|
|
|
|
|
2024-07-24 23:20:45 +03:00
|
|
|
import { DataCallback } from '@/backend/apiTransport';
|
|
|
|
import { getProfile, patchProfile } from '@/backend/users';
|
2024-06-07 20:17:03 +03:00
|
|
|
import { ErrorData } from '@/components/info/InfoError';
|
|
|
|
import { IUserProfile, IUserUpdateData } from '@/models/user';
|
|
|
|
import { contextOutsideScope } from '@/utils/labels';
|
|
|
|
|
|
|
|
import { useUsers } from './UsersContext';
|
|
|
|
|
|
|
|
interface IUserProfileContext {
|
|
|
|
user: IUserProfile | undefined;
|
|
|
|
loading: boolean;
|
|
|
|
processing: boolean;
|
|
|
|
error: ErrorData;
|
|
|
|
errorProcessing: ErrorData;
|
|
|
|
setError: (error: ErrorData) => void;
|
|
|
|
updateUser: (data: IUserUpdateData, callback?: DataCallback<IUserProfile>) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ProfileContext = createContext<IUserProfileContext | null>(null);
|
|
|
|
|
|
|
|
export const useUserProfile = () => {
|
|
|
|
const context = useContext(ProfileContext);
|
|
|
|
if (!context) {
|
|
|
|
throw new Error(contextOutsideScope('useUserProfile', 'UserProfileState'));
|
|
|
|
}
|
|
|
|
return context;
|
|
|
|
};
|
|
|
|
|
2024-09-19 17:48:48 +03:00
|
|
|
export const UserProfileState = ({ children }: React.PropsWithChildren) => {
|
2024-06-07 20:17:03 +03:00
|
|
|
const { users } = useUsers();
|
|
|
|
const [user, setUser] = useState<IUserProfile | undefined>(undefined);
|
2024-06-23 19:42:54 +03:00
|
|
|
const [loading, setLoading] = useState(true);
|
2024-06-07 20:17:03 +03:00
|
|
|
const [processing, setProcessing] = useState(false);
|
|
|
|
const [error, setError] = useState<ErrorData>(undefined);
|
|
|
|
const [errorProcessing, setErrorProcessing] = useState<ErrorData>(undefined);
|
|
|
|
|
|
|
|
const reload = useCallback(() => {
|
|
|
|
setError(undefined);
|
|
|
|
setUser(undefined);
|
|
|
|
getProfile({
|
|
|
|
showError: true,
|
|
|
|
setLoading: setLoading,
|
|
|
|
onError: setError,
|
|
|
|
onSuccess: newData => setUser(newData)
|
|
|
|
});
|
|
|
|
}, [setUser]);
|
|
|
|
|
|
|
|
const updateUser = useCallback(
|
|
|
|
(data: IUserUpdateData, callback?: DataCallback<IUserProfile>) => {
|
|
|
|
setErrorProcessing(undefined);
|
|
|
|
patchProfile({
|
|
|
|
data: data,
|
|
|
|
showError: true,
|
|
|
|
setLoading: setProcessing,
|
|
|
|
onError: setErrorProcessing,
|
|
|
|
onSuccess: newData => {
|
|
|
|
setUser(newData);
|
|
|
|
const libraryUser = users.find(item => item.id === user?.id);
|
|
|
|
if (libraryUser) {
|
|
|
|
libraryUser.first_name = newData.first_name;
|
|
|
|
libraryUser.last_name = newData.last_name;
|
|
|
|
}
|
2024-12-04 22:52:45 +03:00
|
|
|
callback?.(newData);
|
2024-06-07 20:17:03 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
[setUser, users, user?.id]
|
|
|
|
);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
reload();
|
|
|
|
}, [reload]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ProfileContext.Provider value={{ user, updateUser, error, loading, setError, processing, errorProcessing }}>
|
|
|
|
{children}
|
|
|
|
</ProfileContext.Provider>
|
|
|
|
);
|
|
|
|
};
|