parent
5a63496625
commit
42f873b48c
@ -0,0 +1,68 @@
|
||||
import {FETCH_ARTISTE,ACCESS_TOKEN} from '../constants';
|
||||
import {Artist} from "../../Model/Artist";
|
||||
|
||||
|
||||
export const setArtistList = (artistList: Artist[]) => {
|
||||
return {
|
||||
type: FETCH_ARTISTE,
|
||||
payload: artistList,
|
||||
};
|
||||
}
|
||||
|
||||
export const getArtistList = (recherche) => {
|
||||
return async dispatch => {
|
||||
try{
|
||||
const response = await fetch(`https://genius.com/api/search/artists?q=${encodeURIComponent(recherche)}`);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Network response was not ok');
|
||||
}
|
||||
|
||||
const artistListJson = await response.json();
|
||||
console.log(artistListJson);
|
||||
|
||||
|
||||
/*artistListJson.response.hits.map((hit: any) =>
|
||||
/*new Artist(hit.result.name, hit.result.image_url)*/
|
||||
//console.log(hit)
|
||||
//);
|
||||
const artistList: Artist[] = artistListJson.response.sections[0].hits.map(hit => new Artist(hit.result.name, hit.result.image_url));
|
||||
|
||||
console.log(artistList);
|
||||
|
||||
|
||||
dispatch(setArtistList(artistList));
|
||||
} catch (error) {
|
||||
console.error('Error:', error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const getArtistInfo = (idArtist) => {
|
||||
return async dispatch => {
|
||||
try{
|
||||
const response = await fetch(`https://api.genius.com/artists/${idArtist}`, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${ACCESS_TOKEN}`
|
||||
}
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Network response was not ok');
|
||||
}
|
||||
|
||||
const artistInfoJson = await response.json();
|
||||
console.log(artistInfoJson);
|
||||
|
||||
|
||||
/*const artistList: Artist[] = artistListJson.response.hits.map((hit: any) =>
|
||||
new Artist(hit.result.primary_artist.name, hit.result.primary_artist.age)
|
||||
);*/
|
||||
|
||||
|
||||
//dispatch(setArtistList(artistList));
|
||||
} catch (error) {
|
||||
console.error('Error:', error);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
export const FETCH_ARTISTE = 'FETCH_ARTISTE';
|
||||
|
||||
export const ACCESS_TOKEN = 'nhmGn3gceDRH04YeeCqOSg1nD3cAXxIM_MOAOdh7lADuizSGEIuhAIT1dZ6hmkDU';
|
@ -1,8 +1,10 @@
|
||||
import {FETCH_ARTISTE} from '../constants';
|
||||
|
||||
const initialState = {
|
||||
artists: [],
|
||||
}
|
||||
|
||||
export default appReducer = (state = initialState, action) => {
|
||||
export default function appReducer(state = initialState, action){
|
||||
switch (action.type) {
|
||||
case FETCH_ARTISTE:
|
||||
return {...state, artists: action.payload};
|
@ -1,5 +1,5 @@
|
||||
import {configureStore} from '@reduxjs/toolkit'
|
||||
import appReducer from './appReducer';
|
||||
import appReducer from './reducers/reducer';
|
||||
|
||||
// Reference here all your application reducers
|
||||
const reducer = {
|
@ -1,9 +0,0 @@
|
||||
import {FETCH_ARTISTE} from '../constants';
|
||||
import Artist from '../defineObject/Artist';
|
||||
|
||||
export const setArtistList = (ArtistList: Artist[]) => {
|
||||
return {
|
||||
type: FETCH_ARTISTE,
|
||||
payload: ArtistList,
|
||||
};
|
||||
}
|
Loading…
Reference in new issue