From 7cd9cd7d89c930e8a20c7be64da7066480a040d0 Mon Sep 17 00:00:00 2001 From: lukas-heiligenbrunner Date: Thu, 5 May 2022 19:35:46 +0200 Subject: [PATCH] add a settings entry for nr of random videos to display --- apiGo/api/Settings.go | 7 ++++-- apiGo/api/types/Types.go | 1 + apiGo/database/Database.go | 4 ++-- .../migrations/20220505195845_randomnr.sql | 11 ++++++++++ apiGo/database/settings/DBSettings.go | 5 +++-- src/pages/RandomPage/RandomPage.tsx | 22 ++++++++++--------- src/pages/SettingsPage/GeneralSettings.tsx | 20 ++++++++++++++++- src/types/ApiTypes.ts | 2 ++ src/utils/GlobalInfos.ts | 10 +++++++++ src/utils/context/LoginContextProvider.tsx | 1 + 10 files changed, 66 insertions(+), 17 deletions(-) create mode 100644 apiGo/database/migrations/20220505195845_randomnr.sql diff --git a/apiGo/api/Settings.go b/apiGo/api/Settings.go index fb1f1ce..91fb892 100644 --- a/apiGo/api/Settings.go +++ b/apiGo/api/Settings.go @@ -74,6 +74,7 @@ func getSettingsFromDB() { TVShowPath string TVShowEnabled bool FullDeleteEnabled bool + RandomNR uint32 } regexMatchUrl := regexp.MustCompile("^http(|s)://([0-9]){1,3}\\.([0-9]){1,3}\\.([0-9]){1,3}\\.([0-9]){1,3}:[0-9]{1,5}") @@ -90,6 +91,7 @@ func getSettingsFromDB() { TVShowPath: serverTVShowPath, TVShowEnabled: !config.GetConfig().Features.DisableTVSupport, FullDeleteEnabled: config.GetConfig().Features.FullyDeletableVideos, + RandomNR: sett.RandomNR, } context.Json(res) @@ -127,12 +129,13 @@ func saveSettingsToDB() { password=?, mediacenter_name=?, TMDB_grabbing=?, - DarkMode=? + DarkMode=?, + random_nr=? WHERE 1` // todo avoid conversion context.Text(string(database.SuccessQuery(query, args.VideoPath, args.EpisodePath, args.Password, - args.MediacenterName, args.TMDBGrabbing, args.DarkMode))) + args.MediacenterName, args.TMDBGrabbing, args.DarkMode, args.RandomNR))) }) } diff --git a/apiGo/api/types/Types.go b/apiGo/api/types/Types.go index 4e96f1b..75e1a12 100644 --- a/apiGo/api/types/Types.go +++ b/apiGo/api/types/Types.go @@ -48,6 +48,7 @@ type SettingsType struct { PasswordEnabled bool TMDBGrabbing bool DarkMode bool + RandomNR uint32 } type SettingsSizeType struct { diff --git a/apiGo/database/Database.go b/apiGo/database/Database.go index fc0981a..1c6f8b0 100644 --- a/apiGo/database/Database.go +++ b/apiGo/database/Database.go @@ -126,7 +126,7 @@ func GetSettings() (result types.SettingsType, PathPrefix string, sizes types.Se SELECT COUNT(*) FROM video_tags ) AS tagsadded, - video_path, episode_path, password, mediacenter_name, TMDB_grabbing, DarkMode + video_path, episode_path, password, mediacenter_name, TMDB_grabbing, DarkMode, random_nr FROM settings LIMIT 1`, DBName) @@ -134,7 +134,7 @@ func GetSettings() (result types.SettingsType, PathPrefix string, sizes types.Se var TMDBGrabbing int err := QueryRow(query).Scan(&sizes.VideoNr, &sizes.DBSize, &sizes.DifferentTags, &sizes.TagsAdded, - &result.VideoPath, &result.EpisodePath, &result.Password, &result.MediacenterName, &TMDBGrabbing, &DarkMode) + &result.VideoPath, &result.EpisodePath, &result.Password, &result.MediacenterName, &TMDBGrabbing, &DarkMode, &result.RandomNR) if err != nil { fmt.Println(err.Error()) diff --git a/apiGo/database/migrations/20220505195845_randomnr.sql b/apiGo/database/migrations/20220505195845_randomnr.sql new file mode 100644 index 0000000..f06fff2 --- /dev/null +++ b/apiGo/database/migrations/20220505195845_randomnr.sql @@ -0,0 +1,11 @@ +-- +goose Up +-- +goose StatementBegin +alter table settings + add random_nr int default 3 null; +-- +goose StatementEnd + +-- +goose Down +-- +goose StatementBegin +alter table settings + drop random_nr; +-- +goose StatementEnd diff --git a/apiGo/database/settings/DBSettings.go b/apiGo/database/settings/DBSettings.go index 75cbf51..73fe293 100644 --- a/apiGo/database/settings/DBSettings.go +++ b/apiGo/database/settings/DBSettings.go @@ -20,15 +20,16 @@ type SettingsType struct { MediacenterName string VideoPath string TVShowPath string + RandomNR uint32 } func LoadSettings() *SettingsType { - query := "SELECT DarkMode, password, mediacenter_name, video_path, episode_path from settings" + query := "SELECT DarkMode, password, mediacenter_name, video_path, episode_path, random_nr from settings" result := SettingsType{} var darkmode uint8 - err := database.QueryRow(query).Scan(&darkmode, &result.Pasword, &result.MediacenterName, &result.VideoPath, &result.TVShowPath) + err := database.QueryRow(query).Scan(&darkmode, &result.Pasword, &result.MediacenterName, &result.VideoPath, &result.TVShowPath, &result.RandomNR) if err != nil { fmt.Println("error while parsing db data: " + err.Error()) } diff --git a/src/pages/RandomPage/RandomPage.tsx b/src/pages/RandomPage/RandomPage.tsx index 50e9161..5971108 100644 --- a/src/pages/RandomPage/RandomPage.tsx +++ b/src/pages/RandomPage/RandomPage.tsx @@ -11,6 +11,7 @@ import {addKeyHandler, removeKeyHandler} from '../../utils/ShortkeyHandler'; import {IconButton} from '../../elements/GPElements/Button'; import {faPlusCircle} from '@fortawesome/free-solid-svg-icons'; import AddTagPopup from '../../elements/Popups/AddTagPopup/AddTagPopup'; +import GlobalInfos from '../../utils/GlobalInfos'; interface state { videos: VideoTypes.VideoUnloadedType[]; @@ -28,8 +29,6 @@ interface GetRandomMoviesType { * Randompage shuffles random viedeopreviews and provides a shuffle btn */ class RandomPage extends React.Component<{}, state> { - readonly LoadNR = 3; - constructor(props: {}) { super(props); @@ -46,7 +45,7 @@ class RandomPage extends React.Component<{}, state> { componentDidMount(): void { addKeyHandler(this.keypress); - this.loadShuffledvideos(this.LoadNR); + this.loadShuffledvideos(); } componentWillUnmount(): void { @@ -56,7 +55,7 @@ class RandomPage extends React.Component<{}, state> { render(): JSX.Element { return (
- + Visible Tags: @@ -79,7 +78,7 @@ class RandomPage extends React.Component<{}, state> { {this.state.videos.length !== 0 ? :
No Data found!
}
-
@@ -88,7 +87,7 @@ class RandomPage extends React.Component<{}, state> { onHide={(): void => this.setState({addTagPopup: false})} submit={(tagId: number, tagName: string): void => { this.setState({filterTags: [...this.state.filterTags, {TagId: tagId, TagName: tagName}]}, (): void => { - this.loadShuffledvideos(this.LoadNR); + this.loadShuffledvideos(); }); }} /> @@ -99,12 +98,15 @@ class RandomPage extends React.Component<{}, state> { /** * load random videos from backend - * @param nr number of videos to load */ - loadShuffledvideos(nr: number): void { + loadShuffledvideos(): void { callAPI( APINode.Video, - {action: 'getRandomMovies', Number: nr, TagFilter: this.state.filterTags.map((t) => t.TagId)}, + { + action: 'getRandomMovies', + Number: GlobalInfos.getRandomNR(), + TagFilter: this.state.filterTags.map((t) => t.TagId) + }, (result) => { this.setState({videos: []}); // needed to trigger rerender of main videoview this.setState({ @@ -122,7 +124,7 @@ class RandomPage extends React.Component<{}, state> { private keypress(event: KeyboardEvent): void { // bind s to shuffle if (event.key === 's') { - this.loadShuffledvideos(4); + this.loadShuffledvideos(); } } } diff --git a/src/pages/SettingsPage/GeneralSettings.tsx b/src/pages/SettingsPage/GeneralSettings.tsx index 5ba2f7c..32305c1 100644 --- a/src/pages/SettingsPage/GeneralSettings.tsx +++ b/src/pages/SettingsPage/GeneralSettings.tsx @@ -33,7 +33,8 @@ class GeneralSettings extends React.Component { Password: '', PasswordEnabled: false, TMDBGrabbing: false, - VideoPath: '' + VideoPath: '', + RandomNR: 3 }, sizes: { DBSize: 0, @@ -199,6 +200,23 @@ class GeneralSettings extends React.Component { /> + + Number of random videos on Random page + + this.setState({ + generalSettings: { + ...this.state.generalSettings, + RandomNR: parseInt(e.target.value, 10) + } + }) + } + /> + + diff --git a/src/types/ApiTypes.ts b/src/types/ApiTypes.ts index 538c52a..95271df 100644 --- a/src/types/ApiTypes.ts +++ b/src/types/ApiTypes.ts @@ -40,6 +40,7 @@ export namespace SettingsTypes { TVShowPath: string; TVShowEnabled: boolean; FullDeleteEnabled: boolean; + RandomNR: number; } export interface SettingsType { @@ -50,6 +51,7 @@ export namespace SettingsTypes { PasswordEnabled: boolean; TMDBGrabbing: boolean; DarkMode: boolean; + RandomNR: number; } export interface SizesType { diff --git a/src/utils/GlobalInfos.ts b/src/utils/GlobalInfos.ts index a339991..b0dcde3 100644 --- a/src/utils/GlobalInfos.ts +++ b/src/utils/GlobalInfos.ts @@ -11,6 +11,7 @@ class StaticInfos { private tvshowpath: string = ''; private TVShowsEnabled: boolean = false; private fullDeleteable: boolean = false; + private RandomNR: number = 3; /** * check if the current theme is the dark theme @@ -44,6 +45,7 @@ class StaticInfos { } handlers: (() => void)[] = []; + onThemeChange(func: () => void): void { this.handlers.push(func); } @@ -71,6 +73,14 @@ class StaticInfos { getTVShowPath(): string { return this.tvshowpath; } + + setRandomNR(nr: number): void { + this.RandomNR = nr; + } + + getRandomNR(): number { + return this.RandomNR; + } } export default new StaticInfos(); diff --git a/src/utils/context/LoginContextProvider.tsx b/src/utils/context/LoginContextProvider.tsx index fa68baa..d582204 100644 --- a/src/utils/context/LoginContextProvider.tsx +++ b/src/utils/context/LoginContextProvider.tsx @@ -32,6 +32,7 @@ export const LoginContextProvider: FunctionComponent = (props): JSX.Element => { GlobalInfos.enableDarkTheme(result.DarkMode); GlobalInfos.setVideoPaths(result.VideoPath, result.TVShowPath); + GlobalInfos.setRandomNR(result.RandomNR); features.setTVShowEnabled(result.TVShowEnabled); features.setVideosFullyDeleteable(result.FullDeleteEnabled);