Skip to content

Commit

Permalink
fix: refresh post data on refresh (#803)
Browse files Browse the repository at this point in the history
  • Loading branch information
gkasdorf authored Jul 22, 2023
1 parent 2b9cbc7 commit 73e60f3
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/components/screens/Post/PostScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ function PostScreen({ navigation }: IProps) {
() => (
<RefreshControl
refreshing={commentsStatus.commentsLoading}
onRefresh={postHook.doLoad}
onRefresh={postHook.doRefresh}
/>
),
[commentsStatus.commentsLoading]
Expand Down
8 changes: 8 additions & 0 deletions src/hooks/post/usePost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import { setPostCollapsed } from "../../stores/posts/actions";
import loadPostComments from "../../stores/posts/actions/loadPostComments";
import loadCommunity from "../../stores/communities/actions/loadCommunity";
import { getBaseUrl } from "../../helpers/LinkHelper";
import refreshPost from "../../stores/posts/actions/refreshPost";

export interface UsePost {
doLoad: () => void;
doRefresh: () => void;
onPostPress: () => void;
setPostCommentsSort: (sortType: CommentSortType) => void;
}
Expand Down Expand Up @@ -45,8 +47,14 @@ const usePost = (): UsePost => {
});
}, []);

const doRefresh = useCallback(() => {
doLoad();
refreshPost(postKey).then();
}, []);

return {
doLoad,
doRefresh,
onPostPress,
setPostCommentsSort,
};
Expand Down
9 changes: 7 additions & 2 deletions src/stores/posts/actions/addPost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ const addPost = (postKey: string, post: PostView) => {
usePostsStore.setState((state) => {
state.posts.set(postKey, {
post,
postError: false,
postLoading: false,

status: {
loading: false,
error: false,
refreshing: false,
},

collapsed: false,
rerenderComments: false,

Expand Down
32 changes: 32 additions & 0 deletions src/stores/posts/actions/refreshPost.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { usePostsStore } from "../postsStore";
import { lemmyAuthToken, lemmyInstance } from "../../../LemmyInstance";
import { handleLemmyError } from "../../../helpers/LemmyErrorHelper";

const refreshPost = async (postKey) => {
const currentState = usePostsStore.getState().posts.get(postKey);

usePostsStore.setState((state) => {
state.posts.get(postKey).status.refreshing = true;
});

try {
const res = await lemmyInstance.getPost({
auth: lemmyAuthToken,
id: currentState.post.post.id,
});

usePostsStore.setState((state) => {
const prev = state.posts.get(postKey);
prev.post = res.post_view;
prev.status.refreshing = false;
});
} catch (e) {
handleLemmyError(e.toString());

usePostsStore.setState((state) => {
state.posts.get(postKey).status.refreshing = false;
});
}
};

export default refreshPost;
8 changes: 6 additions & 2 deletions src/stores/posts/postsStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ export interface PostCommentsState {

export interface PostState {
post: PostView;
postLoading: boolean;
postError: boolean;

status: {
loading: boolean;
error: boolean;
refreshing: boolean;
};

collapsed: boolean;

Expand Down

0 comments on commit 73e60f3

Please sign in to comment.