Skip to content

Commit

Permalink
moved total and current items to Tickets.tsx
Browse files Browse the repository at this point in the history
  • Loading branch information
elraphty committed Dec 9, 2023
1 parent 28df49b commit 64560e2
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 25 deletions.
30 changes: 28 additions & 2 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,9 +437,35 @@ func (db database) GetUserBountiesCount(personKey string, tabType string) int64
return count
}

func (db database) GetBountiesCount() int64 {
func (db database) GetBountiesCount(r *http.Request) int64 {
keys := r.URL.Query()
open := keys.Get("Open")
assingned := keys.Get("Assigned")
paid := keys.Get("Paid")
openQuery := ""
assignedQuery := ""
paidQuery := ""

if open != "" && open == "true" {
openQuery = "AND assignee = ''"
assignedQuery = ""
}
if assingned != "" && assingned == "true" {
if open != "" && open == "true" {
assignedQuery = "OR assignee != ''"
} else {
assignedQuery = "AND assignee != ''"
}
}
if paid != "" && paid == "true" {
paidQuery = "AND paid = true"
}

var count int64
db.db.Model(&Bounty{}).Where("show != ?", false).Count(&count)

query := "SELECT COUNT(*) FROM bounty WHERE show != false"
allQuery := query + " " + openQuery + " " + assignedQuery + " " + paidQuery
db.db.Raw(allQuery).Scan(&count)
return count
}

Expand Down
41 changes: 37 additions & 4 deletions frontend/app/src/pages/tickets/Tickets.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { observer } from 'mobx-react-lite';
import FirstTimeScreen from 'people/main/FirstTimeScreen';
import BountyHeader from 'people/widgetViews/BountyHeader';
import WidgetSwitchViewer from 'people/widgetViews/WidgetSwitchViewer';
import React, { useEffect, useState } from 'react';
import React, { useCallback, useEffect, useState } from 'react';
import { useHistory } from 'react-router';
import { queryLimit } from 'store/main';
import { colors } from '../../config/colors';
import { useIsMobile } from '../../hooks';
import { useStores } from '../../store';
Expand All @@ -23,6 +24,9 @@ function BodyComponent() {
Paid: false
});
const [checkboxIdToSelectedMapLanguage, setCheckboxIdToSelectedMapLanguage] = useState({});
const [page, setPage] = useState<number>(1);
const [currentItems, setCurrentItems] = useState<number>(queryLimit);
const [totalBounties, setTotalBounties] = useState(0);

const color = colors['light'];

Expand All @@ -37,7 +41,7 @@ function BodyComponent() {
await main.getPeopleBounties({ page: 1, resetPage: true, ...checkboxIdToSelectedMap });
setLoading(false);
})();
}, [main]);
}, [main, checkboxIdToSelectedMap]);

