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