diff --git a/src/lib/actions/tooltip.ts b/src/lib/actions/tooltip.ts index 1636a14ec..b4dc14a5f 100644 --- a/src/lib/actions/tooltip.ts +++ b/src/lib/actions/tooltip.ts @@ -1,17 +1,24 @@ import type { Action } from 'svelte/action'; -import type { Props } from 'tippy.js'; +import type { Props as TippyProps } from 'tippy.js'; import tippy from 'tippy.js'; +type Props = TippyProps & { + disabled?: boolean; +}; + export const tooltip: Action> = (node, config) => { const instance = tippy(node, config); + if (config.disabled) instance.disable(); return { - update({ content }) { + update({ content, disabled }) { if (content !== instance.props.content) { instance.setProps({ content }); } + + disabled ? instance.disable() : instance.enable(); }, destroy() { instance.destroy(); diff --git a/src/lib/elements/forms/inputCheckbox.svelte b/src/lib/elements/forms/inputCheckbox.svelte index cecc2a441..4c9088a07 100644 --- a/src/lib/elements/forms/inputCheckbox.svelte +++ b/src/lib/elements/forms/inputCheckbox.svelte @@ -2,6 +2,7 @@ import { FormItem, Helper } from '.'; export let label: string; + export let optionalText: string | undefined = undefined; export let showLabel = true; export let id: string; export let value = false; @@ -26,7 +27,13 @@ - + +
- + +
- + +
- + +
(arr: T[], index: number): T[] { // Remove the element at the given index, return a new array return [...arr.slice(0, index), ...arr.slice(index + 1)]; } + +/** + * Get nth item of Array. Negative for backward + * + * @export + * @template T + * @param {(readonly T[] | [])} array + * @param {number} index + * @returns {(T | undefined)} + */ +export function at(array: readonly [], index: number): undefined; +export function at(array: readonly T[], index: number): T; +export function at(array: readonly T[] | [], index: number): T | undefined { + const len = array.length; + if (!len) return undefined; + + if (index < 0) index += len; + + return array[index]; +} + +/** + * Get last item + * + * @export + * @template T + * @param {readonly T[]} array + * @returns {(T | undefined)} + */ +export function last(array: readonly []): undefined; +export function last(array: readonly T[]): T; +export function last(array: readonly T[]): T | undefined { + return at(array, -1); +} diff --git a/src/lib/helpers/object.ts b/src/lib/helpers/object.ts index eb50ed316..90948eb8c 100644 --- a/src/lib/helpers/object.ts +++ b/src/lib/helpers/object.ts @@ -44,3 +44,69 @@ export function objectMapKeys( return result; } + +type KeyTypesMap = Record | ((v: unknown) => boolean) | string>; +/** + * Checks if the given value is an object of type T, given a map of keys and + * their types/validators. + * + * @category Object + * + * @template T + * @param {unknown} object + * @param {KeyTypesMap} keyTypesMap + * @returns {value is T} + * + * @example + * ``` + * type ExampleType = { + * a: string; + * b: number; + * c: string | number; + * d: string[] + * e: { + * f: string; + * g: number; + * } + * } + * + * export const isExampleType = (value: unknown): value is ExampleType => { + * return isObjectType(value, { + * a: 'string', + * b: 'number', + * c: ['string', 'number'], + * d: (v) => Array.isArray(v) && v.every((v) => typeof v === 'string'), + * e: (v) => isObjectType(v, { + * f: 'string', + * g: 'number', + * }), + * }); + * }; + * ``` + */ +export const isObjectType = ( + object: unknown, + keyTypesMap: Partial> +): object is T => { + if (typeof object !== 'object' || object === null) { + return false; + } + + for (const [key, check] of objectEntries(keyTypesMap)) { + const value = (object as Record)[key]; + + if (typeof check === 'function') { + if (!check(value)) { + return false; + } + } else if (typeof check === 'string') { + if (typeof value !== check) { + return false; + } + } else if (!check?.includes(typeof value)) { + return false; + } + } + + return true; +}; diff --git a/src/lib/helpers/string.ts b/src/lib/helpers/string.ts new file mode 100644 index 000000000..e67be0e07 --- /dev/null +++ b/src/lib/helpers/string.ts @@ -0,0 +1,10 @@ +/** + * Capitalizes the first letter of a string + * + * @export + * @param {string} str + * @returns {string} + */ +export function capitalize(str: string): string { + return str.charAt(0).toUpperCase() + str.slice(1); +} diff --git a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/[[page]]/+page.svelte b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/[[page]]/+page.svelte index 4aed513a9..a5416a323 100644 --- a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/[[page]]/+page.svelte +++ b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/[[page]]/+page.svelte @@ -19,6 +19,7 @@ import { page } from '$app/stores'; import { PAGE_LIMIT } from '$lib/constants'; import CreateAttribute from '../createAttribute.svelte'; + import { tooltip } from '$lib/actions/tooltip'; export let data: PageData; let showCreateAttribute = false; @@ -33,6 +34,44 @@ title: attribute.key })) ]; + + function formatArray(array: any[]) { + if (array.length === 0) return '[ ]'; + + let formattedFields: string[] = []; + for (const item of array) { + if (typeof item === 'string') { + formattedFields.push(`"${item}"`); + } else { + formattedFields.push(`${item}`); + } + } + + return `[${formattedFields.join(', ')}]`; + } + + function formatColumn(column: any) { + let formattedColumn: string; + + if (typeof column === 'string') { + formattedColumn = column; + } else if (Array.isArray(column)) { + formattedColumn = formatArray(column); + } else if (!column) { + formattedColumn = 'n/a'; + } else { + formattedColumn = `${column}`; + } + + return { + value: + formattedColumn.length > 20 + ? `${formattedColumn.slice(0, 20)}...` + : formattedColumn, + truncated: formattedColumn.length > 20, + whole: formattedColumn + }; + } @@ -70,8 +109,15 @@ {#each columns as column} + {@const formatted = formatColumn(document[column.key])} - {document[column.key] ?? 'n/a'} +
+ {formatted.value} +
{/each} diff --git a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/createDocument.svelte b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/createDocument.svelte index ceb3d0559..18febce98 100644 --- a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/createDocument.svelte +++ b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/createDocument.svelte @@ -24,7 +24,7 @@ $createDocument.attributes = $attributes.filter((a) => a.status === 'available'); $attributes.forEach((attr) => { if (attr.array) { - $createDocument.document[attr.key] = [null]; + $createDocument.document[attr.key] = []; } else { $createDocument.document[attr.key] = null; } @@ -43,12 +43,15 @@ $createDocument.document, $createDocument.permissions ); + addNotification({ message: 'Document has been created', type: 'success' }); trackEvent('submit_document_create'); invalidate(Dependencies.DOCUMENTS); + + createDocument.reset(); wizard.hide(); } catch (error) { addNotification({ diff --git a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/document-[document]/attribute.svelte b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/document-[document]/attribute.svelte index 2a584d25b..27c773c21 100644 --- a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/document-[document]/attribute.svelte +++ b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/document-[document]/attribute.svelte @@ -9,6 +9,7 @@ export let id: string; export let label: string; export let value: string | number | boolean; + export let optionalText: string | undefined = undefined; export let attribute: | Models.AttributeBoolean | Models.AttributeEmail @@ -56,5 +57,5 @@ {#if cmp} - + {/if} diff --git a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/document-[document]/attributeForm.svelte b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/document-[document]/attributeForm.svelte new file mode 100644 index 000000000..298282f79 --- /dev/null +++ b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/document-[document]/attributeForm.svelte @@ -0,0 +1,113 @@ + + +{#if attributes.length} +
    + {#each attributes as attribute} + {@const label = attribute.required ? `${attribute.key}*` : attribute.key} + {#if attribute.array} + {#if formValues[attribute.key].length === 0} +
    + + {label} + + {getAttributeType(attribute)} + + + + +
    + {/if} + + {#if formValues[attribute.key].length !== 0} +
      + {#each [...formValues[attribute.key].keys()] as index} +
    • +
      + +
      +
      + +
      +
    • + {/each} +
    + + {/if} + {:else} + + + + {/if} + {/each} + + {#if customId !== undefined} + {#if !showCustomId} +
    + (showCustomId = !showCustomId)}> + +
    + {:else} + + {/if} + {/if} +
+{/if} diff --git a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/document-[document]/attributes/boolean.svelte b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/document-[document]/attributes/boolean.svelte index 2beea6cbc..dd705b075 100644 --- a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/document-[document]/attributes/boolean.svelte +++ b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/document-[document]/attributes/boolean.svelte @@ -6,12 +6,14 @@ export let label: string; export let value: boolean; export let attribute: Models.AttributeBoolean; + export let optionalText: string | undefined = undefined; {#if attribute?.array} {:else} - + {/if} diff --git a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/document-[document]/attributes/datetime.svelte b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/document-[document]/attributes/datetime.svelte index f6ee0dfdb..f624c4406 100644 --- a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/document-[document]/attributes/datetime.svelte +++ b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/document-[document]/attributes/datetime.svelte @@ -6,6 +6,13 @@ export let label: string; export let value: string; export let attribute: Models.AttributeDatetime; + export let optionalText: string | undefined = undefined; - + diff --git a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/document-[document]/attributes/enum.svelte b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/document-[document]/attributes/enum.svelte index 3790fc0d8..0499984d0 100644 --- a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/document-[document]/attributes/enum.svelte +++ b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/document-[document]/attributes/enum.svelte @@ -6,6 +6,7 @@ export let label: string; export let value: string; export let attribute: Models.AttributeEnum; + export let optionalText: string | undefined = undefined; $: options = attribute.elements.map((element) => { return { @@ -20,5 +21,6 @@ {options} {id} {label} + {optionalText} placeholder="Select a value" showLabel={!!label?.length} /> diff --git a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/document-[document]/attributes/integer.svelte b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/document-[document]/attributes/integer.svelte index b5067c22b..37c33acf8 100644 --- a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/document-[document]/attributes/integer.svelte +++ b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/document-[document]/attributes/integer.svelte @@ -6,11 +6,13 @@ export let label: string; export let value: number; export let attribute: Models.AttributeInteger; + export let optionalText: string | undefined = undefined; - ![ - '$id', - '$collection', - '$collectionId', - '$databaseId', - '$createdAt', - '$updatedAt' - ].includes(key) - ) + .filter((key) => { + return ![ + '$id', + '$collection', + '$collectionId', + '$databaseId', + '$createdAt', + '$updatedAt' + ].includes(key); + }) .reduce((obj, key) => { obj[key] = $doc[key]; return obj; @@ -43,7 +43,7 @@ currentDoc = JSON.stringify($work); }); - $: if (currentDoc && $work) { + $: { if (currentDoc !== JSON.stringify($work)) { disableUpdate = false; } else { @@ -81,90 +81,13 @@ }); } } - - function addArrayItem(key: string) { - work.update((n) => { - if (!Array.isArray(n[key])) { - n[key] = []; - } - n[key].push(null); - - return n; - }); - } - - function removeArrayItem(key: string, index: number) { - work.update((n) => { - n[key].splice(index, 1); - - return n; - }); - } Update Data

Update document data based on the attributes created earlier.

-
- {#each $collection.attributes.filter((a) => a.status === 'available') as attribute} - {#if attribute.array} -
    - {#each [...$work[attribute.key].keys()] as index} -
  • -
    - -
    -
    - -
    -
  • - {:else} -
  • -
    - -
    -
    - -
    -
  • - {/each} -
- - - {:else} -
    - -
- {/if} - {/each} -
+
diff --git a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/store.ts b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/store.ts index 6d30597ee..e4f17fe04 100644 --- a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/store.ts +++ b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/store.ts @@ -1,6 +1,7 @@ import { derived } from 'svelte/store'; import { page } from '$app/stores'; import type { Models } from '@aw-labs/appwrite-console'; +import { isObjectType } from '$lib/helpers/object'; export type Attributes = | Models.AttributeBoolean @@ -12,6 +13,12 @@ export type Attributes = | Models.AttributeString | Models.AttributeUrl; +export function isAttributeEnum(attribute: unknown): attribute is Models.AttributeEnum { + return isObjectType(attribute, { + format: (v) => v === 'enum' + }); +} + type Collection = Omit & { attributes: Array; }; diff --git a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/wizard/step1.svelte b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/wizard/step1.svelte index 54325c4fb..1df88ae0c 100644 --- a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/wizard/step1.svelte +++ b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/wizard/step1.svelte @@ -1,32 +1,7 @@ @@ -35,61 +10,8 @@ Provide document data based on attributes you created earlier. - {#if $createDocument.attributes.length} - - {#each $createDocument.attributes as attribute} - {@const label = attribute.required ? `${attribute.key}*` : attribute.key} - {#if attribute.array} - {#each [...$createDocument.document[attribute.key].keys()] as index} -
  • -
    - -
    -
    - -
    -
  • - {/each} - - {:else} - - - - {/if} - {/each} - {#if !showCustomId} -
    - (showCustomId = !showCustomId)}> - -
    - {:else} - - {/if} -
    - {/if} + diff --git a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/wizard/store.ts b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/wizard/store.ts index 6fcef3b1d..87939df62 100644 --- a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/wizard/store.ts +++ b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/wizard/store.ts @@ -1,7 +1,7 @@ import { writable } from 'svelte/store'; import type { Attributes } from '../store'; -type Store = { +type CreateDocument = { id?: string | null; // TODO: Improve this type // eslint-disable-next-line @typescript-eslint/no-explicit-any @@ -10,9 +10,24 @@ type Store = { attributes: Attributes[]; }; -export const createDocument = writable({ +const initialCreateDocument: CreateDocument = { id: null, document: {}, permissions: [], attributes: [] -}); +}; + +function createDocumentWritable() { + const store = writable({ ...initialCreateDocument }); + + const reset = () => { + store.set({ ...initialCreateDocument }); + }; + + return { + ...store, + reset + }; +} + +export const createDocument = createDocumentWritable(); diff --git a/tests/unit/helpers/object.test.ts b/tests/unit/helpers/object.test.ts new file mode 100644 index 000000000..75777aa6e --- /dev/null +++ b/tests/unit/helpers/object.test.ts @@ -0,0 +1,74 @@ +import { isObjectType, objectEntries } from '$lib/helpers/object'; + +test('objectEntries', () => { + const object = { + a: 1, + b: 2, + c: 3 + }; + + expect(objectEntries(object)).toEqual(Object.entries(object)); +}); + +test('isObjectType', () => { + type TestType = { + a: string; + b: number; + c: string | number; + d: string[]; + e: { + f: string; + g: number; + }; + h: undefined | null | string; + }; + + const valid_type = { + a: '1', + b: 1, + c: '1', + d: ['1', '2'], + e: { + f: '1', + g: 1 + }, + h: null + }; + + const invalid_type = { + a: 1, + b: '1', + c: true + }; + + const invalid_type_2 = { + ...valid_type, + d: [1, '2'] + }; + + const invalid_type_3 = { + ...valid_type, + c: null + }; + + const isTestType = (value: unknown): value is TestType => { + return isObjectType(value, { + a: 'string', + b: 'number', + c: ['string', 'number'], + d: (v) => Array.isArray(v) && v.every((v) => typeof v === 'string'), + e: (v) => + isObjectType(v, { + f: 'string', + g: 'number' + }), + h: (v) => v === null || v === undefined || typeof v === 'string' + }); + }; + + expect(isTestType(valid_type)).toBe(true); + expect(isTestType(invalid_type)).toBe(false); + expect(isTestType(invalid_type_2)).toBe(false); + expect(isTestType(invalid_type_3)).toBe(false); + expect(isTestType('12331')).toBe(false); +});