Skip to content

Commit

Permalink
chore: Fix client params undefined issue
Browse files Browse the repository at this point in the history
  • Loading branch information
0xemc committed Nov 27, 2023
1 parent 622ec6e commit 7491829
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 40 deletions.
23 changes: 12 additions & 11 deletions carbonmark/.generated/carbonmark-api-sdk/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,18 @@ export const fetchClient = async <
>(
request: RequestConfig<TVariables>
): Promise<ResponseConfig<TData>> => {
const response = await fetch(
`${urls.api.base}${request.url}${request.params ?? ""}`,
{
method: request.method,
body: JSON.stringify(request.data),
headers: {
"Content-Type": "application/json",
...request.headers,
},
}
);
const params = new URLSearchParams(
request.params as Record<string, string>
).toString();

const response = await fetch(`${urls.api.base}${request.url}?${params}`, {
method: request.method,
body: JSON.stringify(request.data),
headers: {
"Content-Type": "application/json",
...request.headers,
},
});

if (!response.ok) {
const errorData = await response.json();
Expand Down
5 changes: 4 additions & 1 deletion carbonmark/components/UserTracker/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useGetUsersWalletorhandle } from ".generated/carbonmark-api-sdk/hooks";
import { useWeb3 } from "@klimadao/lib/utils";
import { LO } from "lib/luckyOrange";
import { notNil } from "lib/utils/functional.utils";
import { FC, useEffect } from "react";

interface Props {
Expand All @@ -11,7 +12,9 @@ interface Props {
export const UserTracker: FC<Props> = (props) => {
const { address, isConnectionFromCache } = useWeb3();
const { data: carbonmarkUser, isLoading } = useGetUsersWalletorhandle(
address ?? ""
address ?? "",
{},
{ shouldFetch: notNil(address) }
);
useEffect(() => {
// Start tracking only if we finished loading carbonmarkUser data
Expand Down
3 changes: 1 addition & 2 deletions carbonmark/components/pages/Portfolio/Retire/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import client from ".generated/carbonmark-api-sdk/client";
import { useGetUsersWalletorhandle } from ".generated/carbonmark-api-sdk/hooks";
import { useWeb3 } from "@klimadao/lib/utils";
import { Messages } from "@lingui/core";
Expand Down Expand Up @@ -40,7 +39,7 @@ export const Retire: NextPage<RetirePageProps> = (props) => {
const { data: carbonmarkUser, isLoading } = useGetUsersWalletorhandle(
address ?? "",
{ network: networkLabel, expiresAfter: "0" },
{ query: { fetcher: notNil(address) ? client : async () => undefined } }
{ shouldFetch: notNil(address) }
);

const [retirementAsset, setRetirementAsset] =
Expand Down
3 changes: 1 addition & 2 deletions carbonmark/components/pages/Portfolio/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import client from ".generated/carbonmark-api-sdk/client";
import { useGetUsersWalletorhandle } from ".generated/carbonmark-api-sdk/hooks";
import { useWeb3 } from "@klimadao/lib/utils";
import { t, Trans } from "@lingui/macro";
Expand Down Expand Up @@ -33,7 +32,7 @@ export const Portfolio: NextPage = () => {
} = useGetUsersWalletorhandle(
address ?? "",
{ network: networkLabel, expiresAfter: "0" },
{ query: { fetcher: notNil(address) ? client : async () => undefined } }
{ shouldFetch: notNil(address) }
);

const [isPending, setIsPending] = useState(false);
Expand Down
5 changes: 4 additions & 1 deletion carbonmark/components/pages/Retire/FromPortfolio/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ButtonPrimary } from "components/Buttons/ButtonPrimary";
import { SpinnerWithLabel } from "components/SpinnerWithLabel";
import { Text } from "components/Text";
import { addProjectsToAssets, AssetWithProject } from "lib/actions";
import { notNil } from "lib/utils/functional.utils";
import { isListableToken } from "lib/utils/listings.utils";
import { FC, useEffect, useState } from "react";
import { AssetProject } from "./AssetProject";
Expand All @@ -15,7 +16,9 @@ export type Props = {

export const RetireFromPortfolio: FC<Props> = (props) => {
const { data: carbonmarkUser, isLoading } = useGetUsersWalletorhandle(
props.address
props.address,
{},
{ shouldFetch: notNil(props.address) }
);

const [isLoadingAssets, setIsLoadingAssets] = useState(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import LaunchIcon from "@mui/icons-material/Launch";
import { ProfileLogo } from "components/pages/Users/ProfileLogo";
import { Text } from "components/Text";
import { urls } from "lib/constants";
import { notNil } from "lib/utils/functional.utils";
import { FC } from "react";
import * as styles from "./styles";

Expand All @@ -15,7 +16,9 @@ type Props = {

export const BeneficiaryDetails: FC<Props> = (props) => {
const { data: carbonmarkUser } = useGetUsersWalletorhandle(
props.beneficiaryAddress
props.beneficiaryAddress,
{},
{ shouldFetch: notNil(props.beneficiaryAddress) }
);
return (
<div className={styles.beneficiaryCard}>
Expand Down
12 changes: 8 additions & 4 deletions carbonmark/components/pages/Users/SellerConnected/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,14 @@ export const SellerConnected: FC<Props> = (props) => {
data: carbonmarkUser,
isLoading,
mutate,
} = useGetUsersWalletorhandle(props.userAddress, {
network,
expiresAfter: address === props.userAddress ? "0" : undefined,
});
} = useGetUsersWalletorhandle(
props.userAddress,
{
network,
expiresAfter: address === props.userAddress ? "0" : undefined,
},
{ shouldFetch: notNil(address) }
);
const [isPending, setIsPending] = useState(false);
const [showEditProfileModal, setShowEditProfileModal] = useState(false);
const [showCreateListingModal, setShowCreateListingModal] = useState(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Text } from "components/Text";
import { Col, TwoColLayout } from "components/TwoColLayout";
import { createProjectPurchaseLink } from "lib/createUrls";
import { getActiveListings, getSortByUpdateListings } from "lib/listingsGetter";
import { notNil } from "lib/utils/functional.utils";
import { FC } from "react";
import { Listing } from "../Listing";
import { ProfileHeader } from "../ProfileHeader";
Expand All @@ -25,7 +26,8 @@ export const SellerUnconnected: FC<Props> = (props) => {
{
network: networkLabel,
expiresAfter: address === props.userAddress ? "0" : undefined,
}
},
{ shouldFetch: notNil(address) }
);

const activeListings = getActiveListings(carbonmarkUser?.listings ?? []);
Expand Down
10 changes: 4 additions & 6 deletions carbonmark/components/pages/Users/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@ const Page: NextPage<PageProps> = (props) => {
return (
<>
<PageHead
title={t`${
props.carbonmarkUser?.handle || concatAddress(props.userAddress)
} | Profile | Carbonmark`}
mediaTitle={`${
props.carbonmarkUser?.handle || concatAddress(props.userAddress)
}'s Profile on Carbonmark`}
title={t`${props.carbonmarkUser?.handle || concatAddress(props.userAddress)
} | Profile | Carbonmark`}
mediaTitle={`${props.carbonmarkUser?.handle || concatAddress(props.userAddress)
}'s Profile on Carbonmark`}
metaDescription={t`Create and edit listings, and track your activity with your Carbonmark profile.`}
/>

Expand Down
30 changes: 19 additions & 11 deletions carbonmark/lib/api/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { urls } from "lib/constants";
import { pickBy } from "lodash/fp";

export type RequestConfig<TVariables = unknown> = {
method: "get" | "put" | "patch" | "post" | "delete";
Expand All @@ -15,24 +16,31 @@ export type ApiError<TError> = {
data?: TError;
};

/** Removes all undefined or null values */
const definedParams = pickBy((value) => value !== undefined && value !== null);

export const fetchClient = async <
TData,
TError = unknown,
TVariables = unknown,
>(
request: RequestConfig<TVariables>
): Promise<ResponseConfig<TData>> => {
const response = await fetch(
`${urls.api.base}${request.url}${request.params ?? ""}`,
{
method: request.method,
body: JSON.stringify(request.data),
headers: {
"Content-Type": "application/json",
...request.headers,
},
}
);
const params = new URLSearchParams(
definedParams(request.params as Record<string, string>) as Record<
string,
string
>
).toString();

const response = await fetch(`${urls.api.base}${request.url}?${params}`, {
method: request.method,
body: JSON.stringify(request.data),
headers: {
"Content-Type": "application/json",
...request.headers,
},
});

if (!response.ok) {
const errorData = await response.json();
Expand Down

0 comments on commit 7491829

Please sign in to comment.