diff --git a/carbonmark/components/pages/Users/SellerConnected/Forms/EditProfile.tsx b/carbonmark/components/pages/Users/SellerConnected/Forms/EditProfile.tsx index 94c18bd4e9..5da2e46538 100644 --- a/carbonmark/components/pages/Users/SellerConnected/Forms/EditProfile.tsx +++ b/carbonmark/components/pages/Users/SellerConnected/Forms/EditProfile.tsx @@ -1,13 +1,12 @@ -import { getUsersWalletorhandle } from ".generated/carbonmark-api-sdk/clients"; +import { getUsersWalletorhandle, postLogin, postLoginVerify, postUsers, putUsersWallet } from ".generated/carbonmark-api-sdk/clients"; import { useWeb3 } from "@klimadao/lib/utils"; -import { t, Trans } from "@lingui/macro"; +import { Trans, t } from "@lingui/macro"; import { ButtonPrimary } from "components/Buttons/ButtonPrimary"; +import { Text } from "components/Text"; import { InputField } from "components/shared/Form/InputField"; import { TextareaField } from "components/shared/Form/TextareaField"; import { Spinner } from "components/shared/Spinner"; -import { Text } from "components/Text"; import { isAddress } from "ethers-v6"; -import { loginUser, postUser, putUser, verifyUser } from "lib/api"; import { VALID_HANDLE_REGEX } from "lib/constants"; import { User } from "lib/types/carbonmark.types"; import { isNil } from "lodash"; @@ -74,28 +73,32 @@ export const EditProfile: FC = (props) => { setIsLoading(true); if (!address) return; - const loginRes = await loginUser(address); + const loginRes = await postLogin(address); if (!signer) return; const signature = await signer.signMessage( editSignMessage(loginRes.nonce) ); - const verifyResponse = await verifyUser({ + const verifyResponse = await postLoginVerify({ address, signature, }); - let response; + let response: User; if (isExistingUser) { - response = await putUser({ - user: values, - token: verifyResponse.token, + response = await putUsersWallet(values.wallet, values, { + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${verifyResponse.token}`, + } }); } else { - response = await postUser({ - user: values, - token: verifyResponse.token, + response = await postUsers(values, { + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${verifyResponse.token}`, + } }); } @@ -147,23 +150,23 @@ export const EditProfile: FC = (props) => { "handle", !isExistingUser // validate only if handle can be changed ? { - required: { - value: true, - message: t`Handle is required`, - }, - pattern: { - value: VALID_HANDLE_REGEX, // no special characters! - message: t`Handle should not contain any special characters`, - }, - validate: { - isAddress: (v) => - !isAddress(v) || // do not allow polygon addresses - t`Handle should not be an address`, - isNewHandle: async (v) => - (await fetchIsNewHandle(v)) || // ensure unique handles - t`Sorry, this handle already exists`, - }, - } + required: { + value: true, + message: t`Handle is required`, + }, + pattern: { + value: VALID_HANDLE_REGEX, // no special characters! + message: t`Handle should not contain any special characters`, + }, + validate: { + isAddress: (v) => + !isAddress(v) || // do not allow polygon addresses + t`Handle should not be an address`, + isNewHandle: async (v) => + (await fetchIsNewHandle(v)) || // ensure unique handles + t`Sorry, this handle already exists`, + }, + } : undefined ), }} diff --git a/carbonmark/lib/api.ts b/carbonmark/lib/api.ts index d05cb20a04..78dba30145 100644 --- a/carbonmark/lib/api.ts +++ b/carbonmark/lib/api.ts @@ -1,94 +1,9 @@ import { getUsersWalletorhandle } from ".generated/carbonmark-api-sdk/clients"; import { KlimaRetire } from "@klimadao/lib/types/subgraph"; -import { urls } from "lib/constants"; import { pollUntil } from "lib/pollUntil"; import { User } from "lib/types/carbonmark.types"; import { createDownloadLink } from "./createDownloadLink"; -export const loginUser = async (wallet: string): Promise<{ nonce: string }> => { - const res = await fetch(`${urls.api.users}/login`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - wallet, - }), - }); - - const data = await res.json(); - - if (!res.ok || !data.nonce) { - throw new Error(data.message); - } - return data; -}; - -export const verifyUser = async (params: { - address: string; - signature: string; -}): Promise<{ token: string }> => { - const res = await fetch(`${urls.api.users}/login/verify`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - wallet: params.address, - signature: params.signature, - }), - }); - - const data = await res.json(); - - if (!res.ok || !data.token) { - throw new Error(data.message); - } - return data; -}; - -export const putUser = async (params: { - user: User; - token: string; -}): Promise => { - const res = await fetch(`${urls.api.users}/${params.user.wallet}`, { - method: "PUT", - headers: { - "Content-Type": "application/json", - Authorization: `Bearer ${params.token}`, - }, - body: JSON.stringify(params.user), - }); - - const data = await res.json(); - - if (!res.ok || data.error) { - throw new Error(data.message); - } - return data; -}; - -export const postUser = async (params: { - user: User; - token: string; -}): Promise => { - const res = await fetch(urls.api.users, { - method: "POST", - headers: { - "Content-Type": "application/json", - Authorization: `Bearer ${params.token}`, - }, - body: JSON.stringify(params.user), - }); - - const data = await res.json(); - - if (!res.ok || data.error) { - throw new Error(data.message); - } - return data; -}; - export const getRetirements = async (params: { beneficiaryAddress: string; limit?: number;