Skip to content

Commit

Permalink
latest fixes (#254)
Browse files Browse the repository at this point in the history
* moved global css files to _app.tsx

* latest fixes

* reviewed changes
  • Loading branch information
ssani7 authored Nov 12, 2023
1 parent 5fc5086 commit d915d1f
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 58 deletions.
32 changes: 23 additions & 9 deletions src/components/Application/ApplicationOwnerHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,22 @@ const ApplicationOwnerHeader = (props: ApplicationOwnerProps) => {
const [openPopup, setOpenPopup] = useState(false);
const [success, setSuccess] = useState<boolean>(false);
const [error, setError] = useState<any>();
const [loadingWallet, setLoadingWallet] = useState<string>('');
const [loadingWallet, setLoadingWallet] = useState<string>('loading');
const [imbueBalance, setImbueBalance] = useState<string>();
const [firstLoad, setFirstLoad] = useState<boolean>(true);

const router = useRouter();

useEffect(() => {
const showBalance = async () => {

if (loadingWallet === 'loading' && !firstLoad) return;

try {
setLoadingWallet('loading');

if (firstLoad)
setLoadingWallet('loading');

const balance = await getBalance(
Currency.IMBU,
user,
Expand All @@ -73,10 +80,18 @@ const ApplicationOwnerHeader = (props: ApplicationOwnerProps) => {
setError({ message: error });
} finally {
setLoadingWallet('');
if (firstLoad)
setFirstLoad(false)
}
};
user?.web3_address && showBalance();
}, [user?.web3_address, application?.currency_id, user]);
// user?.web3_address && showBalance();

const timer = setInterval(() => {
user?.web3_address && showBalance();
}, 5000);
return () => clearInterval(timer);

}, [user?.web3_address, application.currency_id, user, loadingWallet, firstLoad]);

const startWork = async (account: WalletAccount) => {
setLoading(true);
Expand Down Expand Up @@ -140,7 +155,7 @@ const ApplicationOwnerHeader = (props: ApplicationOwnerProps) => {
</p>
<div className='flex flex-col gap-2'>
<p className='text-[1.25rem] font-normal capitalize text-imbue-purple'>
{briefOwner?.display_name}
{briefOwner?.display_name}
</p>
<CountrySelector user={briefOwner} />
</div>
Expand All @@ -163,9 +178,8 @@ const ApplicationOwnerHeader = (props: ApplicationOwnerProps) => {
</button>
) : (
<button
className={`${
applicationStatusId[application?.status_id]
}-btn in-dark text-xs lg:text-base rounded-full py-[7px] px-3 lg:px-6 lg:py-[10px]`}
className={`${applicationStatusId[application?.status_id]
}-btn in-dark text-xs lg:text-base rounded-full py-[7px] px-3 lg:px-6 lg:py-[10px]`}
>
{applicationStatusId[application?.status_id]}
</button>
Expand All @@ -175,7 +189,7 @@ const ApplicationOwnerHeader = (props: ApplicationOwnerProps) => {
<p className='text-sm lg:text-base mt-5 mb-3 text-imbue-purple text-right'>
{loadingWallet === 'loading' && 'Loading Wallet...'}
{loadingWallet === 'connecting' && 'Connecting Wallet...'}
{!loadingWallet &&
{loadingWallet !== 'loading' &&
(imbueBalance === undefined
? 'No wallet found'
: `Your Balance: ${imbueBalance} $${Currency[Currency.IMBU]}`)}
Expand Down
30 changes: 22 additions & 8 deletions src/components/Application/BriefOwnerHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const BriefOwnerHeader = (props: BriefOwnerHeaderProps) => {

const [balance, setBalance] = useState<string>();
const [imbueBalance, setImbueBalance] = useState<string>();
const [loadingWallet, setLoadingWallet] = useState<string>('');
const [loadingWallet, setLoadingWallet] = useState<string>('loading');
const [error, setError] = useState<any>();

const [openPopup, setOpenPopup] = useState<boolean>(false);
Expand Down Expand Up @@ -112,10 +112,17 @@ const BriefOwnerHeader = (props: BriefOwnerHeaderProps) => {
}
};

const [firstLoad, setFirstLoad] = useState<boolean>(true);

useEffect(() => {
const showBalance = async () => {

if (loadingWallet === 'loading' && !firstLoad) return;

try {
setLoadingWallet('loading');
if (firstLoad)
setLoadingWallet('loading');

const balance = await getBalance(
application?.currency_id ?? Currency.IMBU,
user,
Expand All @@ -133,10 +140,18 @@ const BriefOwnerHeader = (props: BriefOwnerHeaderProps) => {
setError({ message: error });
} finally {
setLoadingWallet('');
if (firstLoad)
setFirstLoad(false)
}
};
user?.web3_address && showBalance();
}, [user?.web3_address, application?.currency_id]);
// user?.web3_address && showBalance();

const timer = setInterval(() => {
user?.web3_address && showBalance();
}, 5000);
return () => clearInterval(timer);

}, [user?.web3_address, application.currency_id, user, loadingWallet, firstLoad]);

const mobileView = useMediaQuery('(max-width:480px)');

Expand Down Expand Up @@ -244,9 +259,8 @@ const BriefOwnerHeader = (props: BriefOwnerHeaderProps) => {
)}
</button>
<button
className={`${
applicationStatusId[application?.status_id]
}-btn in-dark text-xs lg:text-base rounded-full py-3 px-3 lg:px-6 lg:py-[10px]`}
className={`${applicationStatusId[application?.status_id]
}-btn in-dark text-xs lg:text-base rounded-full py-3 px-3 lg:px-6 lg:py-[10px]`}
>
{applicationStatusId[application?.status_id]}
</button>
Expand Down Expand Up @@ -327,7 +341,7 @@ const BriefOwnerHeader = (props: BriefOwnerHeaderProps) => {
<p className='text-sm text-imbue-purple'>
{loadingWallet === 'loading' && 'Loading Wallet...'}
{loadingWallet === 'connecting' && 'Connecting Wallet...'}
{!loadingWallet &&
{loadingWallet !== 'loading' &&
(balance === undefined
? 'No wallet found'
: `$${Currency[application?.currency_id ?? 0]}: ${balance}`)}
Expand Down
20 changes: 17 additions & 3 deletions src/components/Project/ProjectBalance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { getBalance } from '@/utils/helper';
import { Currency, OffchainProjectState, Project, User } from '@/model';

type ProjectBalanceType = {
balance: number;
balance: number | undefined;
project: Project;
user: User;
handlePopUpForUser: () => void;
Expand Down Expand Up @@ -44,16 +44,21 @@ const Currencies = [
const ProjectBalance = (props: ProjectBalanceType) => {
const { balance, project, user, handlePopUpForUser, setBalance, balanceLoading, setBalanceLoading } = props;
const [currency_id, setCurrency_id] = useState<number>();
const [firstLoad, setFirstLoad] = useState<boolean>(true);

const [anchorEl, setAnchorEl] = React.useState<null | HTMLElement>(null);
const showOptions = Boolean(anchorEl);

useEffect(() => {
const getAndSetBalace = async () => {
if (balanceLoading && !firstLoad) return;

if (
currency_id === undefined
) return

setBalanceLoading(true)

try {
const balance = await getBalance(
currency_id,
Expand All @@ -64,7 +69,11 @@ const ProjectBalance = (props: ProjectBalanceType) => {
if (!balance && project.status_id !== OffchainProjectState.Completed) {
handlePopUpForUser();
}

setBalance(balance || 0);
if (firstLoad)
setFirstLoad(false)

} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
Expand All @@ -78,8 +87,13 @@ const ProjectBalance = (props: ProjectBalanceType) => {
setCurrency_id(project.currency_id);
}

const timer = setInterval(() => {
getAndSetBalace();
}, 5000);
return () => clearInterval(timer);

// eslint-disable-next-line react-hooks/exhaustive-deps
}, [currency_id, project?.escrow_address, project.status_id, user.id])
}, [currency_id, project?.escrow_address, project.status_id, user.id, firstLoad, balanceLoading])

const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => {
setAnchorEl(event.currentTarget);
Expand All @@ -94,7 +108,7 @@ const ProjectBalance = (props: ProjectBalanceType) => {

return (
<div className='text-sm text-[#868686] mt-2'>
{balanceLoading
{balanceLoading && firstLoad
? (
<p className='text-xs font-semibold'> Loading Balance...</p>)
: (
Expand Down
45 changes: 28 additions & 17 deletions src/components/Project/V2/ExpandableMilestone.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import BackDropLoader from '@/components/LoadingScreen/BackDropLoader';
import VoteModal from '@/components/ReviewModal/VoteModal';
import Web3WalletModal from '@/components/WalletModal/Web3WalletModal';

import { Currency, Milestone, OffchainProjectState, Project, User } from '@/model';
import { Currency, Milestone, Project, User } from '@/model';
import ChainService, { ImbueChainEvent } from '@/redux/services/chainService';
import {
uploadMilestoneAttachments,
Expand All @@ -43,7 +43,7 @@ interface ExpandableMilestonProps {
canVote: boolean;
loading: boolean;
targetUser: any;
balance: number;
balance: number | undefined;
balanceLoading: boolean;
// hasMilestoneAttachments: boolean;
}
Expand All @@ -62,9 +62,9 @@ const ExpandableMilestone = (props: ExpandableMilestonProps) => {
setSuccess,
targetUser,
balance,
balanceLoading,
// hasMilestoneAttachments = false
} = props;

const [milestoneKeyInView, setMilestoneKeyInView] = useState<number>(0);
const [submittingMilestone, setSubmittingMilestone] =
useState<boolean>(false);
Expand Down Expand Up @@ -265,15 +265,24 @@ const ExpandableMilestone = (props: ExpandableMilestonProps) => {
while (onChainWithdrawalRequired) {
if (result.status || result.txError) {
if (result.status) {
const haveAllMilestonesBeenApproved = projectMilestones
.map((m: any) => m.is_approved)
.every(Boolean);

if (haveAllMilestonesBeenApproved) {
project.status_id = OffchainProjectState.Completed;
project.completed = true;
await updateProject(Number(project?.id), project);
}
// const haveAllMilestonesBeenApproved = projectMilestones
// .map((m: any) => m.is_approved)
// .every(Boolean);

// if (haveAllMilestonesBeenApproved) {
// project.status_id = OffchainProjectState.Completed;
// project.completed = true;
// await updateProject(Number(project?.id), project);
// }

projectMilestones.forEach((m) => {
if (m.milestone_index <= milestoneKeyInView && !m.withdrawn_onchain) {
project.milestones[m.milestone_index].withdrawn_onchain = true;
project.milestones[m.milestone_index].withdrawal_transaction_hash = result?.transactionHash || "";
}
})

await updateProject(Number(project?.id), project);

if (project.currency_id < 100) {
setSuccess(true);
Expand All @@ -283,7 +292,7 @@ const ExpandableMilestone = (props: ExpandableMilestonProps) => {

} else if (result.txError) {
// setLoading(false);
setError({ message: 'Error : ' + result.errorMessage });
setError({ message: 'Error : ' + (result.errorMessage || "Withdrawal unsuccessful. Please verify if you have any funds available for withdrawal.") });
}
break;
}
Expand All @@ -297,7 +306,7 @@ const ExpandableMilestone = (props: ExpandableMilestonProps) => {
if (withdrawResult.txError) {
setSuccess(false);
setError({ message: withdrawResult.errorMessage });
} else if(Number(withdrawResult.withdrawn) > 0) {
} else if (Number(withdrawResult.withdrawn) > 0) {
setSuccess(true);
setSuccessTitle('Withdraw successful');
}
Expand All @@ -306,6 +315,8 @@ const ExpandableMilestone = (props: ExpandableMilestonProps) => {

} catch (error) {
setError({ message: 'Error' + error });
} finally {
setLoading(false);
}
};

Expand Down Expand Up @@ -526,7 +537,7 @@ const ExpandableMilestone = (props: ExpandableMilestonProps) => {


{
balance === 0 && !balanceLoading && project?.brief_id && (
balance === 0 && !project?.brief_id && (
<div className='lg:flex gap-1 lg:items-center rounded-2xl bg-imbue-coral px-3 py-1 text-sm text-white w-fit ml-auto mt-3 '>
<ErrorOutlineOutlinedIcon className='h-4 w-4 inline' />
<p className='inline'>
Expand All @@ -542,9 +553,9 @@ const ExpandableMilestone = (props: ExpandableMilestonProps) => {

{isApplicant && milestone.is_approved && (
<button
className='primary-btn ml-auto in-dark w-button lg:w-1/5'
className={`primary-btn ml-auto in-dark w-button lg:w-1/5 text-center ${milestone?.withdrawn_onchain && '!bg-gray-300 !text-gray-400'}`}
style={{ textAlign: 'center' }}
onClick={() => handleWithdraw(milestone.milestone_index)}
onClick={() => !milestone?.withdrawn_onchain && handleWithdraw(milestone.milestone_index)}
>
Withdraw
</button>
Expand Down
9 changes: 9 additions & 0 deletions src/pages/api/project/[...id].ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,15 @@ export default nextConnect()
// if (project_in_milestone_voting)
// newProject.project_in_milestone_voting = project_in_milestone_voting;

const haveAllMilestonesBeenApproved = milestones
.map((m: any) => m.is_approved)
.every(Boolean);

if (haveAllMilestonesBeenApproved) {
newProject.status_id = OffchainProjectState.Completed;
newProject.completed = true;
}

const project = await models.updateProject(projectId, newProject)(tx);
const filter = new Filter();

Expand Down
7 changes: 6 additions & 1 deletion src/pages/api/project/vote/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ export default nextConnect()

const allVotersRes = await fetchProjectApprovers(projectId)(tx);

console.log('🚀 ~ file: index.ts:118 ~ db.transaction ~ yes:', {
yes,
no,
allVotersRes
});

if (yes.length / allVotersRes.length >= 0.75) {
// closing voting round and approving milestone if treshold reached
await updateProjectVoting(Number(projectId), false)(tx);
Expand All @@ -133,7 +139,6 @@ export default nextConnect()
}

res.status(200).json({ status: 'success', milestoneApproved });

} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
Expand Down
Loading

0 comments on commit d915d1f

Please sign in to comment.