Ignore:
Timestamp:
12/26/23 18:50:43 (6 months ago)
Author:
gjoko kostadinov <gjokokostadinov@…>
Branches:
master
Children:
1413ee2
Parents:
950fa0d
Message:

Add entire code

File:
1 edited

Legend:

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

    • Property mode changed from 100644 to 100755
    r950fa0d r77205be  
    33    var businesses = {};
    44    var date = new Date();
     5    var selectedServices = {};
     6    var events = [];
    57    var d = date.getDate();
    68    var m = date.getMonth();
     
    6971
    7072    $("#companyType").change(function () {
     73        resetRatingCard();
    7174        var selectedVal = $(this).find(':selected').val();
    7275        var selectedObj = businessTypes[selectedVal - 1];
     
    7578        }).then(function (data) {
    7679            businesses = data;
    77             console.log(data);
    7880            var $el = $("#company");
    7981            var $servicesEl = $("#service");
     
    8486                $el.append("<option value=" + obj.id + ">" + obj.companyName + "</option>");
    8587            });
     88
     89            if (data && data.length === 1) {
     90                selectedServices = data[0]["services"];
     91                resetAndAppendServices(selectedServices);
     92            }
    8693        });
    8794    });
    8895
    8996    $("#company").change(function () {
     97        resetRatingCard();
    9098        var selectedVal = $(this).find(':selected').val();
    9199        $.ajax({
    92100            url: "http://localhost:8080/api/appointment/business/" + selectedVal
    93101        }).then(function (data) {
    94             console.log(data);
    95             var $el = $("#service");
    96             emptyDropdown($el);
    97 
    98             var services = businesses.find(item => item.id == selectedVal)['services'];
    99             console.log(services);
    100 
    101             $.each(services, function (index, obj) {
    102                 $el.append("<option value=" + obj.id + ">" + obj.serviceType.name + "</option>");
    103             });
    104         });
    105     });
    106 
     102            // get already stored service from in-memory
     103            selectedServices = businesses.find(item => item.id == selectedVal)['services'];
     104            resetAndAppendServices(selectedServices);
     105        });
     106        $('#calendar').fullCalendar('refetchEvents');
     107    });
     108
     109    $("#service").change(function () {
     110        var selectedVal = $("#service").find(':selected').val();
     111        var service = selectedServices.find(item => item.id == selectedVal);
     112        setRatingCard(service['serviceType']['name'], service['rating'], service['reviewsCount'], selectedVal);
     113    });
     114
     115    $("#startdatetime").change(function() {
     116        var date = new Date(document.getElementById("startdatetime").valueAsDate);
     117        var selectedVal = $("#service").find(':selected').val();
     118        var minutes = selectedServices.find(item => item.id == selectedVal)['duration'];
     119        date.setMinutes(date.getMinutes() + minutes);
     120        document.getElementById("enddatetime").value = date.toISOString().slice(0, 16);
     121    });
    107122
    108123    /* initialize the calendar
    109124    -----------------------------------------------------------------*/
    110 
    111     var calendar =  $('#calendar').fullCalendar({
     125    loadCalendar();
     126
     127    resetRatingCard();
     128
     129    $("#createAppointment").click(function() {
     130        var appointment = {};
     131        var business  = {};
     132        business['id'] = parseInt($("#company").val());
     133        appointment['service'] = {'id': parseInt($("#service").val()), 'business':business};
     134        appointment['startTime'] = $("#startdatetime").val();
     135
     136        $.ajax({
     137            url: "http://localhost:8080/api/appointment",
     138            type:"POST",
     139            data: JSON.stringify(appointment),
     140            contentType:"application/json; charset=utf-8",
     141            dataType: 'text',
     142            success: function(succ){
     143                alert("Successful appointment!");
     144                var companyId = parseInt($("#company").find(':selected').val());
     145                getAppointmentsByBusiness(companyId, events);
     146                destroyCalendar();
     147                loadCalendar(events);
     148            },
     149            error: function(error) {
     150                alert(error.responseText);
     151            }
     152        });
     153    });
     154
     155});
     156
     157document.getElementById("login").addEventListener("click", function(event){
     158   window.location = "/login";
     159});
     160
     161function resetAndAppendServices(services) {
     162    var $el = $("#service");
     163    emptyDropdown($el);
     164
     165    $.each(services, function (index, obj) {
     166        $el.append("<option value=" + obj.id + ">" + obj.serviceType.name + "</option>");
     167    });
     168}
     169
     170function emptyDropdown(element) {
     171    var defaultOption = element.children().get(0);
     172    element.children().remove();
     173    element.append(defaultOption);
     174}
     175
     176/*loadCalendar([{'title': 'Gjoko', 'start': '2023-09-01 01:00:00', 'end': '2023-09-01 01:30:00', 'allDay':false, 'color': 'blue'}])*/
     177
     178function getAppointmentsByBusiness(businessId, events) {
     179    $.ajax({
     180        url: "http://localhost:8080/api/appointment/business/" + businessId,
     181        type:"GET",
     182        contentType:"application/json; charset=utf-8",
     183        dataType: 'text',
     184        success: function(data){
     185            // reset events
     186            events = [];
     187            console.log(data);
     188           $.each(JSON.parse(data), function (index, object) {
     189               var event = {}
     190               event['title'] = object['title'];
     191               event['start'] = object['startTime'].replace('T', ' ');
     192               event['end'] = object['endTime'].replace('T', ' ');
     193               event['allDay'] = false;
     194               event['color'] = 'blue';
     195               events.push(event);
     196           });
     197
     198        },
     199        error: function(error) {
     200            console.log(error.responseText);
     201        }
     202    });
     203}
     204
     205function loadCalendar(events) {
     206    $('#calendar').fullCalendar({
    112207        header: {
    113208            left: 'title',
     
    178273
    179274        },
    180 
    181         events: [
    182             {
    183                 title: 'All Day Event',
    184                 start: new Date(y, m, 1)
    185             },
    186             {
    187                 id: 999,
    188                 title: 'Repeating Event',
    189                 start: new Date(y, m, d-3, 16, 0),
    190                 allDay: false,
    191                 className: 'info'
    192             },
    193             {
    194                 id: 999,
    195                 title: 'Repeating Event',
    196                 start: new Date(y, m, d+4, 16, 0),
    197                 allDay: false,
    198                 className: 'info'
    199             },
    200             {
    201                 title: 'Meeting',
    202                 start: new Date(y, m, d, 10, 30),
    203                 allDay: false,
    204                 className: 'info'
    205             },
    206             {
    207                 title: 'Lunch',
    208                 start: new Date(y, m, d, 12, 0),
    209                 end: new Date(y, m, d, 14, 0),
    210                 allDay: false,
    211                 className: 'info'
    212             },
    213             {
    214                 title: 'Birthday Party',
    215                 start: new Date(y, m, d+1, 19, 0),
    216                 end: new Date(y, m, d+1, 22, 30),
    217                 allDay: false,
    218             },
    219             {
    220                 title: 'Click for Google',
    221                 start: new Date(y, m, 28),
    222                 end: new Date(y, m, 29),
    223                 url: 'http://google.com/',
    224                 className: 'success'
    225             }
    226         ],
    227     });
    228 
    229     $("#createAppointment").click(function() {
    230         var appointment = {};
    231         appointment['business'] = {'id': parseInt($("#company").val())};
    232         appointment['service'] = {'id': parseInt($("#service").val())};
    233         appointment['startTime'] = $("#startdatetime").val();
    234 
    235         $.ajax({
    236             url: "http://localhost:8080/api/appointment",
    237             type:"POST",
    238             data: JSON.stringify(appointment),
    239             contentType:"application/json; charset=utf-8",
    240             dataType: 'text',
    241             success: function(succ){
    242                 console.log('success');
    243             },
    244             error: function(err) {
    245                 console.log(JSON.stringify(err));
    246             }
    247         });
    248     })
    249 
    250 });
    251 
    252 document.getElementById("login").addEventListener("click", function(event){
    253    window.location = "/login";
    254 });
    255 
    256 function emptyDropdown(element) {
    257     var defaultOption = element.children().get(0);
    258     element.children().remove().end().append(defaultOption);
    259 }
     275        events: function (start, end, callback) {
     276            var selectedVal = $("#company").find(':selected').val();
     277            if(!isNaN(parseInt(selectedVal))) {
     278                $.ajax({
     279                    url: "http://localhost:8080/api/appointment/business/" + selectedVal,
     280                    type:"GET",
     281                    contentType:"application/json; charset=utf-8",
     282                    dataType: 'text',
     283                    success: function(data){
     284                        // reset events
     285                        var events = [];
     286                        $.each(JSON.parse(data), function (index, object) {
     287                            var event = {}
     288                            event['title'] = object['title'];
     289                            event['start'] = object['startTime'].replace('T', ' ');
     290                            event['end'] = object['endTime'].replace('T', ' ');
     291                            event['allDay'] = false;
     292                            event['color'] = '#3b71ca';
     293                            event['textColor'] = '#FFFFFF';
     294                            events.push(event);
     295                        });
     296                        // load events
     297                        callback(events);
     298                    },
     299                    error: function(error) {
     300                        console.log(error.responseText);
     301                    }
     302                });
     303            }
     304        },
     305    });
     306}
     307
     308function goToProfile() {
     309    window.location = "/customer_admin";
     310}
     311
     312function destroyCalendar() {
     313    $('#calendar').fullCalendar('destroy');
     314}
     315
     316function resetRatingCard() {
     317    $('#rating-service-name').empty();
     318    $('#rating-value').empty();
     319    $('#rating-count').empty();
     320    $('#reviews-li').remove();
     321}
     322
     323function setRatingCard(name, value, count, serviceId) {
     324    $('#rating-service-name').text(name);
     325    $('#rating-value').text(value);
     326    $('#rating-count').text(count);
     327    $('#reviews-li').remove();
     328    getReviewsForService(serviceId);
     329
     330}
     331
     332function getReviewsForService(serviceId){
     333    $.ajax({
     334        url: "http://localhost:8080/api/review/" + serviceId
     335    }).then(function (data) {
     336        var $el = $("#reviewsModalBody");
     337        $('#reviews-ul').append($('<td class="form-outline mb-4"><button type="button" id="reviews-li" class="btn btn-primary btn-block" data-bs-toggle="modal" data-bs-target="#showReviewsModal">Checkout reviews</button></td>'));
     338        $el.empty();
     339
     340        $.each(data, function (index, obj) {
     341            var element = '<div class="card m-3" style="max-width: 300px; padding: 0;">';
     342            element +=  '<div class="card-header" style="' + generateHeaderStyle(obj['rating']) + '">' + generateStars(obj['rating']) + '</div>';
     343            element += '<ul class="list-group list-group-flush">';
     344            element += '<li class="list-group-item"><i><b>Business:</b></i> ' + obj['businessName'] + '</li>';
     345            element += '<li class="list-group-item"><i><b>Service:</b></i> ' + obj['serviceName'] + '</li>';
     346            element += '<li class="list-group-item"><i><b>Reviewer:</b></i> ' + obj['customerName'] + '</li>';
     347            element += '<li class="list-group-item"><i><b>Comment:</b></i> ' + obj['comment'] + '</li>';
     348            element += '<li class="list-group-item"><small class="text-body-secondary"><i>Created:</i> ' + obj['created'] + '</small></li>';
     349            element += '</ul>';
     350            element += '</div>';
     351
     352            $el.append(element);
     353        });
     354    });
     355}
     356
     357function generateStars(number) {
     358    return '☆'.repeat(number);
     359}
     360
     361function generateHeaderStyle(number) {
     362    var style = '';
     363    switch (number) {
     364        case 5:
     365            style = 'background-color: RGBA(13,110,253,var(--bs-bg-opacity,1)) !important; ';
     366            break;
     367        case 4:
     368            style = 'background-color: RGBA(25,135,84,var(--bs-bg-opacity,1)) !important; ';
     369            break;
     370        case 3:
     371            style = 'background-color: RGBA(108,117,125,var(--bs-bg-opacity,1)) !important; ';
     372            break;
     373        case 2:
     374            style = 'background-color: RGBA(255,193,7,var(--bs-bg-opacity,1)) !important; ';
     375            break;
     376        case 1:
     377            style = 'background-color: RGBA(220,53,69,var(--bs-bg-opacity,1)) !important; ';
     378            break;
     379    }
     380
     381    style += ' color: #fff !important;'
     382    return style;
     383}
Note: See TracChangeset for help on using the changeset viewer.