Ignore:
Timestamp:
01/03/24 00:00:07 (5 months ago)
Author:
gjoko kostadinov <gjokokostadinov@…>
Branches:
master
Children:
53765dd
Parents:
77205be
Message:

Add all bug fixes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/main/resources/static/js/business_admin.js

    r77205be r1413ee2  
    22    var business = {};
    33
    4     getBusinessInfo(business);
     4    getBusinessInfo().then(function (data) {
     5        business = data;
     6    });
    57
    68    getAppointments();
     
    3335    $("#update_services_button").click(function () {
    3436        businesses = [];
    35         console.log("Gjoko");
    3637        servicesObj = [];
    3738        $.each($('#predefined_services_admin_panel input:checked').siblings(), function (index, label) {
     
    4344            var price = $($($(label).parent()).siblings()[1]).children()[0].value;
    4445
    45             var serviceType = {}
     46            var serviceType = {};
    4647            if (parseInt(id) != -1) {
    4748                service['id'] = parseInt(id);
     
    5657            servicesObj.push(service);
    5758        });
    58         business['services'] = servicesObj;
    59 
    60         businesses.push(business);
    61         console.log(JSON.stringify(businesses));
    62 
    63         $.ajax({
    64             url: "http://localhost:8080/api/business",
    65             type:"PATCH",
    66             data: JSON.stringify(businesses),
    67             contentType:"application/json; charset=utf-8",
    68             dataType: 'text',
    69             success: function(succ){
    70                 alert( "Updates applied successfully" );
    71             },
    72             error: function(err) {
    73                 alert(err);
    74             }
    75         });
     59
     60        var servicesForDelete=[];
     61        $.each($('#predefined_services_admin_panel input:not(:checked)').siblings(), function (index, label) {
     62            let id = $(label).prop('for');
     63            let foundService = business['services'].find(obj => obj.id === parseInt(id));
     64            if (foundService !== undefined && foundService['serviceStatus'] === 'ACTIVE') {
     65                let service1 = {};
     66                service1['id'] = id;
     67                servicesForDelete.push(service1);
     68            }
     69        });
     70
     71
     72        updateServices(servicesObj).then(function (response) {
     73            getBusinessInfo().then(function (data) {
     74                business = data;
     75            });
     76        });
     77
     78        if (servicesForDelete.length > 0) {
     79            deleteServices(servicesForDelete).then(function (response) {
     80                getBusinessInfo().then(function (data) {
     81                    business = data;
     82                });
     83            });
     84        }
     85
    7686        event.preventDefault();
    7787    });
     
    7989    $("#update_owner_button").click(function() {
    8090        businesses = [];
    81         business['owner']['firstName'] = $('#firstName').val();
    82         business['owner']['lastName'] = $('#lastName').val();
    83         business['owner']['email'] = $('#email').val();
    84         business['owner']['phoneNumber'] = $('#phoneNumber').val();
    85         business['owner']['username'] = $('#username').val();
     91        owner={};
     92        owner['firstName'] = $('#firstName').val();
     93        owner['lastName'] = $('#lastName').val();
     94        owner['email'] = $('#email').val();
     95        owner['phoneNumber'] = $('#phoneNumber').val();
     96        owner['username'] = $('#username').val();
     97
     98        business['owner'] = owner;
    8699
    87100        businesses.push(business);
    88         console.log(JSON.stringify(businesses));
    89 
    90         $.ajax({
    91             url: "http://localhost:8080/api/business",
    92             type:"PATCH",
    93             data: JSON.stringify(businesses),
    94             contentType:"application/json; charset=utf-8",
    95             dataType: 'text',
    96             success: function(succ){
    97                 alert( "Updates applied successfully" );
    98             },
    99             error: function(err) {
    100                 alert(err);
    101             }
     101
     102        updateBusinessInfo(businesses).then(function() {
     103            getBusinessInfo().then(function (found) {
     104                business = found;
     105            });
    102106        });
    103107    });
    104108    event.preventDefault();
    105109});
     110
     111function updateBusinessInfo(businessList) {
     112    return $.ajax({
     113        url: "http://localhost:8080/api/business",
     114        type:"PATCH",
     115        data: JSON.stringify(businessList),
     116        contentType:"application/json; charset=utf-8",
     117        dataType: 'text',
     118        success: function(succ){
     119            alert( "Updates applied successfully" );
     120        },
     121        error: function(err) {
     122            alert(err);
     123        }
     124    });
     125}
     126
     127function updateServices(serviceList) {
     128    return $.ajax({
     129        url: "http://localhost:8080/api/service",
     130        type:"PATCH",
     131        data: JSON.stringify(serviceList),
     132        contentType:"application/json; charset=utf-8",
     133        dataType: 'text',
     134        success: function(succ){
     135            alert( "Updates applied successfully" );
     136        },
     137        error: function(err) {
     138            alert(err);
     139        }
     140    });
     141}
     142
     143function deleteServices(services) {
     144    return $.ajax({
     145        url: "http://localhost:8080/api/service/delete",
     146        type:"POST",
     147        data: JSON.stringify(services),
     148        contentType:"application/json; charset=utf-8",
     149        dataType: 'text',
     150        success: function(succ){
     151            console.log("services deleted successfully");
     152        },
     153        error: function(err) {
     154            console.error(err);
     155        }
     156    });
     157}
    106158
    107159function cancelAppointment(appointmentId) {
     
    123175        url: "http://localhost:8080/api/appointment/future/me"
    124176    }).then(function (data) {
    125         console.log(data);
    126177        var $el = $("#bookings-table-body");
    127178        $el.empty();
     
    154205}
    155206
    156 function getBusinessInfo(business) {
    157     $.ajax({
     207function getBusinessInfo() {
     208    return $.ajax({
    158209        url: "http://localhost:8080/api/business/me"
    159     }).then(function (data) {
    160         business = data;
     210    }).then(function (business) {
    161211        var $header = $("#header");
    162212
    163213        // header
    164         $header.text($header.text() + " " + business["owner"]['firstName'] + " " + business['owner']['lastName']);
     214        $header.text("Welcome back " + business["owner"]['firstName'] + " " + business['owner']['lastName']);
    165215
    166216        // business info
     
    181231        var $el = $("#predefined_services_admin_panel");
    182232        $el.empty();
     233
    183234        $.each(business['services'], function (index, obj) {
    184             $el.append(
    185                 '<div class=\"form-outline mb-4\">' +
     235
     236            var element = '<div class=\"form-outline mb-4\">' +
    186237                '    <div class="row">' +
    187238                '        <div class="col-md-6">\n' +
    188                 '            <input class="form-check-input" type="checkbox" checked value=\"' + obj.id + '\" id=\"' + obj.id + '\">\n' +
     239                '            <input class="form-check-input" type="checkbox" ';
     240
     241            if (obj['serviceStatus'] === 'ACTIVE') {
     242                element += 'checked';
     243            }
     244
     245            element += ' value=\"' + obj.id + '\" id=\"' + obj.id + '\">\n' +
    189246                '            <label class="form-check-label" for=\"' + obj.id + '\">\n' + obj['serviceType'].name + '</label>\n' +
    190247                '        </div>' +
     
    196253                '        </div>' +
    197254                '    </div>' +
    198                 '</div>');
    199         });
    200     });
    201 }
     255                '</div>';
     256
     257            $el.append(element);
     258        });
     259        return business;
     260    });
     261}
Note: See TracChangeset for help on using the changeset viewer.