summaryrefslogtreecommitdiffstatshomepage
path: root/frontend/src/lib/scraper/ComicScrapeForm.svelte
blob: 6cc345132992b3095788e28c887d99d4d986b435 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<script lang="ts">
	import { upsertComics } from '$gql/Mutations';
	import { comicScrapersQuery, scrapeComic } from '$gql/Queries';
	import { isError } from '$gql/Utils';
	import { OnMissing, type FullComicFragment, type ScrapeComicQuery } from '$gql/graphql';
	import { toastError, toastFinally } from '$lib/Toasts';
	import Select from '$lib/components/Select.svelte';
	import Spinner from '$lib/components/Spinner.svelte';
	import { getContextClient, type OperationResult } from '@urql/svelte';
	import { getScraperContext, ScrapedComicSelector } from './Scraper.svelte';
	import SelectorGroup from './components/SelectorGroup.svelte';
	import SelectorItem from './components/SelectorItem.svelte';

	let client = getContextClient();
	const context = getScraperContext();

	interface Props {
		comic: FullComicFragment;
		onupsert: () => void;
	}

	let { comic, onupsert }: Props = $props();
	let createMissing = $state(false);
	let loading = $state(false);

	let scrapersResult = $derived(comicScrapersQuery(client, { id: comic.id }));
	let scrapers = $derived($scrapersResult.data?.comicScrapers);

	function scrape(event: SubmitEvent) {
		event.preventDefault();
		if (!context.scraper) return;

		loading = true;
		scrapeComic(client, { id: comic.id, scraper: context.scraper })
			.then(handleScrapeResult)
			.catch(toastFinally)
			.finally(() => (loading = false));
	}

	function handleScrapeResult(result: OperationResult<ScrapeComicQuery>) {
		if (result.error) {
			toastError(result.error.message);
			return;
		}

		if (result.data) {
			if (isError(result.data.scrapeComic)) {
				toastError(result.data.scrapeComic.message);
				return;
			}

			if (result.data.scrapeComic.__typename === 'ScrapeComicResult') {
				context.selector = new ScrapedComicSelector(result.data.scrapeComic.data, comic);
				context.warnings = result.data.scrapeComic.warnings;
			}
		}
	}

	function upsert(event: SubmitEvent) {
		event.preventDefault();
		if (!context.selector) return;

		const input = context.selector.input(createMissing ? OnMissing.Create : OnMissing.Ignore);
		upsertComics(client, { ids: comic.id, input })
			.then(() => {
				onupsert();
				context.reset();
			})
			.catch(toastFinally);
	}
</script>

<div class="flex flex-col gap-4 text-sm">
	{#if scrapers && scrapers.length === 0}
		<h2 class="text-base">No scrapers available.</h2>
	{:else}
		<form onsubmit={scrape}>
			<div class="grid grid-cols-6 gap-2">
				<div class="col-span-5">
					<Select
						id="scrapers"
						options={scrapers}
						placeholder={'Select scraper...'}
						bind:value={context.scraper}
					/>
				</div>
				<button type="submit" disabled={!context.scraper} class="btn-blue">Scrape</button>
			</div>
		</form>
	{/if}

	{#if loading}
		<Spinner />
	{:else if context.selector}
		{#if context.warnings.length > 0}
			<div class="flex flex-col gap-2">
				<h2 class="flex gap-1 border-b border-slate-700 text-base font-medium">Warnings</h2>
				<ul class="ml-2 list-inside list-disc">
					{#each context.warnings as warning}
						<li>{warning}</li>
					{/each}
				</ul>
			</div>
		{/if}
		{#if !context.selector.pending()}
			<h2 class="text-base">No data to merge.</h2>
		{:else}
			<div class="flex flex-col gap-2">
				<h2 class="border-b border-slate-700 text-base font-medium">Results</h2>
				<form onsubmit={upsert}>
					<div class="grid grid-cols-6 gap-4 pb-2">
						<SelectorItem title="Title" selector={context.selector.title} />
						<SelectorItem title="Original Title" selector={context.selector.originalTitle} />
						<SelectorItem title="URL" selector={context.selector.url} />
						<SelectorItem title="Date" selector={context.selector.date} --span="2" />
						<SelectorItem title="Category" selector={context.selector.category} --span="2" />
						<SelectorItem title="Language" selector={context.selector.language} --span="2" />
						<SelectorItem title="Rating" selector={context.selector.rating} --span="2" />
						<SelectorItem title="Censorship" selector={context.selector.censorship} --span="2" />
						<SelectorItem title="Direction" selector={context.selector.direction} --span="2" />
						<SelectorItem title="Layout" selector={context.selector.layout} --span="2" />
						<SelectorGroup title="Artists" selectors={context.selector.artists} />
						<SelectorGroup title="Circles" selectors={context.selector.circles} />
						<SelectorGroup title="Characters" selectors={context.selector.characters} />
						<SelectorGroup title="Worlds" selectors={context.selector.worlds} />
						<SelectorGroup title="Tags" selectors={context.selector.tags} />
					</div>
					<div class="flex flex-col gap-2">
						<h2 class="border-b border-slate-700 text-base font-medium">Options</h2>
						<div class="flex items-center gap-1">
							<input
								class="h-4 w-4"
								type="checkbox"
								id="create-missing"
								bind:checked={createMissing}
							/>
							<label class="shrink-0" for="create-missing">Create missing items</label>
						</div>
					</div>
					<div class="flex gap-4">
						<button type="submit" class="btn-blue">Merge</button>
					</div>
				</form>
			</div>
		{/if}
	{/if}
</div>