@@ -352,8 +352,9 @@ const createRecipeElement = async (recipe) => {
352
352
const starContainer = document . createElement ( 'div' ) ;
353
353
starContainer . classList . add ( 'rating' ) ;
354
354
355
- const getCustomData = await getSelectedCustomDataOfDashboard ( recipeIndex ) ;
356
- const rate = getCustomData . rate ;
355
+ //const getCustomData = await getSelectedCustomDataOfDashboard(recipeIndex);
356
+ const rate = recipe . rate ;
357
+ console . log ( "Rate değeri: " , rate )
357
358
358
359
console . log ( "Star Info: " , rate ) ;
359
360
@@ -371,7 +372,7 @@ const createRecipeElement = async (recipe) => {
371
372
starContainer . appendChild ( star ) ;
372
373
}
373
374
// Create average star
374
- const generalRateOfRecipe = getCustomData . avgRate ;
375
+ const generalRateOfRecipe = recipe . avgRate ;
375
376
recipeIndex ++ ;
376
377
377
378
const averageRatingSpan = document . createElement ( 'span' ) ;
@@ -399,7 +400,7 @@ const createRecipeElement = async (recipe) => {
399
400
document . body . appendChild ( heartContainer ) ;
400
401
401
402
heartContainer . classList . add ( 'favorite-heart' ) ;
402
- if ( getCustomData . isLiked ) {
403
+ if ( recipe . isLiked ) {
403
404
heartContainer . style . color = 'red' ;
404
405
}
405
406
// heart.onclick = () => toggleFavorite(heart);
@@ -439,7 +440,7 @@ const displayDashboard = async (recipes) => {
439
440
// This fetch method closed in order to reduce usage of database. Open before starting development
440
441
const fetchData = async ( ) => {
441
442
try {
442
- const response = await fetch ( 'https://recipiebeckend.azurewebsites.net/recipes/get-custom-data-dashboard ' ) ;
443
+ const response = await fetch ( 'https://recipiebeckend.azurewebsites.net/recipes/home ' ) ;
443
444
const data = await response . json ( ) ;
444
445
445
446
console . log ( 'Fetched Data:' , data ) ;
@@ -689,7 +690,7 @@ function basicSearch() {
689
690
// return starAndHeartInfoArray;
690
691
// }
691
692
async function getSelectedCustomDataOfDashboard ( index ) {
692
- var apiUrl = 'https://recipiebeckend.azurewebsites.net/recipes/get-custom-data-dashboard ' ;
693
+ var apiUrl = 'https://recipiebeckend.azurewebsites.net/recipes/home ' ;
693
694
const JWTAccessToken = sessionStorage . getItem ( 'accessToken' ) ;
694
695
const response = await fetch (
695
696
apiUrl ,
@@ -711,13 +712,7 @@ async function getSelectedCustomDataOfDashboard(index) {
711
712
if ( contentType && contentType . includes ( 'application/json' ) ) {
712
713
const data = await response . json ( ) ;
713
714
console . log ( "Data: " , data ) ;
714
- // Check if the array has elements and if the specified index is valid
715
- if ( data . length > index ) {
716
- return data [ index ] ;
717
- } else {
718
- console . error ( 'Index out of bounds or empty array' ) ;
719
- return null ; // or handle it according to your application's logic
720
- }
715
+ return data [ index ] ;
721
716
} else {
722
717
// Handle non-JSON response or empty response
723
718
console . error ( 'Invalid or empty JSON response' ) ;
@@ -731,4 +726,138 @@ function openNewTab() {
731
726
732
727
// Open a new tab with the constructed URL
733
728
window . open ( baseURL , '_blank' ) ;
729
+ }
730
+
731
+ // $(document).ready(function() {
732
+ // $('#pagination li a').on('click', function(e) {
733
+ // e.preventDefault();
734
+ // $('#next').on('click', function(e) {
735
+ // e.preventDefault();
736
+ // var currentPage = $('#pagination li.active').index() + 1;
737
+ // console.log("Current page: " , currentPage);
738
+ // var key = currentPage - 1;
739
+ // paging(key);
740
+ // });
741
+ // $('#prev').on('click', function(e) {
742
+ // e.preventDefault();
743
+ // var currentPage = $('#pagination li.active').index() + 1;
744
+ // var key = currentPage - 2;
745
+ // paging(key);
746
+ // });
747
+ // if (!$(this).is('#next, #prev')) {
748
+ // $('#pagination li').removeClass('active');
749
+ // $(this).parent().addClass('active');
750
+ // var key = parseInt($(this).text().trim()) - 1;
751
+ // paging(key);
752
+ // }
753
+ // });
754
+ // });
755
+
756
+ $ ( document ) . ready ( function ( ) {
757
+ var key ; // Declare the key variable outside of the event handlers
758
+
759
+ // Add click event to pagination links
760
+ $ ( '#pagination li a' ) . on ( 'click' , function ( e ) {
761
+ e . preventDefault ( ) ;
762
+
763
+ if ( ! $ ( this ) . is ( '#next, #prev' ) ) {
764
+ // Remove active class from all pagination links
765
+ $ ( '#pagination li' ) . removeClass ( 'active' ) ;
766
+
767
+ // Add active class to the clicked link
768
+ $ ( this ) . parent ( ) . addClass ( 'active' ) ;
769
+
770
+ // Extract the key from the clicked pagination link and subtract 1
771
+ key = parseInt ( $ ( this ) . text ( ) . trim ( ) ) - 1 ;
772
+
773
+ // Call the paging function with the adjusted key
774
+ paging ( key ) ;
775
+ }
776
+ } ) ;
777
+
778
+ $ ( document ) . ready ( function ( ) {
779
+ var key = 0 ; // Declare the key variable outside of the event handlers
780
+
781
+ // Add click event to pagination links
782
+ $ ( '#pagination li a' ) . on ( 'click' , function ( e ) {
783
+ e . preventDefault ( ) ;
784
+
785
+ if ( ! $ ( this ) . is ( '#next, #prev' ) ) {
786
+ // Remove active class from all pagination links
787
+ $ ( '#pagination li' ) . removeClass ( 'active' ) ;
788
+
789
+ // Add active class to the clicked link
790
+ $ ( this ) . parent ( ) . addClass ( 'active' ) ;
791
+
792
+ // Extract the key from the clicked pagination link and subtract 1
793
+ key = parseInt ( $ ( this ) . text ( ) . trim ( ) ) - 1 ;
794
+ console . log ( "Key: " , key ) ;
795
+ // Call the paging function with the adjusted key
796
+ paging ( key ) ;
797
+ }
798
+ } ) ;
799
+
800
+ $ ( '#next' ) . on ( 'click' , function ( e ) {
801
+ e . preventDefault ( ) ;
802
+
803
+ // Get the current active page number
804
+ var currentPage = $ ( '#pagination li.active' ) . index ( ) + 1 ;
805
+
806
+ // If no page is active, default to the first page
807
+ if ( currentPage === 0 ) {
808
+ currentPage = 1 ;
809
+ }
810
+
811
+ // Calculate the key for the next page
812
+ var nextPageKey = currentPage % ( $ ( '#pagination li' ) . length - 1 ) ;
813
+ if ( nextPageKey === 0 ) {
814
+ nextPageKey = ( $ ( '#pagination li' ) . length - 1 ) ;
815
+ } else {
816
+ nextPageKey -= 1 ;
817
+ }
818
+
819
+ // Remove active class from all pagination links
820
+ $ ( '#pagination li' ) . removeClass ( 'active' ) ;
821
+
822
+ // Add active class to the next page
823
+ $ ( '#pagination li:eq(' + nextPageKey + ')' ) . addClass ( 'active' ) ;
824
+
825
+ // Call the paging function with the adjusted key
826
+ paging ( nextPageKey ) ;
827
+ } ) ;
828
+ // Prev button click event handler
829
+ $ ( '#prev' ) . on ( 'click' , function ( e ) {
830
+ e . preventDefault ( ) ;
831
+
832
+ // Get the current active page number
833
+ var currentPage = $ ( '#pagination li.active' ) . index ( ) + 1 ;
834
+
835
+
836
+ // Calculate the key for the previous page
837
+ var key = ( currentPage - 2 + ( $ ( '#pagination li' ) . length - 1 ) ) % ( $ ( '#pagination li' ) . length - 1 ) ; // Corrected the calculation
838
+ console . log ( "Key: " , key ) ;
839
+ // Call the paging function with the adjusted key
840
+ paging ( key ) ;
841
+ } ) ;
842
+ } ) ;
843
+ } ) ;
844
+
845
+
846
+
847
+
848
+
849
+ function paging ( key ) {
850
+ //var targetWord = document.getElementById('.main-submit').value;
851
+ var apiUrl = 'https://recipiebeckend.azurewebsites.net/recipes/paging?key=' + key ;
852
+ fetch ( apiUrl )
853
+ . then ( response => {
854
+ if ( ! response . ok ) {
855
+ throw new Error ( 'Network response was not ok' ) ;
856
+ }
857
+ return response . json ( ) ;
858
+ } )
859
+ . then ( data => {
860
+ displayDashboard ( data ) ;
861
+ } )
862
+ . catch ( error => console . error ( 'Error fetching data:' , error ) ) ;
734
863
}
0 commit comments