databag/app/mobile/src/context/useProfileContext.hook.js

85 lines
2.6 KiB
JavaScript
Raw Normal View History

2022-09-14 07:27:49 +00:00
import { useState, useRef, useContext } from 'react';
import { getProfile } from 'api/getProfile';
import { setProfileData } from 'api/setProfileData';
import { setProfileImage } from 'api/setProfileImage';
import { getProfileImageUrl } from 'api/getProfileImageUrl';
import { getHandle } from 'api/getHandle';
2022-09-14 07:27:49 +00:00
import { StoreContext } from 'context/StoreContext';
export function useProfileContext() {
const [state, setState] = useState({
profile: {},
imageUrl: null,
});
const store = useContext(StoreContext);
const session = useRef(null);
const curRevision = useRef(null);
const setRevision = useRef(null);
const syncing = useRef(false);
const updateState = (value) => {
setState((s) => ({ ...s, ...value }))
}
const sync = async () => {
if (!syncing.current && setRevision.current !== curRevision.current) {
syncing.current = true;
2022-09-14 19:18:16 +00:00
try {
const revision = curRevision.current;
const { server, appToken, guid } = session.current;
const profile = await getProfile(server, appToken);
await store.actions.setProfile(guid, profile);
await store.actions.setProfileRevision(guid, revision);
updateState({ profile, imageUrl: getProfileImageUrl(server, appToken, revision) });
setRevision.current = revision;
}
catch(err) {
console.log(err);
2022-09-15 08:03:20 +00:00
syncing.current = false;
return;
2022-09-14 19:18:16 +00:00
}
2022-09-14 07:27:49 +00:00
syncing.current = false;
sync();
}
};
const actions = {
setSession: async (access) => {
const { guid, server, appToken } = access;
const profile = await store.actions.getProfile(guid);
const revision = await store.actions.getProfileRevision(guid);
updateState({ profile, imageUrl: getProfileImageUrl(server, appToken, revision) });
setRevision.current = revision;
curRevision.current = revision;
session.current = access;
},
clearSession: () => {
session.current = {};
2022-09-26 19:14:06 +00:00
updateState({ profile: {} });
2022-09-14 07:27:49 +00:00
},
setRevision: (rev) => {
curRevision.current = rev;
sync();
},
setProfileData: async (name, location, description) => {
const { server, appToken } = session.current;
await setProfileData(server, appToken, name, location, description);
},
setProfileImage: async (image) => {
const { server, appToken } = session.current;
await setProfileImage(server, appToken, image);
},
getHandle: async (name) => {
const { server, appToken } = session.current;
return await getHandle(server, appToken, name);
},
2022-09-14 07:27:49 +00:00
}
return { state, actions }
}