From: Muhammad Rizki <[email protected]>
To: Ammar Faizi <[email protected]>
Cc: Muhammad Rizki <[email protected]>,
Alviro Iskandar Setiawan <[email protected]>,
GNU/Weeb Mailing List <[email protected]>
Subject: [PATCH v2 07/12] fix(profile-avatar): add delete avatar method
Date: Sun, 9 Mar 2025 02:26:49 +0700 [thread overview]
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>
Added delete avatar method to remove user's avatar from the server.
Signed-off-by: Muhammad Rizki <[email protected]>
---
.../(protected)/settings/profile/+page.svelte | 73 +++++++++++++++----
1 file changed, 57 insertions(+), 16 deletions(-)
diff --git a/src/routes/(protected)/settings/profile/+page.svelte b/src/routes/(protected)/settings/profile/+page.svelte
index 664894e..43ad382 100644
--- a/src/routes/(protected)/settings/profile/+page.svelte
+++ b/src/routes/(protected)/settings/profile/+page.svelte
@@ -24,7 +24,6 @@
import Seo from "$components/customs/seo.svelte";
let { data } = $props();
- let showModalConfirmation = $state(false);
const form = superForm(data.form, {
SPA: true,
@@ -101,6 +100,9 @@
const auth = useAuth();
let avatarImg = $state(auth.user?.photo);
+ let openEditAvatar = $state(false);
+ let showModalConfirmation = $state(false);
+
const avatar = $derived(avatarImg);
const getShortName = () => {
@@ -132,6 +134,34 @@
handleOpenModal(false);
};
+ const deleteAvatar = async () => {
+ openEditAvatar = false;
+
+ if (!auth.user?.photo) {
+ // delete draft avatar
+ avatarImg = "";
+ $formData.photo = null;
+ return;
+ }
+
+ const {
+ data: { res },
+ status
+ } = await http<typing.ResponseAPI<{}>>({
+ params: { action: "delete_user_photo" },
+ method: "GET"
+ });
+
+ if (status === 200) {
+ avatarImg = "";
+ $formData.photo = null;
+
+ toast.info("Success delete profile picture", {
+ description: res?.msg
+ });
+ }
+ };
+
const isSubmittable = $derived(
Boolean($formData.full_name && $formData.ext_email && $formData.gender)
);
@@ -162,7 +192,7 @@
{/if}
</Avatar.Fallback>
</Avatar.Root>
- <Popover.Root>
+ <Popover.Root open={openEditAvatar} onOpenChange={(e) => (openEditAvatar = e)}>
<Popover.Trigger
class="absolute bottom-3 left-0 flex h-max w-max items-center gap-x-1 rounded-lg border border-input bg-background px-2 py-1 hover:bg-accent hover:text-accent-foreground"
>
@@ -174,16 +204,20 @@
<Form.Label
for="photo"
class="w-full cursor-pointer rounded-md px-2 py-1.5 text-start text-xs"
+ onclick={() => (openEditAvatar = false)}
>
Upload...
</Form.Label>
</Button>
- <Button
- variant="ghost"
- class="flex h-max w-full justify-start px-2 py-1.5 text-xs"
- >
- Delete
- </Button>
+ {#if avatar || $formData.photo}
+ <Button
+ onclick={deleteAvatar}
+ variant="ghost"
+ class="flex h-max w-full justify-start px-2 py-1.5 text-xs text-destructive hover:text-destructive"
+ >
+ Delete
+ </Button>
+ {/if}
</Popover.Content>
</Popover.Root>
</Form.Label>
@@ -380,7 +414,7 @@
{/if}
</Avatar.Fallback>
</Avatar.Root>
- <Popover.Root>
+ <Popover.Root open={openEditAvatar} onOpenChange={(e) => (openEditAvatar = e)}>
<Popover.Trigger
class="absolute bottom-3 left-0 flex h-max w-max items-center gap-x-1 rounded-lg border border-input bg-background px-2 py-1 hover:bg-accent hover:text-accent-foreground xl:left-2.5"
>
@@ -388,7 +422,11 @@
<span class="text-xs font-medium">Edit</span>
</Popover.Trigger>
<Popover.Content class="flex max-w-[8rem] flex-col gap-y-1 p-1 text-sm">
- <Button variant="ghost" class="h-max w-full px-0 py-0">
+ <Button
+ variant="ghost"
+ class="h-max w-full px-0 py-0"
+ onclick={() => (openEditAvatar = false)}
+ >
<Form.Label
for="photo"
class="w-full cursor-pointer rounded-md px-2 py-1.5 text-start text-xs"
@@ -396,12 +434,15 @@
Upload...
</Form.Label>
</Button>
- <Button
- variant="ghost"
- class="flex h-max w-full justify-start px-2 py-1.5 text-xs"
- >
- Delete
- </Button>
+ {#if avatar || $formData.photo}
+ <Button
+ onclick={deleteAvatar}
+ variant="ghost"
+ class="flex h-max w-full justify-start px-2 py-1.5 text-xs text-destructive hover:text-destructive"
+ >
+ Delete
+ </Button>
+ {/if}
</Popover.Content>
</Popover.Root>
</Form.Label>
--
Muhammad Rizki
next prev parent reply other threads:[~2025-03-08 19:27 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-08 19:26 [PATCH v2 00/12] Fix Auth Guard, Move SEO Muhammad Rizki
2025-03-08 19:26 ` [PATCH v2 01/12] fix(svelte): use relative false Muhammad Rizki
2025-03-08 19:26 ` [PATCH v2 02/12] fix(avatar): change avatarImg state to use from auth.user.photo state Muhammad Rizki
2025-03-08 19:26 ` [PATCH v2 03/12] chore(profile): add toUpperCase() on getShortName() Muhammad Rizki
2025-03-08 19:26 ` [PATCH v2 04/12] fix(profile): make social fields default to empty string Muhammad Rizki
2025-03-08 19:26 ` [PATCH v2 05/12] chore(toaster): change toast message position and use richColors Muhammad Rizki
2025-03-08 19:26 ` [PATCH v2 06/12] chore(profile): reset password value on success Muhammad Rizki
2025-03-08 19:26 ` Muhammad Rizki [this message]
2025-03-08 19:26 ` [PATCH v2 08/12] chore(profile): add space for password confirmation form Muhammad Rizki
2025-03-08 19:26 ` [PATCH v2 09/12] feat(ui): add dropdown-menu and update bits-ui version Muhammad Rizki
2025-03-08 19:26 ` [PATCH v2 10/12] chore(sidebar-menu): change sidebar menu look Muhammad Rizki
2025-03-08 19:26 ` [PATCH v2 11/12] chore(seo): move seo from layout to /home page Muhammad Rizki
2025-03-08 19:26 ` [PATCH v2 12/12] fix(auth): fix auth guard when credentials is invalid Muhammad Rizki
2025-03-08 19:38 ` [PATCH v2 00/12] Fix Auth Guard, Move SEO Ammar Faizi
2025-03-08 19:51 ` Alviro Iskandar Setiawan
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
[email protected] \
[email protected] \
[email protected] \
[email protected] \
[email protected] \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox