<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: leonyangela</title>
    <description>The latest articles on DEV Community by leonyangela (@leonyangela).</description>
    <link>https://dev.to/leonyangela</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F946840%2Fd3d5962f-793a-4150-8b91-06b0bb750bcc.png</url>
      <title>DEV Community: leonyangela</title>
      <link>https://dev.to/leonyangela</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/leonyangela"/>
    <language>en</language>
    <item>
      <title>Build your App with Appwrite + jQuery</title>
      <dc:creator>leonyangela</dc:creator>
      <pubDate>Mon, 17 Oct 2022 14:26:48 +0000</pubDate>
      <link>https://dev.to/leonyangela/build-your-app-with-appwrite-jquery-55ij</link>
      <guid>https://dev.to/leonyangela/build-your-app-with-appwrite-jquery-55ij</guid>
      <description>&lt;p&gt;&lt;em&gt;You can found this code on my github: &lt;a href="https://github.com/leonyangela/jquery-demo-appwrite"&gt;https://github.com/leonyangela/jquery-demo-appwrite&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Installing Appwrite
&lt;/h2&gt;

&lt;p&gt;To install appwrite, you need to have docker installed. After you've finished installing docker. You can install appwrite by using&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run -it --rm ^
    --volume //var/run/docker.sock:/var/run/docker.sock ^
    --volume "%cd%"/appwrite:/usr/src/code/appwrite:rw ^
    --entrypoint="install" ^
    appwrite/appwrite:1.0.3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It will let you choose your own parameter or you can use the default one.&lt;/p&gt;

&lt;p&gt;After you have specified these details, it will take a few moments. If it's successful, you will get a message saying &lt;code&gt;Appwrite installed successfully&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You can now access your appwrite dashboard by visiting &lt;code&gt;http://&amp;lt;hostname or custom domain&amp;gt;:&amp;lt;HTTP port&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You could register a new account and explore the appwrite (&lt;a href="https://appwrite.io/docs"&gt;Docs&lt;/a&gt;).&lt;/p&gt;

&lt;h2&gt;
  
  
  Account
&lt;/h2&gt;

&lt;p&gt;To create an account, you can use these &lt;a href="https://appwrite.io/docs/client/account"&gt;API&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Database
&lt;/h2&gt;

&lt;p&gt;To insert into database, you can use these &lt;a href="https://appwrite.io/docs/client/databases"&gt;API&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  App Init
&lt;/h2&gt;

&lt;p&gt;I use webpack as front-end and &lt;code&gt;webpack-dev-server&lt;/code&gt; to start the app.&lt;/p&gt;

&lt;p&gt;to hide sensitive information, I use &lt;a href="https://www.npmjs.com/package/dotenv-webpack"&gt;dotenv-webpack&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;to use jquery, install the &lt;a href="https://www.npmjs.com/package/jquery"&gt;jquery&lt;/a&gt; with &lt;br&gt;
&lt;code&gt;npm i --save-dev jquery&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I use &lt;a href="https://www.npmjs.com/package/bootstrap"&gt;bootstrap v5 &lt;/a&gt; as CSS framework.&lt;/p&gt;
&lt;h2&gt;
  
  
  .env
&lt;/h2&gt;

&lt;p&gt;create a file on parent directory and add these to your .env file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;API_ENDPOINT=[YOUR_API_ENDPOINT]
PROJECT_ID=[YOUR_PROJECT_ID]
DB_COLLECTION_ID=[YOUR_DATABASE_COLLECTION_ID]
DB_ID=[YOUR_DATABASE_ID]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Index.js
&lt;/h2&gt;

&lt;p&gt;create a new file under &lt;code&gt;/src&lt;/code&gt; folder and add&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import $ from 'jquery'
import { GenerateApp } from "./generateApp"

class App {
    static init() {
        console.log('init....')
        let app = new GenerateApp()
    }
}

App.init()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  GenerateApp.js
&lt;/h2&gt;

&lt;p&gt;create a new file generateApp.js under &lt;code&gt;/src&lt;/code&gt; folder to add all the functions related to the app functionality.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Client, Account, ID, Databases } from 'appwrite'

export class GenerateApp {

