Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>ToDo</title>
- <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
- <script src="https://unpkg.com/vue/dist/vue.js"></script>
- </head>
- <body>
- <div class="container" id="app">
- <div class="jumbotron text-center">
- <h1>To-do with PHP, API and Vue.JS</h1>
- <hr>
- <div class="col-sm-8 offset-sm-3" id="login-form-block">
- <form class="form-inline" onsubmit="return false">
- <div class="form-group">
- <input type="text" class="form-control" id="userid" placeholder="[email protected]">
- </div>
- <div class="form-group mx-sm-3">
- <input type="password" class="form-control" id="userpw" placeholder="password">
- </div>
- <button id="btn-sign-in" class="btn btn-primary" @click="signIn">Sign-in</button>
- <button id="btn-sign-up" class="btn btn-info ml-2" @click="signUp">Sign-up</button>
- </form>
- </div>
- <a href="#!" id="btn-sign-out" @click="signOut" class="btn btn-dark col-sm-2 float-right">Sign Out</a>
- </div>
- <div id="todo-form">
- <form onsubmit="return false">
- <div class="form-group row">
- <div class="col-sm-8">
- <input type="text" id="todo" class="form-control" placeholder="input your todo here">
- <input type="hidden" id="id" value="">
- <input type="hidden" id="_method" value="POST">
- </div>
- <a href="#!" @click="saveTodo" id="btn-todo" class="btn btn-primary">Add New</a>
- <a href="#!" @click="clearTodo" class="btn btn-success ml-2">Clear</a>
- </div>
- </form>
- </div>
- <div id="sign-up-form">
- <form onsubmit="return false">
- <div class="form-group row">
- <div class="col-sm-3"><input type="text" id="sign-up-name" class="form-control" placeholder="your name"></div>
- <div class="col-sm-3 ml-2"><input type="email" id="sign-up-id" class="form-control" placeholder="your email"></div>
- <div class="col-sm-3 ml-2"><input type="password" id="sign-up-pw" class="form-control" placeholder="your password"></div>
- <a href="#!" @click="registMe" id="btn-todo" class="btn btn-primary ml-2">Join</a>
- <a href="#!" @click="clearMe" class="btn btn-success ml-2">Clear</a>
- </div>
- </form>
- </div>
- <div>
- <div class="card mb-2" v-for="todo in todos">
- <div class="card-header">
- <a :id="todo.id"><strong>{{todo.username}}</strong> #{{todo.id}}</a>
- <a href="#!" class="btn btn-danger btn-sm float-right" @click="removeTodo(todo.id)">Remove</a>
- <a href="#!" class="btn btn-warning btn-sm float-right mr-1" @click="showUpdate(todo.id)">Update</a>
- </div>
- <div class="card-body">
- <blockquote class="blockquote mb-0">
- <p>{{todo.todo}}</p>
- <footer class="blockquote-footer">{{todo.created_at}} / {{todo.userid}}</footer>
- </blockquote>
- </div>
- </div>
- </div>
- </div>
- <script>
- //app.js
- const app = new Vue({
- el: '#app',
- data: {
- todos: [],
- todoSelected: null,
- user: {}
- },
- methods: {
- todoList: function(){
- if( this.user.token ) {
- fetch("./api/todo/" + this.user.token)
- .then(response => response.json())
- .then( json => this.todos = json )
- } else {
- this.todos = [];
- }
- },
- saveTodo: function(event){
- event.preventDefault();
- let todo = document.getElementById("todo").value;
- let id = document.getElementById("id").value;
- let _method = document.getElementById("_method").value;
- let data = {"todo": todo};
- fetch("./api/todo/" + this.user.token + "/" + id,{
- method: _method,
- body: JSON.stringify(data),
- headers: {"Content-type": "application/json; charset=UTF-8"}
- })
- .then( res => res.json() )
- .then( res => {
- if( res.success ) {
- this.todoList();
- if( _method === "PUT" && id ) location.href = "#" + id;
- this.clearTodo();
- } else {
- alert(res.msg);
- }
- } )
- .catch(error => {
- console.error('Error:', error);
- });
- },
- getTodo: async function(id){
- await fetch("./api/todo/"+this.user.token+"/" + id)
- .then(response => response.json())
- .then(json => this.todoSelected = json);
- return this.todoSelected;
- },
- removeTodo: async function(id){
- if( ! id ) return;
- if( ! confirm("Are you want to remove this todo?") ) return;
- await fetch("./api/todo/"+this.user.token+"/" + id,{
- method: "DELETE",
- body: JSON.stringify({}),
- headers: {"Content-type": "application/json; charset=UTF-8"}
- }
- )
- .then(res => res.json())
- .then(res => {
- if( res.success ) {
- this.todoSelected = null;
- this.todoList();
- } else {
- alert(res.msg);
- }
- });
- },
- showUpdate: async function(id){
- let todo = await this.getTodo(id);
- if( ! id ) return;
- document.getElementById("id").value = todo.id;
- document.getElementById("todo").value = todo.todo;
- document.getElementById("todo").style.backgroundColor = "yellow";
- document.getElementById("_method").value = "PUT";
- document.getElementById("btn-todo").text = "Update";
- location.href = "#";
- },
- clearTodo: function(){
- document.getElementById("todo").value = "";
- document.getElementById("todo").value = "";
- document.getElementById("todo").value = "";
- document.getElementById("todo").style.backgroundColor = "white";
- document.getElementById("_method").value = "POST";
- document.getElementById("btn-todo").text = "Add New";
- },
- signIn: function(){
- let userid = document.getElementById("userid").value;
- let userpw = document.getElementById("userpw").value;
- let data = {"userid": userid, "userpw": userpw};
- fetch("./api/user/login",{
- method: "POST",
- body: JSON.stringify(data),
- headers: {"Content-type": "application/json; charset=UTF-8"}
- }
- )
- .then(response => response.json())
- .then(res => {
- if( res.token ) {
- this.user = res;
- localStorage.setItem('user', JSON.stringify(res));
- this.init();
- }
- });
- },
- signOut: function(){
- fetch("./api/user/logout/" + this.user.token, {
- method: "POST",
- body: JSON.stringify({}),
- headers: {"Content-type": "application/json; charset=UTF-8"}
- }
- )
- .then(response => response.json())
- .then(res => {
- if( res.logout ) {
- this.user = {};
- localStorage.setItem('user', '');
- this.init();
- }
- });
- },
- init: async function(){
- let user = localStorage.getItem('user');
- if( user ) this.user = JSON.parse(user);
- else this.user = {};
- document.getElementById("sign-up-form").style.display = "none";
- if( this.user.userid ) {
- document.getElementById("login-form-block").style.display = "none";
- document.getElementById("btn-sign-out").style.display = "block";
- document.getElementById("btn-sign-out").text = this.user.username + " Sign Out";
- document.getElementById("todo-form").style.display = "block";
- } else {
- document.getElementById("login-form-block").style.display = "block";
- document.getElementById("btn-sign-out").style.display = "none";
- document.getElementById("btn-sign-out").text = "Sign Out";
- document.getElementById("todo-form").style.display = "none";
- document.getElementById("userid").value = "";
- document.getElementById("userpw").value = "";
- }
- await this.todoList();
- },
- signUp: function(){
- document.getElementById("sign-up-name").value = "";
- document.getElementById("sign-up-id").value = "";
- document.getElementById("sign-up-pw").value = "";
- document.getElementById("sign-up-form").style.display = "block";
- },
- registMe: function(event){
- event.preventDefault();
- let username = document.getElementById("sign-up-name").value;
- let userid = document.getElementById("sign-up-id").value;
- let userpw = document.getElementById("sign-up-pw").value;
- let _method = "POST";
- let data = {"username": username, "userid": userid, "userpw": userpw};
- fetch("./api/user/regist", {
- method: _method,
- body: JSON.stringify(data),
- headers: {"Content-type": "application/json; charset=UTF-8"}
- })
- .then( res => res.json() )
- .then( res => {
- alert(res.msg);
- if( res.regist ) {
- document.getElementById("sign-up-name").value = "";
- document.getElementById("sign-up-id").value = "";
- document.getElementById("sign-up-pw").value = "";
- document.getElementById("sign-up-form").style.display = "none";
- }
- } )
- .catch(error => {
- console.error('Error:', error);
- });
- },
- clearMe: function(){
- }
- },
- async mounted() {
- await this.init();
- }
- });
- </script>
- </body>
- </html>
Add Comment
Please, Sign In to add comment