Skip to content

Commit

Permalink
feat: check item exists on request
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaisberg authored and Gaisberg committed Oct 22, 2024
1 parent 87fa3b0 commit d56cf6c
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 39 deletions.
26 changes: 22 additions & 4 deletions src/lib/components/home-items.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
import { Star, CalendarDays, Clapperboard, MoveUpRight } from 'lucide-svelte';
import ItemRequest from './item-request.svelte';
import { roundOff } from '$lib/helpers';
import { writable } from 'svelte/store';
import { goto } from '$app/navigation';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export let data: any;
export let name: string;
export let type: string;
const hoveredItem = writable(null);
</script>

<div class="flex h-full w-full flex-col p-8 md:px-24 lg:px-32">
Expand All @@ -28,6 +32,19 @@
{#each data.results as item}
<div
class="group relative mb-2 flex w-1/2 flex-shrink-0 flex-col gap-2 rounded-lg p-2 sm:w-1/4 lg:w-1/6 xl:p-[.4rem]"
role="button"
tabindex="0"
on:mouseenter={() => ($hoveredItem = item.id)}
on:mouseleave={() => ($hoveredItem = null)}
on:focus={() => ($hoveredItem = item.id)}
on:blur={() => ($hoveredItem = null)}
on:keydown={(e) => {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
$hoveredItem = item.id;
}
}}
on:click={() => goto(`/${type}/${item.id}`)}
>
<div class="relative aspect-[1/1.5] w-full overflow-hidden rounded-lg">
<img
Expand All @@ -44,12 +61,13 @@
{roundOff(item.vote_average)}
</span>
</div>
<a
href="/{type}/{item.id}"
<div
class="absolute inset-0 hidden flex-col justify-end from-zinc-900/70 p-2 group-hover:flex group-hover:bg-gradient-to-t"
>
<ItemRequest data={item} {type} />
</a>
{#if $hoveredItem === item.id}
<ItemRequest data={item} {type} />
{/if}
</div>
</div>
</div>
{/each}
Expand Down
86 changes: 60 additions & 26 deletions src/lib/components/item-request.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,34 @@
import { toast } from 'svelte-sonner';
import { getExternalID } from '$lib/tmdb';
import { ItemsService } from '$lib/client';
import { onMount } from 'svelte';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export let data: any;
export let type: string;
let isInLibrary = false;
let isChecking = true;
async function checkItemExists(id: number) {
try {
const externalIds = await getExternalID(fetch, type, id);
const response = await ItemsService.getItems({
query: {
search: externalIds.imdb_id
}
});
if (response.data && response.data.items.length > 0) {
isInLibrary = true;
}
} catch {
//pass
} finally {
isChecking = false;
}
}
async function requestItem(id: number) {
const externalIds = await getExternalID(fetch, type, id);
const response = await ItemsService.addItems({
Expand All @@ -19,36 +42,47 @@
if (!response.error) {
toast.success('Media requested successfully');
isInLibrary = true;
} else {
toast.error('An error occurred while requesting the media');
}
}
onMount(() => {
checkItemExists(data.id);
});
</script>

<AlertDialog.Root>
<AlertDialog.Trigger asChild let:builder>
<Button
on:click={(e) => {
e.preventDefault();
e.stopPropagation();
}}
variant="outline"
builders={[builder]}>Request</Button
>
</AlertDialog.Trigger>
<AlertDialog.Content>
<AlertDialog.Header>
<AlertDialog.Title
>Requesting {data.title || data.name || data.original_name}</AlertDialog.Title
>
</AlertDialog.Header>
<AlertDialog.Footer>
<AlertDialog.Cancel>Cancel</AlertDialog.Cancel>
<AlertDialog.Action
on:click={async () => {
await requestItem(data.id);
}}>Continue</AlertDialog.Action
{#if isChecking}
<Button variant="outline" disabled>Checking...</Button>
{:else if isInLibrary}
<Button variant="outline" disabled>In Library</Button>
{:else}
<AlertDialog.Root>
<AlertDialog.Trigger asChild let:builder>
<Button
on:click={(e) => {
e.preventDefault();
e.stopPropagation();
}}
variant="outline"
builders={[builder]}>Request</Button
>
</AlertDialog.Footer>
</AlertDialog.Content>
</AlertDialog.Root>
</AlertDialog.Trigger>
<AlertDialog.Content>
<AlertDialog.Header>
<AlertDialog.Title
>Requesting {data.title || data.name || data.original_name}</AlertDialog.Title
>
</AlertDialog.Header>
<AlertDialog.Footer>
<AlertDialog.Cancel>Cancel</AlertDialog.Cancel>
<AlertDialog.Action
on:click={async () => {
await requestItem(data.id);
}}>Continue</AlertDialog.Action
>
</AlertDialog.Footer>
</AlertDialog.Content>
</AlertDialog.Root>
{/if}
11 changes: 2 additions & 9 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { Button } from '$lib/components/ui/button';
import { roundOff } from '$lib/helpers';
import HomeItems from '$lib/components/home-items.svelte';
import ItemRequest from '$lib/components/item-request.svelte';
export let data: PageData;
</script>

Expand Down Expand Up @@ -62,15 +63,7 @@
<p class="line-clamp-2 text-base">{nowPlaying.overview}</p>
</div>
<div class="mt-2 flex gap-2">
<Button
size="lg"
variant="default"
class="flex items-center gap-2"
href="/movie/{nowPlaying.id}"
>
<Play class="h-4 w-4" />
<span>Request</span>
</Button>
<ItemRequest data={nowPlaying} type={'movie'} />

Check failure on line 66 in src/routes/+page.svelte

View workflow job for this annotation

GitHub Actions / Lint

Unexpected mustache interpolation with a string literal value
<Button
size="lg"
variant="ghost"
Expand Down

0 comments on commit d56cf6c

Please sign in to comment.