-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
826b229
commit 658f31b
Showing
12 changed files
with
481 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
app/screens/notification-history-screen/notification-history-screen.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { gql } from "@apollo/client" | ||
import { Screen } from "@app/components/screen" | ||
import { useStatefulNotificationsQuery } from "@app/graphql/generated" | ||
import { useIsAuthed } from "@app/graphql/is-authed-context" | ||
import { useI18nContext } from "@app/i18n/i18n-react" | ||
import { testProps } from "@app/utils/testProps" | ||
import { useIsFocused } from "@react-navigation/native" | ||
import { Text, makeStyles, useTheme } from "@rneui/themed" | ||
import { FlatList, RefreshControl } from "react-native-gesture-handler" | ||
import { Notification } from "./notification" | ||
|
||
gql` | ||
query StatefulNotifications($after: String) { | ||
me { | ||
statefulNotifications(first: 10, after: $after) { | ||
nodes { | ||
id | ||
title | ||
body | ||
deepLink | ||
createdAt | ||
acknowledgedAt | ||
} | ||
pageInfo { | ||
endCursor | ||
hasNextPage | ||
hasPreviousPage | ||
startCursor | ||
} | ||
} | ||
} | ||
} | ||
` | ||
|
||
export const NotificationHistoryScreen = () => { | ||
const styles = useStyles() | ||
const { | ||
theme: { colors }, | ||
} = useTheme() | ||
const isFocused = useIsFocused() | ||
|
||
const { LL } = useI18nContext() | ||
|
||
const { data, fetchMore, refetch, loading } = useStatefulNotificationsQuery({ | ||
skip: !useIsAuthed(), | ||
}) | ||
const notifications = data?.me?.statefulNotifications | ||
|
||
const fetchNextNotificationsPage = () => { | ||
const pageInfo = notifications?.pageInfo | ||
|
||
if (pageInfo?.hasNextPage) { | ||
fetchMore({ | ||
variables: { | ||
after: pageInfo.endCursor, | ||
}, | ||
}) | ||
} | ||
} | ||
|
||
return ( | ||
<Screen> | ||
<FlatList | ||
{...testProps("notification-screen")} | ||
contentContainerStyle={styles.scrollViewContainer} | ||
refreshControl={ | ||
<RefreshControl | ||
refreshing={loading && isFocused} | ||
onRefresh={refetch} | ||
colors={[colors.primary]} // Android refresh indicator colors | ||
tintColor={colors.primary} // iOS refresh indicator color | ||
/> | ||
} | ||
data={notifications?.nodes} | ||
renderItem={({ item }) => <Notification {...item} />} | ||
onEndReached={fetchNextNotificationsPage} | ||
onEndReachedThreshold={0.5} | ||
onRefresh={refetch} | ||
refreshing={loading} | ||
ListEmptyComponent={ | ||
loading ? <></> : <Text>{LL.NotificationHistory.noNotifications()}</Text> | ||
} | ||
></FlatList> | ||
</Screen> | ||
) | ||
} | ||
|
||
const useStyles = makeStyles(() => ({ | ||
scrollViewContainer: {}, | ||
})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { | ||
StatefulNotification, | ||
StatefulNotificationsDocument, | ||
useStatefulNotificationAcknowledgeMutation, | ||
} from "@app/graphql/generated" | ||
import { Text, makeStyles } from "@rneui/themed" | ||
import { View } from "react-native" | ||
import { timeAgo } from "./utils" | ||
import { gql } from "@apollo/client" | ||
import { TouchableWithoutFeedback } from "react-native-gesture-handler" | ||
import { useLinkTo } from "@react-navigation/native" | ||
import { useState } from "react" | ||
|
||
gql` | ||
mutation StatefulNotificationAcknowledge( | ||
$input: StatefulNotificationAcknowledgeInput! | ||
) { | ||
statefulNotificationAcknowledge(input: $input) { | ||
notification { | ||
acknowledgedAt | ||
} | ||
} | ||
} | ||
` | ||
|
||
export const Notification: React.FC<StatefulNotification> = ({ | ||
id, | ||
title, | ||
body, | ||
createdAt, | ||
acknowledgedAt, | ||
deepLink, | ||
}) => { | ||
const [isAcknowledged, setIsAcknowledged] = useState(Boolean(acknowledgedAt)) | ||
const styles = useStyles({ isAcknowledged }) | ||
|
||
const [ack, _] = useStatefulNotificationAcknowledgeMutation({ | ||
variables: { input: { notificationId: id } }, | ||
refetchQueries: [StatefulNotificationsDocument], | ||
}) | ||
|
||
const linkTo = useLinkTo() | ||
|
||
return ( | ||
<TouchableWithoutFeedback | ||
onPress={() => { | ||
setIsAcknowledged(true) | ||
!isAcknowledged && ack() | ||
deepLink && linkTo(deepLink) | ||
}} | ||
> | ||
<View style={styles.container}> | ||
<Text type="p2" style={styles.text}> | ||
{title} | ||
</Text> | ||
<Text type="p3" style={styles.text}> | ||
{body} | ||
</Text> | ||
<Text type="p4" style={styles.text}> | ||
{timeAgo(createdAt)} | ||
</Text> | ||
</View> | ||
</TouchableWithoutFeedback> | ||
) | ||
} | ||
|
||
const useStyles = makeStyles( | ||
({ colors }, { isAcknowledged }: { isAcknowledged: boolean }) => ({ | ||
container: { | ||
padding: 10, | ||
borderBottomWidth: 1, | ||
borderBottomColor: colors.grey5, | ||
}, | ||
text: { | ||
color: isAcknowledged ? colors.grey3 : colors.black, | ||
}, | ||
}), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export const timeAgo = (pastDate: number): string => { | ||
const now = new Date() | ||
const past = new Date(pastDate * 1000) | ||
const diff = Number(now) - Number(past) | ||
|
||
const seconds = Math.floor(diff / 1000) | ||
const minutes = Math.floor(seconds / 60) | ||
const hours = Math.floor(minutes / 60) | ||
const days = Math.floor(hours / 24) | ||
|
||
if (seconds < 60) { | ||
return `a few seconds ago` | ||
} else if (minutes < 60) { | ||
return `${minutes} minute${minutes > 1 ? "s" : ""} ago` | ||
} else if (hours < 24) { | ||
return `${hours} hour${hours > 1 ? "s" : ""} ago` | ||
} | ||
return `${days} day${days > 1 ? "s" : ""} ago` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters