diff --git a/angular.json b/angular.json index 683effd..6873ff6 100644 --- a/angular.json +++ b/angular.json @@ -1,5 +1,8 @@ { "$schema": "./node_modules/@angular/cli/lib/config/schema.json", + "cli": { + "analytics": false + }, "version": 1, "newProjectRoot": "projects", "projects": { diff --git a/package-lock.json b/package-lock.json index 3ac63c9..9492058 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21,7 +21,7 @@ "@tailwindcss/forms": "^0.3.2", "@tailwindcss/typography": "^0.4.0", "angular-svg-icon": "^13.0.0", - "appwrite": "^8.0.0", + "appwrite": "^9.0.1", "rxjs": "^7.4.0", "tslib": "^2.0.0", "zone.js": "~0.11.4" @@ -3246,9 +3246,9 @@ } }, "node_modules/appwrite": { - "version": "8.0.0", - "resolved": "https://registry.npmjs.org/appwrite/-/appwrite-8.0.0.tgz", - "integrity": "sha512-/0GMOUtg5+P9t4XTz3z1/FTXoAyEJ23CQ3zkXZybQJzdqy2TRG1iaktHsOmu4zkxEKBTjIsgSkTrKEgdYC/HHw==", + "version": "9.0.1", + "resolved": "https://registry.npmjs.org/appwrite/-/appwrite-9.0.1.tgz", + "integrity": "sha512-yLxb5H2fqlK0l4q6eEzrb5HGs3xA2894wcLIseOJ2v/iqUmjuIjXfLUpWG+DC94CQmEdZCxwvIFUVO/AG/t+cw==", "dependencies": { "cross-fetch": "3.1.5", "isomorphic-form-data": "2.0.0" @@ -17175,9 +17175,9 @@ "dev": true }, "appwrite": { - "version": "8.0.0", - "resolved": "https://registry.npmjs.org/appwrite/-/appwrite-8.0.0.tgz", - "integrity": "sha512-/0GMOUtg5+P9t4XTz3z1/FTXoAyEJ23CQ3zkXZybQJzdqy2TRG1iaktHsOmu4zkxEKBTjIsgSkTrKEgdYC/HHw==", + "version": "9.0.1", + "resolved": "https://registry.npmjs.org/appwrite/-/appwrite-9.0.1.tgz", + "integrity": "sha512-yLxb5H2fqlK0l4q6eEzrb5HGs3xA2894wcLIseOJ2v/iqUmjuIjXfLUpWG+DC94CQmEdZCxwvIFUVO/AG/t+cw==", "requires": { "cross-fetch": "3.1.5", "isomorphic-form-data": "2.0.0" diff --git a/package.json b/package.json index f3d077e..6c47923 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "@tailwindcss/forms": "^0.3.2", "@tailwindcss/typography": "^0.4.0", "angular-svg-icon": "^13.0.0", - "appwrite": "^8.0.0", + "appwrite": "^9.0.1", "rxjs": "^7.4.0", "tslib": "^2.0.0", "zone.js": "~0.11.4" diff --git a/src/app/components/todo/todo.component.ts b/src/app/components/todo/todo.component.ts index 262fe1b..7b430af 100644 --- a/src/app/components/todo/todo.component.ts +++ b/src/app/components/todo/todo.component.ts @@ -1,5 +1,5 @@ import { Component, OnInit } from '@angular/core'; -import { FormBuilder, FormGroup, Validators } from '@angular/forms'; +import { FormBuilder, FormGroup, NgForm, Validators } from '@angular/forms'; import { Select, Store } from '@ngxs/store'; import { Observable } from 'rxjs'; import { Todo } from 'src/app/models/Todo'; @@ -11,7 +11,6 @@ import { Account, AccountState, Todos, TodoState } from 'src/app/store'; styleUrls: ['./todo.component.css'], }) export class TodoComponent implements OnInit { - @Select(TodoState.getTodos) todos$: Observable; addTodoForm: FormGroup; @@ -21,8 +20,8 @@ export class TodoComponent implements OnInit { content: ['', [Validators.required]], }); } - - ngOnInit() { + + ngOnInit() { this.store.dispatch(new Todos.Fetch()); } @@ -31,6 +30,8 @@ export class TodoComponent implements OnInit { content: this.addTodoForm.value.content, isComplete: false, } as Todo; + + this.addTodoForm.reset({ content: '' }); const userId = this.store.selectSnapshot(AccountState.userId); const read = [`user:${userId}`]; const write = read; diff --git a/src/app/helpers/api.ts b/src/app/helpers/api.ts index 2cb75a5..5f66154 100644 --- a/src/app/helpers/api.ts +++ b/src/app/helpers/api.ts @@ -1,17 +1,33 @@ -import { Appwrite } from 'appwrite'; +import { Client, Databases, Account } from 'appwrite'; import { Server } from '../utils/config'; export class Api { - private static sdk: Appwrite | null; + private static sdk: Client | null; + private static db: Databases | null; + private static acc: Account | null; - static provider() { + private static client() { if (this.sdk) return this.sdk; - let client = new Appwrite(); + let client = new Client(); + client .setEndpoint(Server.endpoint) .setProject(Server.project) .setLocale('en-US'); this.sdk = client; + return this.sdk; } + + static database() { + if (this.db) return this.db; + this.db = new Databases(this.client(), Server.databaseID); + return this.db; + } + + static account() { + if (this.acc) return this.acc; + this.acc = new Account(this.client()); + return this.acc; + } } diff --git a/src/app/helpers/auth-guard.guard.ts b/src/app/helpers/auth-guard.guard.ts index a31b672..663d61e 100644 --- a/src/app/helpers/auth-guard.guard.ts +++ b/src/app/helpers/auth-guard.guard.ts @@ -9,8 +9,8 @@ export class AuthGuard implements CanActivate { constructor(private router: Router) {} canActivate(): Promise { - return Api.provider() - .account.getSession('current') + return Api.account() + .getSession('current') .then((isAuthenticated) => { if (!isAuthenticated) { this.router.navigate(['/login']); diff --git a/src/app/store/account/index.ts b/src/app/store/account/index.ts index 52bb189..2ad27cf 100644 --- a/src/app/store/account/index.ts +++ b/src/app/store/account/index.ts @@ -69,8 +69,8 @@ export class AccountState { ) { let { email, password } = action.payload; try { - await Api.provider().account.createSession(email, password); - let account = await Api.provider().account.get(); + await Api.account().createEmailSession(email, password); + let account = await Api.account().get(); patchState({ account: account, }); @@ -94,13 +94,13 @@ export class AccountState { ) { let { email, password, name } = action.payload; try { - let account = await Api.provider().account.create( + let account = await Api.account().create( 'unique()', email, password, name ); - let session = await Api.provider().account.createSession(email, password); + let session = await Api.account().createEmailSession(email, password); patchState({ account, session, @@ -124,7 +124,7 @@ export class AccountState { action: Account.FetchAccount ) { try { - let account = await Api.provider().account.get(); + let account = await Api.account().get(); patchState({ account: account, }); @@ -146,13 +146,13 @@ export class AccountState { action: Account.Logout ) { try { - await Api.provider().account.deleteSession('current'); + await Api.account().deleteSession('current'); patchState({ account: null, session: null, }); dispatch(new Account.Redirect({ path: '' })); - } catch (e: any) { + } catch (e: any) { console.log('Error Loggin Out'); dispatch( new GlobalActions.setAlert({ diff --git a/src/app/store/todos/index.ts b/src/app/store/todos/index.ts index bea25f3..4c68b3a 100644 --- a/src/app/store/todos/index.ts +++ b/src/app/store/todos/index.ts @@ -61,9 +61,7 @@ export class TodoState { action: Todos.Fetch ) { try { - let todos = await Api.provider().database.listDocuments( - Server.collectionID - ); + let todos = await Api.database().listDocuments(Server.collectionID); setState({ todos: todos.documents, }); @@ -86,7 +84,7 @@ export class TodoState { ) { try { let { data, read, write } = action.payload; - let todo = await Api.provider().database.createDocument( + let todo = await Api.database().createDocument( Server.collectionID, 'unique()', data, @@ -116,7 +114,7 @@ export class TodoState { ) { let { documentId, data, read, write } = action.payload; try { - let updatedTodo = await Api.provider().database.updateDocument( + let updatedTodo = await Api.database().updateDocument( Server.collectionID, documentId, data, @@ -124,9 +122,7 @@ export class TodoState { write ); let todoList = [...getState().todos]; - const index = todoList.findIndex( - (todo) => todo.$id === updatedTodo.$id - ); + const index = todoList.findIndex((todo) => todo.$id === updatedTodo.$id); if (index !== -1) { todoList[index] = updatedTodo; patchState({ @@ -152,10 +148,7 @@ export class TodoState { ) { let { documentId } = action.payload; try { - await Api.provider().database.deleteDocument( - Server.collectionID, - documentId - ); + await Api.database().deleteDocument(Server.collectionID, documentId); let todos = getState().todos; todos = todos.filter((todo) => todo.$id !== documentId); patchState({ diff --git a/src/app/utils/config.ts b/src/app/utils/config.ts index 884f7e2..ddbb1a5 100644 --- a/src/app/utils/config.ts +++ b/src/app/utils/config.ts @@ -1,7 +1,8 @@ import { environment } from '../../environments/environment'; export const Server = { - endpoint : environment.APP_ENDPOINT, - project: environment.APP_PROJECT, - collectionID : environment.APP_COLLECTION_ID -} \ No newline at end of file + endpoint: environment.APP_ENDPOINT, + project: environment.APP_PROJECT_ID, + databaseID: environment.APP_DATABASE_ID, + collectionID: environment.APP_COLLECTION_ID, +}; diff --git a/src/environments/environment.ts b/src/environments/environment.ts index 3620f7c..507b730 100644 --- a/src/environments/environment.ts +++ b/src/environments/environment.ts @@ -4,9 +4,10 @@ export const environment = { production: false, - APP_ENDPOINT: "https://demo.appwrite.io/v1", - APP_PROJECT: "6062f9c2c09ce", - APP_COLLECTION_ID:"606621a04837c" + APP_ENDPOINT: '', + APP_PROJECT_ID: '', + APP_DATABASE_ID: '', + APP_COLLECTION_ID: '', }; /*