2022-04-24 02:49:27 +00:00
|
|
|
import { useEffect, useState, useRef } from 'react';
|
|
|
|
import { getProfile } from '../Api/getProfile';
|
|
|
|
import { setProfileData } from '../Api/setProfileData';
|
|
|
|
import { setProfileImage } from '../Api/setProfileImage';
|
|
|
|
import { getProfileImageUrl } from '../Api/getProfileImageUrl';
|
|
|
|
|
|
|
|
export function useProfileContext() {
|
|
|
|
const [state, setState] = useState({
|
|
|
|
profile: {},
|
|
|
|
});
|
2022-04-24 07:27:28 +00:00
|
|
|
const access = useRef(null);
|
|
|
|
const revision = useRef(null);
|
2022-04-24 02:49:27 +00:00
|
|
|
const next = useRef(null);
|
|
|
|
|
|
|
|
const updateState = (value) => {
|
|
|
|
setState((s) => ({ ...s, ...value }))
|
|
|
|
}
|
|
|
|
|
2022-04-24 07:27:28 +00:00
|
|
|
const setProfile = async (rev) => {
|
2022-04-24 02:49:27 +00:00
|
|
|
if (next.current == null) {
|
2022-04-24 07:27:28 +00:00
|
|
|
if (revision.current != rev) {
|
|
|
|
let profile = await getProfile(access.current);
|
|
|
|
updateState({ profile });
|
|
|
|
revision.current = rev;
|
|
|
|
}
|
2022-04-24 02:49:27 +00:00
|
|
|
if (next.current != null) {
|
2022-04-24 07:27:28 +00:00
|
|
|
let r = next.current;
|
2022-04-24 02:49:27 +00:00
|
|
|
next.current = null;
|
2022-04-24 07:27:28 +00:00
|
|
|
setProfile(r);
|
2022-04-24 02:49:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2022-04-24 07:27:28 +00:00
|
|
|
next.current = rev;
|
2022-04-24 02:49:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const actions = {
|
|
|
|
setToken: (token) => {
|
2022-04-24 07:27:28 +00:00
|
|
|
access.current = token;
|
2022-04-24 02:49:27 +00:00
|
|
|
},
|
2022-04-24 07:27:28 +00:00
|
|
|
setRevision: (rev) => {
|
|
|
|
setProfile(rev);
|
2022-04-24 02:49:27 +00:00
|
|
|
},
|
|
|
|
setProfileData: async (name, location, description) => {
|
2022-04-25 19:16:25 +00:00
|
|
|
await setProfileData(access.current, name, location, description);
|
2022-04-24 02:49:27 +00:00
|
|
|
},
|
|
|
|
setProfileImage: async (image) => {
|
2022-04-25 19:16:25 +00:00
|
|
|
await setProfileImage(access.current, image);
|
2022-04-24 02:49:27 +00:00
|
|
|
},
|
2022-04-24 07:27:28 +00:00
|
|
|
profileImageUrl: () => getProfileImageUrl(access.current, revision.current),
|
2022-04-24 02:49:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return { state, actions }
|
|
|
|
}
|
|
|
|
|
|
|
|
|