source: src/main/resources/static/js/business_admin.js@ 950fa0d

Last change on this file since 950fa0d was 950fa0d, checked in by Gjoko Kostadinov <gjoko.kostadinov@…>, 14 months ago

Periodic update

  • Property mode set to 100644
File size: 5.9 KB
Line 
1$(document).ready(function() {
2
3 var business = {};
4
5 $.ajax({
6 url: "http://localhost:8080/api/business/me"
7 }).then(function (data) {
8 business = data;
9 var $header = $("#header");
10
11 // header
12 $header.text($header.text() + " " + business["owner"]['firstName'] + " " + business['owner']['lastName']);
13
14 // business info
15 if(business['businessStatus'] == 'NEW') {
16 $('#new_business_warning').removeAttr("hidden");
17 }
18 $('#business_status').val(business['businessStatus']);
19 $('#business_type').val(business['businessType']['text']);
20
21 // owner info
22 $('#firstName').val(business['owner']['firstName']);
23 $('#lastName').val(business['owner']['lastName']);
24 $('#email').val(business['owner']['email']);
25 $('#username').val(business['owner']['username']);
26
27 // services info
28 var $el = $("#predefined_services_admin_panel");
29 $el.empty();
30 $.each(business['services'], function (index, obj) {
31 $el.append(
32 '<div class=\"form-outline mb-4\">' +
33 ' <div class="row">' +
34 ' <div class="col-md-6">\n' +
35 ' <input class="form-check-input" type="checkbox" checked value=\"' + obj.id + '\" id=\"' + obj.id + '\">\n' +
36 ' <label class="form-check-label" for=\"' + obj.id + '\">\n' +
37 obj['serviceType'].name +
38 ' </label>\n' +
39 ' </div>' +
40 ' <div class=\"form-outline col-md-2 d-grid\">' +
41 ' <input type=\"text\" id=\"' + obj.id + obj['serviceType'].name.replace(/\s/g, "") + "duration" + '\" class=\"form-control\" placeholder="time" value=\"' + obj.duration + '" />' +
42 ' </div>' +
43 ' <div class=\"form-outline col-md-2 d-grid\">' +
44 ' <input type=\"text\" id=\"' + obj.id + obj['serviceType'].name.replace(/\s/g, "") + "price" + '\" class=\"form-control\" placeholder="price" value=\"' + obj.price + '" />' +
45 ' </div>' +
46 ' </div>' +
47 '</div>');
48 });
49 });
50
51 $("#add_service").click(function () {
52 var input_service = $("#input_service").val();
53 // clear the input
54 $("#input_service").val('');
55
56 $("#predefined_services_admin_panel").append(
57 '<div class="form-outline mb-4">' +
58 ' <div class="row">' +
59 ' <div class="col-md-6">\n' +
60 ' <input class="form-check-input" type="checkbox" checked value="' + -1 + '" id="' + -1 + '">\n' +
61 ' <label class="form-check-label" for="' + -1 + '">\n' +
62 input_service +
63 ' </label>\n' +
64 ' </div>' +
65 ' <div class="form-outline col-md-2 d-grid">' +
66 ' <input type="text" id="' + -1 + input_service.replace(/\s/g, "") + "duration" + '" class="form-control" placeholder="time" />' +
67 ' </div>' +
68 ' <div class="form-outline col-md-2 d-grid">' +
69 ' <input type="text" id="' + -1 + input_service.replace(/\s/g, "") + "price"+ '" class="form-control" placeholder="price" />' +
70 ' </div>' +
71 ' </div>' +
72 '</div>');
73 event.preventDefault();
74 });
75
76 $("#update_services_button").click(function () {
77 businesses = [];
78 console.log("Gjoko");
79 servicesObj = [];
80 $.each($('#predefined_services_admin_panel input:checked').siblings(), function (index, label) {
81
82 let service = {};
83 var id = $(label).prop('for');
84 var text = $(label).text();
85 var time = $($($(label).parent()).siblings()[0]).children()[0].value;
86 var price = $($($(label).parent()).siblings()[1]).children()[0].value;
87
88 var serviceType = {}
89 if (parseInt(id) != -1) {
90 service['id'] = parseInt(id);
91 serviceType['id'] = business['services'].find(obj => obj.id === parseInt(id))['serviceType']['id'];
92 } else {
93
94 }
95 serviceType['name'] = text.trim();
96 service['serviceType'] = serviceType;
97 service['duration'] = parseInt(time);
98 service['price'] = parseInt(price);
99 servicesObj.push(service);
100 });
101 business['services'] = servicesObj;
102
103 businesses.push(business);
104 console.log(JSON.stringify(businesses));
105
106 $.ajax({
107 url: "http://localhost:8080/api/business",
108 type:"PATCH",
109 data: JSON.stringify(businesses),
110 contentType:"application/json; charset=utf-8",
111 dataType: 'text',
112 success: function(succ){
113 alert( "Updates applied successfully" );
114 },
115 error: function(err) {
116 alert(err);
117 }
118 });
119 event.preventDefault();
120 });
121
122 $("#update_owner_button").click(function() {
123 businesses = [];
124 console.log("Gjoko");
125 business['owner']['firstName'] = $('#firstName').val();
126 business['owner']['lastName'] = $('#lastName').val();
127 business['owner']['email'] = $('#email').val();
128 business['owner']['username'] = $('#username').val();
129
130 businesses.push(business);
131 console.log(JSON.stringify(businesses));
132
133 $.ajax({
134 url: "http://localhost:8080/api/business",
135 type:"PATCH",
136 data: JSON.stringify(businesses),
137 contentType:"application/json; charset=utf-8",
138 dataType: 'text',
139 success: function(succ){
140 alert( "Updates applied successfully" );
141 },
142 error: function(err) {
143 alert(err);
144 }
145 });
146 });
147 event.preventDefault();
148});
Note: See TracBrowser for help on using the repository browser.