import {actionPromise} from "../reducers";
import {call, select, takeEvery} from "redux-saga/effects";
import {promiseWorker} from "../reducers/promiseReducer";
import {gql} from "./index";

export const actionUserFind = () => actionPromise('userFind', gql(`query {
            UserFind(query: "[{}]"){
                _id login 
            }
        }`))



export const actionPlaylistFindByOwner = () =>
    ({type:'FIND_MY_PLAYLISTS'})

export function* playlistFindByOwnerWorker () {
    let { auth } = yield select();
    let userId = auth?.payload?.sub?.id;
    yield call (promiseWorker, actionPromise('playlistFindByOwner', gql(`query playlistFindByOwner ($q: String) {
    PlaylistFind(query: $q) {
        _id name owner {login}
    }
}`, { q: JSON.stringify([{ ___owner: userId }]) })))

}

export function* playlistFindByOwnerWatcher() {
    yield takeEvery ('FIND_MY_PLAYLISTS',playlistFindByOwnerWorker)
}


