From 9ca27ba9da466f9dd4dd6ab8229ca728ee51de5d Mon Sep 17 00:00:00 2001 From: ItzNotABug Date: Mon, 23 Sep 2024 14:02:43 +0530 Subject: [PATCH] update: remove unnecessary responsibility from createPolicy. --- src/lib/helpers/backups.ts | 3 - .../project-[project]/databases/create.svelte | 15 +- .../database-[database]/backups/+page.svelte | 58 +- .../backups/createPolicy.svelte | 598 +++++------------- 4 files changed, 229 insertions(+), 445 deletions(-) diff --git a/src/lib/helpers/backups.ts b/src/lib/helpers/backups.ts index 427b2de59..a597b7902 100644 --- a/src/lib/helpers/backups.ts +++ b/src/lib/helpers/backups.ts @@ -1,5 +1,3 @@ -import { ID } from '@appwrite.io/console'; - export type UserBackupPolicy = { id?: string; label: string; @@ -17,7 +15,6 @@ export type UserBackupPolicy = { export const cronExpression = (policy: UserBackupPolicy) => { const now = new Date(); - if (!policy.id) policy.id = ID.unique(); if (policy.plainTextFrequency === 'hourly') { const utcMinute = now.getUTCMinutes(); diff --git a/src/routes/(console)/project-[project]/databases/create.svelte b/src/routes/(console)/project-[project]/databases/create.svelte index 48942bb51..d03884c4a 100644 --- a/src/routes/(console)/project-[project]/databases/create.svelte +++ b/src/routes/(console)/project-[project]/databases/create.svelte @@ -24,7 +24,7 @@ let showCustomId = false; let showPlanUpgradeAlert = true; - const createPolicies = async () => { + const createPolicies = async (resourceId: string) => { if (!totalPolicies.length) return; const totalPoliciesPromise = totalPolicies.map((policy) => { @@ -36,7 +36,7 @@ policy.retained, policy.schedule, policy.label, - id + resourceId ); }); @@ -45,8 +45,9 @@ const create = async () => { try { - const database = await sdk.forProject.databases.create(id ? id : ID.unique(), name); - await createPolicies(); + const databaseId = id ? id : ID.unique(); + const database = await sdk.forProject.databases.create(databaseId, name); + await createPolicies(databaseId); showCreate = false; dispatch('created', database); @@ -85,7 +86,7 @@ {#if !showCustomId}
(showCustomId = !showCustomId)} - >
@@ -105,9 +106,9 @@ {/if} {:else} {/if} diff --git a/src/routes/(console)/project-[project]/databases/database-[database]/backups/+page.svelte b/src/routes/(console)/project-[project]/databases/database-[database]/backups/+page.svelte index b0fb2a0b2..09246dd3f 100644 --- a/src/routes/(console)/project-[project]/databases/database-[database]/backups/+page.svelte +++ b/src/routes/(console)/project-[project]/databases/database-[database]/backups/+page.svelte @@ -21,9 +21,12 @@ import { app } from '$lib/stores/app'; import { onMount } from 'svelte'; import { feedback } from '$lib/stores/feedback'; + import { cronExpression, type UserBackupPolicy } from '$lib/helpers/backups'; let showCreatePolicy = false; + let policyCreateError: string; let showCreateManualBackup = false; + let totalPolicies: UserBackupPolicy[] = []; let isDisabled = isSelfHosted || (isCloud && $organization?.billingPlan === BillingPlan.FREE); export let data: PageData; @@ -82,6 +85,46 @@ } }; + const createPolicies = async () => { + const totalPoliciesPromise = totalPolicies.map((policy) => { + cronExpression(policy); + + return sdk.forProject.backups.createPolicy( + policy.id, + ['databases'], + policy.retained, + policy.schedule, + policy.label, + data.database.$id + ); + }); + + try { + await Promise.all(totalPoliciesPromise); + + const message = + totalPolicies.length > 1 + ? `Backup policies have been created` + : `${totalPolicies[0].label} policy has been created`; + + addNotification({ + isHtml: true, + type: 'success', + message + }); + + invalidate(Dependencies.BACKUPS); + } catch (err) { + addNotification({ + type: 'error', + message: err.message + }); + } finally { + totalPolicies = []; + showCreatePolicy = false; + } + }; + onMount(() => { return sdk.forConsole.client.subscribe('console', (response) => { if (response.events.includes('archives.*') || response.events.includes('policies.*')) { @@ -165,7 +208,20 @@ - + + + + + + + + + - import { Modal } from '$lib/components'; - import { - Button, - FormList, - Helper, - InputCheckbox, - InputSelect, - InputText, - InputTime - } from '$lib/elements/forms'; + import { Button, FormList, Helper, InputCheckbox, InputSelect, InputText, InputTime } from '$lib/elements/forms'; import { ID } from '@appwrite.io/console'; import { capitalize } from '$lib/helpers/string'; - import { backupRetainingOptions, database } from '../store'; + import { backupRetainingOptions } from '../store'; import { policyPricing, presetPolicies } from './store'; - import { sdk } from '$lib/stores/sdk'; - import { addNotification } from '$lib/stores/notifications'; - import { invalidate } from '$app/navigation'; - import { Dependencies } from '$lib/constants'; - import { - backupFrequencies, - backupPolicyDescription, - cronExpression, - type UserBackupPolicy - } from '$lib/helpers/backups'; - - export let isModal = true; - export let showCreate = false; + import { backupFrequencies, backupPolicyDescription, type UserBackupPolicy } from '$lib/helpers/backups'; + export let isShowing: boolean; export let title: string | undefined = undefined; export let subtitle: string | undefined = undefined; + export let totalPolicies: UserBackupPolicy[] = []; let showCustomPolicy = false; @@ -37,9 +18,11 @@ $: totalPolicies = [ ...listOfCustomPolicies, ...$presetPolicies.filter((policy) => policy.checked) - ]; + ].map((policy) => { + if (!policy.id) policy.id = ID.unique(); + return policy; + }); - let error: string; let policyInEdit = null; let policyRetention = 30; let selectedTime = '00:00'; @@ -49,7 +32,6 @@ let monthlyBackupFrequency = 'end'; const resetFormVariables = () => { - error = null; policyInEdit = null; policyRetention = 30; selectedTime = '00:00'; @@ -58,47 +40,6 @@ monthlyBackupFrequency = 'end'; }; - export const createPolicies = async () => { - const totalPoliciesPromise = totalPolicies.map((policy) => { - cronExpression(policy); - - return sdk.forProject.backups.createPolicy( - policy.id, - ['databases'], - policy.retained, - policy.schedule, - policy.label, - $database.$id - ); - }); - - try { - await Promise.all(totalPoliciesPromise); - - const message = - totalPolicies.length > 1 - ? `Backup policies have been created` - : `${totalPolicies[0].label} has been created`; - - addNotification({ - isHtml: true, - type: 'success', - message - }); - - showCreate = false; - resetFormVariables(); - totalPolicies = []; - listOfCustomPolicies = []; - invalidate(Dependencies.BACKUPS); - } catch (err) { - addNotification({ - type: 'error', - message: err.message - }); - } - }; - const handleSavePolicy = () => { const userBackupPolicy = { default: false, @@ -118,7 +59,16 @@ }; const markPolicyChecked = (event: Event, policy: UserBackupPolicy) => { - policy.checked = (event.target as HTMLInputElement).checked; + const isChecked = (event.target as HTMLInputElement).checked; + + presetPolicies.update((all) => { + return all.map((p) => { + if (p.label === policy.label) { + return { ...p, checked: isChecked }; + } + return p; + }); + }); }; $: customPolicyDescription = (policy: UserBackupPolicy) => { @@ -138,7 +88,7 @@ customPolicySection?.scrollIntoView({ behavior: 'auto' }); } - $: if (!showCreate) { + $: if (isShowing) { resetFormVariables(); showCustomPolicy = false; listOfCustomPolicies = []; @@ -151,53 +101,59 @@ } -{#if isModal} - - -
-
- {#each $presetPolicies as policy, index (index)} -
-
- markPolicyChecked(event, policy)} /> +
+ {#if title || subtitle} +
+ {#if title} +

{title}

+ {/if} -
-
+ {#if subtitle} + {subtitle} + {/if} +
+ {/if} + + +
+
+ {#each $presetPolicies as policy, index (index)} +
+
+ markPolicyChecked(event, policy)} /> + +
+
+

{policy.label}

+ $20.00 +
+ + {policy.description} +
+
+
+ {/each} +
+ + {#if listOfCustomPolicies.length} +
+ {#each listOfCustomPolicies as policy} +
+
+
+

{policy.label}

$20.00
- {policy.description} -
-
-
- {/each} -
- - {#if listOfCustomPolicies.length} -
- {#each listOfCustomPolicies as policy} -
-
-
-
-

{policy.label}

- $20.00 -
- -
-
- - {customPolicyDescription(policy)} -
-
- {/each} -
- {/if} - - {#if showCustomPolicy || policyInEdit} - - {:else} -
- - Total: ${totalPolicies.length * policyPricing}.00 -
- {/if} -
-
- - - - - - -{:else} -
- {#if title || subtitle} -
- {#if title} -

{title}

- {/if} - - {#if subtitle} - {subtitle} - {/if} -
- {/if} - - -
-
- {#each $presetPolicies as policy, index (index)} -
-
- markPolicyChecked(event, policy)} /> - -
-
-

{policy.label}

- $20.00 -
- - {policy.description} -
+ {customPolicyDescription(policy)}
{/each}
+ {/if} - {#if listOfCustomPolicies.length} -
- {#each listOfCustomPolicies as policy} -
-
-
-

{policy.label}

- -
-
-
- - {customPolicyDescription(policy)} -
-
- {/each} -
- {/if} - - {#if showCustomPolicy || policyInEdit} - - {:else} -
- - Total: ${totalPolicies.length * policyPricing}.00
- {/if} -
-
-
-{/if} + + {:else} +
+ + Total: ${totalPolicies.length * policyPricing}.00 +
+ {/if} +
+ +