Skip to content

Commit

Permalink
fix try catch add current track
Browse files Browse the repository at this point in the history
  • Loading branch information
teticio committed Aug 15, 2021
1 parent 18a54dd commit 2217679
Show file tree
Hide file tree
Showing 19 changed files with 193 additions and 193 deletions.
2 changes: 1 addition & 1 deletion backend/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ class Playlist(Base):
num_ratings = Column(Integer, default=0)
track_ids = Column(String)
tracks = Column(String, default="")
waypoints = Column(String, default="")
waypoints = Column(String, default="")
5 changes: 3 additions & 2 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ function App() {
.then((playlists) => {
setPlaylists(playlists);
setScreen('latest-playlists');
});
}).catch(error => console.error('Error:', error));
break;
case 'top-playlists':
GetTopPlaylists(8)
.then((playlists) => {
setPlaylists(playlists);
setScreen('top-playlists');
});
}).catch(error => console.error('Error:', error));
break;
case 'search-playlists':
setScreen('search-playlists');
Expand All @@ -85,6 +85,7 @@ function App() {
size={size}
creativity={creativity}
noise={noise}
spotify={spotify}
onCreate={(playlist, waypoints) => {
setWaypoints(waypoints);
setPlaylist(playlist);
Expand Down
5 changes: 3 additions & 2 deletions src/components/AddTrack.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { useState } from "react";
import TrackSelector from "./TrackSelector";
import { FaPlus } from "react-icons/fa";
import Spinner from 'react-bootstrap/Spinner';
import TrackSelector from "./TrackSelector";

var searches = 0;

export default function AddTrack({ numTracks = 0, onAdd = f => f }) {
export default function AddTrack({ numTracks = 0, spotify = null, onAdd = f => f }) {
const [currentId, setCurrentId] = useState(null);
const [spinner, setSpinner] = useState(0);

return (
<div className="d-flex flex-row align-items-center">
<TrackSelector
spotify={spotify}
onSelect={(id) => setCurrentId(id)}
onSearch={() => { searches++; if (searches !== 0) setSpinner(true); }}
onSearchEnd={() => { searches--; if (searches === 0) setSpinner(false); }}
Expand Down
2 changes: 1 addition & 1 deletion src/components/Banner.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center center;
}
}
2 changes: 1 addition & 1 deletion src/components/Banner.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,4 @@ export default function Banner({ loggedIn = false, onSelect = f => f }) {
</Navbar>
</>
);
}
}
20 changes: 16 additions & 4 deletions src/components/CreatePlaylist.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ import RemovablePlaylist from './RemovablePlaylist';
import GeneratePlaylist from './GeneratePlaylist';
import SavePlaylist from './SavePlaylist';

export default function CreatePlaylist({ waypoints = { track_ids: [] }, size = 10, creativity = 0.5, noise = 0, onCreate = f => f, onSettings = f => f }) {
export default function CreatePlaylist({
waypoints = { track_ids: [] },
size = 10,
creativity = 0.5,
noise = 0,
onCreate = f => f,
onSettings = f => f,
spotify = null
}) {
const [_waypoints, setWaypoints] = useState(waypoints);
const [spinner, setSpinner] = useState(false);

Expand All @@ -17,9 +25,12 @@ export default function CreatePlaylist({ waypoints = { track_ids: [] }, size = 1
<Card.Title>
Choose the waypoints in your musical journey
</Card.Title>
<AddTrack numTracks={ _waypoints.track_ids.length } onAdd={(id) => {
setWaypoints({ 'track_ids': _waypoints.track_ids.concat(id) });
}} />
<AddTrack
numTracks={_waypoints.track_ids.length}
spotify={spotify}
onAdd={(id) => {
setWaypoints({ 'track_ids': _waypoints.track_ids.concat(id) });
}} />
<hr />
<RemovablePlaylist {..._waypoints} onRemove={(id) => {
setWaypoints({ 'track_ids': _waypoints.track_ids.filter((element, index) => index !== id) });
Expand All @@ -42,6 +53,7 @@ export default function CreatePlaylist({ waypoints = { track_ids: [] }, size = 1
return playlist;
})
.then(playlist => onCreate(playlist, _waypoints))
.catch(error => console.error('Error:', error));
}}
/>
<FaCog
Expand Down
2 changes: 1 addition & 1 deletion src/components/Footer.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
width: 100%;
text-align: center;
opacity: 0.7
}
}
2 changes: 1 addition & 1 deletion src/components/Footer.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ export default function Footer() {
</Row>
</>
);
}
}
50 changes: 23 additions & 27 deletions src/components/GeneratePlaylist.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
export default async function GeneratePlaylist(playlist, size = 10, creativity = 0.5, noise = 0) {
try {
const waypoints = playlist.track_ids
const response = await fetch(process.env.REACT_APP_API_URL + '/playlist', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
'track_ids': waypoints,
'size': size,
'creativity': creativity,
'noise': noise
})
});
const json = await response.json();
const new_playlist = {
'name': 'Deej-A.I.',
'track_ids': json.track_ids,
'tracks': json.tracks,
'waypoints': waypoints,
'av_rating': 0,
'num_ratings': 0
};
return new_playlist;
} catch (error) {
console.error('Error:', error);
const waypoints = playlist.track_ids
const response = await fetch(process.env.REACT_APP_API_URL + '/playlist', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
'track_ids': waypoints,
'size': size,
'creativity': creativity,
'noise': noise
})
});
const json = await response.json();
const new_playlist = {
'name': 'Deej-A.I.',
'track_ids': json.track_ids,
'tracks': json.tracks,
'waypoints': waypoints,
'av_rating': 0,
'num_ratings': 0
};
}
return new_playlist;
}
110 changes: 47 additions & 63 deletions src/components/SavePlaylist.js
Original file line number Diff line number Diff line change
@@ -1,75 +1,59 @@
export default async function SavePlaylist(playlist) {
const now = new Date();

try {
const response = await fetch(process.env.REACT_APP_API_URL + '/create_playlist', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
'created': now.toISOString(),
'track_ids': JSON.stringify(playlist.track_ids),
'tracks': JSON.stringify(playlist.tracks),
'waypoints': JSON.stringify(playlist.waypoints)
})
});
const db_item = await response.json();
return db_item.id;
} catch (error) {
console.error('Error:', error);
};
const response = await fetch(process.env.REACT_APP_API_URL + '/create_playlist', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
'created': now.toISOString(),
'track_ids': JSON.stringify(playlist.track_ids),
'tracks': JSON.stringify(playlist.tracks),
'waypoints': JSON.stringify(playlist.waypoints)
})
});
const db_item = await response.json();
return db_item.id;
}

