Skip to content

Commit

Permalink
Merge branch 'dev/refacto' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben8t committed Sep 30, 2023
2 parents 4be1c80 + 7e41357 commit dc30b4c
Show file tree
Hide file tree
Showing 17 changed files with 454 additions and 339 deletions.
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
"dependencies": {
"axios": "^1.5.0",
"file-saver": "^2.0.5",
"inversify": "^6.0.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"reactflow": "^11.8.3",
"reflect-metadata": "^0.1.13",
"use-debounce": "^9.0.4"
},
"devDependencies": {
Expand Down
186 changes: 44 additions & 142 deletions src/Search.tsx
Original file line number Diff line number Diff line change
@@ -1,147 +1,49 @@
import React from 'react';
import SearchList from './SearchList'
import {useContext, useState} from 'react'
import axios from 'axios'
import {SpotifyContext} from './context/SpotifyContext'
import {FlowContext} from './context/FlowContext'
import {saveAs} from 'file-saver';

const CLIENT_ID = '0350c90137454dc5a748549664e5ba75'
const REDIRECT_URI = window.location.href
const AUTH_ENDPOINT = 'https://accounts.spotify.com/authorize'
const RESPONSE_TYPE = 'token'
import { useContext, useState } from 'react'
import { OAuthMusicContext } from './context/OAuthMusicContext'
import SearchList from './SearchList.tsx'
import { Track } from './services/music/interface.ts'

function Search() {
const {token, logout} = useContext(SpotifyContext)
const {nodes, setNodes, edges, setEdges} = useContext(FlowContext)

const [searchKey, setSearchKey] = useState('')
const [tracks, setTracks] = useState([])

async function getTrackFeatures(token, track_id) {
const response = await axios.get(`https://api.spotify.com/v1/audio-features/${track_id}`, {
headers: {
Authorization: `Bearer ${token}`
}
});
return response.data;
};

async function searchTracks(e) {
e.preventDefault()
const {data} = await axios.get('https://api.spotify.com/v1/search', {
headers: {
Authorization: `Bearer ${token}`
},
params: {
q: searchKey,
type: 'track'
}
})

const trackItems = data.tracks.items;

const trackFeaturesPromises = trackItems.map(async (item) => {
const features = await getTrackFeatures(token, item.id);
return {track: item, features: features};
});
const trackFeatures = await Promise.all(trackFeaturesPromises);

setTracks(trackFeatures)
}

function handleExport(e) {
const data = {
nodes: nodes,
edges: edges
};
const json = JSON.stringify(data);
const blob = new Blob([json], {type: 'application/json'});
saveAs(blob, 'track_lineage_export.json');
}

const fileInputRef = React.createRef();

function handleImportButtonClick() {
fileInputRef.current.click();
}


function handleImport(event) {
const file = event.target.files[0];
const reader = new FileReader();

reader.onload = (e) => {
try {
const data = JSON.parse(e.target.result);

if (data.nodes && data.edges) {
setNodes(data.nodes);
setEdges(data.edges);
alert('Import successful!');
} else {
alert('Invalid file format. Please select a valid JSON file.');
}
} catch (error) {
alert('Error parsing JSON. Please check the file format.');
}
};

reader.readAsText(file);
}

return (
<div className="search rounded-lg bg-gray-100 gap-2">
{
!token ? (
<a className="hover:bg-purple-700 col-span-1 mb-2 rounded bg-purple px-4 py-2 font-mono font-bold text-white"
href={
`${AUTH_ENDPOINT}?client_id=${CLIENT_ID}&redirect_uri=${REDIRECT_URI}&response_type=${RESPONSE_TYPE}`
}>
Login to Spotify
</a>
) : (
<button className="hover:bg-purple-700 col-span-1 mb-2 rounded bg-purple px-4 py-2 font-mono font-bold text-white"
onClick={logout}>
Logout
</button>
)
}
<div>
<button className="hover:bg-purple-700 col-span-1 mb-2 rounded bg-purple px-4 py-2 font-mono font-bold text-white"
onClick={handleImportButtonClick}>
Import
</button>
<input type="file" accept=".json"
ref={fileInputRef}
style={
{display: 'none'}
}
onChange={handleImport}/>
</div>

<button className="hover:bg-purple-700 col-span-1 mb-2 rounded bg-purple px-4 py-2 font-mono font-bold text-white"
onClick={handleExport}>
Export
</button>
<form onSubmit={searchTracks}
className="form grid grid-cols-6 gap-2">
<input type="text"
onChange={
(e) => setSearchKey(e.target.value)
}
className="col-span-4 block w-full rounded-lg border border-gray-300 bg-gray-50 p-2.5 text-sm text-gray-900 focus:border-blue-500 focus:ring-blue-500 dark:border-gray-600 dark:bg-gray-700 dark:text-white dark:placeholder-gray-400 dark:focus:border-blue-500 dark:focus:ring-blue-500"/>
<button className="hover:bg-purple-700 col-span-1 col-span-2 rounded bg-purple px-4 py-2 font-mono font-bold text-white"
type={'submit'}>
Search
</button>
</form>
<hr className="my-2 h-px border-0 bg-light-purple"/>
<SearchList nodes={nodes}
setNodes={setNodes}
tracks={tracks}/>
</div>
)
const { login, logout } = useContext(OAuthMusicContext)
const { search, token } = useContext(OAuthMusicContext)
const [key, setKey] = useState<string>('')
const [tracks, setTracks] = useState<Track[]>([])

return (
<div className="search rounded-lg bg-gray-100">
{!token?.token ? (
<button
className="hover:bg-purple-700 col-span-1 mb-2 rounded bg-purple px-4 py-2 font-mono font-bold text-white"
onClick={login}
>
Login
</button>
) : (
<button
className="hover:bg-purple-700 col-span-1 mb-2 rounded bg-purple px-4 py-2 font-mono font-bold text-white"
onClick={logout}
>
Logout
</button>
)}
<input
type="text"
value={key}
onChange={(e) => setKey(e.target.value)}
className="col-span-4 block w-full rounded-lg border border-gray-300 bg-gray-50 p-2.5 text-sm text-gray-900 focus:border-blue-500 focus:ring-blue-500 dark:border-gray-600 dark:bg-gray-700 dark:text-white dark:placeholder-gray-400 dark:focus:border-blue-500 dark:focus:ring-blue-500"
/>
<button
className="hover:bg-purple-700 col-span-1 col-span-2 rounded bg-purple px-4 py-2 font-mono font-bold text-white"
onClick={async () => {
setTracks(await search(key))
}}
>
Search
</button>
<hr className="my-2 h-px border-0 bg-light-purple" />
<SearchList tracks={tracks} />
</div>
)
}

export default Search
Loading

0 comments on commit dc30b4c

Please sign in to comment.