Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues 105 #108

Merged
merged 2 commits into from
Jan 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 58 additions & 19 deletions components/user/home-screen/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Image,
View,
Button,
SectionList,
} from 'react-native';
import Post from './Post';
import {useJournalEntries} from '@/providers/JournalEntriesProvider';
Expand Down Expand Up @@ -96,6 +97,22 @@ export default function HomeScreen() {
setSelectedPostId(id);
}
};
// Group entries by month
const [sections, setSections] = useState([]);
useEffect(() => {
const groupedEntries = initialJournalEntries.reduce((acc, entry) => {
const date = new Date(entry.date);
const monthYear = `${date.toLocaleString('default', { month: 'long' })} ${date.getFullYear()}`;
if (!acc[monthYear]) {
acc[monthYear] = [];
}
acc[monthYear].push(entry);
return acc;
}, {});

const formattedSections = Object.entries(groupedEntries).map(([title, data]) => ({ title, data }));
setSections(formattedSections);
}, [initialJournalEntries]);

return (
<SafeAreaView style={styles.view} edges={['left', 'right']}>
Expand All @@ -109,23 +126,29 @@ export default function HomeScreen() {
source={require('../../../assets/images/home-screen/gradient-home-screen.png')}
>
{/* Start of lists */}
<FlatList
data={journalEntries}
renderItem={({ item }) => (
<Post
id={item.id}
date={item.date}
title={item.title}
content={item.content}
tags={item.tags}
onOpen={() => openPopupMenu(item.id)}
/>
)}
style={styles.list}
keyExtractor={(item) => item.id}
refreshing={isLoading}
onRefresh={handleRefresh}
/>

<SectionList
sections={sections}
renderItem={({ item }) => (
<Post
id={item.id}
date={item.date}
title={item.title}
content={item.content}
tags={item.tags}
onOpen={() => openPopupMenu(item.id)}
/>
)}
renderSectionHeader={({ section: { title } }) => (
<View style={styles.sectionHeader}>
<Text style={styles.sectionHeaderText}>{title}</Text>
</View>
)}
style={styles.list}
keyExtractor={(item) => item.id}
refreshing={isLoading}
onRefresh={handleRefresh}
/>

{/* Popup menu to edit, delete selected post */}
<Popup ref={popupRef}>
Expand Down Expand Up @@ -192,8 +215,9 @@ export default function HomeScreen() {
source={require('../../../assets/images/home-screen/white-bg.jpg')}
>
{/* Start of lists */}
<FlatList
data={journalEntries}

<SectionList
sections={sections}
renderItem={({ item }) => (
<Post
id={item.id}
Expand All @@ -204,6 +228,11 @@ export default function HomeScreen() {
onOpen={() => openPopupMenu(item.id)}
/>
)}
renderSectionHeader={({ section: { title } }) => (
<View style={styles.sectionHeader}>
<Text style={styles.sectionHeaderText}>{title}</Text>
</View>
)}
style={styles.list}
keyExtractor={(item) => item.id}
refreshing={isLoading}
Expand Down Expand Up @@ -307,4 +336,14 @@ const styles = StyleSheet.create({
borderBottomWidth: 1.5,
borderBlockColor: '#ECEAEA',
},
sectionHeader: {
padding: 6,
marginTop: 22,
marginLeft: 5
},
sectionHeaderText: {
fontSize: 20,
fontWeight: '700',
color: '#62239B',
},
});