export async function UpdatePlaylistName(id, name) {
try {
fetch(process.env.REACT_APP_API_URL + '/update_playlist_name', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
'id': id,
'name': name
})
});
} catch (error) {
console.error('Error:', error);
};
fetch(process.env.REACT_APP_API_URL + '/update_playlist_name', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
'id': id,
'name': name
})
});
}

export async function UpdatePlaylistRating(id, av_rating, num_ratings) {
try {
fetch(process.env.REACT_APP_API_URL + '/update_playlist_rating', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
'id': id,
'av_rating': av_rating,
'num_ratings': num_ratings
})
});
} catch (error) {
console.error('Error:', error);
};
fetch(process.env.REACT_APP_API_URL + '/update_playlist_rating', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
'id': id,
'av_rating': av_rating,
'num_ratings': num_ratings
})
});
}

export async function UpdatePlaylistId(id, user_id, playlist_id) {
try {
fetch(process.env.REACT_APP_API_URL + '/update_playlist_id', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
'id': id,
'user_id': user_id,
'playlist_id': playlist_id
})
});
} catch (error) {
console.error('Error:', error);
};
}
fetch(process.env.REACT_APP_API_URL + '/update_playlist_id', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
'id': id,
'user_id': user_id,
'playlist_id': playlist_id
})
});
}
6 changes: 3 additions & 3 deletions src/components/SearchScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ export default function SearchScreen({ spotify }) {
SearchPlaylists(searchString, 8)
.then((playlists) => {
setPlaylists(playlists);
})
}).catch(error => console.error('Error:', error));
}}
onKeyUp={event => {
if (event.keyCode === 13) {
setEditing(false);
SearchPlaylists(searchString, 8)
.then((playlists) => {
setPlaylists(playlists);
})
}).catch(error => console.error('Error:', error));
}
}}
/> :
Expand All @@ -44,4 +44,4 @@ export default function SearchScreen({ spotify }) {
spotify={spotify}
/></>
);
}
}
2 changes: 1 addition & 1 deletion src/components/Settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@ export default function Settings({ size, creativity, noise, onChange = f => f, o
<Footer />
</>
);
}
}
13 changes: 8 additions & 5 deletions src/components/ShowPlaylist.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ export default function ShowPlaylist({ playlist, onClose = f => f, spotify = nul
setEditing(false);
setPlaylistUrl(spotify_playlist.external_urls.spotify);
if (userPlaylist) {
UpdatePlaylistId(playlist.id, spotify_playlist.owner.id, spotify_playlist.id);
UpdatePlaylistId(playlist.id, spotify_playlist.owner.id, spotify_playlist.id)
.catch(error => console.error('Error:', error));
}
});
}).catch(error => console.error('Error:', error));
}}
/>}
<div style={{ width: '10px' }} />
Expand All @@ -59,12 +60,14 @@ export default function ShowPlaylist({ playlist, onClose = f => f, spotify = nul
onChange={event => setPlaylistName(event.target.value)}
onBlur={() => {
setEditing(false);
UpdatePlaylistName(playlist.id, playlistName);
UpdatePlaylistName(playlist.id, playlistName)
.catch(error => console.error('Error:', error));
}}
onKeyUp={event => {
if (event.keyCode === 13) {
setEditing(false);
UpdatePlaylistName(playlist.id, playlistName);
UpdatePlaylistName(playlist.id, playlistName)
.catch(error => console.error('Error:', error));
}
}}
/> :
Expand All @@ -86,7 +89,7 @@ export default function ShowPlaylist({ playlist, onClose = f => f, spotify = nul
playlist.id,
(rating + playlist.av_rating) / (playlist.num_ratings + 1),
playlist.num_ratings + 1
);
).catch(error => console.error('Error:', error));
}} /></span> :
<span onClick={() => setRateIt(true)}>
<StarRating rating={playlist.av_rating} />
Expand Down
Loading

0 comments on commit 2217679

Please sign in to comment.