Skip to content

Commit

Permalink
Merge pull request #5 from viditchopra1500/master
Browse files Browse the repository at this point in the history
fixed duplicate entries issue
  • Loading branch information
abhisheknaiidu authored Aug 7, 2020
2 parents bac3846 + 06dbbe9 commit f08222f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"react-dom": "^16.13.1",
"react-player": "^2.3.1",
"react-scripts": "3.4.1",
"subscriptions-transport-ws": "^0.9.16"
"subscriptions-transport-ws": "^0.9.16",
"sweetalert": "^2.1.2"
},
"scripts": {
"start": "react-scripts start",
Expand Down
19 changes: 15 additions & 4 deletions src/components/AddSong.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import { Link, AddBoxOutlined } from '@material-ui/icons'
import ReactPlayer from 'react-player'
import Soundcloud from 'react-player/lib/players/SoundCloud'
import YoutubePlayer from 'react-player/lib/players/YouTube'
import { useMutation } from '@apollo/react-hooks'
import { useMutation,useSubscription } from '@apollo/react-hooks'
import { ADD_SONG } from '../graphql/mutations'
import { GET_SONGS } from '../graphql/subscriptions';
import swal from 'sweetalert';

const useStyles = makeStyles( theme => ( {
container: {
Expand Down Expand Up @@ -34,6 +36,7 @@ function AddSong() {
const[url, setUrl] = React.useState('')
const [dialog, showDialog] = React.useState(false)
const [playable, setPlayable] = React.useState(false)
const { data } = useSubscription(GET_SONGS)
const [song, setSong] = React.useState( {
title: '',
artist: '',
Expand Down Expand Up @@ -97,7 +100,6 @@ async function handleAddSong() {

try {
const { url, thumbnail, duration, title, artist } = song

await addSong({
variables: {
url: url.length > 0 ? url : null,
Expand Down Expand Up @@ -136,6 +138,16 @@ async function handleAddSong() {
showDialog(false)
}

function handleShowDialogBox(){
let flag=0
data.songs.forEach(element => {
if(element.url===url){
flag=1
}
});
flag===1?swal("Oops" , "Song is already present in list!!" , "error"):showDialog(true)
}

const { thumbnail, artist, title } = song
// console.dir(error)
return (
Expand Down Expand Up @@ -213,7 +225,7 @@ async function handleAddSong() {
<Button
disabled={!playable}
className={classes.addSongButton}
onClick={ () => showDialog(true)}
onClick={handleShowDialogBox}
variant="contained" color="secondary"
endIcon={<AddBoxOutlined/>} >
Add
Expand All @@ -226,4 +238,3 @@ async function handleAddSong() {
}

export default AddSong;

0 comments on commit f08222f

Please sign in to comment.