From d1d654ebac2d51e3841675faeb56480e440f622f Mon Sep 17 00:00:00 2001 From: Wolfgang Müller Date: Tue, 5 Mar 2024 18:08:09 +0100 Subject: Initial commit --- frontend/src/lib/Update.ts | 97 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 frontend/src/lib/Update.ts (limited to 'frontend/src/lib/Update.ts') diff --git a/frontend/src/lib/Update.ts b/frontend/src/lib/Update.ts new file mode 100644 index 0000000..507dd52 --- /dev/null +++ b/frontend/src/lib/Update.ts @@ -0,0 +1,97 @@ +import { + UpdateMode, + type UpdateComicInput, + type UpdateOptions, + type UpdateTagInput +} from '$gql/graphql'; + +type Key = string | number | symbol; + +interface AssociationUpdate { + ids?: number[] | string[] | null; + options?: UpdateOptions | null; +} + +type Input = { + [Property in K]?: T | null; +}; + +abstract class Entry { + key: K; + + constructor(key: K) { + this.key = key; + } + + abstract integrate(input: Input): void; + abstract hasInput(): boolean; +} + +class Association extends Entry { + ids = []; + options = { + mode: UpdateMode.Add + }; + + constructor(key: K) { + super(key); + } + + integrate(input: Input) { + if (this.hasInput()) { + input[this.key] = { ids: this.ids, options: this.options }; + } + } + + hasInput() { + return this.ids.length > 0; + } +} + +class Enum extends Entry { + value?: string = undefined; + + constructor(key: K) { + super(key); + } + + integrate(input: Input): void { + if (this.hasInput()) { + input[this.key] = this.value; + } + } + + hasInput() { + return this.value !== undefined && this.value !== null; + } +} + +abstract class Controls { + toInput() { + const input = {} as I; + Object.values(this).forEach((v: Entry) => v.integrate(input)); + return input; + } + + hasInput() { + return Object.values(this).some((i: Entry) => i.hasInput()); + } +} + +export class UpdateTagsControls extends Controls { + namespaces = new Association('namespaces'); +} + +export class UpdateComicsControls extends Controls { + artists = new Association('artists'); + category = new Enum('category'); + censorship = new Enum('censorship'); + direction = new Enum('direction'); + layout = new Enum('layout'); + characters = new Association('characters'); + circles = new Association('circles'); + language = new Enum('language'); + rating = new Enum('rating'); + tags = new Association('tags'); + worlds = new Association('worlds'); +} -- cgit v1.2.3-2-gb3c3