-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(news): add NewsCard component with carousel
- Loading branch information
Showing
11 changed files
with
166 additions
and
12 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import API from '@/api/authenticated-api' | ||
import { UserKindContext } from '@/components/contexts' | ||
import { USER_GUEST } from '@/data/constants' | ||
import { type ThiNews } from '@/types/thi-api' | ||
import { useQuery } from '@tanstack/react-query' | ||
import React, { useContext, useLayoutEffect, useRef } from 'react' | ||
import { useTranslation } from 'react-i18next' | ||
import { Dimensions, Image, Linking, Pressable, Text, View } from 'react-native' | ||
import Carousel from 'react-native-reanimated-carousel' | ||
import { createStyleSheet, useStyles } from 'react-native-unistyles' | ||
|
||
import BaseCard from './BaseCard' | ||
|
||
const NewsCard: React.FC = () => { | ||
const ref = useRef(null) | ||
const { t } = useTranslation('navigation') | ||
const { styles } = useStyles(stylesheet) | ||
const { userKind = USER_GUEST } = useContext(UserKindContext) | ||
const { data } = useQuery({ | ||
queryKey: ['thiNews'], | ||
queryFn: async () => await API.getThiNews(), | ||
staleTime: 1000 * 60 * 10, // 10 minutes | ||
gcTime: 1000 * 60 * 60 * 24, // 24 hours | ||
enabled: userKind !== USER_GUEST, | ||
}) | ||
const [cardWidth, setCardWidth] = React.useState( | ||
Dimensions.get('window').width - 32 | ||
) | ||
useLayoutEffect(() => { | ||
if (ref.current != null) { | ||
// @ts-expect-error unstable_getBoundingClientRect is not in the types | ||
const width = ref.current.unstable_getBoundingClientRect() | ||
.width as number | ||
setCardWidth(width) | ||
} | ||
}, []) | ||
|
||
const renderEvent = (event: ThiNews): JSX.Element => { | ||
return ( | ||
<Pressable | ||
style={styles.eventContainer} | ||
onPress={() => { | ||
void Linking.openURL(event.href) | ||
}} | ||
> | ||
<Image | ||
style={styles.imageContainer} | ||
source={{ | ||
uri: event.img, | ||
}} | ||
/> | ||
<Text style={styles.eventTitle} numberOfLines={3}> | ||
{event.title} | ||
</Text> | ||
</Pressable> | ||
) | ||
} | ||
|
||
return ( | ||
<View ref={ref}> | ||
<BaseCard title="news" onPressRoute="news"> | ||
{data != null && data.length > 0 && ( | ||
<Carousel | ||
loop | ||
height={115} | ||
width={cardWidth * 0.87} | ||
style={styles.carousel} | ||
data={data} | ||
snapEnabled={true} | ||
autoPlay={true} | ||
autoPlayInterval={7500} | ||
renderItem={({ index }) => ( | ||
<View style={styles.cardsFilled}> | ||
{renderEvent(data[index])} | ||
</View> | ||
)} | ||
></Carousel> | ||
)} | ||
{data != null && data.length > 2 && ( | ||
<View style={styles.cardsFilled}> | ||
<Text style={styles.description} numberOfLines={3}> | ||
{t('cards.news.more', { | ||
count: data.length, | ||
})} | ||
</Text> | ||
</View> | ||
)} | ||
</BaseCard> | ||
</View> | ||
) | ||
} | ||
|
||
const stylesheet = createStyleSheet((theme) => ({ | ||
cardsFilled: { gap: 8, paddingTop: 12 }, | ||
carousel: { | ||
alignItems: 'center', | ||
justifyContent: 'center', | ||
marginTop: 4, | ||
width: '100%', | ||
}, | ||
description: { | ||
color: theme.colors.labelColor, | ||
fontSize: 14, | ||
fontWeight: '500', | ||
}, | ||
eventContainer: { | ||
alignItems: 'center', | ||
backgroundColor: theme.colors.cardButton, | ||
borderRadius: theme.radius.md, | ||
flexDirection: 'row', | ||
gap: 12, | ||
marginHorizontal: 4, | ||
padding: 10, | ||
}, | ||
eventTitle: { | ||
color: theme.colors.text, | ||
flexShrink: 1, | ||
fontSize: 15, | ||
fontWeight: '500', | ||
}, | ||
imageContainer: { | ||
borderRadius: theme.radius.sm, | ||
height: 80, | ||
resizeMode: 'cover', | ||
width: '35%', | ||
}, | ||
})) | ||
|
||
export default NewsCard |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -143,6 +143,12 @@ | |
"licenseUrl": "https://github.com/expo/expo", | ||
"parents": "neuland" | ||
}, | ||
"[email protected]": { | ||
"licenses": "MIT", | ||
"repository": "https://github.com/expo/expo", | ||
"licenseUrl": "https://github.com/expo/expo", | ||
"parents": "neuland" | ||
}, | ||
"[email protected]": { | ||
"licenses": "MIT", | ||
"repository": "https://github.com/expo/expo", | ||
|
@@ -191,7 +197,7 @@ | |
"licenseUrl": "https://github.com/expo/expo", | ||
"parents": "neuland" | ||
}, | ||
"[email protected].13": { | ||
"[email protected].16": { | ||
"licenses": "MIT", | ||
"repository": "https://github.com/expo/expo", | ||
"licenseUrl": "https://github.com/expo/expo", | ||
|
@@ -203,7 +209,7 @@ | |
"licenseUrl": "https://github.com/expo/expo", | ||
"parents": "neuland" | ||
}, | ||
"[email protected].15": { | ||
"[email protected].17": { | ||
"licenses": "MIT", | ||
"repository": "https://github.com/expo/expo", | ||
"licenseUrl": "https://github.com/expo/expo", | ||
|
@@ -215,12 +221,6 @@ | |
"licenseUrl": "https://github.com/krisk/Fuse/raw/master/LICENSE", | ||
"parents": "neuland" | ||
}, | ||
"[email protected]": { | ||
"licenses": "MIT", | ||
"repository": "https://github.com/jasonkuhrt/graphql-request", | ||
"licenseUrl": "https://github.com/jasonkuhrt/graphql-request/raw/master/LICENSE", | ||
"parents": "neuland" | ||
}, | ||
"[email protected]": { | ||
"licenses": "MIT", | ||
"repository": "https://github.com/graphql/graphql-js", | ||
|
@@ -329,6 +329,12 @@ | |
"licenseUrl": "https://github.com/callstack/react-native-paper/raw/master/LICENSE.md", | ||
"parents": "neuland" | ||
}, | ||
"[email protected]": { | ||
"licenses": "MIT", | ||
"repository": "https://github.com/dohooo/react-native-reanimated-carousel", | ||
"licenseUrl": "https://github.com/dohooo/react-native-reanimated-carousel/raw/master/LICENSE", | ||
"parents": "neuland" | ||
}, | ||
"[email protected]": { | ||
"licenses": "MIT", | ||
"repository": "https://github.com/software-mansion/react-native-reanimated", | ||
|
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