-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1223 from Shelf-nu/1222-feat-request-to-delete-ac…
…count feat: request to delete account
- Loading branch information
Showing
8 changed files
with
193 additions
and
63 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,4 +60,7 @@ GEOCODE_API_KEY="geocode-api-key" | |
SENTRY_ORG="sentry-org" | ||
SENTRY_PROJECT="sentry-project" | ||
SENTRY_DSN="sentry-dsn" | ||
# CHROME_EXECUTABLE_PATH="/usr/bin/chromium" | ||
# CHROME_EXECUTABLE_PATH="/usr/bin/chromium" | ||
|
||
# Used for sending emails to admins for stuff like Request user delete. Optional. Defaults to [email protected] | ||
ADMIN_EMAIL="[email protected]" |
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,13 @@ | ||
import { useNavigation, type Fetcher } from "@remix-run/react"; | ||
import { isFormProcessing } from "~/utils/form"; | ||
|
||
/** | ||
* Used to know if a button should be disabled on navigation. | ||
* By default it works with navigation state | ||
* Optionally it can receive a fetcher to use as state | ||
*/ | ||
export function useDisabled(fetcher?: Fetcher) { | ||
const navigation = useNavigation(); | ||
const state = fetcher ? fetcher.state : navigation.state; | ||
return isFormProcessing(state); | ||
} |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import type { User } from "@prisma/client"; | ||
import type { ActionFunctionArgs, MetaFunction } from "@remix-run/node"; | ||
import { json, redirect } from "@remix-run/node"; | ||
|
||
|
@@ -10,6 +11,7 @@ import { Form } from "~/components/custom-form"; | |
import FormRow from "~/components/forms/form-row"; | ||
import Input from "~/components/forms/input"; | ||
import { Button } from "~/components/shared/button"; | ||
import { DeleteUser } from "~/components/user/delete-user"; | ||
import PasswordResetForm from "~/components/user/password-reset-form"; | ||
import ProfilePicture from "~/components/user/profile-picture"; | ||
|
||
|
@@ -24,10 +26,12 @@ import { appendToMetaTitle } from "~/utils/append-to-meta-title"; | |
import { checkExhaustiveSwitch } from "~/utils/check-exhaustive-switch"; | ||
import { delay } from "~/utils/delay"; | ||
import { sendNotification } from "~/utils/emitter/send-notification.server"; | ||
import { ADMIN_EMAIL } from "~/utils/env"; | ||
import { makeShelfError } from "~/utils/error"; | ||
import { isFormProcessing } from "~/utils/form"; | ||
import { getValidationErrors } from "~/utils/http"; | ||
import { data, error, parseData } from "~/utils/http.server"; | ||
import { sendEmail } from "~/utils/mail.server"; | ||
import { zodFieldIsRequired } from "~/utils/zod"; | ||
|
||
export const UpdateFormSchema = z.object({ | ||
|
@@ -44,8 +48,9 @@ export const UpdateFormSchema = z.object({ | |
|
||
const Actions = z.discriminatedUnion("intent", [ | ||
z.object({ | ||
intent: z.literal("resetPassword"), | ||
intent: z.enum(["resetPassword", "deleteUser"]), | ||
email: z.string(), | ||
reason: z.string(), | ||
}), | ||
UpdateFormSchema.extend({ | ||
intent: z.literal("updateUser"), | ||
|
@@ -102,6 +107,34 @@ export async function action({ context, request }: ActionFunctionArgs) { | |
|
||
return json(data({ success: true })); | ||
} | ||
case "deleteUser": { | ||
let reason = "No reason provided"; | ||
if ("reason" in payload && payload.reason) { | ||
reason = payload?.reason; | ||
} | ||
|
||
void sendEmail({ | ||
to: ADMIN_EMAIL || "[email protected]", | ||
subject: "Delete account request", | ||
text: `User with id ${userId} and email ${payload.email} has requested to delete their account. \n\n Reason: ${reason}\n\n`, | ||
}); | ||
|
||
void sendEmail({ | ||
to: payload.email, | ||
subject: "Delete account request received", | ||
text: `We have received your request to delete your account. It will be processed within 72 hours.\n\n Kind regards,\nthe Shelf team \n\n`, | ||
}); | ||
|
||
sendNotification({ | ||
title: "Account deletion request", | ||
message: | ||
"Your request has been sent to the admin and will be processed within 24 hours. You will receive an email confirmation.", | ||
icon: { name: "success", variant: "success" }, | ||
senderId: authSession.userId, | ||
}); | ||
|
||
return json(data({ success: true })); | ||
} | ||
default: { | ||
checkExhaustiveSwitch(intent); | ||
return json(data(null)); | ||
|
@@ -132,7 +165,7 @@ export default function UserPage() { | |
const transition = useNavigation(); | ||
const disabled = isFormProcessing(transition.state); | ||
const data = useActionData<typeof action>(); | ||
const user = useUserData(); | ||
const user = useUserData() as unknown as User; | ||
const usernameError = | ||
getValidationErrors<typeof UpdateFormSchema>(data?.error)?.username | ||
?.message || zo.errors.username()?.message; | ||
|
@@ -268,6 +301,14 @@ export default function UserPage() { | |
<p className="text-sm text-gray-600">Update your password here</p> | ||
</div> | ||
<PasswordResetForm userEmail={user?.email || ""} /> | ||
|
||
<div className="my-6"> | ||
<h3 className="text-text-lg font-semibold">Delete account</h3> | ||
<p className="text-sm text-gray-600"> | ||
Send a request to delete your account. | ||
</p> | ||
<DeleteUser /> | ||
</div> | ||
</div> | ||
); | ||
} |
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