Skip to content
Snippets Groups Projects
findorder.jsp 6.46 KiB
Newer Older
Fajar Nugroho's avatar
Fajar Nugroho committed
<%@ page import="utility.CookieChecker" %>
<%@ page import="utility.CookiesMap" %>
<%@ page import="java.util.Map" %><%--
Fajar Nugroho's avatar
Fajar Nugroho committed
  Created by IntelliJ IDEA.
  User: fajar
  Date: 23/11/17
  Time: 0:22
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Fajar Nugroho's avatar
Fajar Nugroho committed
<%
    if (!CookieChecker.check(request.getCookies())) {
        response.sendRedirect("index.jsp");
    }
    Map<String, String> cook = CookiesMap.getCookiesMap(request.getCookies());

%>
<html ng-app="app">
Fajar Nugroho's avatar
Fajar Nugroho committed
<head>
    <title>Find Order</title>
    <link rel="stylesheet" href="css/master.css">
    <link rel="icon" href="img/gojek.png">
    <script src="js/master.js"></script>
    <script src="js/angular.min.js"></script>
Fajar Nugroho's avatar
Fajar Nugroho committed
    <script src="js/chatfordriver.js"></script>
    <script src="https://www.gstatic.com/firebasejs/4.2.0/firebase.js"></script>
    <script>

    </script>
Fajar Nugroho's avatar
Fajar Nugroho committed
</head>
Fajar Nugroho's avatar
Fajar Nugroho committed
<body ng-controller="maincontrol">
Fajar Nugroho's avatar
Fajar Nugroho committed
    <div class="order">
        <%@ include file = "navbar.jsp" %>
        <script type="text/javascript">chooseNavbar(0);</script>
        <h1>LOOKING FOR AN ORDER</h1>
        <br/><br/><br/><br/>
Fajar Nugroho's avatar
Fajar Nugroho committed
        <div class="btn-wrapper" ng-hide="find">
            <button class="green-button" data-ng-click="findOrder();">FIND ORDER</button>
        </div>
        <div class="btn-wrapper" ng-show="cancelFind">
            <h5>Finding Order....</h5>
            <br>
            <br>
            <button class="red-button" data-ng-click="cancelFindOrder();">CANCEL</button>
        </div>
        <div ng-show="chat" >
            <div class="chat-box" >
                <div ng-repeat="msg in msgs" class="{{msg.pos}}">
                    {{msg.text}}
                </div>
            </div>
            <div class="chat-form">
                <form class="" id="chat-text-input">
                    <input type="text" class="chat-input" ng-model="cht" name="chatmessage" placeholder="message"/>
                    <button type="submit" class="green-button" data-ng-click="submit(cht); cht = '';" name="send">SEND</button>
                </form>
            </div>
Fajar Nugroho's avatar
Fajar Nugroho committed
        </div>

Fajar Nugroho's avatar
Fajar Nugroho committed

Fajar Nugroho's avatar
Fajar Nugroho committed
    </div>

</body>
Fajar Nugroho's avatar
Fajar Nugroho committed
<script>
    var app = angular.module("app", []);
    app.controller("maincontrol", ($scope, $http, $window) => {
        $scope.msgs = [];
        $scope.find = false;
        $scope.cancelFind = false;
        $scope.chat = false;
        $scope.custId = null;
        $scope.userId = <%=cook.get("id")%>;

        $http.get("http://localhost:3000/driver/available").then((response) => {
            console.log(response);
            if (response.data.data.indexOf($scope.userId) != -1) {
                enableChat();
                $scope.find = true;
                $scope.cancelFind = true;
        }
        }).catch((err) => {
        console.log(err);
        });

        $scope.submit = (cht) =>{
            if (cht.length != 0) {


                var config = {
                    headers: {
                        'Content-Type': 'application/json'
                    }
                }
                var body = {
                    customer: $scope.custId,
                    driver: $scope.userId,
                    chat: {
                        sender: $scope.userId,
                        title : "pesan",
                        message: cht
                    }
                }
                $http.post('http://localhost:3000/chat/send', body, config)
                    .then((success) => {
                    $scope.msgs.push({pos: "bubble-right", text: cht});
                 })
                .catch((err) => {
                    console.log(err);
               });
            }
        }
        $scope.findOrder = () => {
            var config = {
                headers : {
                    "Content-Type" : "application/json"
                }
            }
            var body = {
                userId : $scope.userId
            }

            $http.post("http://localhost:3000/driver/available", body, config).then((res) => {
                if (res.data.status == 200){
                    enableChat();
                    $scope.find = true;
                    $scope.cancelFind = true;
                } else {
                     console.log("cant add to available driver");
                }
            })
            .catch((err) => {
                console.log(err);
            })

        }
        $scope.inOrder = () => {
            $scope.chat =
            $http.get('http://localhost:3000/chat?customer='+$scope.custId+'&driver='+$scope.userId).then( (res) => {
                for(var i = 0;i < res.data.content.length;i++){
                    if (res.data.content[i].sender == res.data.customer) {
                        $scope.msgs.push({pos: "bubble-right", msg: res.data.content[i].message});
                    } else {
                        $scope.msgs.push({pos: "bubble-left", msg: res.data.content[i].message});
                    }
                }

             }, (err) =>{
                console.error(err);
            });
        }

        $scope.cancelFindOrder = () => {
            var config = {
                headers :{
                    "Content-Type" : "application/json"
                }
            }
            var body = {
                "userId" : $scope.userId
            }
            $http.post("http://localhost:3000/driver/delete", body, config).then((res) => {
                console.log(res);
                if (res.data.status == 200) {
                    $scope.find = false;
                    $scope.cancelFind = false;
                } else {
                    alert("error");
                 }
            })
        }
        $scope.sendTokenToServer = (utoken) => {
            var config = {
                headers : {
                    "Content-Type" : "application/json"
                }
            }
            var body ={
                userId : parseInt($scope.userId),
                token : utoken
            }
            $http.post("http://localhost:3000/user/save", body, config).then((response) => {
                if (response.status == 200){
                    console.log("Send Token Success");
                } else {
                    console.log("Send Token Error");
                }
            }, (err) => {
                console.log(err);
        })

        }

        $scope.insertMsg = (payload) => {
            $scope.msgs.push({pos : "bubble-left", text : payload.notification.body});
            console.log("HEHEH");
        }
    })
</script>
Fajar Nugroho's avatar
Fajar Nugroho committed
</html>