Skip to content

Commit

Permalink
Dogfooding Fixes: Microcredit (#498)
Browse files Browse the repository at this point in the history
Co-authored-by: Hugo Marques <[email protected]>
  • Loading branch information
hugomarquesdev and Hugo Marques authored Aug 2, 2023
1 parent 5538998 commit 8811a04
Show file tree
Hide file tree
Showing 17 changed files with 130 additions and 145 deletions.
15 changes: 0 additions & 15 deletions src/api/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,20 +116,6 @@ export const userApi = emptySplitApi.injectEndpoints({
url: 'users'
})
}),
// Get notifications
getNotifications: builder.mutation<
Notification[],
{ limit: number; offset: number }
>({
query: ({ limit, offset }) => ({
method: 'GET',
url: `users/notifications?${
!!offset ? `&offset=${offset}` : ''
}${!!limit ? `&limit=${limit}` : ''}`
}),
transformResponse: (response: { data: Notification[] }) =>
response.data
}),
// Get preSigned URL for image upload
getPreSigned: builder.mutation<PreSigned, void>({
query: (type) => ({
Expand Down Expand Up @@ -190,7 +176,6 @@ export const {
useAnonymousReportMutation,
useCreateUserMutation,
useDeleteUserMutation,
useGetNotificationsMutation,
useGetUserMutation,
useGetUserByIdMutation,
useRecoverAccountMutation,
Expand Down
5 changes: 3 additions & 2 deletions src/components/FlexibleTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ export type TabItem = {

type FlexibleTabProps = {
tabs: TabItem[];
index?: number;
};

const FlexibleTab: React.FC<FlexibleTabProps> = ({ tabs }) => {
const FlexibleTab: React.FC<FlexibleTabProps> = ({ tabs, index }) => {
return (
<Tabs>
<Tabs defaultIndex={index}>
<TabList>
{tabs.map((tab, index) => (
<Tab
Expand Down
11 changes: 7 additions & 4 deletions src/components/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
import { formatAddress } from '../utils/formatAddress';
import { getCookie, hasCookie, setCookie } from 'cookies-next';
import { getImage } from '../utils/images';
import { getNotifications } from '../state/slices/notifications';
import { getUserMenu } from './UserMenu';
import { getUserName } from '../utils/users';
import { languagesOptions } from 'src/utils/languages';
Expand Down Expand Up @@ -246,7 +245,6 @@ const SidebarMobileActions = (props: { user?: User }) => {
const Sidebar = () => {
const { asPath, locale, push, replace } = useRouter();
const { user } = useSelector(selectCurrentUser);
const { notifications } = useSelector(getNotifications);
const { address } = useWallet();

const [data, setData] = useState<MenusState | undefined>();
Expand Down Expand Up @@ -298,11 +296,16 @@ const Sidebar = () => {

setData({
commonMenu,
flags: notifications,
flags: [
{
key: 'notifications',
value: user?.notificationsCount
}
],
footerMenu,
menus
});
}, [asPath, user, notifications, locale, address]);
}, [asPath, user, locale, address]);

const footerMenu = () => {
const userBeneficiary = user?.roles?.includes('beneficiary');
Expand Down
15 changes: 0 additions & 15 deletions src/helpers/handleKnownErrors.ts

This file was deleted.

16 changes: 16 additions & 0 deletions src/helpers/handleKnownErrors.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { toast } from '@impact-market/ui';
import Message from 'src/libs/Prismic/components/Message';

export const handleKnownErrors = (error: any) => {
const errorMessage = error.toString().toLowerCase();

if (errorMessage.includes('insufficient funds')) {
toast.error(<Message id="insufficientFunds" />);
} else if (errorMessage.includes("don't have enough funds to borrow")) {
toast.error(<Message id="notEnoughFunds" />);
} else if (errorMessage.includes('user already has an active loan')) {
toast.error(<Message id="alreadyHasLoan" />);
} else {
toast.error(<Message id="errorOccurred" />);
}
};
2 changes: 1 addition & 1 deletion src/hooks/useNotifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function useNotifications(filters?: any[]) {
}).then((res) => res.json());

const { data, mutate, error } = useSWR(
`/users/notifications/${
`/users/notifications?${
!!filters?.length
? filters?.map((filter: any) => filter).join('&')
: ''
Expand Down
18 changes: 11 additions & 7 deletions src/modals/Microcredit/ApproveLoan.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ import useTranslations from 'src/libs/Prismic/hooks/useTranslations';

const ApproveLoan = () => {
const { extractFromView } = usePrismicData();
const { enterLoanMaturity, approveLoan } = extractFromView(
'messages'
) as any;
const {
enterLoanMaturity,
approveLoan,
loansApproved,
loanMaturityHint,
enterLoanAmount
} = extractFromView('messages') as any;

const { handleClose, address, mutate } = useModal();
const { t } = useTranslations();
Expand Down Expand Up @@ -96,7 +100,7 @@ const ApproveLoan = () => {

if (status) {
mutate();
toast.success('Loan(s) approved successfully!');
toast.success(loansApproved);
handleClose();
} else {
toast.error(<Message id="errorOccurred" />);
Expand Down Expand Up @@ -148,11 +152,11 @@ const ApproveLoan = () => {
control={control}
name="period"
withError={!!errors?.period}
hint="Max. 12 months"
hint={loanMaturityHint}
/>
<Input
type="number"
placeholder="Enter loan amount..."
placeholder={enterLoanAmount}
wrapperProps={{
mt: 1,
w: '100%'
Expand Down Expand Up @@ -202,7 +206,7 @@ const ApproveLoan = () => {
}
isLoading={isSubmitting}
>
Approve Loan
{approveLoan}
</Button>
</Box>
</Box>
Expand Down
16 changes: 12 additions & 4 deletions src/modals/Signature.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,35 @@ import { handleSignature } from 'src/helpers/handleSignature';
import { useRouter } from 'next/router';
import { useSignatures } from '@impact-market/utils/useSignatures';
import Message from 'src/libs/Prismic/components/Message';
import React from 'react';
import React, { useState } from 'react';
import String from '../libs/Prismic/components/String';
import processTransactionError from 'src/utils/processTransactionError';
import useTranslations from 'src/libs/Prismic/hooks/useTranslations';
import useWallet from 'src/hooks/useWallet';

const Signature = () => {
const { t } = useTranslations();
const { signMessage, signTypedData } = useSignatures();
const { disconnect } = useWallet();
const { handleClose } = useModal();
const router = useRouter();
const [isLoading, setIsLoading] = useState(false);

const handleSignMessage = async () => {
try {
setIsLoading(true);

await handleSignature(signMessage, signTypedData);
toast.success('Logged in successfully.');

setIsLoading(false);
toast.success(t('loggedIn'));
handleClose();
} catch (error) {
console.log(error);
processTransactionError(error, 'global_signature');
toast.error(<Message id="errorOccurred" />);
}
setIsLoading(false);
};

const handleDisconnectClick = async () => {
Expand All @@ -51,7 +59,7 @@ const Signature = () => {
<Message g500 small id="validateSignature" />
<Box mt={1.25} flex style={{ gap: '1rem' }}>
<Button
isLoading={false}
isLoading={isLoading}
onClick={() => {
handleSignMessage();
}}
Expand All @@ -65,7 +73,7 @@ const Signature = () => {
handleDisconnectClick();
}}
>
Logout
{t('logout')}
</Button>
</Box>
</Box>
Expand Down
22 changes: 0 additions & 22 deletions src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
} from '@impact-market/ui';
import { PrismicDataProvider } from '../libs/Prismic/components/PrismicDataProvider';
import { Provider, useSelector } from 'react-redux';
import { addNotification } from '../state/slices/notifications';
import { getCookie, hasCookie } from 'cookies-next';
import {
selectCurrentUser,
Expand All @@ -20,7 +19,6 @@ import { setRates } from '../state/slices/rates';
import { store } from '../state/store';
import { useAccount } from 'wagmi';
import { useGetExchangeRatesMutation } from '../api/generic';
import { useNotifications } from 'src/hooks/useNotifications';
import ErrorPage from 'next/error';
import GoogleAnalytics from '../components/GoogleAnalytics';
import React, { useEffect } from 'react';
Expand Down Expand Up @@ -71,26 +69,6 @@ const InnerApp = (props: AppProps) => {
init();
}, []);

const { notifications } = useNotifications([
`unreadOnly=true`,
`isWebApp=true`
]);

useEffect(() => {
try {
store.dispatch(
addNotification({
notification: {
key: 'notifications',
value: notifications?.rows || 0
}
})
);
} catch (error) {
console.log(error);
}
}, [notifications]);

const Content = Component as any;

return (
Expand Down
47 changes: 0 additions & 47 deletions src/state/slices/notifications.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/state/store.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { emptySplitApi as api } from '../api';
import { configureStore } from '@reduxjs/toolkit';
import authReducer from './slices/auth';
import notificationsReducer from './slices/notifications';
import ratesReducer from './slices/rates';

import learnAndEarnReduce from './slices/learnAndEarn';
Expand All @@ -14,7 +13,6 @@ export const store = configureStore({
reducer: {
auth: authReducer,
learnAndEarn: learnAndEarnReduce,
notifications: notificationsReducer,
rates: ratesReducer,
[api.reducerPath]: api.reducer
}
Expand Down
14 changes: 11 additions & 3 deletions src/views/MicrocreditManager/ApproveRejectTab/BorrowersList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ const loanStatus = (status: any) => {
case 3: // Requested Changes
badgeContent = (
<>
<Icon icon={'menu'} p700 mr={0.2} />
<Icon icon={'edit'} p700 mr={0.2} />
<Text g900 extrasmall medium>
In Revision
{t('revise')}
</Text>
</>
);
Expand Down Expand Up @@ -389,7 +389,7 @@ const getColumns = (props: any) => {
{
minWidth: 10,
render: (data: any) => loanStatus(data?.application?.status),
title: 'Status',
title: t('status'),
value: 'status',
width: '15%'
},
Expand All @@ -413,6 +413,14 @@ const getColumns = (props: any) => {
rejectLoan(auth, data?.application?.id, mutate),
title: rejectLoanText
},
// {
// icon: 'bookOpen',
// onClick: () =>
// router.push(
// `/microcredit/form/${data?.application?.id}`
// ),
// title: 'Loan Application'
// },
{
icon: 'user',
onClick: () => router.push(`/user/${data.address}`),
Expand Down
Loading

0 comments on commit 8811a04

Please sign in to comment.