Skip to content

Commit

Permalink
Füge Link-Synchronisierungsfunktion hinzu in +page.svelte
Browse files Browse the repository at this point in the history
  • Loading branch information
jdoppelhofer committed Apr 11, 2024
1 parent b1d8706 commit 5bb19c5
Showing 1 changed file with 66 additions and 61 deletions.
127 changes: 66 additions & 61 deletions app/src/routes/settings/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,75 +1,80 @@
<script lang="ts">
import { db } from '$lib/db';
import { Input, Button, P, Heading, Label } from 'flowbite-svelte';
let link: string = '';
let request_success = false;
let replicationError: string | null = null;
import { Input, Button, P, Heading, Label } from 'flowbite-svelte';

Check failure on line 3 in app/src/routes/settings/+page.svelte

View workflow job for this annotation

GitHub Actions / test

'P' is defined but never used
let link: string = '';
let request_success = false;

Check failure on line 5 in app/src/routes/settings/+page.svelte

View workflow job for this annotation

GitHub Actions / test

'request_success' is assigned a value but never used
let replicationError: string | null = null;

Check failure on line 6 in app/src/routes/settings/+page.svelte

View workflow job for this annotation

GitHub Actions / test

'replicationError' is assigned a value but never used
let storedLink = localStorage.getItem('syncLink');
function validateLink(value: string): boolean {
value = value.trim();
const pattern: RegExp = /^https?:\/\/.+/;
return pattern.test(value);
}
$: isValidLink = link !== '' && validateLink(link);
function validateLink(value: string): boolean {
value = value.trim();
const pattern: RegExp = /^https?:\/\/.+/;
return pattern.test(value);
}
$: isValidLink = link !== '' && validateLink(link);
// Handler für Sync-Ereignisse
function onSyncChange(change: any) {
console.log('Sync change:', change);
}
// Handler für Sync-Ereignisse
function onSyncChange(change: any) {

Check failure on line 17 in app/src/routes/settings/+page.svelte

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
console.log('Sync change:', change);
}
function onSyncPaused(error: any) {
if (error) {
console.log('Sync paused due to replication error', error);
} else {
console.log('Sync paused/resumed');
}
}
function onSyncPaused(error: any) {

Check failure on line 21 in app/src/routes/settings/+page.svelte

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
if (error) {
console.log('Sync paused due to replication error', error);
} else {
console.log('Sync paused/resumed');
}
}
function onSyncError(error: any) {
console.error('Sync error:', error);
replicationError = "Fehler bei der Synchronisation. Bitte überprüfen Sie den Link und versuchen Sie es erneut.";
}
function onSyncError(error: any) {

Check failure on line 29 in app/src/routes/settings/+page.svelte

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
console.error('Sync error:', error);
replicationError =
'Fehler bei der Synchronisation. Bitte überprüfen Sie den Link und versuchen Sie es erneut.';
}
async function handleReplication() {
if (isValidLink) {
const opts = { live: true, retry: true };
request_success = false;
replicationError = null;
$db.replicate.from(link).on('complete', function() {
$db.sync(link, opts)
.on('change', onSyncChange)
.on('paused', onSyncPaused)
.on('error', onSyncError);
request_success = true;
}).on('error', onSyncError);
}
}
async function handleReplication() {
if (isValidLink) {
const opts = { live: true, retry: true };
request_success = false;
replicationError = null;
$db.replicate
.from(link)
.on('complete', function () {
$db
.sync(link, opts)
.on('change', onSyncChange)
.on('paused', onSyncPaused)
.on('error', onSyncError);
request_success = true;
})
.on('error', onSyncError);
localStorage.setItem('syncLink', link);
storedLink = link;
}
}
function clearLink() {
localStorage.removeItem('syncLink');
storedLink = null;
}
</script>

<svelte:head>
<title>Settings - G'schäft'lhaberer</title>
<title>Settings - G'schäft'lhaberer</title>
</svelte:head>

<form on:submit|preventDefault={handleReplication}>
<Heading class="my-5">Settings</Heading>
<div class="flex flex-col gap-4 mt-6">
<Label>Link für die Synchronisation</Label>
<Input
bind:value={link}
placeholder="https://example.com"
invalid={!isValidLink && link !== ''}
aria-label="Link eingeben"
class={isValidLink ? 'valid-input' : link !== '' ? 'invalid-input' : ''}
/>

<Button color="green" type="submit" disabled={!isValidLink} class={isValidLink ? 'valid-button' : ''}>
Bestätigen
</Button>
{#if replicationError}
<P class="text-red-500">{replicationError}</P>
{/if}
{#if request_success}
<P>Einmalige Replikation abgeschlossen. Kontinuierliche Synchronisation gestartet.</P>
{/if}
</div>
<Heading class="my-5">Settings</Heading>
<div class="mt-6 flex flex-col gap-4">
<Label>Link für die Synchronisation</Label>
<Input bind:value={link} placeholder="https://example.com" />
<Button type="submit" class="mt-4">Synchronisieren</Button>
{#if storedLink}
<div class="mt-4">
<p>Gespeicherter Link: {storedLink}</p>
<Button on:click={clearLink} class="mt-2">Link löschen</Button>
</div>
{/if}
</div>
</form>

0 comments on commit 5bb19c5

Please sign in to comment.