import getGQL from "../utils/getGQL";
import * as actions from "../constants/actions/index";

const setTracks = (tracks) => ({
  type: actions.SET_TRACKS,
  tracks,
});

export const setCurrentTrack = (trackIndex) => ({
  type: actions.SET_CURRENT_TRACK,
  trackIndex,
});

export const getTracks = () => async (dispatch) => {
  try {
    let response = await getGQL("/graphql", {
      Authorization: `Bearer ${localStorage.authToken}`,
    })(
      `query tracksFind($query: String!){
              TrackFind(query: $query){
                  _id
                  url
                  originalFileName
              }
            }`,
      { query: JSON.stringify([{}]) }
    );
    // setTracks(response.data.TrackFind)
    // console.log(tracks)
    // console.log('connected')
    console.log(response);

    dispatch(setTracks(response.data.TrackFind));

    console.log(response.data.TrackFind);
  } catch (error) {
    console.log(error);
  }
};

export const clearTrackList = () => ({
  type: actions.CLEAR_TRACK_LIST
})
















// export const getTracks = (state) => async (dispatch) => {

// // const List = ({track: {_id, url, originalFileName}}) =>
// // <li>
// //     <p>
// //         <span>{originalFileName}</span>
// //         {/* <span>{_id}</span>  */}
// //         {/* <span>{url}</span>  */}
// //     </p>
// // </li>
// const TrackList = () => {
//   const [tracks, setTracks] = useState([]);
//   console.log(tracks);
//   useEffect( async () => {
//     // {debugger}
//     let response = await getGQL("/graphql")
//     (`query tracksFind($query: String!){
//         TrackFind(query: $query){
//             _id
//             url
//             originalFileName
//         }
//       }`,{query: JSON.stringify([{}])})//накой эта строчка нужна?
//       console.log(response);
//       (response)? setTracks(response.data.TrackFind) : alert('data is waiting');
//   },[])//запрос работает, нужно залогиниться, получить токен, и затем перейти во вкладку libraries.
//   const [list, setList] = useState([]);
//   (tracks)? setList(tracks) &&  console.log(list) : alert('data is waiting');
//   return (
//       <div className="trackList">
//           <ul>
//               {/* {(tracks)? tracks.map(track => <List track={track}/>) : alert('data is waiting')} */}
//           </ul>
//       </div>
//   )
// }
// }
