feat: add delete button

This commit is contained in:
May 2024-11-20 07:38:36 -08:00
parent 269db33bec
commit 88ec069c55
Signed by: split
GPG key ID: C325C61F0BF517C0
5 changed files with 60 additions and 30 deletions

View file

@ -298,3 +298,10 @@ export async function createNewAvatar(
return time return time
} }
export function deleteAvatar(id: string) {
const targetAvatarDirectory = join(avatarDirectory, id)
return prisma.avatar
.delete({ where: { id } })
.then(_ => rm(targetAvatarDirectory, { recursive: true, force: true }))
}

View file

@ -12,29 +12,15 @@
} }
.error { .error {
--color: #d20f39; --color: var(--red);
} }
.warn { .warn {
--color: #df8e1d; --color: var(--yellow);
} }
.success { .success {
--color: #40a02b; --color: var(--green);
}
@media (prefers-color-scheme: dark) {
.error {
--color: #f38ba8;
}
.warn {
--color: #f9e2af;
}
.success {
--color: #a6e3a1;
}
} }
</style> </style>
<div class={status}> <div class={status}>

View file

@ -21,15 +21,23 @@
--link: #333; --link: #333;
--background: white; --background: white;
--crust: #eee; --crust: #eee;
--red: #d20f39;
--yellow: #df8e1d;
--green: #40a02b;
} }
@media (prefers-color-scheme:dark) { @media (prefers-color-scheme:dark) {
:root { :root {
--text: white; --text: white;
--link: #aaa; --link: #aaa;
--background: #111; --background: #111;
--crust: #333; --crust: #333;
--red: #f38ba8;
--yellow: #f9e2af;
--green: #a6e3a1
} }
} }
html { html {
background: var(--background); background: var(--background);
} }

View file

@ -1,9 +1,10 @@
import { getRequestUser, launchLogin } from "$lib/oidc" import { getRequestUser, launchLogin } from "$lib/oidc"
import configuration from "$lib/configuration.js" import configuration from "$lib/configuration.js"
import { fail, redirect } from "@sveltejs/kit" import { error, fail, redirect } from "@sveltejs/kit"
import { import {
avatarDirectory, avatarDirectory,
createNewAvatar, createNewAvatar,
deleteAvatar,
getMetadataForUserId, getMetadataForUserId,
} from "$lib/avatars.js" } from "$lib/avatars.js"
import { join } from "path" import { join } from "path"
@ -12,9 +13,16 @@ export async function load({ request, parent, url, params: { id } }) {
const { user } = await parent() const { user } = await parent()
if (!user) return launchLogin(url.toString()) if (!user) return launchLogin(url.toString())
let avatar = await prisma.avatar.findUnique({
where: { id, userId: user.sub },
})
if (!avatar)
throw error(404, "Avatar is not owned by you or does not exist")
return { return {
url: url.toString(), url: url.toString(),
avatar: await prisma.avatar.findUnique({ where: { id } }), avatar,
} }
} }
@ -23,10 +31,20 @@ export const actions = {
let user = await getRequestUser(request, cookies) let user = await getRequestUser(request, cookies)
if (!user) return fail(401, { error: "unauthenticated" }) if (!user) return fail(401, { error: "unauthenticated" })
let { altText, source } = Object.fromEntries( if (
!(await prisma.avatar.findUnique({
where: { id, userId: user.sub },
}))
)
return fail(404, {
error: "Avatar is not owned by you or does not exist",
})
let { action, altText, source } = Object.fromEntries(
(await request.formData()).entries() (await request.formData()).entries()
) )
if (action == "Save") {
await prisma.avatar.update({ await prisma.avatar.update({
where: { where: {
id, id,
@ -38,5 +56,9 @@ export const actions = {
}) })
return redirect(302, "/set") return redirect(302, "/set")
} else if (action == "Delete") {
await deleteAvatar(id)
return redirect(302, "/set")
}
}, },
} }

View file

@ -58,6 +58,12 @@
form textarea:disabled { form textarea:disabled {
color: var(--link); color: var(--link);
} }
form input[type="submit"][value="Delete"] {
border: 1px solid var(--red);
background-color: color-mix(in srgb, var(--red) 20%, var(--background) 80%);
color: var(--red);
margin-right: auto;
}
</style> </style>
<ReversibleHeading to="/set"> <ReversibleHeading to="/set">
@ -82,6 +88,7 @@
<textarea name="source" placeholder="Provide a source for your image">{data.avatar.source}</textarea> <textarea name="source" placeholder="Provide a source for your image">{data.avatar.source}</textarea>
</div> </div>
<div class="buttons"> <div class="buttons">
<input type="submit" name="action" value="Delete">
<input type="submit" name="action" value="Save"> <input type="submit" name="action" value="Save">
</div> </div>
</form> </form>