-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev/refacto' into develop
- Loading branch information
Showing
17 changed files
with
454 additions
and
339 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.