Randomizing and Locking Content

Thank you for that bamuu.

I tried following your suggestions and ended up with the following code. However, this does not seem to work. It still shuffles, but when I click the lockButton in my preview nothing happens. Can you see any obvious mistakes?

import wixData from ‘wix-data’ ;

let lockedItems = []; // Initialize an empty array to store locked items

//Hämta data från Databasen med följande kod

$w . onReady ( async function () {
$w ( ‘#mainRepeater’ ). onItemReady (( $item , $itemData , index ) => {
$item ( “#questionText” ). text = $itemData . title ;
$item ( “#answerText” ). text = $itemData . answer ;
$item ( “#spotifyLink” ). link = $itemData . songLink ;
// Add a click event handler to the lock button
$item ( ‘#lockButton’ ). onClick ( async () => {
// Get the current item from the Questions collection
let item = await wixData. get ( “Questions” , $itemData._id);

        // Add the current item to the lockedItems array 
        lockedItems . push ( item ); 
    }); 
}); 

**const**  {  items :  Questions  } =  **await**  wixData . query ( 'Questions' ) 
    . limit (10) 
    . find () 

$w ( '#mainRepeater' ). data  =  Questions 

})

export async function generateButton_click ( event ) {
$w ( “#mainRepeater” ). onItemReady (( $item , itemData , index ) => {
$item ( “#questionText” ). text = itemData . title ;
$item ( “#answerText” ). text = itemData . answer ;
$item ( “#spotifyLink” ). link = itemData . songLink ;
});

// Query the Questions collection to get all items 
**let**  results  =  **await**  wixData . query ( "Questions" ). find (); 
**let**  allItems  =  results . items ; 

// Use the array.filter method to remove the items in lockedItems from allItems 
**let**  availableItems  =  allItems . filter ( item  => ! lockedItems . includes ( item )); 

// Shuffle the available items 
**let**  shuffled  =  shuffleArray ( availableItems ); 

// Set the data for the repeater to the first 10 items in the shuffled array 
$w ( '#mainRepeater' ). data  =  shuffled . slice ( 0 ,  10 ); 

}

function shuffleArray ( array ) {
for ( let last = array . length - 1 ; last > 0 ; last --) {
const next = Math . floor ( Math . random () * ( last + 1 ));
[ array [ last ], array [ next ]] = [ array [ next ], array [ last ]];
}
return array ;
}