2022-09-15 08:03:20 +00:00
|
|
|
import { useState, useRef, useContext } from 'react';
|
|
|
|
import { StoreContext } from 'context/StoreContext';
|
2022-09-15 18:26:03 +00:00
|
|
|
import { getCards } from 'api/getCards';
|
|
|
|
import { getCardProfile } from 'api/getCardProfile';
|
|
|
|
import { getCardDetail } from 'api/getCardDetail';
|
2022-09-15 08:03:20 +00:00
|
|
|
|
|
|
|
export function useCardContext() {
|
|
|
|
const [state, setState] = useState({
|
|
|
|
});
|
|
|
|
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;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const revision = curRevision.current;
|
|
|
|
const { server, appToken, guid } = session.current;
|
|
|
|
|
|
|
|
// get and store
|
2022-09-15 18:26:03 +00:00
|
|
|
const delta = await getCards(server, appToken, setRevision.current);
|
|
|
|
|
|
|
|
for (let card of delta) {
|
|
|
|
if (card.data) {
|
|
|
|
if (card.data.cardDetail && card.data.cardProfile) {
|
|
|
|
await store.actions.setCardItem(guid, card);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const view = await store.actions.getCardItemView(guid, card.id);
|
2022-09-15 22:12:06 +00:00
|
|
|
if (view == null) {
|
|
|
|
console.log('alert: expected card not synced');
|
|
|
|
let assembled = JSON.parse(JSON.stringify(card));
|
|
|
|
assembled.data.cardDetail = await getCardDetail(server, appToken, card.id);
|
|
|
|
assembled.data.cardProfile = await getCardProfile(server, appToken, card.id);
|
|
|
|
await store.actions.setCardItem(guid, assembled);
|
2022-09-15 18:26:03 +00:00
|
|
|
}
|
2022-09-15 22:12:06 +00:00
|
|
|
else {
|
|
|
|
if (view.detailRevision != detailRevision) {
|
|
|
|
const detail = await getCardDetail(server, appToken, card.id);
|
|
|
|
await store.actions.setCardItemDetail(guid, card.id, detailRevision, detail);
|
|
|
|
}
|
|
|
|
if (view.profileRevision != profileRevision) {
|
|
|
|
const profile = await getCardProfile(server, appToken, card.id);
|
|
|
|
await store.actions.setCardItemProfile(guid, card.id, profileRevision, profile);
|
|
|
|
}
|
2022-09-15 18:26:03 +00:00
|
|
|
}
|
2022-09-15 22:12:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const status = await store.actions.getCardItemStatus(guid, card.id);
|
|
|
|
if (status.detail.status === 'connected') {
|
|
|
|
const { notifiedView, notifiedProfile, notifiedArticle, notifiedChannel } = card.data;
|
|
|
|
if (status.notifiedView !== notifiedView) {
|
2022-09-15 18:26:03 +00:00
|
|
|
// TODO clear contact and channels
|
|
|
|
// TODO get articles
|
2022-09-15 22:12:06 +00:00
|
|
|
await store.actions.setCardItemNotifiedArticle(guid, card.id, notifiedArticle);
|
2022-09-15 18:26:03 +00:00
|
|
|
// TODO get channels
|
2022-09-15 22:12:06 +00:00
|
|
|
await store.actions.setCardItemNotifiedChannel(guid, card.id, notifiedChannel);
|
2022-09-15 18:26:03 +00:00
|
|
|
|
2022-09-15 22:12:06 +00:00
|
|
|
await store.actions.setCardItemNotifiedView(guid, card.id, notifiedView);
|
2022-09-15 18:26:03 +00:00
|
|
|
}
|
|
|
|
else {
|
2022-09-15 22:12:06 +00:00
|
|
|
if (status.notifiedChannel != notifiedChannel) {
|
2022-09-15 18:26:03 +00:00
|
|
|
// TODO get channel delta
|
2022-09-15 22:12:06 +00:00
|
|
|
await store.actions.setCardItemNotifiedChannel(guid, card.id, notifiedChannel);
|
2022-09-15 18:26:03 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-15 22:12:06 +00:00
|
|
|
if (status.notifiedProflile != notifiedProfile) {
|
|
|
|
// TODO update contact profile if different
|
|
|
|
await store.actions.setCardItemNotifiedProfile(guid, card.id, notifiedProfile);
|
2022-09-15 18:26:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2022-09-15 22:12:06 +00:00
|
|
|
//TODO clear card channel topics
|
|
|
|
await store.actions.clearCardChannelItems(guid, card.id);
|
2022-09-15 18:26:03 +00:00
|
|
|
await store.actions.clearCardItem(guid, card.id);
|
|
|
|
}
|
|
|
|
}
|
2022-09-15 08:03:20 +00:00
|
|
|
|
|
|
|
setRevision.current = revision;
|
2022-09-15 18:26:03 +00:00
|
|
|
await store.actions.setCardRevision(guid, revision);
|
2022-09-15 08:03:20 +00:00
|
|
|
}
|
|
|
|
catch(err) {
|
|
|
|
console.log(err);
|
|
|
|
syncing.current = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
syncing.current = false;
|
|
|
|
sync();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const actions = {
|
|
|
|
setSession: async (access) => {
|
|
|
|
const { guid, server, appToken } = access;
|
|
|
|
|
|
|
|
// load
|
|
|
|
|
|
|
|
const revision = await store.actions.getCardRevision(guid);
|
|
|
|
|
|
|
|
// update
|
|
|
|
|
|
|
|
setRevision.current = revision;
|
|
|
|
curRevision.current = revision;
|
|
|
|
session.current = access;
|
|
|
|
},
|
|
|
|
clearSession: () => {
|
|
|
|
session.current = {};
|
|
|
|
updateState({ account: null });
|
|
|
|
},
|
|
|
|
setRevision: (rev) => {
|
|
|
|
curRevision.current = rev;
|
|
|
|
sync();
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return { state, actions }
|
|
|
|
}
|
|
|
|
|