remember a shuffle seed to remain page reload
This commit is contained in:
parent
1adafef4e1
commit
9b23666fe6
@ -2,6 +2,7 @@ package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"net/url"
|
||||
"openmediacenter/apiGo/api/api"
|
||||
"openmediacenter/apiGo/api/types"
|
||||
@ -163,18 +164,25 @@ func getVideoHandlers() {
|
||||
api.AddHandler("getRandomMovies", api.VideoNode, api.PermUser, func(context api.Context) {
|
||||
var args struct {
|
||||
Number int
|
||||
Seed *int
|
||||
}
|
||||
if api.DecodeRequest(context.GetRequest(), &args) != nil {
|
||||
context.Text("unable to decode request")
|
||||
return
|
||||
}
|
||||
|
||||
// if Seed argument not passed generate random seed
|
||||
if args.Seed == nil {
|
||||
r := rand.Int()
|
||||
args.Seed = &r
|
||||
}
|
||||
|
||||
var result struct {
|
||||
Tags []types.Tag
|
||||
Videos []types.VideoUnloadedType
|
||||
}
|
||||
|
||||
query := fmt.Sprintf("SELECT movie_id,movie_name FROM videos ORDER BY RAND() LIMIT %d", args.Number)
|
||||
query := fmt.Sprintf("SELECT movie_id,movie_name FROM videos ORDER BY RAND(%d) LIMIT %d", *args.Seed, args.Number)
|
||||
result.Videos = readVideosFromResultset(database.Query(query))
|
||||
|
||||
var ids string
|
||||
|
@ -90,14 +90,12 @@ describe('<HomePage/>', function () {
|
||||
const wrapper = shallow(<HomePage/>);
|
||||
|
||||
// expect those default values
|
||||
expect(wrapper.state().sortby).toBe('Date Added');
|
||||
expect(wrapper.instance().sortState).toBe(SortBy.date);
|
||||
expect(wrapper.state().sortby).toBe(0);
|
||||
expect(wrapper.instance().tagState).toBe(DefaultTags.all);
|
||||
|
||||
wrapper.instance().onDropDownItemClick(SortBy.name, 'namesort');
|
||||
wrapper.instance().onDropDownItemClick(SortBy.name);
|
||||
|
||||
expect(wrapper.state().sortby).toBe('namesort');
|
||||
expect(wrapper.instance().sortState).toBe(SortBy.name);
|
||||
expect(wrapper.state().sortby).toBe(SortBy.name);
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -25,9 +25,18 @@ interface GetRandomMoviesType {
|
||||
class RandomPage extends React.Component<{}, state> {
|
||||
readonly LoadNR = 3;
|
||||
|
||||
// random seed to load videos, remains page reload.
|
||||
seed = this.genRandInt();
|
||||
|
||||
constructor(props: {}) {
|
||||
super(props);
|
||||
|
||||
// get previously stored location from localstorage
|
||||
const storedseed = global.localStorage.getItem('randpageseed');
|
||||
if (storedseed != null) {
|
||||
this.seed = parseInt(storedseed, 10);
|
||||
}
|
||||
|
||||
this.state = {
|
||||
videos: [],
|
||||
tags: []
|
||||
@ -36,6 +45,10 @@ class RandomPage extends React.Component<{}, state> {
|
||||
this.keypress = this.keypress.bind(this);
|
||||
}
|
||||
|
||||
genRandInt(): number {
|
||||
return Math.floor(Math.random() * 2147483647) + 1;
|
||||
}
|
||||
|
||||
componentDidMount(): void {
|
||||
addKeyHandler(this.keypress);
|
||||
|
||||
@ -77,15 +90,21 @@ class RandomPage extends React.Component<{}, state> {
|
||||
* click handler for shuffle btn
|
||||
*/
|
||||
shuffleclick(): void {
|
||||
this.genSeed();
|
||||
this.loadShuffledvideos(this.LoadNR);
|
||||
}
|
||||
|
||||
genSeed(): void {
|
||||
this.seed = this.genRandInt();
|
||||
global.localStorage.setItem('randpageseed', this.seed.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* load random videos from backend
|
||||
* @param nr number of videos to load
|
||||
*/
|
||||
loadShuffledvideos(nr: number): void {
|
||||
callAPI<GetRandomMoviesType>(APINode.Video, {action: 'getRandomMovies', Number: nr}, (result) => {
|
||||
callAPI<GetRandomMoviesType>(APINode.Video, {action: 'getRandomMovies', Number: nr, Seed: this.seed}, (result) => {
|
||||
this.setState({videos: []}); // needed to trigger rerender of main videoview
|
||||
this.setState({
|
||||
videos: result.Videos,
|
||||
@ -101,7 +120,8 @@ class RandomPage extends React.Component<{}, state> {
|
||||
private keypress(event: KeyboardEvent): void {
|
||||
// bind s to shuffle
|
||||
if (event.key === 's') {
|
||||
this.loadShuffledvideos(4);
|
||||
this.genSeed();
|
||||
this.loadShuffledvideos(this.LoadNR);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user