saving channel push status

This commit is contained in:
Roland Osborne 2022-11-14 13:30:58 -08:00
parent 09827ea056
commit 160429671e
10 changed files with 121 additions and 9 deletions

View File

@ -0,0 +1,8 @@
import { checkResponse, fetchWithTimeout } from './fetchUtil';
export async function getChannelNotifications(server, token, channelId) {
const notify = await fetchWithTimeout(`https://${server}/content/channels/${channelId}/notification?agent=${token}`, { method: 'GET' });
checkResponse(notify)
return await notify.json()
}

View File

@ -0,0 +1,8 @@
import { checkResponse, fetchWithTimeout } from './fetchUtil';
export async function getContactChannelNotifications(server, token, channelId) {
const notify = await fetchWithTimeout(`https://${server}/content/channels/${channelId}/notification?contact=${token}`, { method: 'GET' });
checkResponse(notify)
return await notify.json()
}

View File

@ -0,0 +1,7 @@
import { checkResponse, fetchWithTimeout } from './fetchUtil';
export async function setChannelNotifications(server, token, channelId, flag) {
const notify = await fetchWithTimeout(`https://${server}/content/channels/${channelId}/notification?agent=${token}`, { method: 'PUT', body: JSON.stringify(flag) });
checkResponse(notify)
}

View File

@ -0,0 +1,7 @@
import { checkResponse, fetchWithTimeout } from './fetchUtil';
export async function setContactChannelNotifications(server, token, channelId, flag) {
const notify = await fetchWithTimeout(`https://${server}/content/channels/${channelId}/notification?contact=${token}`, { method: 'PUT', body: JSON.stringify(flag) });
checkResponse(notify)
}

View File

