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

Pano feed filters #680

Merged
merged 4 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
54 changes: 49 additions & 5 deletions apps/gql/schema/resolvers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,56 @@ export const resolvers = {

return transformUser(user);
},
panoFeed: async (_, args, { loaders }) => {
const posts = await loaders.pano.post.all.load(
new ConnectionKey(null, parseConnectionArgs(args), { orderBy: { createdAt: "desc" } })
);
panoFeed: async (_, args, { loaders, pasaport: { session } }) => {
switch (args.filter) {
case "MOST_COMMENTED": {
const posts = await loaders.pano.post.all.load(
new ConnectionKey(null, parseConnectionArgs(args), {
orderBy: { comments: { _count: "desc" } },
})
);

return transformPanoPostConnection(posts);
}
case "ACTIVE": {
const posts = await loaders.pano.post.all.load(
new ConnectionKey(null, parseConnectionArgs(args), {
include: { comments: { orderBy: { createdAt: "desc" } } },
orderBy: { createdAt: "desc" },
})
);

return transformPanoPostConnection(posts);
}
case "MOST_UPVOTED": {
const posts = await loaders.pano.post.all.load(
new ConnectionKey(null, parseConnectionArgs(args), {
orderBy: { upvotes: { _count: "desc" } },
})
);

return transformPanoPostConnection(posts);
}
case "OWNED": {
if (!session?.user?.id) {
return null;
}

return transformPanoPostConnection(posts);
return transformPanoPostConnection(
await loaders.pano.post.byUserID.load(
new ConnectionKey(session.user.id, parseConnectionArgs(args), {
orderBy: { createdAt: "desc" },
})
)
);
}
default: {
const posts = await loaders.pano.post.all.load(
new ConnectionKey(null, parseConnectionArgs(args), { orderBy: { createdAt: "desc" } })
);
return transformPanoPostConnection(posts);
}
}
},
},

Expand Down
14 changes: 10 additions & 4 deletions apps/gql/schema/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,17 @@ type PanoQuery {
}

enum PanoPostFilter {
ALL
HOT
LIKED
# Return list of posts that's ordered by their most recent comment.
ACTIVE
SELF

# Return a list of posts that's ordered by their comment count.
MOST_COMMENTED

# Return a list of posts that's ordered by their upvote count.
MOST_UPVOTED

# Return viewer's posts
OWNED
}

type PanoPost implements Node & Upvotable {
Expand Down
2 changes: 1 addition & 1 deletion apps/gql/schema/types.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ export type PanoPostEdge = {

export type PanoPostError = InvalidInput | NotAuthorized;

export type PanoPostFilter = "ACTIVE" | "ALL" | "HOT" | "LIKED" | "SELF";
export type PanoPostFilter = "ACTIVE" | "MOST_COMMENTED" | "MOST_UPVOTED" | "OWNED";

export type PanoQuery = {
__typename?: "PanoQuery";
Expand Down
9 changes: 5 additions & 4 deletions apps/kampus/app/pano/PanoFeed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ const fragment = graphql`
fragment PanoFeedFragment on Viewer
@argumentDefinitions(
after: { type: "String" }
first: { type: "Int", defaultValue: 10 }
first: { type: "Int", defaultValue: 20 }
before: { type: "String" }
last: { type: "Int" }
filter: { type: "PanoPostFilter" }
)
@refetchable(queryName: "PanoFeedPaginationQuery") {
panoFeed(first: $first, after: $after, last: $last, before: $before)
panoFeed(first: $first, after: $after, last: $last, before: $before, filter: $filter)
@connection(key: "PanoFeedFragment__panoFeed") {
__id
edges {
Expand Down Expand Up @@ -82,10 +83,10 @@ export function PanoFeed(props: Props) {
})}

<div className="flex gap-2">
<Button variant="secondary" onClick={() => loadPrevious(10)} disabled={!hasPrevious}>
<Button variant="secondary" onClick={() => loadPrevious(20)} disabled={!hasPrevious}>
{"< Prev"}
</Button>
<Button variant="secondary" onClick={() => loadNext(10)} disabled={!hasNext}>
<Button variant="secondary" onClick={() => loadNext(20)} disabled={!hasNext}>
{"Next >"}
</Button>
</div>
Expand Down
4 changes: 2 additions & 2 deletions apps/kampus/app/pano/PostListContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ interface Props {
}

const query = graphql`
query PostListContainerQuery {
query PostListContainerQuery($filter: PanoPostFilter) {
viewer {
...PanoFeedFragment
...PanoFeedFragment @arguments(filter: $filter)
...PanoFeed_viewer
}
}
Expand Down
19 changes: 15 additions & 4 deletions apps/kampus/app/pano/__generated__/PanoFeedFragment.graphql.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading