Skip to content

Commit

Permalink
Merge pull request stakwork#566 from stakwork/feat/workspacelist_budg…
Browse files Browse the repository at this point in the history
…et_update

Update Workspace budget on the workspace list
  • Loading branch information
elraphty authored Jun 5, 2024
2 parents 85f0c60 + 96fbfb0 commit 338cd10
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 12 deletions.
13 changes: 2 additions & 11 deletions src/people/widgetViews/WorkspaceDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,7 @@ import React, { useCallback, useEffect, useState } from 'react';
import { useStores } from 'store';
import { EuiGlobalToastList } from '@elastic/eui';
import { Button } from 'components/common';
import {
BountyRoles,
defaultWorkspaceBudget,
Workspace,
WorkspaceBudget,
PaymentHistory,
Person
} from 'store/interface';
import { BountyRoles, Workspace, PaymentHistory, Person } from 'store/interface';
import MaterialIcon from '@material/react-material-icon';
import { Route, Router, Switch, useRouteMatch } from 'react-router-dom';
import { userHasRole } from 'helpers';
Expand Down Expand Up @@ -60,7 +53,6 @@ const WorkspaceDetails = (props: {
const [isOpenHistory, setIsOpenHistory] = useState<boolean>(false);
const [isOpenEditWorkspace, setIsOpenEditWorkspace] = useState<boolean>(false);
const [showDeleteModal, setShowDeleteModal] = useState<boolean>(false);
const [orgBudget, setWorkspaceBudget] = useState<WorkspaceBudget>(defaultWorkspaceBudget);
const [paymentsHistory, setPaymentsHistory] = useState<PaymentHistory[]>([]);
const [disableFormButtons, setDisableFormButtons] = useState(false);
const [users, setUsers] = useState<Person[]>([]);
Expand Down Expand Up @@ -145,8 +137,7 @@ const WorkspaceDetails = (props: {

const getWorkspaceBudget = useCallback(async () => {
if (!viewReportDisabled) {
const workspaceBudget = await main.getWorkspaceBudget(uuid);
setWorkspaceBudget(workspaceBudget);
main.getWorkspaceBudget(uuid);
}
}, [main, uuid, viewReportDisabled]);

Expand Down
2 changes: 1 addition & 1 deletion src/people/widgetViews/WorkspaceFeature.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ const WorkspaceFeature = () => {
if (featureData?.workspace_uuid && ui.meInfo) {
getUserRoles(ui.meInfo);
}
}, [getUserRoles]);
}, [getUserRoles, ui.meInfo]);

const submitField = async (
field: string,
Expand Down
36 changes: 36 additions & 0 deletions src/people/widgetViews/WorkspaceView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ const WorkspaceActionWrap = styled.div`
}
`;

let interval;

const Workspaces = (props: { person: Person }) => {
const [loading, setIsLoading] = useState<boolean>(true);
const [isOpen, setIsOpen] = useState<boolean>(false);
Expand Down Expand Up @@ -190,6 +192,40 @@ const Workspaces = (props: { person: Person }) => {
setDetailsOpen(false);
};

const pollAllInvoices = useCallback(async () => {
let i = 0;
interval = setInterval(async () => {
try {
await main.pollAllUserWorkspaceBudget();

const count = await main.allUserWorkspaceInvoiceCount();
if (count === 0) {
getUserWorkspaces();
clearInterval(interval);
}

i++;
if (i > 15) {
if (interval) {
getUserWorkspaces();
clearInterval(interval);
}
}
} catch (e) {
console.warn('Poll invoices error', e);
}
}, 2000);
}, []);

useEffect(() => {
if (!detailsOpen) {
pollAllInvoices();
}
return () => {
clearInterval(interval);
};
}, [pollAllInvoices, detailsOpen]);

// renders org as list item
const orgUi = (org: any, key: number) => {
const btnDisabled = (!org.bounty_count && org.bount_count !== 0) || !org.uuid;
Expand Down
38 changes: 38 additions & 0 deletions src/store/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2598,6 +2598,25 @@ export class MainStore {
}
}

async pollAllUserWorkspaceBudget(): Promise<any> {
try {
if (!uiStore.meInfo) return undefined;
const info = uiStore.meInfo;

const r: any = await fetch(`${TribesURL}/workspaces/poll/user/invoices`, {
method: 'GET',
mode: 'cors',
headers: {
'x-jwt': info.tribe_jwt,
'Content-Type': 'application/json'
}
});
return r;
} catch (e) {
console.error('Error pollAllUserWorkspaceBudget', e);
}
}

async workspaceInvoiceCount(workspace_uuid: string): Promise<any> {
try {
if (!uiStore.meInfo) return 0;
Expand All @@ -2617,6 +2636,25 @@ export class MainStore {
}
}

async allUserWorkspaceInvoiceCount(): Promise<any> {
try {
if (!uiStore.meInfo) return 0;
const info = uiStore.meInfo;
const r: any = await fetch(`${TribesURL}/workspaces/user/invoices/count`, {
method: 'GET',
mode: 'cors',
headers: {
'x-jwt': info.tribe_jwt,
'Content-Type': 'application/json'
}
});

return r.json();
} catch (e) {
console.error('Error pollInvoice', e);
}
}

async workspaceDelete(org_uuid: string): Promise<any> {
try {
if (!uiStore.meInfo) return 0;
Expand Down

0 comments on commit 338cd10

Please sign in to comment.