Skip to content

Commit

Permalink
feat: display content randomly (#472)
Browse files Browse the repository at this point in the history
* feat: display content randomly

* feat: get random items using crypto getRandomValues
  • Loading branch information
FredericNumericite authored Jun 23, 2023
1 parent bcdcfd4 commit 59e7675
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions tumeplayMobile/src/views/HomePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,29 @@ const HomePage = ({navigation}) => {
},
});

const shuffleArray = array => {
const crypto = window.crypto || window.msCrypto;
const length = array.length;

for (let i = length - 1; i > 0; i--) {
const j = crypto.getRandomValues(new Uint32Array(1))[0] % (i + 1);
[array[i], array[j]] = [array[j], array[i]];
}

return array;
};

useEffect(() => {
if (data && !loading) {
const shuffledContents = shuffleArray([...data.contents]);

setFreshContents(
data.contents.map(c => {
let tmpContent = JSON.parse(JSON.stringify(c));
tmpContent.image = {
url: tmpContent.etiquette?.image?.url
? tmpContent.etiquette?.image?.url
: tmpContent.image?.url,
};
return tmpContent;
}),
shuffledContents.slice(0, 10).map(c => ({
...c,
image: {
url: c.etiquette?.image?.url || c.image?.url,
},
})),
);
}
}, [data, loading, randomLevel]);
Expand Down Expand Up @@ -166,7 +177,7 @@ const HomePage = ({navigation}) => {
icon
/>
</View>
<Text style={styles.subtitle}> Derniers contenus ajoutés</Text>
<Text style={styles.subtitle}> Contenus à la une</Text>
<View style={styles.carouselContainer}>
<Carousel
data={freshContents}
Expand Down

0 comments on commit 59e7675

Please sign in to comment.