diff --git a/src/App.js b/src/App.js index ffa1748..95bf876 100644 --- a/src/App.js +++ b/src/App.js @@ -34,12 +34,20 @@ class App extends React.Component { exact: true, strict: false }); + if (prevState.videoId !== null){ + if (idMatch && idMatch.params.id !== prevState.videoId) { + localStorage.setItem('prevId', prevState.videoId) + localStorage.setItem('lastId', idMatch.params.id) + return { videoId: idMatch.params.id }; + } + else{ if (idMatch && idMatch.params.id !== prevState.videoId) { localStorage.setItem('lastId', idMatch.params.id) return { videoId: idMatch.params.id }; } return null; - } + }}} + render() { diff --git a/src/Suggestion.js b/src/Suggestion.js index a8854af..f8c8f86 100644 --- a/src/Suggestion.js +++ b/src/Suggestion.js @@ -9,6 +9,7 @@ import popGlobaleAssoluta from './reccomenders/popGlobaleAssoluta'; import Recent from './reccomenders/Recent'; import similarityArtist from './reccomenders/similarityArtist'; import popLocaleAss from './reccomenders/popLocaleAss'; +import PopRelLoc from './reccomenders/popRelLoc'; // and so on.. @@ -45,7 +46,7 @@ class Suggestion extends Component { - + {/* lasciare questo per ultimo */} diff --git a/src/TopBar.js b/src/TopBar.js index b0d4992..5b3dead 100644 --- a/src/TopBar.js +++ b/src/TopBar.js @@ -59,7 +59,7 @@ class TopBar extends Component { - + Relativa diff --git a/src/VideoPlayer.js b/src/VideoPlayer.js index 51a8d00..668dcb5 100644 --- a/src/VideoPlayer.js +++ b/src/VideoPlayer.js @@ -96,23 +96,48 @@ class VideoPlayer extends React.Component { response => console.info(response.data), error => this.setState({error})//console.error(error, videoIdWatched.toString()) ); - // local storage logic for recent reccomender//tmp รจ il video che devo inserire sulla lista - let tmp = JSON.parse(localStorage.getItem('lastWatched')) - let j=tmp.findIndex(el=>el.id.toString()===videoIdWatched.toString()) - if (j<0){ - tmp.push({ + // local storage logic for recent reccomender + let tmp = JSON.parse(localStorage.getItem('lastWatched')) + let j=tmp.findIndex(el=>el.id.toString()===videoIdWatched.toString()) + if (j<0) { + + if (localStorage['prevId']==='1') + { + //per primo video + tmp.push({ 'id': videoIdWatched.toString(), 'lastWatched' : new Date(), - 'timesWatched' : 1 - });} - else { - tmp[j].timesWatched++; - tmp[j].lastWatched= new Date(); + 'timesWatched' : 1, + 'prevVideos': [] + }) } - tmp.sort((a, b) => new Date(b.lastWatched).getTime() - new Date(a.lastWatched).getTime()); - - - localStorage.setItem('lastWatched', JSON.stringify(tmp)); + else { + //Prima volta video generico + tmp.push({ + 'id': videoIdWatched.toString(), + 'lastWatched' : new Date(), + 'timesWatched' : 1, + 'prevVideos' : [{id:[localStorage["prevId"]].toString()}] + }) + } + } + else { + //altre volte + tmp[j].timesWatched++; + tmp[j].lastWatched= new Date(); + let z=tmp[j].prevVideos.findIndex(el=>el.id === localStorage["prevId"].toString()) + if (z<0){ + tmp[j].prevVideos.push({ + 'id': localStorage["prevId"].toString() + })} + + } + + tmp.sort((a, b) => new Date(b.lastWatched).getTime() - new Date(a.lastWatched).getTime()); + + + + localStorage.setItem('lastWatched', JSON.stringify(tmp)); //.finally(()=>{}) clearInterval(timerId1); }, diff --git a/src/index.js b/src/index.js index 4c72e38..300f4ff 100644 --- a/src/index.js +++ b/src/index.js @@ -10,6 +10,8 @@ import registerServiceWorker from './registerServiceWorker'; import { HashRouter, BrowserRouter } from 'react-router-dom'; // eslint-disable-line localStorage['lastWatched'] ? function (){}() : localStorage.setItem('lastWatched', JSON.stringify([])) +localStorage['first'] ? function (){}() : localStorage.setItem('first', JSON.stringify([])) +localStorage['prevId'] ? function (){}() : localStorage.setItem('prevId','1') localStorage['lastId'] ? function (){}() : localStorage.setItem('lastId','0J2QdDbelmY') localStorage['currentVideoInfo']? function(){}() : localStorage.setItem('currentVideoInfo', JSON.stringify({ 'id':'0J2QdDbelmY', diff --git a/src/reccomenders/popRelLoc.js b/src/reccomenders/popRelLoc.js new file mode 100644 index 0000000..5bbe538 --- /dev/null +++ b/src/reccomenders/popRelLoc.js @@ -0,0 +1,115 @@ +import React from 'react'; +import { Col, ListGroup, Card, CardDeck } from 'react-bootstrap'; // eslint-disable-line no-unused-vars +import axios from 'axios'; // eslint-disable-line no-unused-vars +import { Link } from 'react-router-dom'; // eslint-disable-line no-unused-vars +/*eslint no-console: ["error", { allow: ["warn", "error", "info"] }] */ +import moment from "moment"; + +class PopRelLoc extends React.Component { + state = { + error: null, + isLoaded: false, + items: [], + headers: null + }; + + ISO_8601parse(a) { + return moment(a, moment.ISO_8601).format('ddd, DD/MM/YYYY hh:mm:ss'); + } + + componentDidMount() { + let tmp = JSON.parse(localStorage.getItem('lastWatched')); + let j=tmp.findIndex(el=>el.id === localStorage["lastId"]); + //this.getThumbnailsNames(tmp[j].prevVideos); + if (tmp.length === 0 || localStorage["prevId"] === '1') + {this.getThumbnailsNames(JSON.parse(localStorage.getItem('first')))} + else + {this.getThumbnailsNames(tmp[j].prevVideos)} + + } + + // METODI + getThumbnailsNames(listVideo) { + let idToLookFor = listVideo.map(item => item.id); + axios.get('https://www.googleapis.com/youtube/v3/videos', { + params: { + part: 'snippet', + id: idToLookFor.toString(), + key: 'AIzaSyA21Odw9UdfiOJnxvA_eDfOW0OxuUfHKig', + fields: 'items(id,snippet/thumbnails/medium,snippet/title)' + } + }) + + .then( + res1 => { + res1.data.items.map(resCurrentValue => + Object.defineProperties( + listVideo.find(videoItem => { + return videoItem.id === resCurrentValue.id; + }), + { + 'thumbnail': + { value: resCurrentValue.snippet.thumbnails.medium.url }, + 'name': + { value: resCurrentValue.snippet.title } + }) + ); + this.setState({ + isLoaded: true, + items: listVideo + }); + }) + } + + + + render() { + const { error, isLoaded, items } = this.state; + if (error) { + return ( + + Error: {error.message} -- Cannot get {error.config.url} + + ); + } else if (!isLoaded) { + return ( + + +

Loading...

+ +
+ ); + } else { + return ( + + {items.map(item => ( + + + + + + + {item.name}
+ {item.lastSelected} +
+
+ +
+ + ))} +
+ ); + } + } +} + + + +export default PopRelLoc; \ No newline at end of file