import {put, select, takeEvery} from "redux-saga/effects";
import {gql} from "./gql-queries-backurl";
import {actionPromise, store} from "../reducers";
import {actionPlaylistFindByOwner} from "./find-actions";

export const actionUserFindOne = (_id, name = "userFindOne") =>
    actionPromise(
        name,
        gql(
            `query userFindOne($myid: String) {
  UserFindOne(query: $myid) {
             _id createdAt login nick avatar {url}
        } 
    }`,
            { myid: JSON.stringify([{ _id }]) }
        )
    );

export const actionAboutMe = () =>
    ({type:'ABOUT_ME'})

export function* aboutMeWorker () {
    let { auth } = yield select();
    let id = auth?.payload?.sub.id;
    if (id) {
        yield put(actionUserFindOne(id, "aboutMe"));
    }
};

export function* aboutMeWatcher() {
    yield takeEvery ('ABOUT_ME',aboutMeWorker)
}


