Open platform to collect and prioritize feedback
Fixing myVotes filter and some refactoring.
Matt Robertsef50934911be
diff --git a/app/handlers/apiv1/post.go b/app/handlers/apiv1/post.goindex 701015aa8..4fae6210a 100644--- a/app/handlers/apiv1/post.go+++ b/app/handlers/apiv1/post.go@@ -26,6 +26,9 @@ func SearchPosts() web.HandlerFunc {Limit: c.QueryParam("limit"),Tags: c.QueryParamAsArray("tags"),}+ if myVotesOnly, err := c.QueryParamAsBool("myvotes"); err == nil {+ searchPosts.MyVotesOnly = myVotesOnly+ }searchPosts.SetStatusesFromStrings(c.QueryParamAsArray("statuses"))if err := bus.Dispatch(c, searchPosts); err != nil {diff --git a/app/handlers/post.go b/app/handlers/post.goindex ee246f8c2..78d0052c6 100644--- a/app/handlers/post.go+++ b/app/handlers/post.go@@ -22,6 +22,11 @@ func Index() web.HandlerFunc {Limit: c.QueryParam("limit"),Tags: c.QueryParamAsArray("tags"),}++ if myVotesOnly, err := c.QueryParamAsBool("myvotes"); err == nil {+ searchPosts.MyVotesOnly = myVotesOnly+ }+searchPosts.SetStatusesFromStrings(c.QueryParamAsArray("statuses"))getAllTags := &query.GetAllTags{}countPerStatus := &query.CountPostPerStatus{}diff --git a/app/models/query/post.go b/app/models/query/post.goindex 6ef577cd6..6c2530aef 100644--- a/app/models/query/post.go+++ b/app/models/query/post.go@@ -34,11 +34,12 @@ type GetPostByNumber struct {}type SearchPosts struct {- Query string- View string- Limit string- Statuses []enum.PostStatus- Tags []string+ Query string+ View string+ Limit string+ Statuses []enum.PostStatus+ Tags []string+ MyVotesOnly boolResult []*entity.Post}diff --git a/app/pkg/web/context.go b/app/pkg/web/context.goindex 0ebca912b..a4fc17c08 100644--- a/app/pkg/web/context.go+++ b/app/pkg/web/context.go@@ -398,6 +398,18 @@ func (c *Context) QueryParamAsInt(key string) (int, error) {return intValue, nil}+func (c *Context) QueryParamAsBool(key string) (bool, error) {+ value := c.QueryParam(key)+ if value == "" {+ return false, nil+ }+ boolValue, err := strconv.ParseBool(value)+ if err != nil {+ return false, errors.Wrap(err, "failed to parse %s to boolean", value)+ }+ return boolValue, nil+}+// QueryParamAsArray returns querystring parameter for given key as an arrayfunc (c *Context) QueryParamAsArray(key string) []string {param := c.QueryParam(key)diff --git a/app/services/sqlstore/postgres/common.go b/app/services/sqlstore/postgres/common.goindex a59ba9032..ce6d15c1f 100644--- a/app/services/sqlstore/postgres/common.go+++ b/app/services/sqlstore/postgres/common.go@@ -41,30 +41,35 @@ func getViewData(query query.SearchPosts) (string, []enum.PostStatus, string) {}}+ if query.MyVotesOnly {+ condition = "AND has_voted = true"+ }+switch query.View {case "recent":sort = "id"- case "my-votes":- condition = "AND has_voted = true"- sort = "id"case "most-wanted":sort = "votes_count"case "most-discussed":sort = "comments_count"- // Depracted: Use status filters instead+ case "my-votes":+ // Depracated: You can instead filter on my votes only for more flexibility than using this view.+ condition = "AND has_voted = true"+ sort = "id"case "planned":+ // Depracated: Use status filters insteadsort = "response_date"statusFilters = []enum.PostStatus{enum.PostPlanned}- // Deprecated: Use status filters insteadcase "started":+ // Depracated: Use status filters insteadsort = "response_date"statusFilters = []enum.PostStatus{enum.PostStarted}- // Deprecated: Use status filters insteadcase "completed":+ // Depracated: Use status filters insteadsort = "response_date"statusFilters = []enum.PostStatus{enum.PostCompleted}- // Deprecated: Use status filters insteadcase "declined":+ // Depracated: Use status filters insteadsort = "response_date"statusFilters = []enum.PostStatus{enum.PostDeclined}case "all":diff --git a/app/services/sqlstore/postgres/post_test.go b/app/services/sqlstore/postgres/post_test.goindex 2b277db87..0287abbaa 100644--- a/app/services/sqlstore/postgres/post_test.go+++ b/app/services/sqlstore/postgres/post_test.go@@ -528,7 +528,8 @@ func TestGetPosts_Different_Statuses(t *testing.T) {addBug := &cmd.AddNewTag{Name: "Bug", Color: "FF0000", IsPublic: true}addFeatureRequest := &cmd.AddNewTag{Name: "Feature Request", Color: "00FF00", IsPublic: false}- bus.MustDispatch(aryaStarkCtx, addBug, addFeatureRequest)+ voteForPostRequest := &cmd.AddVote{Post: newPost.Result, User: aryaStark}+ bus.MustDispatch(aryaStarkCtx, addBug, addFeatureRequest, voteForPostRequest)bus.MustDispatch(aryaStarkCtx, &cmd.AssignTag{Tag: addBug.Result, Post: newPost.Result})bus.MustDispatch(aryaStarkCtx, &cmd.AssignTag{Tag: addFeatureRequest.Result, Post: newPost.Result})bus.MustDispatch(aryaStarkCtx, &cmd.AssignTag{Tag: addBug.Result, Post: completedPost.Result})@@ -571,6 +572,22 @@ func TestGetPosts_Different_Statuses(t *testing.T) {expectedCount: 1,expectedIDs: []int{startedPost.Result.ID},},+ {+ name: "My votes only",+ searchParams: &query.SearchPosts{+ MyVotesOnly: true,+ },+ expectedCount: 1,+ expectedIDs: []int{newPost.Result.ID},+ },+ {+ name: "Legacy view for my votes only should still work",+ searchParams: &query.SearchPosts{+ View: "my-votes",+ },+ expectedCount: 1,+ expectedIDs: []int{newPost.Result.ID},+ },{name: "All statuses",searchParams: &query.SearchPosts{diff --git a/public/pages/Home/components/PostFilter.tsx b/public/pages/Home/components/PostFilter.tsxindex f20e4c87a..aef8e79bd 100644--- a/public/pages/Home/components/PostFilter.tsx+++ b/public/pages/Home/components/PostFilter.tsx@@ -7,11 +7,15 @@ import HeroIconFilter from "@fider/assets/images/heroicons-filter.svg"import { useFider } from "@fider/hooks"import { t } from "@lingui/macro"+import { FilterState } from "./PostsContainer"++type FilterType = "tag" | "status" | "myVotes"+interface OptionItem {- value: string+ value: string | booleanlabel: stringcount?: number- tag?: Tag+ type: FilterType}interface PostFilterProps {@@ -20,35 +24,57 @@ interface PostFilterProps {filtersChanged: (filter: FilterState) => voidtags: Tag[]}-export interface FilterState {- tags: string[]- statuses: string[]++export interface FilterItem {+ type: FilterType+ value: string | boolean+}++const FilterStateToFilterItems = (filterState: FilterState): FilterItem[] => {+ const filterItems: FilterItem[] = []+ filterState.statuses.forEach((s) => {+ filterItems.push({ type: "status", value: s })+ })+ filterState.tags.forEach((t) => {+ filterItems.push({ type: "tag", value: t })+ })+ if (filterState.myVotes) {+ filterItems.push({ type: "myVotes", value: true })+ }+ return filterItems+}++const FilterItemsToFilterState = (filterItems: FilterItem[]): FilterState => {+ const filterState: FilterState = { tags: [], statuses: [], myVotes: false }+ filterItems.forEach((i) => {+ if (i.type === "tag") {+ filterState.tags.push(i.value as string)+ } else if (i.type === "status") {+ filterState.statuses.push(i.value as string)+ } else if (i.type === "myVotes") {+ filterState.myVotes = true+ }+ })+ return filterState}export const PostFilter = (props: PostFilterProps) => {const fider = useFider()+ const filterItems: FilterItem[] = FilterStateToFilterItems(props.activeFilter)+const handleChangeFilter = (item: OptionItem) => () => {- if (item.tag) {- // Handle tag selection- const newTags = props.activeFilter.tags.includes(item.value)- ? props.activeFilter.tags.filter((t) => t !== item.value)- : [...props.activeFilter.tags, item.value]- const newFilters = { ...props.activeFilter, tags: newTags }- props.filtersChanged(newFilters)- } else {- // Handle status selection- const newStatuses = props.activeFilter.statuses.includes(item.value)- ? props.activeFilter.statuses.filter((s) => s !== item.value)- : [...props.activeFilter.statuses, item.value]- const newFilters = { ...props.activeFilter, statuses: newStatuses }- props.filtersChanged(newFilters)- }+ const exists = filterItems.find((i) => i.type === item.type && i.value === item.value)+ const newFilter = exists+ ? filterItems.filter((i) => !(i.type === item.type && i.value === item.value))+ : [...filterItems, { type: item.type, value: item.value }]++ props.filtersChanged(FilterItemsToFilterState(newFilter))}const options: OptionItem[] = []if (fider.session.isAuthenticated) {- options.push({ value: "my-votes", label: t({ id: "home.postfilter.option.myvotes", message: "My Votes" }) })+ options.push({ value: true, label: t({ id: "home.postfilter.option.myvotes", message: "My Votes" }), type: "myVotes" })}PostStatus.All.filter((s) => s.filterable && props.countPerStatus[s.value]).forEach((s) => {@@ -57,6 +83,7 @@ export const PostFilter = (props: PostFilterProps) => {label: t({ id, message: s.title }),value: s.value,count: props.countPerStatus[s.value],+ type: "status",})})@@ -64,17 +91,14 @@ export const PostFilter = (props: PostFilterProps) => {options.push({label: t.name,value: t.slug,- tag: t,+ type: "tag",})})- const filterCount = props.activeFilter.tags.length + props.activeFilter.statuses.length+ const filterCount = filterItems.lengthreturn (<HStack className="mr-4">- {/* <span className="text-category">- <Trans id="home.postfilter.label.view">View</Trans>- </span> */}<DropdownrenderHandle={<HStack className="h-10 text-medium text-xs rounded-md uppercase border border-gray-400 text-gray-800 p-2 px-3">@@ -85,10 +109,11 @@ export const PostFilter = (props: PostFilterProps) => {}>{options.map((o) => {- const isChecked = o.tag ? props.activeFilter.tags.includes(o.value) : props.activeFilter.statuses.includes(o.value)+ const isChecked = filterItems.find((f) => f.type === o.type && f.value === o.value) !== undefined+return (- <Dropdown.ListItem onClick={handleChangeFilter(o)} key={o.value}>- <Checkbox field={o.value} checked={isChecked}>+ <Dropdown.ListItem onClick={handleChangeFilter(o)} key={o.value.toString()}>+ <Checkbox field={o.value.toString()} checked={isChecked}><HStack spacing={2}><span className={isChecked ? "text-semibold" : ""}>{o.label}</span><div className="">diff --git a/public/pages/Home/components/PostsContainer.tsx b/public/pages/Home/components/PostsContainer.tsxindex efc78c160..ebd5ea0eb 100644--- a/public/pages/Home/components/PostsContainer.tsx+++ b/public/pages/Home/components/PostsContainer.tsx@@ -7,7 +7,7 @@ import { Loader, Input } from "@fider/components"import { actions, navigator, querystring } from "@fider/services"import IconSearch from "@fider/assets/images/heroicons-search.svg"import IconX from "@fider/assets/images/heroicons-x.svg"-import { PostFilter, FilterState } from "./PostFilter"+import { PostFilter } from "./PostFilter"import { ListPosts } from "./ListPosts"import { t, Trans } from "@lingui/macro"import { PostsSort } from "./PostsSort"@@ -28,6 +28,12 @@ interface PostsContainerState {limit?: number // Limit}+export interface FilterState {+ tags: string[]+ statuses: string[]+ myVotes: boolean+}+export class PostsContainer extends React.Component<PostsContainerProps, PostsContainerState> {constructor(props: PostsContainerProps) {super(props)@@ -42,7 +48,7 @@ export class PostsContainer extends React.Component<PostsContainerProps, PostsColoading: false,view,query: querystring.get("query"),- filterState: { tags: querystring.getArray("tags"), statuses: querystring.getArray("statuses") },+ filterState: { tags: querystring.getArray("tags"), statuses: querystring.getArray("statuses"), myVotes: querystring.get("myvotes") === "true" },limit: querystring.getNumber("limit"),}}@@ -54,22 +60,31 @@ export class PostsContainer extends React.Component<PostsContainerProps, PostsCoquerystring.stringify({statuses: this.state.filterState.statuses,tags: this.state.filterState.tags,+ myvotes: this.state.filterState.myVotes.toString(),query,view: this.state.view,limit: this.state.limit,}))- this.searchPosts(query, this.state.view, this.state.limit, this.state.filterState.tags, this.state.filterState.statuses, reset)+ this.searchPosts(+ query,+ this.state.view,+ this.state.limit,+ this.state.filterState.tags,+ this.state.filterState.statuses,+ this.state.filterState.myVotes,+ reset+ )})}private timer?: number- private async searchPosts(query: string, view: string, limit: number | undefined, tags: string[], statuses: string[], reset: boolean) {+ private async searchPosts(query: string, view: string, limit: number | undefined, tags: string[], statuses: string[], myVotes: boolean, reset: boolean) {window.clearTimeout(this.timer)this.setState({ posts: reset ? undefined : this.state.posts, loading: true })this.timer = window.setTimeout(() => {- actions.searchPosts({ query, view: view, limit, tags, statuses }).then((response) => {+ actions.searchPosts({ query, view: view, limit, tags, statuses, myVotes }).then((response) => {if (response.ok && this.state.loading) {this.setState({ loading: false, posts: response.data })}diff --git a/public/services/actions/post.ts b/public/services/actions/post.tsindex e434d7a0c..c19438faf 100644--- a/public/services/actions/post.ts+++ b/public/services/actions/post.ts@@ -10,19 +10,22 @@ export interface SearchPostsParams {view?: stringlimit?: numbertags?: string[]+ myVotes?: booleanstatuses?: string[]}export const searchPosts = async (params: SearchPostsParams): Promise<Result<Post[]>> => {- return await http.get<Post[]>(- `/api/v1/posts${querystring.stringify({- tags: params.tags,- statuses: params.statuses,- query: params.query,- view: params.view,- limit: params.limit,- })}`- )+ let qsParams = querystring.stringify({+ tags: params.tags,+ statuses: params.statuses,+ query: params.query,+ view: params.view,+ limit: params.limit,+ })+ if (params.myVotes) {+ qsParams += `&myvotes=true`+ }+ return await http.get<Post[]>(`/api/v1/posts${qsParams}`)}export const deletePost = async (postNumber: number, text: string): Promise<Result> => {