    constructor() {
        this.client = new Client()
            .setEndpoint(process.env.API_ENDPOINT)       // Your API Endpoint
            .setProject(process.env.PROJECT_ID)          // Your project ID
        this.account = new Account(this.client)
        this.databases = new Databases(this.client)

        let data = sessionStorage.getItem("appWriteUser")

        // if local storage is empty, show form or register data
        if (!data) {
            this.register()
            // if local storage is not empty, show to do list
        } else {
            this.todoFunc()
        }

    }

    generateTodo() {
        var htmlAdd = ''

        htmlAdd += '&amp;lt;h1 class=" h5 pt-2"&amp;gt;add your todos below...&amp;lt;/h1&amp;gt;'
        htmlAdd += '&amp;lt;div class="input-group mb-3 pt-2"&amp;gt;'
        htmlAdd += '    &amp;lt;input type="text" class="form-control" id="todoinput" placeholder="add todos..." aria-label="todo lists aria-describedby="button-addon2"&amp;gt;'
        htmlAdd += '    &amp;lt;button class="btn btn-outline-secondary" type="button" id="btnAdd"&amp;gt;Add Item&amp;lt;/button&amp;gt;'
        htmlAdd += '&amp;lt;/div&amp;gt;'

        htmlAdd += '&amp;lt;div class="w-100" id="card-group"&amp;gt;'
        htmlAdd += '    &amp;lt;div class="card w-50" id="no-data"&amp;gt;'
        htmlAdd += '        &amp;lt;div class="card-body"&amp;gt;'
        htmlAdd += '            &amp;lt;h5 class="card-title"&amp;gt;No data....&amp;lt;/h5&amp;gt;'
        htmlAdd += '        &amp;lt;/div&amp;gt;'
        htmlAdd += '    &amp;lt;/div&amp;gt;'
        htmlAdd += '&amp;lt;/div&amp;gt;'
        htmlAdd += '&amp;lt;button type="button" class="btn btn-primary mt-5" id="btnLogout"&amp;gt;Logout&amp;lt;/button&amp;gt; '

        $('.container-fluid').html(htmlAdd)
    }

    generateLoginForm() {
        var htmlAdd = ''
        htmlAdd += '&amp;lt;div class="row justify-content-center align-items-center h-100"&amp;gt;'
        htmlAdd += '    &amp;lt;div class="col-12 col-lg-9 col-xl-7"&amp;gt;'
        htmlAdd += '        &amp;lt;div class="card shadow-2-strong card-login" style="border-radius: 15px"&amp;gt;'
        htmlAdd += '            &amp;lt;div class="card-body p-4 p-md-5"&amp;gt;'
        htmlAdd += '                &amp;lt;h3 class="mb-4 pb-2 pb-md-0 mb-md-5"&amp;gt;Login Form&amp;lt;/h3&amp;gt;'
        htmlAdd += '                &amp;lt;form id="registrationForm"&amp;gt;'
        htmlAdd += '                    &amp;lt;div class="row"&amp;gt;'
        htmlAdd += '                        &amp;lt;div class="col-md-6 mb-4"&amp;gt;'
        htmlAdd += '                            &amp;lt;div class="form-outline"&amp;gt;'
        htmlAdd += '                                &amp;lt;input type="email" id="login_email" class="form-control form-control-lg" /&amp;gt;'
        htmlAdd += '                                &amp;lt;label class="form-label" for="email"&amp;gt;Email&amp;lt;/label&amp;gt;'
        htmlAdd += '                            &amp;lt;/div&amp;gt;'
        htmlAdd += '                        &amp;lt;/div&amp;gt;'
        htmlAdd += '                        &amp;lt;div class="col-md-6 mb-4"&amp;gt;'
        htmlAdd += '                            &amp;lt;div class="form-outline"&amp;gt;'
        htmlAdd += '                                &amp;lt;input type="password" id="login_password" class="form-control form-control-lg" /&amp;gt;'
        htmlAdd += '                                &amp;lt;label class="form-label" for="password"&amp;gt;Password&amp;lt;/label &amp;gt; '
        htmlAdd += '                            &amp;lt;/div&amp;gt;'
        htmlAdd += '                        &amp;lt;/div&amp;gt;'
        htmlAdd += '                    &amp;lt;/div&amp;gt;'
        htmlAdd += '                    &amp;lt;div class="mt-4 pt-2"&amp;gt;'
        htmlAdd += '                        &amp;lt;input class="btn btn-primary btn-lg" type="submit" value="Login" id="btnLogin" /&amp;gt;'
        htmlAdd += '                        &amp;lt;input class="btn btn-primary btn-lg" type="submit" value="Register" id="goToRegister" /&amp;gt;'
        htmlAdd += '                    &amp;lt;/div&amp;gt;'
        htmlAdd += '                &amp;lt;/form&amp;gt;'
        htmlAdd += '            &amp;lt;/div&amp;gt;'
        htmlAdd += '        &amp;lt;/div&amp;gt;'
        htmlAdd += '    &amp;lt;/div&amp;gt;'
        htmlAdd += '&amp;lt;/div&amp;gt;'

        $('.container-fluid').html(htmlAdd)
    }

