able to change shortener project

pull/3/head
TZGyn 2 years ago
parent f06d78b4a8
commit d5583d0d6d
Signed by: TZGyn
GPG Key ID: 122EAF77AE81FD4A

@ -7,10 +7,13 @@ export const load = (async (event) => {
const shorteners = await db.query.shortener.findMany({ const shorteners = await db.query.shortener.findMany({
with: { with: {
visitor: true, visitor: true,
project: true,
}, },
where: (shortener, { eq, and, isNull }) => where: (shortener, { eq, and, isNull }) =>
and(eq(shortener.userId, user.id), isNull(shortener.projectId)), and(eq(shortener.userId, user.id), isNull(shortener.projectId)),
}) })
return { shorteners } const projects = await db.query.project.findMany()
return { shorteners, projects }
}) satisfies PageServerLoad }) satisfies PageServerLoad

@ -5,6 +5,7 @@
import * as Dialog from '$lib/components/ui/dialog' import * as Dialog from '$lib/components/ui/dialog'
import * as Card from '$lib/components/ui/card' import * as Card from '$lib/components/ui/card'
import * as DropdownMenu from '$lib/components/ui/dropdown-menu' import * as DropdownMenu from '$lib/components/ui/dropdown-menu'
import * as Select from '$lib/components/ui/select'
import { Input } from '$lib/components/ui/input' import { Input } from '$lib/components/ui/input'
import { Label } from '$lib/components/ui/label' import { Label } from '$lib/components/ui/label'
import { import {
@ -43,12 +44,14 @@
let editDialogOpen = false let editDialogOpen = false
let editShortenerCode = '' let editShortenerCode = ''
let editShortenerLink = '' let editShortenerLink = ''
let editShortenerCategory: any = undefined
let isEditLoading = false let isEditLoading = false
const openEditDialog = (code: string, link: string) => { const openEditDialog = (code: string, link: string) => {
editDialogOpen = true editDialogOpen = true
editShortenerCode = code editShortenerCode = code
editShortenerLink = link editShortenerLink = link
editShortenerCategory = undefined
} }
const editShortener = async (code: string, link: string) => { const editShortener = async (code: string, link: string) => {
@ -57,6 +60,9 @@
method: 'put', method: 'put',
body: JSON.stringify({ body: JSON.stringify({
link, link,
projectId: editShortenerCategory
? editShortenerCategory.value
: undefined,
}), }),
}) })
await invalidateAll() await invalidateAll()
@ -184,6 +190,22 @@
bind:value={editShortenerLink} bind:value={editShortenerLink}
class="col-span-3" /> class="col-span-3" />
</div> </div>
<div class="grid grid-cols-4 items-center gap-4">
<Label class="text-right">Category</Label>
<Select.Root
bind:selected={editShortenerCategory}
multiple={false}>
<Select.Trigger class="col-span-3">
<Select.Value placeholder="Select a Category" />
</Select.Trigger>
<Select.Content>
{#each data.projects as project}
<Select.Item value={project.id}
>{project.name}</Select.Item>
{/each}
</Select.Content>
</Select.Root>
</div>
</div> </div>
<Dialog.Footer> <Dialog.Footer>
<Button <Button

@ -27,7 +27,9 @@ export const load = (async (event) => {
), ),
}) })
return { project, shorteners } const projects = await db.query.project.findMany()
return { project, shorteners, projects }
} catch (error) { } catch (error) {
throw redirect(303, '/projects') throw redirect(303, '/projects')
} }

@ -5,6 +5,7 @@
import * as Dialog from '$lib/components/ui/dialog' import * as Dialog from '$lib/components/ui/dialog'
import * as Card from '$lib/components/ui/card' import * as Card from '$lib/components/ui/card'
import * as DropdownMenu from '$lib/components/ui/dropdown-menu' import * as DropdownMenu from '$lib/components/ui/dropdown-menu'
import * as Select from '$lib/components/ui/select'
import { Input } from '$lib/components/ui/input' import { Input } from '$lib/components/ui/input'
import { Label } from '$lib/components/ui/label' import { Label } from '$lib/components/ui/label'
import { import {
@ -43,12 +44,14 @@
let editDialogOpen = false let editDialogOpen = false
let editShortenerCode = '' let editShortenerCode = ''
let editShortenerLink = '' let editShortenerLink = ''
let editShortenerCategory: any = undefined
let isEditLoading = false let isEditLoading = false
const openEditDialog = (code: string, link: string) => { const openEditDialog = (code: string, link: string) => {
editDialogOpen = true editDialogOpen = true
editShortenerCode = code editShortenerCode = code
editShortenerLink = link editShortenerLink = link
editShortenerCategory = undefined
} }
const editShortener = async (code: string, link: string) => { const editShortener = async (code: string, link: string) => {
@ -57,6 +60,9 @@
method: 'put', method: 'put',
body: JSON.stringify({ body: JSON.stringify({
link, link,
projectId: editShortenerCategory
? editShortenerCategory.value
: undefined,
}), }),
}) })
await invalidateAll() await invalidateAll()
@ -184,6 +190,22 @@
bind:value={editShortenerLink} bind:value={editShortenerLink}
class="col-span-3" /> class="col-span-3" />
</div> </div>
<div class="grid grid-cols-4 items-center gap-4">
<Label class="text-right">Category</Label>
<Select.Root
bind:selected={editShortenerCategory}
multiple={false}>
<Select.Trigger class="col-span-3">
<Select.Value placeholder="Select a Category" />
</Select.Trigger>
<Select.Content>
{#each data.projects as project}
<Select.Item value={project.id}
>{project.name}</Select.Item>
{/each}
</Select.Content>
</Select.Root>
</div>
</div> </div>
<Dialog.Footer> <Dialog.Footer>
<Button <Button

@ -31,6 +31,7 @@ export const GET: RequestHandler = async (event) => {
const updateShortenerSchema = z.object({ const updateShortenerSchema = z.object({
link: z.string().url(), link: z.string().url(),
projectId: z.number().nullable(),
}) })
export const PUT: RequestHandler = async (event) => { export const PUT: RequestHandler = async (event) => {
@ -52,7 +53,10 @@ export const PUT: RequestHandler = async (event) => {
await db await db
.update(shortenerSchema) .update(shortenerSchema)
.set({ link: updateShortener.data.link }) .set({
link: updateShortener.data.link,
projectId: updateShortener.data.projectId ?? undefined,
})
.where( .where(
and( and(
eq(shortenerSchema.code, shortenerId), eq(shortenerSchema.code, shortenerId),

Loading…
Cancel
Save