Skip to content

Commit

Permalink
feat(app): unlink account dialog (#339)
Browse files Browse the repository at this point in the history
* feat(app): unlink account dialog
closes #315

* feat(app): unlink acc dialog no state

* fix(app): send() response

---------

Co-authored-by: Vojtěch Vidra <vojtechvidra@gmail.com>
  • Loading branch information
pesickadavid and VojtechVidra committed Jun 7, 2024
1 parent 6f76118 commit d420383
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 14 deletions.
56 changes: 44 additions & 12 deletions apps/app/src/app/(dashboard)/user/settings/connected-account.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
"use client";
import { css } from "@flows/styled-system/css";
import type { User, UserIdentity } from "@supabase/supabase-js";
import { mutate } from "hooks/use-fetch";
import { useSend } from "hooks/use-send";
import { GitHub16, Google16, Mail16 } from "icons";
import { api } from "lib/api";
import { useRouter } from "next/navigation";
import { t } from "translations";
import { Button, Text, Tooltip } from "ui";
import {
Button,
Dialog,
DialogActions,
DialogClose,
DialogContent,
DialogTitle,
Text,
Tooltip,
} from "ui";

import { PasswordChangeDialog } from "./password-change-dialog";

Expand All @@ -26,13 +36,16 @@ export const ConnectedAccount = ({
user,
hasPassword,
}: ConnectedAccountProps): JSX.Element => {
const { send, loading, error } = useSend();
const { send, loading } = useSend();
const router = useRouter();
const handleUnlink = async (): Promise<void> => {
await send(api["DELETE /me/identities/:providerId"](identity.id), {
const res = await send(api["DELETE /me/identities/:providerId"](identity.id), {
errorMessage: t.toasts.accountUnlinkFailed,
});
if (!error) router.refresh();
if (!res.error) {
void mutate("/organizations", []);
router.refresh();
}
};

const isDisabled = !hasPassword && user.identities?.length === 1;
Expand Down Expand Up @@ -67,15 +80,34 @@ export const ConnectedAccount = ({
text={isDisabled ? t.personal.connectedAccounts.lastProvider : ""}
trigger={
<div>
<Button
disabled={isDisabled}
loading={loading}
onClick={handleUnlink}
size="small"
variant="danger"
<Dialog
trigger={
<Button disabled={isDisabled} loading={loading} size="small" variant="danger">
{t.actions.unlink}
</Button>
}
>
{t.actions.unlink}
</Button>
<DialogTitle>{t.personal.connectedAccounts.unlinkDialog.title}</DialogTitle>
<DialogContent>
<Text mb="space24">{t.personal.connectedAccounts.unlinkDialog.description}</Text>
</DialogContent>
<DialogActions>
<DialogClose asChild>
<Button shadow="none" size="small" variant="secondary">
{t.actions.close}
</Button>
</DialogClose>
<Button
type="button"
size="small"
loading={loading}
variant="primary"
onClick={handleUnlink}
>
{t.personal.connectedAccounts.unlinkDialog.confirm}
</Button>
</DialogActions>
</Dialog>
</div>
}
/>
Expand Down
5 changes: 5 additions & 0 deletions apps/app/src/translations/translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,11 @@ export const t = {
"This is the only way for you to login. To unlink it delete your account or create a password.",
changePassword: "Change password",
createPassword: "Create password",
unlinkDialog: {
title: "Unlink account",
description: "Are you sure you want to unlink this account?",
confirm: "Unlink account",
},
},
deleteAccount: {
title: "Delete account",
Expand Down
4 changes: 2 additions & 2 deletions apps/backend/src/users/users.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Body, Controller, Delete, Get, Post } from "@nestjs/common";
import { Body, Controller, Delete, Get, Param, Post } from "@nestjs/common";
import { ApiBearerAuth, ApiTags } from "@nestjs/swagger";

import { type Auth, Authorization } from "../auth";
Expand Down Expand Up @@ -47,7 +47,7 @@ export class UsersController {
@Delete("me/identities/:providerId")
deleteIdentity(
@Authorization() auth: Auth,
@UUIDParam("providerId") providerId: string,
@Param("providerId") providerId: string,
): Promise<void> {
return this.usersService.deleteIdentity({ auth, providerId });
}
Expand Down

0 comments on commit d420383

Please sign in to comment.