Skip to content

Commit

Permalink
user profile start (Memmy-App#109)
Browse files Browse the repository at this point in the history
* add search bar

* make button one selectable

* selectable is optional

* make icon optional for buttonone

* adjustable py for button one

* button two

* start on search screen

* scroll to top when pressing feed icon again

* search screen with buttons

* search, working for communities

* push to post

* add feeds to search stack

* dont use autocomplete or auto cap for search

* user profile
  • Loading branch information
gkasdorf authored Jun 21, 2023
1 parent 9024bca commit 1ea6101
Show file tree
Hide file tree
Showing 10 changed files with 361 additions and 47 deletions.
56 changes: 50 additions & 6 deletions Stack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,57 @@ function SearchStackScreen() {
},
}}
>
<SearchStack.Screen
name="Search"
component={SearchScreen}
options={{
title: "Search",
<SearchStack.Group>
<SearchStack.Screen
name="Search"
component={SearchScreen}
options={{
title: "Search",
}}
/>
<SearchStack.Screen
name="FeedScreen"
component={FeedsIndexScreen}
options={{
title: "Feed",
}}
/>
<SearchStack.Screen name="Post" component={PostScreen} />
<SearchStack.Screen name="Community" component={CommunityFeedScreen} />
<SearchStack.Screen
name="Subscriptions"
component={SubscriptionsScreen}
/>
<SearchStack.Screen
name="UserProfile"
component={UserProfileScreen}
options={{
title: "Your Profile",
}}
/>
</SearchStack.Group>

<SearchStack.Group
screenOptions={{
presentation: "modal",
}}
/>
>
<SearchStack.Screen
name="NewComment"
component={NewCommentScreen}
options={{ title: "New Comment" }}
/>
<SearchStack.Screen
name="NewPost"
component={NewPostScreen}
options={{ title: "New Post" }}
/>
<SearchStack.Screen
name="CommunityAbout"
component={CommunityAboutScreen}
options={{ title: "About" }}
/>
</SearchStack.Group>
</SearchStack.Navigator>
);
}
Expand Down
80 changes: 80 additions & 0 deletions components/hooks/profile/useProfile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { useEffect, useState } from "react";
import {
CommentView,
PersonViewSafe,
PostView,
SortType,
} from "lemmy-js-client";
import { lemmyAuthToken, lemmyInstance } from "../../../lemmy/LemmyInstance";
import { writeToLog } from "../../../helpers/LogHelper";
import { useAppSelector } from "../../../store";
import { selectCurrentAccount } from "../../../slices/accounts/accountsSlice";

interface UseProfile {
loading: boolean;
error: boolean;
profile: PersonViewSafe;
posts: PostView[];
comments: CommentView[];
doLoad: () => Promise<void>;
self: boolean;
}

const useProfile = (fullUsername?: string): UseProfile => {
const currentAccount = useAppSelector(selectCurrentAccount);

const [loading, setLoading] = useState<boolean>(true);
const [error, setError] = useState<boolean>(false);
const [profile, setProfile] = useState<PersonViewSafe>(null);
const [posts, setPosts] = useState<PostView[]>(null);
const [comments, setComments] = useState<CommentView[]>(null);
const [self] = useState(!fullUsername);

useEffect(() => {
doLoad().then();
}, []);

const doLoad = async () => {
let searchUsername = fullUsername;

if (!fullUsername) {
searchUsername = `${currentAccount.username}@${currentAccount.instance}`;
}

setLoading(true);
setError(false);

try {
const res = await lemmyInstance.getPersonDetails({
auth: lemmyAuthToken,
username: searchUsername,
sort: SortType.New,
limit: 20,
});

setProfile(res.person_view);
setComments(res.comments);
setPosts(res.posts);
setLoading(false);
} catch (e) {
writeToLog("Error getting person.");
writeToLog(e.toString());

setLoading(false);
setError(true);
}
};

return {
loading,
error,
profile,
posts,
comments,
self,

doLoad,
};
};

export default useProfile;
1 change: 0 additions & 1 deletion components/screens/feeds/CommunityFeedScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { subscribeToCommunity } from "../../../slices/communities/communitiesAct
import { useAppDispatch, useAppSelector } from "../../../store";
import { selectPost } from "../../../slices/post/postSlice";
import NotFoundView from "../../ui/Loading/NotFoundView";
import NoPostsView from "../../ui/Feed/NoPostsView";

