1 | $(document).ready(function() {
|
---|
2 | let companies = [];
|
---|
3 |
|
---|
4 | getCompanies().then(function (data) {
|
---|
5 | companies = data;
|
---|
6 | });
|
---|
7 |
|
---|
8 | $('#save_button').click(function () {
|
---|
9 | var companiesToSave = [];
|
---|
10 | $.each($('#table_body tr'), function(index, row) {
|
---|
11 | if((companies[index].businessStatus === "NEW" || companies[index].businessStatus === "DEACTIVATED") && $($($(row).children() [3]).children()[0]).is(':checked')) {
|
---|
12 | let cloneCompany = { ...companies[index] }
|
---|
13 | cloneCompany['businessStatus'] = 'ACTIVE';
|
---|
14 | companiesToSave.push(cloneCompany);
|
---|
15 | }
|
---|
16 | if(companies[index].businessStatus === "ACTIVE" && !$($($(row).children() [3]).children()[0]).is(':checked')) {
|
---|
17 | let cloneCompany = { ...companies[index] }
|
---|
18 | cloneCompany['businessStatus'] = 'DEACTIVATED';
|
---|
19 | companiesToSave.push(cloneCompany);
|
---|
20 | }
|
---|
21 | });
|
---|
22 |
|
---|
23 |
|
---|
24 | $.ajax({
|
---|
25 | url: "http://localhost:8080/api/admin",
|
---|
26 | type:"PATCH",
|
---|
27 | data: JSON.stringify(companiesToSave),
|
---|
28 | contentType:"application/json; charset=utf-8",
|
---|
29 | dataType: 'text',
|
---|
30 | success: function(succ){
|
---|
31 | getCompanies().then(function (data) {
|
---|
32 | companies = data;
|
---|
33 | });
|
---|
34 | alert( "Updates applied successfully" );
|
---|
35 | },
|
---|
36 | error: function(err) {
|
---|
37 | alert(err);
|
---|
38 | }
|
---|
39 | });
|
---|
40 | event.preventDefault();
|
---|
41 | });
|
---|
42 |
|
---|
43 | function getCompanies() {
|
---|
44 | return $.ajax({
|
---|
45 | url: "http://localhost:8080/api/business"
|
---|
46 | }).then(function (data) {
|
---|
47 | var $el = $("#table_body");
|
---|
48 | $("#new_table tbody").html("");
|
---|
49 | $.each(data, function (index, obj) {
|
---|
50 | if(obj.businessStatus == "NEW" || obj.businessStatus == "DEACTIVATED") {
|
---|
51 | $el.append("<tr>\n" +
|
---|
52 | " <th scope=\"row\">" + obj.id + "</th>\n" +
|
---|
53 | " <td>" + obj.companyName + "</td>\n" +
|
---|
54 | " <td>" + obj.owner.firstName + " " + obj.owner.lastName + "</td>\n" +
|
---|
55 | " <td><input class=\"form-check-input\" type=\"checkbox\" value=\"" + obj.id +"\"></td>\n" +
|
---|
56 | " </tr>")
|
---|
57 | }
|
---|
58 | if (obj.businessStatus == "ACTIVE") {
|
---|
59 | $el.append("<tr>\n" +
|
---|
60 | " <th scope=\"row\">" + obj.id + "</th>\n" +
|
---|
61 | " <td>" + obj.companyName + "</td>\n" +
|
---|
62 | " <td>" + obj.owner.firstName + " " + obj.owner.lastName + "</td>\n" +
|
---|
63 | " <td><input class=\"form-check-input\" type=\"checkbox\" checked value=\"" + obj.id +"\"></td>\n" +
|
---|
64 | " </tr>")
|
---|
65 | }
|
---|
66 | });
|
---|
67 | return data;
|
---|
68 | });
|
---|
69 | }
|
---|
70 | }); |
---|