summaryrefslogtreecommitdiffstatshomepage
path: root/frontend/src/lib/Form.ts
blob: ab0f4f785a7412bbd2e23e20e9c51426cd86e9ef (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
import type { FullComicFragment, FullTag, Namespace } from '$gql/graphql';
import type { OmitIdentifiers } from '$gql/Utils';
import equal from 'fast-deep-equal';
import type { Snippet } from 'svelte';

export interface FormProps<I, P> {
	initial: OmitIdentifiers<I>;
	submit: (input: P) => void;
	children?: Snippet;
}

interface Item {
	id: number | string;
	name: string;
}

function stringPending(a?: string | null, b?: string | null) {
	if (a?.length === 0) {
		a = null;
	}

	if (b?.length === 0) {
		b = null;
	}

	return a !== b;
}

function associationPending(as: Item[], bs: Item[]) {
	return !equal(
		as.map((a) => a.id),
		bs.map((b) => b.id)
	);
}

export function itemPending(initial: OmitIdentifiers<Item>, current: OmitIdentifiers<Item>) {
	return stringPending(initial.name, current.name);
}

export function namespacePending(
	initial: OmitIdentifiers<Namespace>,
	current: OmitIdentifiers<Namespace>
) {
	return itemPending(initial, current) || stringPending(initial.sortName, current.sortName);
}

export function tagPending(a: OmitIdentifiers<FullTag>, b: OmitIdentifiers<FullTag>) {
	return (
		itemPending(a, b) ||
		stringPending(a.description, b.description) ||
		associationPending(a.namespaces, b.namespaces)
	);
}

export function comicPending(a?: FullComicFragment, b?: OmitIdentifiers<FullComicFragment>) {
	if (a === undefined) return b === undefined;
	if (b === undefined) return a === undefined;

	return (
		stringPending(a.title, b.title) ||
		stringPending(a.originalTitle, b.originalTitle) ||
		stringPending(a.url, b.url) ||
		stringPending(a.date, b.date) ||
		a.category !== b.category ||
		a.rating !== b.rating ||
		a.censorship !== b.censorship ||
		a.language !== b.language ||
		a.direction !== b.direction ||
		a.layout !== b.layout ||
		associationPending(a.artists, b.artists) ||
		associationPending(a.circles, b.circles) ||
		associationPending(a.characters, b.characters) ||
		associationPending(a.tags, b.tags) ||
		associationPending(a.worlds, b.worlds)
	);
}