function FeedsCommunityScreen({
route,
Expand Down
47 changes: 36 additions & 11 deletions components/screens/search/SearchScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useEffect } from "react";
import { NativeStackNavigationProp } from "@react-navigation/native-stack";
import { HStack, ScrollView, Text, useTheme, VStack } from "native-base";
import { SearchType } from "lemmy-js-client";
import { PersonViewSafe, PostView, SearchType } from "lemmy-js-client";
import useSearch from "../../hooks/search/useSearch";
import SearchBar from "../../ui/search/SearchBar";
import ButtonTwo from "../../ui/buttons/ButtonTwo";
Expand All @@ -10,6 +10,8 @@ import GenericSearchResult from "../../ui/search/GenericSearchResult";
import { getBaseUrl } from "../../../helpers/LinkHelper";
import LoadingView from "../../ui/Loading/LoadingView";
import { getCommunityFullName } from "../../../lemmy/LemmyHelpers";
import { setPost } from "../../../slices/post/postSlice";
import { useAppDispatch } from "../../../store";

function SearchScreen({
navigation,
Expand All @@ -18,11 +20,20 @@ function SearchScreen({
}) {
const search = useSearch();
const theme = useTheme();
const dispatch = useAppDispatch();

useEffect(() => {
console.log(search.searchType);
console.log(search.result);
}, [search]);
const onPostPress = (post: PostView) => {
dispatch(setPost(post));
navigation.push("Post");
};

const onUserPress = (person: PersonViewSafe) => {
navigation.push("UserProfile", {
fullUsername: `${person.person.name}@${getBaseUrl(
person.person.actor_id
)}`,
});
};

return (
<VStack flex={1} backgroundColor={theme.colors.app.backgroundSecondary}>
Expand All @@ -33,17 +44,20 @@ function SearchScreen({
/>
<HStack px={4} py={4} space={2}>
<ButtonTwo
onPress={() => search.doSearch(SearchType.Communities)}
// onPress={() => search.doSearch(SearchType.Communities)}
onPress={() => {}}
text="Communities"
selectable
/>
<ButtonTwo
onPress={() => search.doSearch(SearchType.Posts)}
// onPress={() => search.doSearch(SearchType.Posts)}
onPress={() => {}}
text="Posts"
selectable
/>
<ButtonTwo
onPress={() => search.doSearch(SearchType.Users)}
// onPress={() => search.doSearch(SearchType.Users)}
onPress={() => {}}
text="Users"
selectable
/>
Expand Down Expand Up @@ -74,6 +88,7 @@ function SearchScreen({
<SearchResultTypeHeader type={SearchType.Communities} />
{search.result.communities.map((r) => (
<GenericSearchResult
key={r.community.id}
header={r.community.name}
footer={getBaseUrl(r.community.actor_id)}
side={`${r.counts.subscribers} subscribers`}
Expand All @@ -95,11 +110,14 @@ function SearchScreen({
{search.result.users.length > 0 &&
search.result.users.map((r) => (
<GenericSearchResult
key={r.person.id}
header={r.person.name}
footer={getBaseUrl(r.person.actor_id)}
side={`${r.counts.post_count} posts`}
image={r.person.avatar}
onPress={() => {}}
onPress={() => {
onUserPress(r);
}}
/>
))}
</>
Expand All @@ -110,16 +128,23 @@ function SearchScreen({
{search.result.posts.length > 0 &&
search.result.posts.map((r) => (
<GenericSearchResult
key={r.post.id}
header={r.post.name}
footer={getBaseUrl(r.post.ap_id)}
image={r.community.icon}
onPress={() => {}}
onPress={() => {
onPostPress(r);
}}
/>
))}
</>
)}
</>
)) || <Text>Somethjing went wrong.</Text>}
)) || (
<Text fontSize="xl" fontWeight="semibold" textAlign="center">
Please enter a search term.
</Text>
)}
</VStack>
</ScrollView>
</VStack>
Expand Down
Loading

0 comments on commit 1ea6101

Please sign in to comment.