    generateRegisterForm() {
        var htmlAdd = ''

        htmlAdd += '&amp;lt;div class="row justify-content-center align-items-center h-100"&amp;gt;'
        htmlAdd += '    &amp;lt;div class="col-12 col-lg-9 col-xl-7"&amp;gt;'
        htmlAdd += '        &amp;lt;div class="card shadow-2-strong card-registration" style="border-radius: 15px"&amp;gt;'
        htmlAdd += '            &amp;lt;div class="card-body p-4 p-md-5"&amp;gt;'
        htmlAdd += '                &amp;lt;h3 class="mb-4 pb-2 pb-md-0 mb-md-5"&amp;gt;Registration Form&amp;lt;/h3&amp;gt;'
        htmlAdd += '                &amp;lt;form id="registrationForm"&amp;gt;'
        htmlAdd += '                    &amp;lt;div class="row"&amp;gt;'
        htmlAdd += '                        &amp;lt;div class="col-md-6 mb-4"&amp;gt;'
        htmlAdd += '                            &amp;lt;div class="form-outline"&amp;gt;'
        htmlAdd += '                                &amp;lt;input type="text" id="name" class="form-control form-control-lg" /&amp;gt;'
        htmlAdd += '                                &amp;lt;label class="form-label" for="name"&amp;gt;Name&amp;lt;/label&amp;gt;'
        htmlAdd += '                            &amp;lt;/div&amp;gt;'
        htmlAdd += '                        &amp;lt;/div&amp;gt;'
        htmlAdd += '                        &amp;lt;div class="col-md-6 mb-4"&amp;gt;'
        htmlAdd += '                            &amp;lt;div class="form-outline"&amp;gt;'
        htmlAdd += '                                &amp;lt;input type="email" id="email" class="form-control form-control-lg" /&amp;gt;'
        htmlAdd += '                                &amp;lt;label class="form-label" for="email"&amp;gt;Email&amp;lt;/label&amp;gt;'
        htmlAdd += '                            &amp;lt;/div&amp;gt;'
        htmlAdd += '                        &amp;lt;/div&amp;gt;'
        htmlAdd += '                    &amp;lt;/div&amp;gt;'
        htmlAdd += '                    &amp;lt;div class="row"&amp;gt;'
        htmlAdd += '                        &amp;lt;div class="col-md-6 mb-4 pb-2"&amp;gt;'
        htmlAdd += '                            &amp;lt;div class="form-outline"&amp;gt;'
        htmlAdd += '                                &amp;lt;input type="password" id="password" class="form-control form-control-lg" /&amp;gt;'
        htmlAdd += '                                &amp;lt;label class="form-label" for="password"&amp;gt;Password&amp;lt;/label&amp;gt;'
        htmlAdd += '                            &amp;lt;/div&amp;gt;'
        htmlAdd += '                        &amp;lt;/div&amp;gt;'
        htmlAdd += '                    &amp;lt;/div&amp;gt;'
        htmlAdd += '                    &amp;lt;div class="mt-4 pt-2"&amp;gt;'
        htmlAdd += '                        &amp;lt;input class="btn btn-primary btn-lg" type="submit" value="Register" id="btnRegister" /&amp;gt;'
        htmlAdd += '                        &amp;lt;input class="btn btn-primary btn-lg" type="submit" value="Login" id="goToLogin" /&amp;gt;'
        htmlAdd += '                    &amp;lt;/div&amp;gt;'
        htmlAdd += '                &amp;lt;/form&amp;gt;'
        htmlAdd += '            &amp;lt;/div&amp;gt;'
        htmlAdd += '        &amp;lt;/div&amp;gt;'
        htmlAdd += '    &amp;lt;/div&amp;gt;'
        htmlAdd += '&amp;lt;/div&amp;gt;'

        $('.container-fluid').html(htmlAdd)

    }

