mirror of
https://github.com/balzack/databag.git
synced 2025-02-14 12:39:17 +00:00
handle context init
This commit is contained in:
parent
aa85bede1f
commit
f69c0d1d35
@ -4,6 +4,7 @@ import { getAccountStatus } from '../Api/getAccountStatus';
|
|||||||
|
|
||||||
export function useAccountContext() {
|
export function useAccountContext() {
|
||||||
const [state, setState] = useState({
|
const [state, setState] = useState({
|
||||||
|
init: false,
|
||||||
status: null,
|
status: null,
|
||||||
});
|
});
|
||||||
const access = useRef(null);
|
const access = useRef(null);
|
||||||
@ -18,7 +19,7 @@ export function useAccountContext() {
|
|||||||
if (next.current == null) {
|
if (next.current == null) {
|
||||||
if (revision.current != rev) {
|
if (revision.current != rev) {
|
||||||
let status = await getAccountStatus(access.current);
|
let status = await getAccountStatus(access.current);
|
||||||
updateState({ status });
|
updateState({ init: true, status });
|
||||||
revision.current = rev;
|
revision.current = rev;
|
||||||
}
|
}
|
||||||
if (next.current != null) {
|
if (next.current != null) {
|
||||||
|
@ -20,6 +20,7 @@ import { removeCard } from '../Api/removeCard';
|
|||||||
|
|
||||||
export function useCardContext() {
|
export function useCardContext() {
|
||||||
const [state, setState] = useState({
|
const [state, setState] = useState({
|
||||||
|
init: false,
|
||||||
cards: new Map(),
|
cards: new Map(),
|
||||||
});
|
});
|
||||||
const access = useRef(null);
|
const access = useRef(null);
|
||||||
@ -86,14 +87,11 @@ export function useCardContext() {
|
|||||||
}
|
}
|
||||||
if (cur.data.notifiedChannel != card.data.notifiedChannel) {
|
if (cur.data.notifiedChannel != card.data.notifiedChannel) {
|
||||||
// update remote channels
|
// update remote channels
|
||||||
console.log("UPDATE CHANNEL: ", cur.data.notifiedChannel, card.data.notifiedChannel);
|
|
||||||
|
|
||||||
await updateContactChannels(card.id, cur.data.cardProfile.guid, cur.data.cardDetail.token, cur.data.notifiedView, cur.data.notifiedChannel, cur.channels);
|
await updateContactChannels(card.id, cur.data.cardProfile.guid, cur.data.cardDetail.token, cur.data.notifiedView, cur.data.notifiedChannel, cur.channels);
|
||||||
cur.data.notifiedChannel = card.data.notifiedChannel;
|
cur.data.notifiedChannel = card.data.notifiedChannel;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cur.revision = card.revision;
|
cur.revision = card.revision;
|
||||||
console.log("SAVE:", cur.data.notifiedChannel);
|
|
||||||
cards.current.set(card.id, cur);
|
cards.current.set(card.id, cur);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -103,8 +101,6 @@ console.log("SAVE:", cur.data.notifiedChannel);
|
|||||||
}
|
}
|
||||||
|
|
||||||
const updateContactChannels = async (cardId, guid, token, viewRevision, channelRevision, channelMap) => {
|
const updateContactChannels = async (cardId, guid, token, viewRevision, channelRevision, channelMap) => {
|
||||||
console.log("UPDATE CONTACT CHANNELS: ", viewRevision, channelRevision);
|
|
||||||
|
|
||||||
let delta = await getContactChannels(guid + "." + token, viewRevision, channelRevision);
|
let delta = await getContactChannels(guid + "." + token, viewRevision, channelRevision);
|
||||||
for (let channel of delta) {
|
for (let channel of delta) {
|
||||||
if (channel.data) {
|
if (channel.data) {
|
||||||
@ -142,7 +138,7 @@ console.log("UPDATE CONTACT CHANNELS: ", viewRevision, channelRevision);
|
|||||||
next.current = rev;
|
next.current = rev;
|
||||||
if (revision.current != rev) {
|
if (revision.current != rev) {
|
||||||
await updateCards();
|
await updateCards();
|
||||||
updateState({ cards: cards.current });
|
updateState({ init: true, cards: cards.current });
|
||||||
revision.current = rev;
|
revision.current = rev;
|
||||||
}
|
}
|
||||||
let r = next.current;
|
let r = next.current;
|
||||||
|
@ -8,6 +8,7 @@ import { getChannelTopic } from '../Api/getChannelTopic';
|
|||||||
|
|
||||||
export function useChannelContext() {
|
export function useChannelContext() {
|
||||||
const [state, setState] = useState({
|
const [state, setState] = useState({
|
||||||
|
init: false,
|
||||||
channels: new Map(),
|
channels: new Map(),
|
||||||
});
|
});
|
||||||
const access = useRef(null);
|
const access = useRef(null);
|
||||||
@ -53,7 +54,7 @@ export function useChannelContext() {
|
|||||||
next.current = rev;
|
next.current = rev;
|
||||||
if (revision.current != rev) {
|
if (revision.current != rev) {
|
||||||
await updateChannels();
|
await updateChannels();
|
||||||
updateState({ channels: channels.current });
|
updateState({ init: true, channels: channels.current });
|
||||||
revision.current = rev;
|
revision.current = rev;
|
||||||
}
|
}
|
||||||
let r = next.current;
|
let r = next.current;
|
||||||
|
@ -3,6 +3,7 @@ import { getGroups } from '../Api/getGroups';
|
|||||||
|
|
||||||
export function useGroupContext() {
|
export function useGroupContext() {
|
||||||
const [state, setState] = useState({
|
const [state, setState] = useState({
|
||||||
|
init: false,
|
||||||
groups: new Map(),
|
groups: new Map(),
|
||||||
});
|
});
|
||||||
const access = useRef(null);
|
const access = useRef(null);
|
||||||
@ -30,7 +31,7 @@ export function useGroupContext() {
|
|||||||
if (next.current == null) {
|
if (next.current == null) {
|
||||||
if (revision.current != rev) {
|
if (revision.current != rev) {
|
||||||
await updateGroups();
|
await updateGroups();
|
||||||
updateState({ groups: groups.current });
|
updateState({ init: true, groups: groups.current });
|
||||||
revision.current = rev;
|
revision.current = rev;
|
||||||
}
|
}
|
||||||
if (next.current != null) {
|
if (next.current != null) {
|
||||||
|
@ -6,6 +6,7 @@ import { getProfileImageUrl } from '../Api/getProfileImageUrl';
|
|||||||
|
|
||||||
export function useProfileContext() {
|
export function useProfileContext() {
|
||||||
const [state, setState] = useState({
|
const [state, setState] = useState({
|
||||||
|
init: false,
|
||||||
profile: {},
|
profile: {},
|
||||||
});
|
});
|
||||||
const access = useRef(null);
|
const access = useRef(null);
|
||||||
@ -20,7 +21,7 @@ export function useProfileContext() {
|
|||||||
if (next.current == null) {
|
if (next.current == null) {
|
||||||
if (revision.current != rev) {
|
if (revision.current != rev) {
|
||||||
let profile = await getProfile(access.current);
|
let profile = await getProfile(access.current);
|
||||||
updateState({ profile });
|
updateState({ init: true, profile });
|
||||||
revision.current = rev;
|
revision.current = rev;
|
||||||
}
|
}
|
||||||
if (next.current != null) {
|
if (next.current != null) {
|
||||||
|
@ -148,7 +148,7 @@ export function useContact() {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
const updateContact = () => {
|
||||||
let contact = card.actions.getCardByGuid(guid);
|
let contact = card.actions.getCardByGuid(guid);
|
||||||
if (contact) {
|
if (contact) {
|
||||||
let profile = contact.data.cardProfile;
|
let profile = contact.data.cardProfile;
|
||||||
@ -201,6 +201,12 @@ export function useContact() {
|
|||||||
updateState({ status: 'unsaved' });
|
updateState({ status: 'unsaved' });
|
||||||
updateState({ showButtons: { save: true, saveRequest: true }});
|
updateState({ showButtons: { save: true, saveRequest: true }});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (card.state.init) {
|
||||||
|
updateContact();
|
||||||
|
}
|
||||||
}, [card, guid])
|
}, [card, guid])
|
||||||
|
|
||||||
return { state, actions };
|
return { state, actions };
|
||||||
|
@ -101,7 +101,9 @@ export function useConversation() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
updateConversation();
|
if (card.state.init && channel.state.init) {
|
||||||
|
updateConversation();
|
||||||
|
}
|
||||||
}, [card, channel]);
|
}, [card, channel]);
|
||||||
|
|
||||||
return { state, actions };
|
return { state, actions };
|
||||||
|
Loading…
Reference in New Issue
Block a user