Skip to content

Commit

Permalink
Feature: Added Borrower + Page permission component + Connect Wallet …
Browse files Browse the repository at this point in the history
…component (#701)

Co-authored-by: Hugo Marques <[email protected]>
  • Loading branch information
hugomarquesdev and Hugo Marques authored Apr 4, 2024
1 parent 4ab40ff commit d7eb473
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/api/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface PutPostUser {
avatarMediaPath?: string;
beneficiary?: any;
bio?: string;
borrower?: any;
children?: number;
councilMember?: any;
country?: string;
Expand Down
38 changes: 38 additions & 0 deletions src/components/CheckPermission.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {
Box,
Card,
CircledIcon,
Display,
ViewContainer
} from '@impact-market/ui';
import Message from 'src/libs/Prismic/components/Message';
import React from 'react';
import styled from 'styled-components';

const CardStyled = styled(Card)`
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
text-align: center;
gap: 1rem;
padding: 2rem;
`;

const CheckPermission = ({ title }: { title?: string }) => (
<ViewContainer {...({} as any)}>
{title && (
<Display g900 medium mb={2}>
{title}
</Display>
)}
<CardStyled>
<CircledIcon icon="forbidden" error medium />
<Box>
<Message id="noPermission" g900 large medium />
</Box>
</CardStyled>
</ViewContainer>
);

export default CheckPermission;
72 changes: 72 additions & 0 deletions src/components/ConnectWallet.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import {
Box,
Button,
Card,
CircledIcon,
Display,
ViewContainer
} from '@impact-market/ui';
import Message from 'src/libs/Prismic/components/Message';
import React, { useState } from 'react';
import String from '../libs/Prismic/components/String';
import styled from 'styled-components';
import useWallet from 'src/hooks/useWallet';

const CardStyled = styled(Card)`
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
text-align: center;
gap: 1rem;
padding: 2rem;
`;

const ConnectWallet = ({ title }: { title?: string }) => {
const { connect } = useWallet();

const ConnectButton = (props: any) => {
const [isConnecting, setIsConnecting] = useState(false);

const handleConnectClick = async () => {
setIsConnecting(true);

await connect();

setIsConnecting(false);
};

return (
<Button
{...props}
icon="coins"
isLoading={isConnecting}
onClick={handleConnectClick}
secondary
>
<String id="connectWallet" />
</Button>
);
};

return (
<ViewContainer {...({} as any)}>
{title && (
<Display g900 medium mb={2}>
{title}
</Display>
)}
<CardStyled>
<CircledIcon icon="alertCircle" warning medium />
<Box>
<Message id="connectWalletAccess" g900 large medium />
</Box>
<Box margin="0 auto">
<ConnectButton />
</Box>
</CardStyled>
</ViewContainer>
);
};

export default ConnectWallet;
3 changes: 3 additions & 0 deletions src/components/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ const getUserType = (user: User) => {
if (user?.roles?.includes('loanManager')) {
return 'loan-manager';
}
if (user?.roles?.includes('borrower')) {
return 'borrower';
}

return 'donor';
};
Expand Down
1 change: 1 addition & 0 deletions src/constants/userTypes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const types = {
ambassador: 'ambassador',
beneficiary: 'beneficiary',
borrower: 'borrower',
councilMember: 'councilMember',
demo: 'demo',
donor: 'donor',
Expand Down
5 changes: 5 additions & 0 deletions src/hooks/useGuard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
getUserTypes,
userAmbassador,
userBeneficiary,
userBorrower,
userCouncilMember,
userLoanManager,
userManager
Expand Down Expand Up @@ -91,6 +92,10 @@ const useGuard = (options: UseGuardType) => {
if (type?.includes(userCouncilMember)) {
userPaths = userPaths.concat(councilMemberRoutes);
}

if (type?.includes(userBorrower)) {
userPaths = userPaths.concat(borrowerRoutes);
}
}

// TEMPORARY
Expand Down
3 changes: 2 additions & 1 deletion src/utils/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export const publicRoutes: Routes = [
...laePublicRoutes,
'/microcredit/application',
'/microcredit/apply',
'/microcredit/form/[id]'
'/microcredit/form/[id]',
'/microcredit'
];

export const privateRoutes: Routes = ['/profile', '/settings', '/mycommunity'];
Expand Down
2 changes: 2 additions & 0 deletions src/utils/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const userManager = 'manager';
export const userBeneficiary = 'beneficiary';
export const userCouncilMember = 'councilMember';
export const userLoanManager = 'loanManager';
export const userBorrower = 'borrower';

export const getUserTypes = (user: PutPostUser) => {
const types = [userDonor];
Expand All @@ -20,6 +21,7 @@ export const getUserTypes = (user: PutPostUser) => {
if (user?.councilMember) types.push(userCouncilMember);
if (user?.ambassador) types.push(userAmbassador);
if (user?.loanManager) types.push(userLoanManager);
if (user?.borrower) types.push(userBorrower);

return types;
};
Expand Down
23 changes: 8 additions & 15 deletions src/views/MicroCredit/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { useLazyGetBorrowerQuery } from '../../api/microcredit';
import { useRouter } from 'next/router';
import { useSelector } from 'react-redux';
import ClaimLoan from './ClaimLoan';
import ConnectWallet from 'src/components/ConnectWallet';
import LoanCompleted from './LoanCompleted';
import LoanRejected from './LoanRejected';
import LoanRepayment from './LoanRepayment';
Expand All @@ -17,7 +18,6 @@ import String from '../../libs/Prismic/components/String';
import useFilters from 'src/hooks/useFilters';
import useTranslations from '../../libs/Prismic/hooks/useTranslations';
// import InfoAccordion from './InfoAccordion';

import BigNumber from 'bignumber.js';
import processTransactionError from 'src/utils/processTransactionError';
import usePactPriceUSD from 'src/hooks/usePactPriceUSD';
Expand Down Expand Up @@ -53,7 +53,7 @@ const MicroCredit = (props: any) => {
).toString();

getEstimatedLoanRewards(result);
}, [loan]);
}, [loan, auth?.user?.address]);

const router = useRouter();
const { getByKey } = useFilters();
Expand Down Expand Up @@ -131,9 +131,8 @@ const MicroCredit = (props: any) => {
useEffect(() => {
if (isErrorGetBorrower) {
processTransactionError(errorGetBorrower, 'loading_borrower_page');
router.push('/');
}
}, [isErrorGetBorrower]);
}, [isErrorGetBorrower, auth?.user?.address]);

useEffect(() => {
const getLoans = async () => {
Expand Down Expand Up @@ -171,20 +170,20 @@ const MicroCredit = (props: any) => {
} else {
setLoanId(activeLoanId);
}
} else {
router.push('/');
}
};

getLoans().catch((e) => {
console.log(e);
processTransactionError(e, 'loading_borrower_page');
router.push('/');
});
}, []);
}, [auth?.user?.address]);

useEffect(() => {
setIsOverviewOpen(loan.startDate === 0);
}, [isReady]);
}, [isReady, auth?.user?.address]);

if (!auth?.user?.address) return <ConnectWallet title={headingTitle} />;

return (
<ViewContainer
Expand All @@ -193,12 +192,6 @@ const MicroCredit = (props: any) => {
(!isReady && loading) || !isPactPriceReady || !isRewardsReady
}
>
{/* <Alert
warning
icon="alertTriangle"
mb={1.5}
message={'This is a warning'}
/> */}
{getByKey('contractAddress') && (
<Box as="a" onClick={() => router.back()}>
<Label
Expand Down

0 comments on commit d7eb473

Please sign in to comment.