diff --git a/src/App.js b/src/App.js index 0218b3b..ba44f9d 100644 --- a/src/App.js +++ b/src/App.js @@ -7,17 +7,63 @@ import songData from './data/tracks.json'; songData.forEach((song, i) => { song.id = i; + song.favorite = false; }); class App extends Component { + + constructor() { + super(); + + this.state = { + allSongs: songData + }; + } + + sortTracks = (track) => { + if (track.props.side === "Morning") { + for (let song of this.state.allSongs) { + if (song.id === track.props.id) { + let trackArray = [...this.state.allSongs] + let removedTrack = trackArray.splice(song.id, 1) + trackArray.unshift(removedTrack[0]) + this.setState({allSongs: trackArray}, () => { + console.log(this.state.allSongs) + }); + } + } + } else { + // for (let song of this.state.allSongs) { + // if (song.id === track.props.id) { + // let trackArray = [...this.state.allSongs] + // let removedTrack = [...this.state.allSongs].splice(song.id + 42, 1) + // let firstHalf = [...this.state.allSongs].slice(0, 42) + // let secondHalf = [...this.state.allSongs].slice(43, this.state.allSongs.length) + // secondHalf.unshift(removedTrack[0]) + // console.log("second half:") + // console.log(secondHalf) + // let trackArray = firstHalf.concat(secondHalf) + // // console.log(trackArray) + // this.setState({allSongs: trackArray}, () => { + // console.log(this.state.allSongs) + // }); + // } + // } + + } + } + render() { + return (

Radio Lovelace

- +
); diff --git a/src/components/Playlist.js b/src/components/Playlist.js index 46b997c..b9e3033 100644 --- a/src/components/Playlist.js +++ b/src/components/Playlist.js @@ -1,61 +1,66 @@ -import React from 'react' +import React, { Component } from 'react'; import PropTypes from 'prop-types' import './styles/Playlist.css'; import Track from './Track'; -const calculatePlayTime = (tracks) => { - let minutes = 0; - let seconds = 0; - tracks.forEach((track) => { - const times = track.playtime.split(':'); - minutes += parseInt(times[0]); - seconds += parseInt(times[1]); - }); - minutes += Math.floor(seconds / 60); - const hours = Math.floor(minutes / 60); +class Playlist extends Component { - seconds %= 60; - minutes %= 60; - seconds = ("" + seconds).padStart(2, "0"); - minutes = ("" + minutes).padStart(2, "0"); + calculatePlayTime = (tracks) => { + let minutes = 0; + let seconds = 0; + this.props.tracks.forEach((track) => { + const times = track.playtime.split(':'); + minutes += parseInt(times[0]); + seconds += parseInt(times[1]); + }); + + minutes += Math.floor(seconds / 60); + const hours = Math.floor(minutes / 60); + + seconds %= 60; + minutes %= 60; + + seconds = ("" + seconds).padStart(2, "0"); + minutes = ("" + minutes).padStart(2, "0"); + + return `${hours}:${minutes}:${seconds}`; + } + + +render() { + const playtime = this.calculatePlayTime(this.props.tracks) + + const trackList = this.props.tracks + .map((track, i) => { + return ( + + ) + }) - return `${hours}:${minutes}:${seconds}`; -} -const Playlist = (props) => { - const tracks = props.tracks; - const trackCount = tracks.length; - const playtime = calculatePlayTime(tracks); - const trackElements = tracks.map((track, i) => { - // We use "spread syntax" here to pass in all the properties of - // the variable 'track' as props. Go look it up! return ( - +
+

{this.props.side} Playlist

+

+ {this.props.tracks.length} tracks - {playtime} +

+ +
); - }); - - return ( -
-

{props.side} Playlist

-

- {trackCount} tracks - {playtime} -

- -
- ); } -Playlist.propTypes = { - tracks: PropTypes.array, - side: PropTypes.string, } export default Playlist; diff --git a/src/components/RadioSet.js b/src/components/RadioSet.js index 4796d9c..ba38ac6 100644 --- a/src/components/RadioSet.js +++ b/src/components/RadioSet.js @@ -1,28 +1,107 @@ -import React from 'react'; +import React, { Component } from 'react'; import "./styles/RadioSet.css"; import Playlist from './Playlist'; -const RadioSet = (props) => { - console.log(`Radio set for ${props.tracks.length} tracks`); - const playlists = { - morningTracks: props.tracks.slice(0, props.tracks.length / 2), - eveningTracks: props.tracks.slice(props.tracks.length / 2, props.tracks.length) + +class RadioSet extends Component { + + constructor(props) { + super(props); + console.log(`Radio set for ${props.tracks.length} tracks`); + + this.state = { + morningTracks: this.props.tracks.slice(0, this.props.tracks.length / 2), + eveningTracks: this.props.tracks.slice(this.props.tracks.length / 2, this.props.tracks.length) + }; + } + + switchPlayList = (track, favStar) => { + let morningTrackArray = [...this.state.morningTracks] + let eveningTrackArray = [...this.state.eveningTracks] + + if (track.props.side === "Morning") { + for (let song of morningTrackArray) { + if (track.props.title === song.title){ + let removedTrack = morningTrackArray.splice(track.props.id, 1) + eveningTrackArray.unshift(removedTrack[0]) + eveningTrackArray[0].favorite = favStar + + this.setState({morningTracks: morningTrackArray}, () => { + console.log(this.state.morningTracks) + }); + this.setState({eveningTracks: eveningTrackArray}, () => { + console.log(this.state.eveningTracks) + }); + } + } + } else { + for (let song of eveningTrackArray) { + if (track.props.title === song.title){ + let removedTrack = eveningTrackArray.splice(track.props.id, 1) + morningTrackArray.unshift(removedTrack[0]) + morningTrackArray[0].favorite = favStar + + this.setState({morningTracks: morningTrackArray}, () => { + console.log(this.state.morningTracks) + }); + this.setState({eveningTracks: eveningTrackArray}, () => { + console.log(this.state.eveningTracks) + }); + } + } + } + } + + sortTracks = (track) => { + if (track.props.side === "Morning") { + for (let song of this.state.morningTracks) { + if (track.props.title === song.title){ + let trackArray = [...this.state.morningTracks] + let removedTrack = trackArray.splice(track.props.id, 1) + trackArray.unshift(removedTrack[0]) + this.setState({morningTracks: trackArray}, () => { + console.log(this.state.morningTracks) + }); + } + } + } else { + for (let song of this.state.eveningTracks) { + if (track.props.title === song.title) { + let trackArray = [...this.state.eveningTracks] + let removedTrack = trackArray.splice(track.props.id, 1) + trackArray.unshift(removedTrack[0]) + this.setState({eveningTracks: trackArray}, () => { + console.log(this.state.eveningTracks) + }); + } + } + } + } + + + render() { + + return ( +
+
+ + +
+
+ ); }; - return ( -
-
- - -
-
- ); -}; - -export default RadioSet; \ No newline at end of file + +} + +export default RadioSet; diff --git a/src/components/Track.js b/src/components/Track.js index 39103c9..2ea19dc 100644 --- a/src/components/Track.js +++ b/src/components/Track.js @@ -1,42 +1,73 @@ -import React from 'react' +import React, { Component } from 'react'; import PropTypes from 'prop-types' import "./styles/Track.css"; -// Here we use destructuring to extract the props into separate variables -// See https://wesbos.com/destructuring-objects/ -const Track = ({title, artist, playtime, albumart, favorite}) => { - return ( -
  • - {`album -

    {title}

    - -

    {artist}

    -

    {playtime}

    - - -
  • - ); -}; - -Track.propTypes = { - title: PropTypes.string, - artist: PropTypes.string, - playtime: PropTypes.string, - albumart: PropTypes.string, - favorite: PropTypes.bool, -} + +class Track extends Component { + + constructor(props) { + super(props); + + this.state = { + favorite: props.favorite + }; + } + + + onUpClicked = () => { + this.props.sortTracksCallback(this) + } + + onSwitchSidesClicked = () => { + this.props.switchPlayListCallback(this, this.state.favorite) + } + + onFavStar = () => { + if (this.state.favorite === false) { + this.setState({favorite: true}) + } else { + this.setState({favorite: false}) + } + } + + render(props) { + return ( +
  • + {`album +

    {this.props.title}

    + +

    {this.props.artist}

    +

    {this.props.playtime}

    + + +
  • + ); + }; + + + } + + Track.propTypes = { + title: PropTypes.string, + artist: PropTypes.string, + playtime: PropTypes.string, + albumart: PropTypes.string, + favorite: PropTypes.bool, + } + export default Track;