From 8ee5c1f245a24ca609df1c729d40344a881ac94a Mon Sep 17 00:00:00 2001 From: Steve Richter Date: Thu, 17 Jun 2021 02:05:52 -0400 Subject: [PATCH] Initial Last.fm UI implementation --- server/server.go | 10 +++++ ui/src/personal/ScrobbleToggle.js | 65 +++++++++++++++++++++++++------ 2 files changed, 63 insertions(+), 12 deletions(-) diff --git a/server/server.go b/server/server.go index a602d969a..1395738e0 100644 --- a/server/server.go +++ b/server/server.go @@ -15,6 +15,7 @@ import ( "github.com/navidrome/navidrome/log" "github.com/navidrome/navidrome/model" "github.com/navidrome/navidrome/ui" + "github.com/navidrome/navidrome/utils" ) type Server struct { @@ -84,6 +85,15 @@ func (s *Server) initRoutes() { r.Post("/createAdmin", createAdmin(s.ds)) }) + r.Get("/api/lastfm/link/status", func(w http.ResponseWriter, r *http.Request) { + rs := "false" + c := utils.ParamInt(r, "c", 0) + if (c == 4) { + rs = "true" + } + _, _ = w.Write([]byte(rs)) + }) + // Redirect root to UI URL r.Get("/*", func(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, s.appRoot+"/", http.StatusFound) diff --git a/ui/src/personal/ScrobbleToggle.js b/ui/src/personal/ScrobbleToggle.js index d80eae105..06f870128 100644 --- a/ui/src/personal/ScrobbleToggle.js +++ b/ui/src/personal/ScrobbleToggle.js @@ -1,24 +1,63 @@ +import { useState } from 'react' import { useNotify, useTranslate } from 'react-admin' -import { useDispatch, useSelector } from 'react-redux' -import { setNotificationsState } from '../actions' import { FormControl, FormControlLabel, LinearProgress, Switch, } from '@material-ui/core' -import { useState } from 'react' -import { openInNewTab } from '../utils' +import { useInterval } from '../common' +import { baseUrl } from '../utils' + +const Progress = (props) => { + const { setLinked, setCheckingLink } = props + const translate = useTranslate() + const notify = useNotify() + let linkCheckDelay = 2000 + let linkChecks = 5 + // openInNewTab( + // '/api/lastfm/link' + // ) + + const endChecking = (success) => { + linkCheckDelay = null + setCheckingLink(false) + if (success) { + notify(translate('Last.fm successfully linked!'), 'success') + } else { + notify(translate('Last.fm could not be linked'), 'warning') + } + setLinked(success) + } + + useInterval(() => { + fetch(baseUrl(`/api/lastfm/link/status?c=${linkChecks}`)) + .then((response) => response.text()) + .then((response) => { + if (response === 'true') { + endChecking(true) + } + }) + .catch((error) => { + endChecking(false) + throw new Error(error) + }) + + if (--linkChecks === 0) { + endChecking(false) + } + }, linkCheckDelay) + + return linkChecks > 0 && +} export const ScrobbleToggle = (props) => { const translate = useTranslate() const [linked, setLinked] = useState(false) - - const toggleScrobble = (event) => { + const [checkingLink, setCheckingLink] = useState(false) + const toggleScrobble = () => { if (!linked) { - openInNewTab( - 'https://www.last.fm/api/auth/?api_key=c2918986bf01b6ba353c0bc1bdd27bea' - ) + return setCheckingLink(true) } setLinked(!linked) } @@ -30,14 +69,16 @@ export const ScrobbleToggle = (props) => { } label={{translate('Scrobble to Last.FM')}} /> - {linked && } + {checkingLink && ( + + )} ) }