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

feat(console): delete app modal checks custom domain config #2373

Merged
merged 4 commits into from
Jun 12, 2023
Merged
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
167 changes: 103 additions & 64 deletions apps/console/app/components/DeleteAppModal/DeleteAppModal.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import React, { useState } from 'react'
import { useState } from 'react'
import { Link, useFetcher } from '@remix-run/react'
import type { FetcherWithComponents } from '@remix-run/react'

import { Button } from '@proofzero/design-system/src/atoms/buttons/Button'
import { Modal } from '@proofzero/design-system/src/molecules/modal/Modal'
Expand All @@ -8,29 +10,30 @@ import dangerVector from '../../images/danger.svg'
import { Input } from '@proofzero/design-system/src/atoms/form/Input'
import { RiLoader5Fill } from 'react-icons/ri'

import type { appDetailsProps } from '~/types'

export type DeleteAppModalProps = {
clientId: string
appName: string
appDetails: appDetailsProps
isOpen: boolean
deleteAppCallback: (app: any) => void
deleteAppCallback: (state: boolean) => unknown
}

export const DeleteAppModal = ({
clientId,
appName,
appDetails,
isOpen,
deleteAppCallback,
}: DeleteAppModalProps) => {
const [isAppNameMatches, setAppNameMatches] = useState(false)
const [isSubmitting, setIsSubmitting] = useState(false)
const fetcher = useFetcher()
const [hasCustomDomain] = useState(!Boolean(appDetails.customDomain))

return (
<Modal
isOpen={isOpen}
closable={!isSubmitting}
closable={fetcher.state !== 'submitting'}
handleClose={() => deleteAppCallback(false)}
>
<div
className={`w-[62vw] rounded-lg bg-white px-4 pb-4
className={`w-[48vw] rounded-lg bg-white px-4 pb-4
text-left transition-all sm:px-6 sm:pb-6 overflow-y-auto flex items-start space-x-4`}
>
<img src={dangerVector} />
Expand All @@ -40,62 +43,98 @@ export const DeleteAppModal = ({
Delete Application
</Text>

<form
method="post"
action="/apps/delete"
onSubmit={() => {
setIsSubmitting(true)
}}
>
<section className="mb-4">
<Text size="sm" weight="normal" className="text-gray-500 my-3">
Are you sure you want to delete <b>{appName}</b> app? This
action cannot be undone once confirmed.
</Text>
<Text size="sm" weight="normal" className="text-gray-500 my-3">
Confirm you want to delete this application by typing its name
below.
</Text>
<Input
id="client_name"
label="Application Name"
placeholder="My application"
required
className="mb-12"
onChange={(e) => {
setAppNameMatches(appName === e?.target?.value)
}}
/>
</section>
<input type="hidden" name="clientId" value={clientId} />

<div className="flex justify-end items-center space-x-3">
<Button
btnType="secondary-alt"
disabled={isSubmitting}
onClick={() => deleteAppCallback(false)}
>
Cancel
</Button>
<Button
disabled={!isAppNameMatches || isSubmitting}
type="submit"
btnType="dangerous"
className={
isSubmitting
? 'flex items-center justify-between transition'
: ''
}
>
{isSubmitting && (
<RiLoader5Fill className="animate-spin" size={22} />
)}
Delete
</Button>
</div>
</form>
{hasCustomDomain && (
<HasCustomDomain appDetails={appDetails}></HasCustomDomain>
)}
{!hasCustomDomain && (
<DeleteModalAppForm
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The form inside DeleteModalAppForm makes a post to /apps/delete. This API route also needs to do this check on the backend and throw an error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

fetcher={fetcher}
appDetails={appDetails}
callback={deleteAppCallback}
/>
)}
</div>
</div>
</Modal>
)
}

type DeleteModalAppFormProps = {
fetcher: FetcherWithComponents<any>
appDetails: appDetailsProps
callback: (state: boolean) => unknown
}

const DeleteModalAppForm = ({
fetcher,
appDetails,
callback,
}: DeleteModalAppFormProps) => {
const [isAppNameMatches, setAppNameMatches] = useState(false)
return (
<fetcher.Form method="post" action="/apps/delete" reloadDocument={true}>
<section className="mb-4">
<Text size="sm" weight="normal" className="text-gray-500 my-3">
Are you sure you want to delete <b>{appDetails.app.name}</b> app? This
action cannot be undone once confirmed.
</Text>
<Text size="sm" weight="normal" className="text-gray-500 my-3">
Confirm you want to delete this application by typing its name below.
</Text>
<Input
id="client_name"
label="Application Name"
placeholder="My application"
required
className="mb-12"
onChange={(e) =>
setAppNameMatches(appDetails.app.name === e?.target?.value)
}
/>
</section>
<input type="hidden" name="clientId" value={appDetails.clientId} />

<div className="flex justify-end items-center space-x-3">
<Button
btnType="secondary-alt"
disabled={fetcher.state === 'submitting'}
onClick={() => callback(false)}
>
Cancel
</Button>
<Button
disabled={!isAppNameMatches || fetcher.state === 'submitting'}
type="submit"
btnType="dangerous"
className={
fetcher.state === 'submitting'
? 'flex items-center justify-between transition'
: ''
}
>
{fetcher.state === 'submitting' && (
<RiLoader5Fill className="animate-spin" size={22} />
)}
Delete
</Button>
</div>
</fetcher.Form>
)
}

type HasCustomDomainProps = {
appDetails: appDetailsProps
}

const HasCustomDomain = ({ appDetails }: HasCustomDomainProps) => (
<section className="flex flex-col mb-4">
<Text size="sm" weight="normal" className="text-gray-500 my-3">
This application has a custom domain configured. You need to delete it
before you can delete the application.
</Text>

<Link to={`/apps/${appDetails.clientId}/domain-wip`} className="self-end">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be set to final route as part of #2374

<Button btnType="secondary-alt">Go to Custom Domain</Button>
</Link>
</section>
)
40 changes: 24 additions & 16 deletions apps/console/app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -224,47 +224,47 @@ export default function App() {
)
}

export const ErrorBoundary = ({
error,
}: {
error?: {
stack: any
message: string
}
}) => {
// https://remix.run/docs/en/v1/guides/errors
// @ts-ignore
export function ErrorBoundary({ error }) {
const nonce = useContext(NonceContext)

console.error('ErrorBoundary', error)

return (
<html lang="en">
<head>
<Meta />
<Links />
</head>

<body className="min-h-[100dvh] flex justify-center items-center">
<div className="w-full">
<ErrorPage
code="Error"
message="Something went terribly wrong!"
trace={error?.stack}
error={error}
pepe={false}
backBtn={false}
/>
</div>

<ScrollRestoration nonce={nonce} />
<Scripts nonce={nonce} />
<LiveReload port={8002} nonce={nonce} />
</body>
</html>
)
}

export function CatchBoundary() {
const caught = useCatch()
console.error('CatchBoundary', { caught })
console.error('CaughtBoundary', caught)

const { status } = caught

let secondary = 'Something went wrong'
switch (caught.status) {
switch (status) {
case 404:
secondary = 'Page not found'
break
Expand All @@ -281,14 +281,22 @@ export function CatchBoundary() {
<Meta />
<Links />
</head>

<body className="min-h-[100dvh] flex justify-center items-center">
<div className="w-full">
<ErrorPage code={caught.status.toString()} message={secondary} />
<body>
<div
className={
'flex flex-col h-[100dvh] gap-4 justify-center items-center'
}
>
<h1>{status}</h1>
<p>
{secondary}
{caught.data?.message && `: ${caught.data?.message}`}
</p>
<p>({caught.data?.traceparent && `${caught.data?.traceparent}`})</p>
</div>

<ScrollRestoration />
<Scripts />
<LiveReload port={8002} />
</body>
</html>
)
Expand Down
11 changes: 3 additions & 8 deletions apps/console/app/routes/apps/$clientId/auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -298,12 +298,9 @@ export default function AppDetailIndexPage() {
<>
{isImgUploading ? <Loader /> : null}
<DeleteAppModal
clientId={appDetails.clientId as string}
appName={appDetails.app.name}
deleteAppCallback={() => {
setDeleteModalOpen(false)
}}
appDetails={appDetails}
isOpen={deleteModalOpen}
deleteAppCallback={() => setDeleteModalOpen(false)}
/>

<Form
Expand Down Expand Up @@ -678,9 +675,7 @@ export default function AppDetailIndexPage() {
<Button
type="button"
btnType="dangerous-alt"
onClick={() => {
setDeleteModalOpen(true)
}}
onClick={() => setDeleteModalOpen(true)}
>
Delete the Application
</Button>
Expand Down
40 changes: 28 additions & 12 deletions apps/console/app/routes/apps/delete.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
import { ActionFunction, json, redirect } from '@remix-run/cloudflare'
import { redirect } from '@remix-run/cloudflare'
import type { ActionFunction } from '@remix-run/cloudflare'
import createStarbaseClient from '@proofzero/platform-clients/starbase'
import { requireJWT } from '~/utilities/session.server'
import { getAuthzHeaderConditionallyFromToken } from '@proofzero/utils'
import { generateTraceContextHeaders } from '@proofzero/platform-middleware/trace'
import { getRollupReqFunctionErrorWrapper } from '@proofzero/utils/errors'
import { InternalServerError } from '@proofzero/errors'
import {
JsonError,
getErrorCause,
getRollupReqFunctionErrorWrapper,
} from '@proofzero/utils/errors'
import { BadRequestError, InternalServerError } from '@proofzero/errors'

export const action: ActionFunction = getRollupReqFunctionErrorWrapper(
async ({ request, context }) => {
const formData = await request.formData()
const clientId = formData.get('clientId')?.toString()

if (!clientId) throw 'Client ID is required'
if (!clientId)
throw new BadRequestError({ message: 'Client ID is required' })

const jwt = await requireJWT(request)

Expand All @@ -20,15 +26,25 @@ export const action: ActionFunction = getRollupReqFunctionErrorWrapper(
...generateTraceContextHeaders(context.traceSpan),
})
try {
await starbaseClient.deleteApp.mutate({
clientId,
})
return redirect(`/`)
await starbaseClient.deleteApp.mutate({ clientId })
return redirect('/')
} catch (error) {
console.error({ error })
return new InternalServerError({
message: 'Could not delete application',
})
const cause = getErrorCause(error)
const traceparent = context.traceSpan.getTraceParent()
if (cause instanceof BadRequestError) {
throw cause
} else {
console.error(error)
throw JsonError(
new InternalServerError({
message: 'Could not delete the application',
cause: error,
}),
traceparent
)
}
}
}
)

export default () => {}
2 changes: 2 additions & 0 deletions apps/console/app/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type {
AuthorizedUser,
AppObject,
EdgesMetadata,
CustomDomain,
} from '@proofzero/platform/starbase/src/types'

export enum RollType {
Expand All @@ -20,6 +21,7 @@ export type appDetailsProps = {
clientId?: string
secretTimestamp?: number
apiKeyTimestamp?: number
customDomain?: CustomDomain
}

export type errorsAuthProps = {
Expand Down
Loading