-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a `/user/unsubscribe?email=<email>` page to allow one-click unsubscribing.
- Loading branch information
1 parent
d8c9808
commit a6c7e08
Showing
4 changed files
with
98 additions
and
0 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
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,79 @@ | ||
import { useMutation } from "@apollo/client"; | ||
import gql from "graphql-tag"; | ||
import Router from "next/router"; | ||
import React, { useEffect } from "react"; | ||
import toast from "react-hot-toast"; | ||
import { Layout } from "../../common-components/Layout/Layout"; | ||
import { | ||
handleGraphQlResponse, | ||
stringifyGraphQlError, | ||
useUrlQuery, | ||
} from "../../lib/utils"; | ||
import { H3 } from "../../common-components/H3/H3"; | ||
import { CommonLink } from "../../common-components/CommonLink/CommonLink"; | ||
|
||
const UnsubscribeUserPage = () => { | ||
const [urlQuery, urlQueryIsReady] = useUrlQuery(); | ||
const email = `${urlQuery.email || ""}`; | ||
const mutation = gql` | ||
mutation unsubscribeUser($email: String!) { | ||
unsubscribeUser(email: $email) { | ||
} | ||
} | ||
`; | ||
interface UnsubscribeUserResponse { | ||
unsubscribeUser: { | ||
email: string; | ||
}; | ||
} | ||
const [unsubscribeUserMutation, { error, loading }] = | ||
useMutation<UnsubscribeUserResponse>(mutation, { variables: { email } }); | ||
|
||
const title = "Unsubscribing..."; | ||
|
||
useEffect(() => { | ||
if (urlQueryIsReady && email.length > 0) { | ||
handleGraphQlResponse<UnsubscribeUserResponse>( | ||
unsubscribeUserMutation(), | ||
).then(async ({ success, result }) => { | ||
if (success) { | ||
toast.success("Account is now disabled."); | ||
const url = { | ||
pathname: "/user", | ||
query: { | ||
email: result?.data?.unsubscribeUser.email, | ||
}, | ||
}; | ||
await Router.push(url, url, { shallow: true }); | ||
} | ||
}); | ||
} | ||
}, [urlQueryIsReady, email, unsubscribeUserMutation]); | ||
|
||
const errorAction = ( | ||
<H3> | ||
Try heading over to your{" "} | ||
<CommonLink href="/user/edit">account page</CommonLink> to unsubscribe. | ||
</H3> | ||
); | ||
|
||
if (error) { | ||
return ( | ||
<Layout error={stringifyGraphQlError(error)} errorAction={errorAction} /> | ||
); | ||
} | ||
|
||
if (urlQueryIsReady && !loading && email?.length === 0) { | ||
return ( | ||
<Layout | ||
error={"No email was found in the URL."} | ||
errorAction={errorAction} | ||
/> | ||
); | ||
} | ||
|
||
return <Layout title={title} isLoading />; | ||
}; | ||
|
||
export default UnsubscribeUserPage; |
a6c7e08
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
app – ./
inboxcomics.com
www.inboxcomics.com
app-git-main-inboxcomics.vercel.app
app-inboxcomics.vercel.app