Skip to content

Commit 60cff0c

Browse files
generalgeneral
authored andcommitted
changed to functions
1 parent 46c7280 commit 60cff0c

File tree

1 file changed

+52
-105
lines changed

1 file changed

+52
-105
lines changed
Lines changed: 52 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11

22
<div class="col py-1">
3-
<section>
4-
<div id="contain-modal">
3+
<!-- <section>
4+
<div id="contain-modal"> -->
55
<!-- HERE IS THE DIALOG PLACE -->
6+
<!-- </div> -->
7+
<!-- </section> -->
8+
<section>
9+
<div id="alert-errors">
10+
<!-- HERE IS THE NOTIFICATION -->
611
</div>
712
</section>
813
<section>
@@ -14,7 +19,6 @@
1419
<div class="contain-table">
1520
<!-- HERE IS THE DATA DISPLAY -->
1621
<?php
17-
1822
$htmldisplaymessageapibutton = 'wait for the system to get the coin rates from the internet.';
1923
if($active)
2024
{
@@ -23,8 +27,6 @@
2327
echo form_button('button-call', $buttonicon, 'class="btn btn-outline-success" id="button-call"');
2428
echo div_close();
2529
}
26-
27-
2830
$htmldatadisplaymycurrencies = 'There is no data today, yet ..'.$htmldisplaymessageapibutton;
2931
if(is_array($currency_list_dbarraypre) )
3032
{
@@ -36,7 +38,6 @@
3638
$htmldatadisplaymycurrencies = $this->table->generate($currency_list_dbarraypre);
3739
}
3840
}
39-
4041
$htmldatadisplayallcurrencies = 'The data of world currency rates today is not yet present.. '.$htmldisplaymessageapibutton;
4142
if(is_array($currency_list_dbarraynow))
4243
{
@@ -48,17 +49,14 @@
4849
$htmldatadisplayallcurrencies = $this->table->generate($currency_list_dbarraynow);
4950
}
5051
}
51-
5252
echo heading('Your preferred currency rates',2,'style="text-align: center"');
5353
echo div_open('');
5454
echo '<section>'.$htmldatadisplaymycurrencies.'</section>';
5555
echo div_close();
56-
5756
echo heading('Your today all currency rates',2,'style="text-align: center"');
5857
echo div_open('');
5958
echo '<section>'.$htmldatadisplayallcurrencies.'</section>';
6059
echo div_close();
61-
6260
?>
6361
</div>
6462
</section>
@@ -68,43 +66,33 @@
6866
let uricall = baseUrl + '/Currency_Api';
6967
let user_st = "<?php echo $active; ?>";
7068
let user_id = "<?php $user_id = 'gonzalez_angel'; echo $user_id; ?>";
71-
69+
let tableId = '#table_id';
70+
let tableId2 = '#table_id2';
71+
let urlUpdateRateAmount = '/updateRateAmount'
7272
$(document).ready( function ()
7373
{
74-
75-
table1 = $('#table_id').DataTable (
76-
{
77-
order: [0], paging:false, searching:false,
78-
columnDefs: [ {title:'code',targets:0},{title:'base',targets:1},{title:'rate',targets:2},{title:'currency',targets:3} ]
79-
}
80-
);
81-
82-
table2 = $('#table_id2').DataTable(
74+
// CREATE TABLE WITH DATATABLE
75+
function createDataTable(id){
76+
let table = $(id).DataTable(
8377
{
8478
order: [0], paging:true, searching:true,
8579
columnDefs: [ {title:'code',targets:0},{title:'base',targets:1},{title:'rate',targets:2},{title:'currency',targets:3} ]
86-
}
87-
);
88-
89-
if(user_st)
90-
{
91-
92-
/* table today prefered currencies data click on amount */
93-
94-
$('#table_id tbody').on('click', 'tr', function ()
80+
});
81+
return table
82+
}
83+
let table1 = createDataTable(tableId)
84+
let table2 = createDataTable(tableId2)
85+
// EVENT TABLE EDIT
86+
function addEventClickTable(id,table,url){
87+
$(id+' tbody').on('click', 'tr', function ()
9588
{
96-
var data = table1.row(this).data()
97-
89+
var data = table.row(this).data()
9890
alert(data,'primary')
99-
10091
let buttonEditData = document.getElementById("edit");
10192
let messageError = document.getElementById("error-message");
102-
10393
buttonEditData.addEventListener("click",function(event)
10494
{
105-
10695
event.preventDefault()
107-
10896
let codTasa = document.getElementById("cod_tasa");
10997
let monTasaMoneda = document.getElementById("mon_tasa_moneda");
11098
let object = {
@@ -114,10 +102,9 @@
114102
mon_tasa_moneda:monTasaMoneda.value
115103
}
116104
let button = document.getElementById('edit')
117-
118105
$.ajax({
119106
type: 'post',
120-
url: uricall + '/updateRateAmount',
107+
url: uricall + url,
121108
data: object,
122109
success: function(result)
123110
{
@@ -149,69 +136,15 @@
149136
});
150137
})
151138
});
152-
153-
/* table today all currencies data click on amount */
154-
155-
$('#table_id2 tbody').on('click', 'tr', function ()
156-
{
157-
var data = table2.row(this).data()
158-
alert(data,'primary')
159-
160-
let buttonEditData = document.getElementById("edit");
161-
let messageError = document.getElementById("error-message");
162-
163-
buttonEditData.addEventListener("click",function(event)
164-
{
165-
event.preventDefault()
166-
let codTasa = document.getElementById("cod_tasa");
167-
let monTasaMoneda = document.getElementById("mon_tasa_moneda");
168-
let object = {
169-
method:'post',
170-
user_id:user_id,
171-
cod_tasa:codTasa.value,
172-
mon_tasa_moneda:monTasaMoneda.value
173-
}
174-
let button = document.getElementById('edit')
175-
176-
$.ajax({
177-
type: 'post',
178-
url: uricall + '/updateRateAmount',
179-
data: object,
180-
success: function(result)
181-
{
182-
let answer = result.split('\n')
183-
answer = JSON.parse(answer[0])
184-
if(answer.result !== 1)
185-
{
186-
button.innerHTML = ['<i class="bi bi-x-octagon-fill" style="color: red;font-size: 25px;"></i>',]
187-
messageError.innerHTML=`${answer.result}`
188-
}
189-
else
190-
{
191-
button.innerHTML ='<i class="bi bi-check-circle" style="font-size: 25px;"></i>';
192-
button.addEventListener('click',function()
193-
{
194-
$('.alert').alert('close')
195-
location.reload()
196-
})
197-
}
198-
},
199-
error: function(result)
200-
{
201-
button.innerHTML = '<i class="bi bi-x-octagon-fill" style="color: red;font-size: 25px;"></i>'
202-
button.addEventListener('click',function()
203-
{
204-
$('.alert').alert('close')
205-
})
206-
}
207-
});
208-
})
209-
});
210-
139+
}
140+
if(user_st)
141+
{
142+
/* table today prefered currencies data click on amount */
143+
addEventClickTable(tableId,table1,urlUpdateRateAmount)
144+
/* table today all currencies data click on amount */
145+
addEventClickTable(tableId2,table2,urlUpdateRateAmount)
211146
}
212-
213147
let buttonGetData = document.getElementById("button-call");
214-
215148
buttonGetData.addEventListener('click',function()
216149
{
217150
let object = {
@@ -221,13 +154,17 @@
221154
}
222155

223156
buttonGetData.innerHTML=['<div class="spinner-border" role="status">','<span class="visually-hidden">Loading...</span>','</div>']
224-
225157
$.ajax({
226-
type: 'get',
158+
type: 'post',
227159
url: uricall + '/callApisAndSaveDB',
228160
data: object,
229161
success: function(result)
230162
{
163+
let answer = result.split('\n')
164+
answer = JSON.parse(answer[0])
165+
if(answer.result !== 1 ){
166+
notificationError(answer.result,'danger')
167+
}
231168
buttonGetData.innerHTML=['<?php echo $buttonicon; ?>',]
232169
location.reload()
233170
},
@@ -238,17 +175,14 @@
238175
}
239176
});
240177
})
241-
242178
})
243-
244179
const alertPlaceholder = document.getElementById('liveAlertPlaceholder')
245180
const alert = (message, type) =>
246181
{
247182
const wrapper = document.createElement('div')
248-
249183
wrapper.innerHTML =
250184
[
251-
'<div class="alert alert-${type} alert-dismissible text-center custom-alerts" role="alert" style=" position: fixed; width: 55%; left: 30%; top: 30%; z-index: 10000000000;">',
185+
`<div class="alert alert-${type} alert-dismissible text-center custom-alerts" role="alert" style=" position: fixed; width: 55%; left: 30%; top: 30%; z-index: 10000000000;">`,
252186
'<br>',
253187
'<form class="" method="POST" action="<?php echo site_url() ?>/Currency_Manager/updatecurrency" target="_self" id="edit-form">',
254188
'<div class="form-group">',
@@ -266,9 +200,22 @@
266200
'<p id="error-message" style="color: red;font-size: 25px;"></p>',
267201
'</div>'
268202
].join('')
269-
270203
alertPlaceholder.append(wrapper)
271204
}
272-
205+
const alertError = document.getElementById('alert-errors')
206+
const notificationError = (message, type) =>
207+
{
208+
const wrapper = document.createElement('div')
209+
wrapper.innerHTML =
210+
[
211+
`<div class="alert alert-${type} alert-dismissible text-center custom-alerts py-0 px-0" role="alert" style=" position: fixed; width: 55%; left: 30%; top: 1%; z-index: 10000000000;">`,
212+
'<br>',
213+
`<p>${message}</p>`,
214+
'<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>',
215+
'<p id="error-message" style="color: red;font-size: 25px;"></p>',
216+
'</div>'
217+
].join('')
218+
alertError.append(wrapper)
219+
}
273220
</script>
274221

0 commit comments

Comments
 (0)