Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace any with Generic Types in POST and PUT Requests #2358

Draft
wants to merge 6 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions pages/sites/[slug]/[locale]/claim/[type]/[code].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ interface Props {
pageProps: PageProps;
}

export type RedeemCodeSubmitData = {
code: string;
};

function ClaimDonation({ pageProps }: Props): ReactElement {
const t = useTranslations('Redeem');
const router = useRouter();
Expand Down Expand Up @@ -79,12 +83,15 @@ function ClaimDonation({ pageProps }: Props): ReactElement {
};

async function redeemingCode(code: string): Promise<void> {
const submitData = {
const submitData: RedeemCodeSubmitData = {
code: code,
};
if (contextLoaded && user) {
try {
const res = await postAuthenticatedRequest<RedeemedCodeData>({
const res = await postAuthenticatedRequest<
RedeemedCodeData,
RedeemCodeSubmitData
>({
tenant: pageProps.tenantConfig.id,
url: `/app/redeem`,
data: submitData,
Expand Down
8 changes: 6 additions & 2 deletions pages/sites/[slug]/[locale]/profile/redeem/[code].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { AbstractIntlMessages } from 'next-intl';
import type { APIError, SerializedError } from '@planet-sdk/common';
import type { Tenant } from '@planet-sdk/common/build/types/tenant';
import type { RedeemedCodeData } from '../../../../../../src/features/common/types/redeem';
import type { RedeemCodeSubmitData } from '../../claim/[type]/[code]';

import { useRouter } from 'next/router';
import { useState, useEffect, useContext } from 'react';
Expand Down Expand Up @@ -73,13 +74,16 @@ const RedeemCode = ({ pageProps: { tenantConfig } }: Props) => {

async function redeemingCode(data: string): Promise<void> {
setIsLoading(true);
const submitData = {
const submitData: RedeemCodeSubmitData = {
code: data,
};

if (contextLoaded && user) {
try {
const res = await postAuthenticatedRequest<RedeemedCodeData>({
const res = await postAuthenticatedRequest<
RedeemedCodeData,
RedeemCodeSubmitData
>({
tenant: tenantConfig?.id,
url: `/app/redeem`,
data: submitData,
Expand Down
5 changes: 4 additions & 1 deletion src/features/user/Account/EditModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,10 @@ export const EditModal = ({

if (Object.keys(bodyToSend).length !== 0) {
try {
const res = await putAuthenticatedRequest<ModifyDonations>({
const res = await putAuthenticatedRequest<
ModifyDonations,
BodyToSendType
>({
tenant: tenantConfig?.id,
url: `/app/subscriptions/${record?.id}?scope=modify`,
data: bodyToSend,
Expand Down
5 changes: 4 additions & 1 deletion src/features/user/BulkCodes/forms/IssueCodesForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,10 @@ const IssueCodesForm = (): ReactElement | null => {
const cleanedData = cleanObject(donationData);

try {
const res = await postAuthenticatedRequest<Donation>({
const res = await postAuthenticatedRequest<
Donation,
PrepaidDonationRequest
>({
tenant: tenantConfig?.id,
url: `/app/donations`,
data: cleanedData,
Expand Down
2 changes: 1 addition & 1 deletion src/features/user/CompleteSignup/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export default function CompleteSignup(): ReactElement | null {
setRequestSent(true);
setIsProcessing(true);
try {
const res = await postRequest<User>({
const res = await postRequest<User, CreateUserRequest>({
tenant: tenantConfig?.id,
url: `/app/profile`,
data: bodyToSend,
Expand Down
19 changes: 17 additions & 2 deletions src/features/user/ManagePayouts/screens/AddBankAccount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ import { PayoutCurrency } from '../../../../utils/constants/payoutConstants';
import { handleError } from '@planet-sdk/common';
import { useTenant } from '../../../common/Layout/TenantContext';

export interface AccountData {
currency: string;
payoutMinAmount: string | undefined;
bankName: string;
bankCountry: string;
bankAddress: string;
holderName: string;
holderAddress: string;
accountNumber: string;
routingNumber: string;
bic: string;
branchCode: string;
remarks: string;
}

const AddBankAccount = (): ReactElement | null => {
const t = useTranslations('ManagePayouts');
const { payoutMinAmounts, setAccounts, accounts } = usePayouts();
Expand All @@ -32,14 +47,14 @@ const AddBankAccount = (): ReactElement | null => {

const handleSaveAccount = async (data: FormData) => {
setIsProcessing(true);
const accountData = {
const accountData: AccountData = {
...data,
currency: data.currency === PayoutCurrency.DEFAULT ? '' : data.currency,
payoutMinAmount:
data.currency === PayoutCurrency.DEFAULT ? '' : data.payoutMinAmount,
};
try {
const res = await postAuthenticatedRequest<BankAccount>({
const res = await postAuthenticatedRequest<BankAccount, AccountData>({
tenant: tenantConfig?.id,
url: '/app/accounts',
data: accountData,
Expand Down
5 changes: 3 additions & 2 deletions src/features/user/ManagePayouts/screens/EditBankAccount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { useUserProps } from '../../../common/Layout/UserPropsContext';
import { PayoutCurrency } from '../../../../utils/constants/payoutConstants';
import { handleError } from '@planet-sdk/common';
import { useTenant } from '../../../common/Layout/TenantContext';
import { AccountData } from './AddBankAccount';

const EditBankAccount = (): ReactElement | null => {
const { accounts, payoutMinAmounts, setAccounts } = usePayouts();
Expand All @@ -37,15 +38,15 @@ const EditBankAccount = (): ReactElement | null => {

const handleSaveAccount = async (data: FormData) => {
setIsProcessing(true);
const accountData = {
const accountData: AccountData = {
...data,
currency: data.currency === PayoutCurrency.DEFAULT ? '' : data.currency,
payoutMinAmount:
data.currency === PayoutCurrency.DEFAULT ? '' : data.payoutMinAmount,
};

try {
const res = await putAuthenticatedRequest<BankAccount>({
const res = await putAuthenticatedRequest<BankAccount, AccountData>({
tenant: tenantConfig?.id,
url: `/app/accounts/${accountToEdit?.id}`,
data: accountData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const PayoutScheduleForm = (): ReactElement | null => {
setIsProcessing(true);

try {
const res = await putAuthenticatedRequest<User>({
const res = await putAuthenticatedRequest<User, FormData>({
tenant: tenantConfig?.id,
url: '/app/profile',
data: { scheduleFrequency: data.scheduleFrequency },
Expand Down
38 changes: 35 additions & 3 deletions src/features/user/ManageProjects/components/BasicDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,36 @@

type ConservationFormData = FormData;

interface SubmitDataBase {
name: string;
slug: string;
website: string;
description: string;
acceptDonations: boolean;
unitCost: number | undefined;
currency: 'EUR'; // Fixed currency
metadata: {
ecosystem: string;
visitorAssistance: boolean;
};
geometry: {
type: 'Point';
coordinates: [number, number];
};
}

interface SubmitDataTrees extends SubmitDataBase {
unitType: 'tree' | 'm2';
classification: string;
countTarget: number;
}

interface SubmitDataConservation extends SubmitDataBase {
purpose: 'conservation';
}

type SubmitData = SubmitDataTrees | SubmitDataConservation;

export default function BasicDetails({
handleNext,
token,
Expand Down Expand Up @@ -242,71 +272,71 @@
}
}, [router]);

useEffect(() => {
if (projectDetails) {
const basicDetails =
purpose === 'trees'
? {
name: projectDetails.name,
slug: projectDetails.slug,
website: projectDetails.website || '',
description: projectDetails.description,
acceptDonations: projectDetails.acceptDonations,
unitType: projectDetails.unitType,
unitCost: getFormattedNumber(
locale,
projectDetails.unitCost || 0
),
latitude: projectDetails.geoLatitude.toString(),
longitude: projectDetails.geoLongitude.toString(),
metadata: {
visitorAssistance:
projectDetails.metadata.visitorAssistance || false,
ecosystem: projectDetails.metadata.ecosystem || '',
},
classification: projectDetails.classification || '',
countTarget: projectDetails.countTarget || '',
}
: {
name: projectDetails.name,
slug: projectDetails.slug,
website: projectDetails.website || '',
description: projectDetails.description,
acceptDonations: projectDetails.acceptDonations,
unitCost: getFormattedNumber(
locale,
projectDetails.unitCost || 0
),
latitude: projectDetails.geoLatitude.toString(),
longitude: projectDetails.geoLongitude.toString(),
metadata: {
visitorAssistance:
projectDetails.metadata.visitorAssistance || false,
ecosystem: projectDetails.metadata.ecosystem || '',
},
};
if (projectDetails.geoLongitude && projectDetails.geoLatitude) {
setProjectCoords([
projectDetails.geoLongitude,
projectDetails.geoLatitude,
]);
setViewPort({
...viewport,
latitude: projectDetails.geoLatitude,
longitude: projectDetails.geoLongitude,
zoom: 7,
});
}
reset(basicDetails);
if (projectDetails.acceptDonations) {
setAcceptDonations(projectDetails.acceptDonations);
}
}
}, [projectDetails]);

Check notice on line 336 in src/features/user/ManageProjects/components/BasicDetails.tsx

View check run for this annotation

codefactor.io / CodeFactor

src/features/user/ManageProjects/components/BasicDetails.tsx#L275-L336

Complex Method
const onSubmit = async (data: TreeFormData | ConservationFormData) => {
setIsUploadingData(true);
const submitData =
const submitData: SubmitData =
purpose === 'trees'
? {
name: data.name,
Expand Down Expand Up @@ -361,7 +391,8 @@
if (projectGUID) {
try {
const res = await putAuthenticatedRequest<
ProfileProjectTrees | ProfileProjectConservation
ProfileProjectTrees | ProfileProjectConservation,
SubmitData
>({
tenant: tenantConfig?.id,
url: `/app/projects/${projectGUID}`,
Expand All @@ -379,7 +410,8 @@
} else {
try {
const res = await postAuthenticatedRequest<
ProfileProjectTrees | ProfileProjectConservation
ProfileProjectTrees | ProfileProjectConservation,
SubmitData
>({
tenant: tenantConfig?.id,
url: `/app/projects`,
Expand Down
Loading
Loading