    addCard(element) {
        var htmlAdd = ''

        htmlAdd += '&amp;lt;div class="card w-50 rounded-2 mt-3"&amp;gt;'
        htmlAdd += '    &amp;lt;div class="card-body"&amp;gt;'
        htmlAdd += '        &amp;lt;h5 class="card-title"&amp;gt;' + element.todos_name + '&amp;lt;/h5&amp;gt;'
        htmlAdd += '    &amp;lt;/div&amp;gt;'
        htmlAdd += '&amp;lt;/div&amp;gt;'

        return htmlAdd
    }

    changePage() {
        $("#goToLogin").on('click', (e) =&amp;gt; {
            this.login()
        })

        $("#goToRegister").on('click', (e) =&amp;gt; {
            this.register()
        })
    }

    register() {

        // Register User
        let registerObj = {
            userId: ID.unique(),
            email: '',
            password: '',
            name: ''
        }

        this.generateRegisterForm()

        $('#email').on('input', function (el) {
            registerObj.email = el.target.value
        })

        $('#password').on('input', function (el) {
            registerObj.password = el.target.value
        })

        $('#name').on('input', function (el) {
            registerObj.name = el.target.value
        })

        $("#btnRegister").on('click', (e) =&amp;gt; {
            e.preventDefault()

            if (registerObj.email != '' &amp;amp;&amp;amp; registerObj.password != '' &amp;amp;&amp;amp; registerObj.name != '') {
                const promise = this.account.create(registerObj.userId, registerObj.email, registerObj.password, registerObj.name)

                promise.then((response) =&amp;gt; {
                    this.login()
                }, function (error) {
                    console.log(error) // Failure
                })

            } else {
                console.log('registerObj is emptyy!')
            }
        })

        this.changePage()
    }

    login() {

        let loginObj = {
            email: '',
            password: ''
        }

        console.log(loginObj.email, loginObj.password)

        this.generateLoginForm()

        $('#login_email').on('input', function (el) {
            loginObj.email = el.target.value
        })

        $('#login_password').on('input', function (el) {
            loginObj.password = el.target.value
        })

        $("#btnLogin").on('click', (e) =&amp;gt; {
            e.preventDefault()

            if (loginObj.email != '' &amp;amp;&amp;amp; loginObj.password != '') {

                const promise = this.account.createEmailSession(loginObj.email, loginObj.password)

                promise.then((response) =&amp;gt; {
                    // save data to local storage
                    sessionStorage.setItem("appWriteUser", JSON.stringify(response))
                    this.todoFunc()
                }, function (error) {
                    console.log(error) // Failure
                })
            } else {
                console.log('loginObj is emptyy!')
            }

        })

        this.changePage()
    }

