mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-25 12:56:41 +07:00
removed profile redux items
This commit is contained in:
parent
5b28313afd
commit
3c0571816c
55 changed files with 488 additions and 710 deletions
55
src/utils/providers/ProfileProvider.tsx
Normal file
55
src/utils/providers/ProfileProvider.tsx
Normal file
|
@ -0,0 +1,55 @@
|
|||
import { createContext, FC, useCallback, useContext } from "react";
|
||||
import { IUser } from "~/redux/auth/types";
|
||||
import { useGetProfile } from "~/hooks/profile/useGetProfile";
|
||||
import { EMPTY_USER } from "~/redux/auth/constants";
|
||||
import { usePatchProfile } from "~/hooks/profile/usePatchProfile";
|
||||
import { useUser } from "~/hooks/user/userUser";
|
||||
import { useDispatch } from "react-redux";
|
||||
import { authSetUser } from "~/redux/auth/actions";
|
||||
|
||||
interface ProfileProviderProps {
|
||||
username: string;
|
||||
}
|
||||
|
||||
interface ProfileContextValue {
|
||||
profile: IUser;
|
||||
isLoading: boolean;
|
||||
updatePhoto: (file: File) => Promise<unknown>;
|
||||
updateProfile: (user: Partial<IUser>) => Promise<IUser>;
|
||||
}
|
||||
|
||||
const ProfileContext = createContext<ProfileContextValue>({
|
||||
profile: EMPTY_USER,
|
||||
isLoading: false,
|
||||
updatePhoto: async () => {},
|
||||
updateProfile: async () => EMPTY_USER,
|
||||
});
|
||||
|
||||
export const ProfileProvider: FC<ProfileProviderProps> = ({ children, username }) => {
|
||||
const dispatch = useDispatch();
|
||||
|
||||
const user = useUser();
|
||||
const { profile, isLoading, update: updateProfileData } = useGetProfile(username);
|
||||
|
||||
const update = useCallback(
|
||||
async (data: Partial<IUser>) => {
|
||||
if (profile.id === user.id) {
|
||||
await updateProfileData(data);
|
||||
}
|
||||
|
||||
// TODO: user updateUser from useGetUser or something
|
||||
dispatch(authSetUser(data));
|
||||
},
|
||||
[updateProfileData, dispatch, profile, user]
|
||||
);
|
||||
|
||||
const { updatePhoto, updateProfile } = usePatchProfile(update);
|
||||
|
||||
return (
|
||||
<ProfileContext.Provider value={{ profile, isLoading, updatePhoto, updateProfile }}>
|
||||
{children}
|
||||
</ProfileContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
export const useProfileContext = () => useContext(ProfileContext);
|
Loading…
Add table
Add a link
Reference in a new issue