useEffect(() => {
setCheckboxIdToSelectedMap({
Expand All @@ -53,15 +57,34 @@ function BodyComponent() {
}
}, [main, ui.meInfo]);

const onChangeStatus = async (optionId: any) => {
const getTotalBounties = useCallback(
async (statusData: any) => {
const totalBounties = await main.getTotalBountyCount(
statusData.Open,
statusData.Assigned,
statusData.Paid
);
setTotalBounties(totalBounties);
},
[main]
);

useEffect(() => {
getTotalBounties(checkboxIdToSelectedMap);
}, [getTotalBounties]);

const onChangeStatus = (optionId: any) => {
const newCheckboxIdToSelectedMap = {
...checkboxIdToSelectedMap,
...{
[optionId]: !checkboxIdToSelectedMap[optionId]
}
};
setCheckboxIdToSelectedMap(newCheckboxIdToSelectedMap);
await main.getPeopleBounties({ page: 1, resetPage: true, ...newCheckboxIdToSelectedMap });
getTotalBounties(newCheckboxIdToSelectedMap);
// set data to default
setCurrentItems(queryLimit);
setPage(1);
};

const onChangeLanguage = (optionId: any) => {
Expand Down Expand Up @@ -133,6 +156,11 @@ function BodyComponent() {
fromBountyPage={true}
selectedWidget={selectedWidget}
loading={loading}
totalBounties={totalBounties}
currentItems={currentItems}
setCurrentItems={setCurrentItems}
page={page}
setPage={setPage}
/>
</div>

Expand Down Expand Up @@ -195,6 +223,11 @@ function BodyComponent() {
fromBountyPage={true}
selectedWidget={selectedWidget}
loading={loading}
totalBounties={totalBounties}
currentItems={currentItems}
setCurrentItems={setCurrentItems}
page={page}
setPage={setPage}
/>
</div>
</div>
Expand Down
29 changes: 13 additions & 16 deletions frontend/app/src/people/widgetViews/WidgetSwitchViewer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useEffect, useState } from 'react';
import React, { useState } from 'react';
import styled from 'styled-components';
import { observer } from 'mobx-react-lite';
import { useIsMobile } from 'hooks/uiHooks';
Expand Down Expand Up @@ -76,9 +76,11 @@ function WidgetSwitchViewer(props: any) {
const [deletePayload, setDeletePayload] = useState<object>({});
const closeModal = () => setShowDeleteModal(false);
const showModal = () => setShowDeleteModal(true);
const [page, setPage] = useState<number>(1);
const [currentItems, setCurrentItems] = useState<number>(queryLimit);
const [totalBounties, setTotalBounties] = useState<number>(queryLimit);
const { currentItems, setCurrentItems, totalBounties, page: propsPage, setPage } = props;

const items = currentItems ?? 0;
const bountiesTotal = totalBounties ?? 0;
const page = propsPage ?? 0;

const panelStyles = isMobile
? {
Expand Down Expand Up @@ -156,20 +158,15 @@ function WidgetSwitchViewer(props: any) {
closeModal();
};

const getTotalBountiesCount = useCallback(async () => {
const totalBounties = await main.getTotalBountyCount();
setTotalBounties(totalBounties);
}, [main]);

useEffect(() => {
getTotalBountiesCount();
}, [getTotalBountiesCount]);

const nextBounties = async () => {
const currentPage = page + 1;
setPage(currentPage);
setCurrentItems(currentItems + queryLimit);
if (setPage) {
setPage(currentPage);
}

if (setCurrentItems) {
setCurrentItems(currentItems + queryLimit);
}
await main.getPeopleBounties({
limit: queryLimit,
page: currentPage,
Expand Down Expand Up @@ -243,7 +240,7 @@ function WidgetSwitchViewer(props: any) {
) : (
<NoResults />
);
const showLoadMore = totalBounties > currentItems && activeList.length >= queryLimit;
const showLoadMore = bountiesTotal > items && activeList.length >= queryLimit;
return (
<>
{listItems}
Expand Down
6 changes: 4 additions & 2 deletions frontend/app/src/store/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1065,9 +1065,11 @@ export class MainStore {
}
}

async getTotalBountyCount(): Promise<number> {
async getTotalBountyCount(open: boolean, assigned: boolean, paid: boolean): Promise<number> {
try {
const count = await api.get(`gobounties/count`);
const count = await api.get(
`gobounties/count?Open=${open}&Assigned=${assigned}&Paid=${paid}`
);
return await count;
} catch (e) {
console.log('fetch failed getTotalBountyCount: ', e);
Expand Down
2 changes: 1 addition & 1 deletion handlers/bounty.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func GetUserBountyCount(w http.ResponseWriter, r *http.Request) {
}

func GetBountyCount(w http.ResponseWriter, r *http.Request) {
bountyCount := db.DB.GetBountiesCount()
bountyCount := db.DB.GetBountiesCount(r)
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(bountyCount)
}
Expand Down

0 comments on commit 64560e2

Please sign in to comment.