    todoFunc() {
        let todos_name = ''

        this.generateTodo()

        // if user click log out btn, clear local storage
        $("#btnLogout").on('click', (e) =&amp;gt; {
            sessionStorage.removeItem("appWriteUser")
            this.login()
        })

        $('#todoinput').on('input', function (el) {
            todos_name = el.target.value
        })

        $("#btnAdd").on('click', (e) =&amp;gt; {
            const promise = this.databases.createDocument(
                process.env.DB_ID,
                process.env.DB_COLLECTION_ID,
                ID.unique(),
                {
                    todos_name: todos_name
                }
            )

            promise.then((response) =&amp;gt; {
                $('#no-data').remove()
                $('#card-group').append(this.addCard(response))
            }, function (error) {
                console.log(error) // Failure
            })
        })
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  template.html
&lt;/h3&gt;

&lt;p&gt;create a new file template.html under &lt;code&gt;/src&lt;/code&gt; folder, here is your html template that will be rendered, here I use &lt;a href="https://getbootstrap.com/docs/5.0/getting-started/introduction/"&gt;bootstrap v5&lt;/a&gt; as CSS.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;

&amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8"&amp;gt;
    &amp;lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&amp;gt;
    &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&amp;gt;
    &amp;lt;title&amp;gt;Document&amp;lt;/title&amp;gt;
    &amp;lt;link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous"&amp;gt;
&amp;lt;/head&amp;gt;

&amp;lt;body&amp;gt;

    &amp;lt;div class="container-fluid p-3 position-relative"&amp;gt;

        &amp;lt;div class="row justify-content-center align-items-center h-100"&amp;gt;
            &amp;lt;div class="col-12 col-lg-9 col-xl-7"&amp;gt;
                &amp;lt;div class="card shadow-2-strong card-registration" style="border-radius: 15px;"&amp;gt;
                    &amp;lt;div class="card-body p-4 p-md-5"&amp;gt;
                        &amp;lt;h3 class="mb-4 pb-2 pb-md-0 mb-md-5"&amp;gt;Registration Form&amp;lt;/h3&amp;gt;
                        &amp;lt;form id="registrationForm"&amp;gt;

                            &amp;lt;div class="row"&amp;gt;
                                &amp;lt;div class="col-md-6 mb-4"&amp;gt;
                                    &amp;lt;div class="form-outline"&amp;gt;
                                        &amp;lt;input type="text" id="name" class="form-control form-control-lg" /&amp;gt;
                                        &amp;lt;label class="form-label" for="name"&amp;gt;Name&amp;lt;/label&amp;gt;
                                    &amp;lt;/div&amp;gt;

                                &amp;lt;/div&amp;gt;
                                &amp;lt;div class="col-md-6 mb-4"&amp;gt;

                                    &amp;lt;div class="form-outline"&amp;gt;
                                        &amp;lt;input type="email" id="email" class="form-control form-control-lg" /&amp;gt;
                                        &amp;lt;label class="form-label" for="email"&amp;gt;Email&amp;lt;/label&amp;gt;
                                    &amp;lt;/div&amp;gt;
                                &amp;lt;/div&amp;gt;
                            &amp;lt;/div&amp;gt;

                            &amp;lt;div class="row"&amp;gt;
                                &amp;lt;div class="col-md-6 mb-4 pb-2"&amp;gt;
                                    &amp;lt;div class="form-outline"&amp;gt;
                                        &amp;lt;input type="password" id="password" class="form-control form-control-lg" /&amp;gt;
                                        &amp;lt;label class="form-label" for="password"&amp;gt;Password&amp;lt;/label&amp;gt;
                                    &amp;lt;/div&amp;gt;

                                &amp;lt;/div&amp;gt;
                            &amp;lt;/div&amp;gt;


                            &amp;lt;div class="mt-4 pt-2"&amp;gt;
                                &amp;lt;input class="btn btn-primary btn-lg" type="submit" value="Register" id="btnRegister" /&amp;gt;
                                &amp;lt;input class="btn btn-primary btn-lg" type="submit" value="Login" id="btnLogin" /&amp;gt;
                            &amp;lt;/div&amp;gt;

                        &amp;lt;/form&amp;gt;
                    &amp;lt;/div&amp;gt;
                &amp;lt;/div&amp;gt;
            &amp;lt;/div&amp;gt;
        &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;

&amp;lt;/body&amp;gt;

&amp;lt;script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/js/bootstrap.bundle.min.js"
    integrity="sha384-OERcA2EqjJCMA+/3y+gxIOqMEjwtxJY7qPCqsdltbNJuaOe923+mo//f6V8Qbsw3"
    crossorigin="anonymous"&amp;gt;&amp;lt;/script&amp;gt;

&amp;lt;script src="https://code.jquery.com/jquery-3.6.1.min.js"&amp;gt;&amp;lt;/script&amp;gt;

&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is the final folder structure:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_UpyN-8a--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/54nht1sjnb1phau29s6q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_UpyN-8a--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/54nht1sjnb1phau29s6q.png" alt="Image description" width="292" height="382"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After everything completed, you can run &lt;code&gt;npm run dev&lt;/code&gt; to start the app.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>appwritehack</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
