Skip to content

Commit aee8cc0

Browse files
authored
Merge pull request #105 from eurofurence/bug/private-message-linker
Fetch new messages when the tabs pager is opened.
2 parents c3d9a53 + 8d9d51a commit aee8cc0

4 files changed

Lines changed: 39 additions & 12 deletions

File tree

src/app/MainMenu/PagerPrimary.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Button } from "../../components/Containers/Button";
77
import { Col } from "../../components/Containers/Col";
88
import { Grid } from "../../components/Containers/Grid";
99
import { Tab } from "../../components/Containers/Tab";
10+
import { useTabs } from "../../components/Containers/Tabs";
1011
import { useAppSelector } from "../../store";
1112
import { selectBrowseableMaps } from "../../store/eurofurence.selectors";
1213
import { RecordId } from "../../store/eurofurence.types";
@@ -30,11 +31,12 @@ export const PagerPrimary: FC<PagerMenuProps> = ({ onMessages, onLogin, onInfo,
3031
const { t } = useTranslation("Menu");
3132
const loggedIn = useAppSelector((state) => state.authorization.isLoggedIn);
3233
const maps = useAppSelector(selectBrowseableMaps);
34+
const tabs = useTabs();
3335

3436
return (
3537
<Col type="stretch">
3638
{loggedIn ? (
37-
<PrivateMessageLinker onOpenMessages={onMessages} />
39+
<PrivateMessageLinker onOpenMessages={onMessages} open={tabs.isOpen} />
3840
) : (
3941
<View style={{ padding: 30 }}>
4042
<Label style={styles.marginBefore} type="caption">

src/app/PrivateMessages/PrivateMessageLinker.tsx

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FC } from "react";
1+
import { FC, useEffect, useRef } from "react";
22
import { useTranslation } from "react-i18next";
33
import { StyleSheet, View } from "react-native";
44

@@ -7,27 +7,36 @@ import { Button } from "../../components/Containers/Button";
77
import { useGetCommunicationsQuery } from "../../store/eurofurence.service";
88

99
type PrivateMessageLinkerProps = {
10+
open?: boolean;
1011
onOpenMessages?: () => void;
1112
};
1213

1314
/**
1415
* Creates a link to the private messages screen
1516
* @constructor
1617
*/
17-
export const PrivateMessageLinker: FC<PrivateMessageLinkerProps> = ({ onOpenMessages }) => {
18+
export const PrivateMessageLinker: FC<PrivateMessageLinkerProps> = ({ onOpenMessages, open }) => {
19+
const prevOpen = useRef(open);
1820
const { t } = useTranslation("Menu");
19-
const { unread } = useGetCommunicationsQuery(undefined, {
20-
refetchOnFocus: true,
21-
refetchOnMountOrArgChange: true,
22-
refetchOnReconnect: true,
21+
const { unread, refetch } = useGetCommunicationsQuery(undefined, {
2322
selectFromResult: (query) => ({
2423
...query,
2524
unread: query.data?.filter((it) => it.ReadDateTimeUtc === null),
2625
}),
2726
});
27+
28+
useEffect(() => {
29+
if (open === true && prevOpen.current !== open) {
30+
console.debug("Fetching new private messages");
31+
refetch();
32+
}
33+
prevOpen.current = open;
34+
console.debug("Tab open has changed status", open);
35+
}, [open]);
36+
2837
return (
2938
<View style={{ padding: 30 }}>
30-
<Label style={styles.marginBefore}>{t("messages", { count: unread?.length ?? 0 })}</Label>
39+
<Label variant={unread?.length ? "bold" : undefined}>{t("messages", { count: unread?.length ?? 0 })}</Label>
3140
<Button style={styles.marginBefore} icon={unread?.length ? "email-multiple-outline" : "email-open-multiple-outline"} onPress={onOpenMessages}>
3241
{t("open_messages")}
3342
</Button>

src/components/Containers/Tabs.tsx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { forwardRef, ReactNode, useCallback, useImperativeHandle, useMemo, useState } from "react";
1+
import { noop } from "lodash";
2+
import { createContext, forwardRef, ReactNode, useCallback, useContext, useImperativeHandle, useMemo, useState } from "react";
23
import { StyleProp, StyleSheet, View, ViewStyle } from "react-native";
34
import { Gesture, GestureDetector, TouchableWithoutFeedback } from "react-native-gesture-handler";
45
import Animated, { cancelAnimation, Easing, runOnJS, useAnimatedStyle, useSharedValue, withTiming } from "react-native-reanimated";
@@ -98,6 +99,21 @@ export type TabsRef = {
9899
closeImmediately(): void;
99100
};
100101

102+
/**
103+
* Allow components in tabs to get access to the Tab properties.
104+
*/
105+
const TabsContext = createContext<TabsRef & { isOpen: boolean }>({
106+
close: noop,
107+
open: noop,
108+
closeImmediately: noop,
109+
isOpen: false,
110+
});
111+
112+
/**
113+
* Expose the Tabs Context as a hook
114+
*/
115+
export const useTabs = () => useContext(TabsContext);
116+
101117
/**
102118
* A row of tabs and a "more" button.
103119
*
@@ -203,7 +219,7 @@ export const Tabs = forwardRef<TabsRef, TabsProps>(({ style, tabs, textMore = "M
203219
// TODO: Integration with back button.
204220

205221
return (
206-
<>
222+
<TabsContext.Provider value={{ close, open, closeImmediately, isOpen: offset.value > 0 }}>
207223
{/* Dismissal area. */}
208224
<Animated.View style={[styles.dismiss, styleDismiss, dynamicDismiss]}>
209225
<TouchableWithoutFeedback containerStyle={StyleSheet.absoluteFill} style={StyleSheet.absoluteFill} onPress={close} />
@@ -227,7 +243,7 @@ export const Tabs = forwardRef<TabsRef, TabsProps>(({ style, tabs, textMore = "M
227243
</View>
228244
</Animated.View>
229245
</GestureDetector>
230-
</>
246+
</TabsContext.Provider>
231247
);
232248
});
233249

src/components/Managers/NotificationReceivedManager.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { addNotificationReceivedListener, Notification, NotificationChannel, removeNotificationSubscription, scheduleNotificationAsync } from "expo-notifications";
1+
import { addNotificationReceivedListener, Notification, removeNotificationSubscription, scheduleNotificationAsync } from "expo-notifications";
22
import moment from "moment";
33
import { useEffect } from "react";
44

0 commit comments

Comments
 (0)