-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
747f183
commit 2ccab2c
Showing
6 changed files
with
248 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
...nPage/subRoutes/CurrentOrgPlan/BillingDetails/EditPaymentMethod/EditPaymentMethodForm.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { PaymentElement, useElements } from '@stripe/react-stripe-js' | ||
import cs from 'classnames' | ||
import { useState } from 'react' | ||
|
||
import { useUpdatePaymentMethod } from 'services/account/useUpdatePaymentMethod' | ||
import { Theme, useThemeContext } from 'shared/ThemeContext' | ||
import Button from 'ui/Button' | ||
|
||
interface PaymentMethodFormProps { | ||
closeForm: () => void | ||
provider: string | ||
owner: string | ||
} | ||
|
||
const EditPaymentMethodForm = ({ | ||
closeForm, | ||
provider, | ||
owner, | ||
}: PaymentMethodFormProps) => { | ||
const [errorState, setErrorState] = useState('') | ||
Check failure on line 20 in src/pages/PlanPage/subRoutes/CurrentOrgPlan/BillingDetails/EditPaymentMethod/EditPaymentMethodForm.tsx GitHub Actions / Run Lint
|
||
const { theme } = useThemeContext() | ||
const isDarkMode = theme === Theme.DARK | ||
|
||
const elements = useElements() | ||
const { | ||
mutate: updatePaymentMethod, | ||
isLoading, | ||
error, | ||
reset, | ||
} = useUpdatePaymentMethod({ | ||
provider, | ||
owner, | ||
}) | ||
|
||
function submit(e: React.FormEvent) { | ||
e.preventDefault() | ||
|
||
if (!elements) { | ||
return null | ||
} | ||
|
||
updatePaymentMethod(elements.getElement(PaymentElement), { | ||
onSuccess: closeForm, | ||
}) | ||
} | ||
|
||
const showError = (error && !reset) || errorState | ||
|
||
return ( | ||
<form onSubmit={submit} aria-label="form"> | ||
<div className={cs('flex flex-col gap-3')}> | ||
<div className="mt-2 flex flex-col gap-2"> | ||
<PaymentElement | ||
options={{ | ||
layout: 'tabs', | ||
defaultValues: { | ||
billingDetails: { | ||
name: 'John Doe', | ||
}, | ||
}, | ||
}} | ||
/> | ||
<p className="mt-1 text-ds-primary-red"> | ||
{showError && (error?.message || errorState)} | ||
</p> | ||
<div className="mb-8 mt-4 flex gap-1"> | ||
<Button | ||
hook="update-payment" | ||
type="submit" | ||
variant="primary" | ||
disabled={isLoading} | ||
to={undefined} | ||
> | ||
Update | ||
</Button> | ||
<Button | ||
type="button" | ||
hook="cancel-payment" | ||
variant="plain" | ||
disabled={isLoading} | ||
onClick={closeForm} | ||
to={undefined} | ||
> | ||
Cancel | ||
</Button> | ||
</div> | ||
</div> | ||
</div> | ||
</form> | ||
) | ||
} | ||
|
||
export default EditPaymentMethodForm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { useQuery } from '@tanstack/react-query' | ||
import { z } from 'zod' | ||
|
||
import Api from 'shared/api' | ||
import { NetworkErrorObject } from 'shared/api/helpers' | ||
|
||
export const StripeSetupIntentSchema = z.object({ | ||
clientSecret: z.string(), | ||
}) | ||
|
||
export interface UseStripeSetupIntentArgs { | ||
provider: string | ||
owner: string | ||
opts?: { | ||
enabled?: boolean | ||
} | ||
} | ||
|
||
function fetchStripeSetupIntent({ | ||
provider, | ||
owner, | ||
signal, | ||
}: { | ||
provider: string | ||
owner: string | ||
signal?: AbortSignal | ||
}) { | ||
const path = `/${provider}/${owner}/account-details/setup_intent` | ||
return Api.get({ path, provider, signal }) | ||
} | ||
|
||
export function useStripeSetupIntent({ | ||
provider, | ||
owner, | ||
opts = {}, | ||
}: UseStripeSetupIntentArgs) { | ||
return useQuery({ | ||
queryKey: ['setupIntent', provider, owner], | ||
queryFn: ({ signal }) => | ||
fetchStripeSetupIntent({ provider, owner, signal }).then((res) => { | ||
const parsedRes = StripeSetupIntentSchema.safeParse(res) | ||
|
||
if (!parsedRes.success) { | ||
return Promise.reject({ | ||
status: 404, | ||
data: {}, | ||
dev: 'useStripeSetupIntent - 404 failed to parse', | ||
} satisfies NetworkErrorObject) | ||
} | ||
|
||
return parsedRes.data | ||
}), | ||
...opts, | ||
}) | ||
} |
Oops, something went wrong.