Skip to content

Commit

Permalink
Merge pull request #545 from bounswe/feat/MB-search-filter-and-colorfix
Browse files Browse the repository at this point in the history
Feat/mb search filter and colorfix
  • Loading branch information
furkansenkal authored Dec 16, 2024
2 parents 8bed908 + 882d37e commit 5002fb6
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 68 deletions.
9 changes: 4 additions & 5 deletions mobile/__tests__/CreatePost.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import React from 'react';
import { render, fireEvent, waitFor } from '@testing-library/react-native';
import CreatePost from '../src/pages/CreatePost'; // Adjust the import as necessary
import { useAuth } from '../src/hooks/useAuth'; // Adjust the import path as necessary
import CreatePost from '../src/pages/CreatePost';
import { useAuth } from '../src/hooks/useAuth';

// Mock useAuth hook
jest.mock('../src/hooks/useAuth', () => ({
useAuth: () => ({
accessToken: 'mockAccessToken', // Provide a default or mock value
accessToken: 'mockAccessToken',
userId: 'mockUserId',
}),
}));
Expand All @@ -18,7 +17,7 @@ describe('CreatePost Component', () => {
it('toggles tag selection', () => {
const { getByText, getByTestId } = render(<CreatePost />);

const tagChip = getByText('Tech'); // Assuming 'Tech' is the text of the tag chip
const tagChip = getByText('Tech');
fireEvent.press(tagChip);

expect(getByText("Tech").props.style.backgroundColor).toBe("#007BFF");
Expand Down
2 changes: 1 addition & 1 deletion mobile/__tests__/Post.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import renderer from 'react-test-renderer';
import { render, waitFor, fireEvent } from '@testing-library/react-native';
import Post from '../src/pages/Post'; // Adjust the import as necessary
import Post from '../src/pages/Post';

jest.mock('node-fetch', () => require('jest-fetch-mock'));

Expand Down
2 changes: 1 addition & 1 deletion mobile/__tests__/Profile-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import renderer from 'react-test-renderer';
import Profile from '../src/pages/ProfilePage';

// Mock the useAuth hook

jest.mock('../src/pages/context/AuthContext', () => ({
useAuth: () => ({
username: 'testuser',
Expand Down
56 changes: 8 additions & 48 deletions mobile/src/pages/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ import Community from './Community';
import Post from './Post';
import CreatePost from './CreatePost';
import StockDetails from './StockDetails';
import Portfolio from './Portfolio';
import PortfolioDetails from './PortfolioDetails';
import CreatePortfolio from './CreatePortfolio';

import { AuthProvider, useAuth } from './context/AuthContext';

Expand Down Expand Up @@ -92,31 +89,6 @@ const MarketsStack = () => (
</Stack.Navigator>
);

const PortfolioStack = () => (
<Stack.Navigator>
<Stack.Screen
name="Markets1"
component={Portfolio}
options={{ header: ({ navigation }) => <CustomHeader navigation={navigation} /> }}
/>
<Stack.Screen
name="StockDetails"
component={StockDetails}
options={{ headerShown: true, title: 'Stock Details' }}
/>
<Stack.Screen
name="PortfolioDetails"
component={PortfolioDetails}
options={{ headerShown: true, title: 'Portfolio Details' }}
/>
<Stack.Screen
name="CreatePortfolio"
component={CreatePortfolio}
options={{ headerShown: true, title: 'Create Portfolio' }}
/>
</Stack.Navigator>
);

const DrawerNavigator = () => {
const { username, userId } = useAuth();

Expand All @@ -130,6 +102,11 @@ const DrawerNavigator = () => {
}}

>
<Drawer.Screen
name="Community"
component={PostStack}
options={{ headerShown: false }}
/>
<Drawer.Screen
name="Home"
component={Home}
Expand All @@ -154,29 +131,12 @@ const DrawerNavigator = () => {
options={{ headerShown: false }}

/>
<Drawer.Screen
name="Community"
component={PostStack}
options={{ headerShown: false }}
/>

<Drawer.Screen
name="News"
component={News}

/>
{ userId ? (
<Drawer.Screen
name="Portfolio"
component={PortfolioStack}
options={{ headerShown: false }}

/>
) : (
<Drawer.Screen
name="Portfolio"
component={LoginStack}
options={{ headerShown: false }}
/>
)}

</Drawer.Navigator>
);
Expand All @@ -198,7 +158,7 @@ const App = () => {
const styles = StyleSheet.create({
customHeader: {
height: 60,
backgroundColor: '#007BFF',
backgroundColor: '#0077B6',
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
Expand Down
79 changes: 77 additions & 2 deletions mobile/src/pages/Community.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import { View, Text, FlatList, ScrollView, Modal, StyleSheet, TextInput, TouchableOpacity, Image, Alert } from 'react-native';
import { Picker } from '@react-native-picker/picker';
import { useState, useEffect } from 'react';
import { useFocusEffect } from '@react-navigation/native';
import config from './config/config';
Expand All @@ -14,6 +15,10 @@ const Community = ({navigation}) => {
const [userMap, setUserMap] = useState([]);
const [showCommentInput, setShowCommentInput] = useState(false);
const [commentText, setCommentText] = useState('');
const [selectedTag, setSelectedTag] = useState(''); // Selected tag for filtering
const [allTags, setAllTags] = useState([]); // List of all available tags
const [searchQuery, setSearchQuery] = useState('');


const fetchPosts = async () => {
const postURL = baseURL + '/posts/?page=1';
Expand All @@ -39,6 +44,24 @@ const Community = ({navigation}) => {
}
};

const fetchTags = async () => {
try {
const response = await fetch(`${baseURL}/tags/`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
});
if (response.ok) {
const data = await response.json();
setAllTags(data);
} else {
console.error("Failed to fetch tags");
}
} catch (error) {
console.error("Error fetching tags:", error);
}
};

const fetchUsers = async () => {
const postURL = baseURL + '/users/';
Expand Down Expand Up @@ -132,6 +155,7 @@ const Community = ({navigation}) => {
React.useCallback(() => {
fetchPosts();
fetchUsers();
fetchTags();
}, [])
);

Expand Down Expand Up @@ -186,9 +210,20 @@ const Community = ({navigation}) => {
return post.author;
}
}
const filteredPosts = posts.filter((post) => {
const matchesSearch = post.title.toLowerCase().includes(searchQuery.toLowerCase()) ||
post.content.toLowerCase().includes(searchQuery.toLowerCase());

const matchesTag = selectedTag === 'All' || post.tags.some(tag => tag.name === selectedTag);

return matchesSearch && matchesTag;
});




const renderItem = ({ item: post }) => (
//console.log("post", post),
console.log(allTags),
<View key={post.id} style={styles.postCard}>
<Text style={styles.postTitle}>{post.title}</Text>
<Text style={styles.postMeta}>
Expand Down Expand Up @@ -235,12 +270,29 @@ const Community = ({navigation}) => {
<TextInput
style={styles.searchBar}
placeholder="Search posts..."
value={searchQuery}
onChangeText={setSearchQuery}
/>

<View style={styles.filterContainer}>
<Text style={styles.filterLabel}>Filter by Tag:</Text>
<Picker
selectedValue={selectedTag}
onValueChange={(itemValue) => setSelectedTag(itemValue)}
style={styles.picker}
>
<Picker.Item label="All Tags" value="" />
{allTags.map((tag) => (
<Picker.Item key={tag.id} label={tag.name} value={tag.name} />
))}
</Picker>
</View>
<FlatList
data={posts}
data={filteredPosts}
renderItem={renderItem}
keyExtractor={(item) => item.id.toString()}
/>

<Modal visible={showCommentInput} animationType="slide" transparent>
<View style={styles.modalContainer}>
<View style={styles.modalContent}>
Expand Down Expand Up @@ -279,6 +331,7 @@ const styles = StyleSheet.create({
fontWeight: 'bold',
textAlign: 'center',
marginVertical: 20,
color: 'black',
},
createPostButton: {
backgroundColor: '#007BFF',
Expand Down Expand Up @@ -425,6 +478,28 @@ const styles = StyleSheet.create({
fontWeight: 'bold',
textAlign: 'center',
},
filterContainer: {
flexDirection: 'row',
alignItems: 'center',
marginBottom: 15,
paddingHorizontal: 10,
},
filterLabel: {
fontSize: 16,
fontWeight: 'bold',
marginRight: 10,
color: 'black',
},
picker: {
flex: 1,
height: 40,
marginLeft: 10,
borderWidth: 1,
borderColor: '#ddd',
borderRadius: 5,
backgroundColor: '#fff',
},

});

export default Community;
4 changes: 2 additions & 2 deletions mobile/src/pages/CreatePost.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const CreatePost = ({ navigation }) => {
useEffect(() => {
console.log("CreatePost access", userId);
fetchTags();
fetchStocks(1); // Fetch the first page of stocks on mount
fetchStocks(1);
}, []);

const fetchTags = async () => {
Expand Down Expand Up @@ -82,7 +82,7 @@ const CreatePost = ({ navigation }) => {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
/* Authorization: `Bearer ${accessToken}`, */
},
body: JSON.stringify({ pattern: query, limit: 10 }),
});
Expand Down
7 changes: 3 additions & 4 deletions mobile/src/pages/Markets.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ const Markets = ({ navigation }) => {
method: 'GET',
headers: {
accept: 'application/json',
Authorization: 'Basic ZnVya2Fuc2Vua2FsOkxvc29sdmlkYWRvcy41NQ==',
'X-CSRFToken': 'HN4gYGlxSnwtGKK91OG9c6WC6gr8091Pm5Kof3t0WoTHOe0Z2ToubTZUdlOkjR34',

},
}
);
Expand Down Expand Up @@ -77,9 +76,9 @@ const Markets = ({ navigation }) => {
method: 'POST',
headers: {
accept: 'application/json',
Authorization: 'Basic ZnVya2Fuc2Vua2FsOkxvc29sdmlkYWRvcy41NQ==',

'Content-Type': 'application/json',
'X-CSRFToken': 'xGhS17H7qedbZRMF0ULpzKQhKe6mG11WcYX0iuPAufAp7l2v1ZtKyxTzRjtyZJ3b',

},
body: JSON.stringify({
pattern: query,
Expand Down
Loading

0 comments on commit 5002fb6

Please sign in to comment.