@ -30,6 +30,9 @@ import { setContactChannelTopicSubject } from 'api/setContactChannelTopicSubject
import { removeContactChannel } from 'api/removeContactChannel';
import { removeContactChannelTopic } from 'api/removeContactChannelTopic';
import { getContactChannelNotifications } from 'api/getContactChannelNotifications';
import { setContactChannelNotifications } from 'api/setContactChannelNotifications';
export function useCardContext() {
const [state, setState] = useState({
cards: new Map(),
@ -576,12 +579,20 @@ export function useCardContext() {
const { detail, profile } = getCardEntry(cardId);
return await addFlag(profile.node, profile.guid, channelId, topicId);
},
getChannelNotifications: async (cardId, channelId) => {
const { detail, profile } = getCardEntry(cardId);
return await getContactChannelNotifications(profile.node, `${profile.guid}.${detail.token}`, channelId);
},
setChannelNotifications: async (cardId, channelId, notify) => {
const { detail, profile } = getCardEntry(cardId);
return await setContactChannelNotifications(profile.node, `${profile.guid}.${detail.token}`, channelId, notify);
},
resync: (cardId) => {
resync.current.push(cardId);
sync();
},
}
return { state, actions }
}

View File

@ -16,6 +16,8 @@ import { setChannelSubject } from 'api/setChannelSubject';
import { setChannelCard } from 'api/setChannelCard';
import { clearChannelCard } from 'api/clearChannelCard';
import { addFlag } from 'api/addFlag';
import { setChannelNotifications } from 'api/setChannelNotifications';
import { getChannelNotifications } from 'api/getChannelNotifications';
export function useChannelContext() {
const [state, setState] = useState({
@ -297,6 +299,14 @@ export function useChannelContext() {
const { server, appToken } = session.current;
return await clearChannelCard(server, appToken, channelId, cardId);
},
getNotifications: async (channelId) => {
const { server, appToken } = session.current;
return await getChannelNotifications(server, appToken, channelId);
},
setNotifications: async (channelId, notify) => {
const { server, appToken } = session.current;
return await setChannelNotifications(server, appToken, channelId, notify);
},
}
return { state, actions }

View File

@ -20,6 +20,7 @@ export function useConversationContext() {
progress: null,
cardId: null,
channelId: null,
pushEnabled: null,
});
const store = useContext(StoreContext);
const upload = useContext(UploadContext);
@ -141,7 +142,19 @@ export function useConversationContext() {
if (cardId) {
return await card.actions.removeChannelTopic(cardId, channelId, topicId);
}
return await channel.actions.remvoeTopic(channelId, topicId);
return await channel.actions.removeTopic(channelId, topicId);
}
const setNotifications = async (cardId, channelId, notify) => {
if (cardId) {
return await card.actions.setChannelNotifications(cardId, channelId, notify);
}
return await channel.actions.setNotifications(channelId, notify);
}
const getNotifications = async (cardId, channelId) => {
if (cardId) {
return await card.actions.getChannelNotifications(cardId, channelId);
}
return await channel.actions.getNotifications(channelId);
}
const setSyncRevision = async (cardId, channelId, revision) => {
if (cardId) {
@ -169,7 +182,7 @@ export function useConversationContext() {
// set channel details
if (detailRevision.current != channelItem.detailRevision) {
if (curView === setView.current) {
setChannel(channelItem);
await setChannel(channelItem);
detailRevision.current = channelItem.detailRevision;
}
}
@ -257,7 +270,7 @@ export function useConversationContext() {
return channel.state.channels.get(channelId);
}
const setChannel = (item) => {
const setChannel = async (item) => {
let contacts = [];
let logo = null;
let topic = null;
@ -337,9 +350,11 @@ export function useConversationContext() {
}
}
const pushEnabled = await getNotifications(item.cardId, item.channelId);
const { enableImage, enableAudio, enableVideo } = item.detail;
updateState({ topic, subject, logo, contacts, host: item.cardId, created: timestamp,
enableImage, enableAudio, enableVideo });
enableImage, enableAudio, enableVideo, pushEnabled });
}
useEffect(() => {
@ -410,6 +425,13 @@ export function useConversationContext() {
await remove(cardId, channelId);
}
},
setNotifications: async (notify) => {
if (conversationId.current) {
const { cardId, channelId } = conversationId.current;
await setNotifications(cardId, channelId, notify);
updateState({ pushEnabled: notify });
}
},
removeTopic: async (topicId) => {
if (conversationId.current) {
const { cardId, channelId } = conversationId.current;

View File

@ -1,4 +1,4 @@
import { KeyboardAvoidingView, FlatList, Alert, Modal, View, Text, TouchableOpacity, TextInput } from 'react-native';
import { KeyboardAvoidingView, FlatList, Alert, Modal, View, Text, Switch, TouchableOpacity, TextInput } from 'react-native';
import { styles } from './Details.styled';
import { useDetails } from './useDetails.hook';
import { Logo } from 'utils/Logo';
@ -28,6 +28,19 @@ export function DetailsBody({ channel, clearConversation }) {
}
}
const setNotifications = async (notify) => {
try {
await actions.setNotifications(notify);
}
catch (err) {
console.log(err);
Alert.alert(
'Failed to Update Channel',
'Please try again.',
)
}
}
const remove = () => {
Alert.alert(
"Removing Topic",
@ -149,6 +162,14 @@ export function DetailsBody({ channel, clearConversation }) {
<Text style={styles.buttonText}>Edit Membership</Text>
</TouchableOpacity>
)}
<View style={styles.notify}>
<TouchableOpacity onPress={() => setNotifications(!state.pushEnabled)} activeOpacity={1}>
<Text style={styles.notifyText}>Enable Notifications</Text>
</TouchableOpacity>
<Switch style={styles.notifySwitch} value={state.pushEnabled} onValueChange={setNotifications} trackColor={styles.switch}/>
</View>
</View>
<View style={styles.members}>

View File

@ -156,6 +156,20 @@ export const styles = StyleSheet.create({
borderRadius: 4,
marginBottom: 8,
height: 250,
},
},
notify: {
display: 'flex',
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
paddingTop: 16,
},
notifyText: {
fontSize: 16,
color: Colors.text,
},
notifySwitch: {
transform: [{ scaleX: .7 }, { scaleY: .7 }],
},
})

View File

@ -15,6 +15,7 @@ export function useDetails() {
editSubject: false,
editMembers: false,
subjectUpdate: null,
pushEnabled: null,
});
const card = useContext(CardContext);
@ -31,8 +32,8 @@ export function useDetails() {
}, [card]);
useEffect(() => {
const { topic, subject, created, logo, host, contacts } = conversation.state;
updateState({ subject, created, logo, hostId: host, subjectUpdate: topic,
const { topic, subject, created, logo, host, contacts, pushEnabled } = conversation.state;
updateState({ subject, created, logo, pushEnabled, hostId: host, subjectUpdate: topic,
count: contacts.length, contacts: contacts.filter(card => card != null) });
}, [conversation]);
@ -64,6 +65,9 @@ export function useDetails() {
report: async() => {
await conversation.actions.addReport();
},
setNotifications: async (notify) => {
await conversation.actions.setNotifications(notify);
},
};
return { state, actions };