-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
110 lines (100 loc) · 2.8 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
var date = "none";
var photoArr = [];
var noPhotos = false;
var pageNum = 1;
validations();
$('#err').text("Welcome to NASA Rover Photo Finder. Just select date and navigate through photos");
$('#imgbtn').on('click',function(){
validations();
main();
});
$('#nextBtn').on('click',function(){
$('#num').val(++pageNum);
validations();
main();
});
$('#prevBtn').on('click',function(){
$('#num').val(--pageNum);
validations();
main();
});
function main(){
pageNum = $('#num').val();
if($('#date').val()===""){
alert('Please enter date!');
}
else{
$('#img-div').empty();
date = $('#date').val();
$.ajax({
type: "GET",
url: "https://api.nasa.gov/mars-photos/api/v1/rovers/curiosity/photos",
data: {
api_key: "D9MESAn8QQeK7TgKcPrJRB2V6gV37iLF4q5MwpAQ",
earth_date: date
},
success: function (data) {
console.log(data);
photoArr = data.photos;
var newImg;
validations();
for(let i=(pageNum-1)*12;i<pageNum*12;i++){
newImg = $('<img/>',{
src: photoArr[i].img_src,
alt: photoArr[i].id,
"class" : "w-full h-full"
})
$('#img-div').append(newImg);
}
}
}).fail(function () {
console.log('Fetching Failed!');
});
}
}
function displayNoImg(status){
}
function validations(){
if(pageNum==1){
$('#prevBtn').attr('disabled','');
$('#prevBtn').addClass('bg-slate-100 hover:bg-slate-100 text-white')
}
else{
$('#prevBtn').removeAttr('disabled','');
$('#prevBtn').removeClass('bg-slate-100 hover:bg-slate-100 text-white')
}
if(photoArr.length===0){
if($('#err').hasClass("hidden")){
$('#err').text("No data available for this date!")
$('#err').removeClass("hidden");
$('#num').val(Math.ceil(photoArr.length/12));
}
else{
$('#err').text("No data available for this date!");
$('#num').val(1);
}
}
else{
if(Math.ceil(photoArr.length/12)+1<=pageNum){
if($('#err').hasClass("hidden")){
alert('End of the list!');
$('#num').val(Math.ceil(photoArr.length/12));
}
else{
$('#err').text("End of the list!");
$('#num').val() = Math.ceil(photoArr.length/12);
}
}
else{
if($('#err').hasClass("hidden")){
}
else{
$('#err').addClass("hidden")
}
}
}
if(pageNum<1){
alert('Enter valid page number!');
$('#num').val(1);
}
}