|
@@ -1,349 +1,13 @@
|
|
|
-import {actionPromise, store} from '../reducers'
|
|
|
-import {actionAuthLogin} from '../reducers'
|
|
|
-import {all, takeEvery, put, call,select} from 'redux-saga/effects';
|
|
|
-import {promiseWorker} from "../reducers/promiseReducer";
|
|
|
+export {actionAboutMe,aboutMeWatcher} from "./about-me-actions";
|
|
|
+export {actionUserFind, actionFindMyTracks, actionTracksFind, actionPlaylistFindByOwner,} from "./find-actions";
|
|
|
+export {getGQL,backURL,gql,queries} from "./gql-queries-backurl";
|
|
|
+export {actionLogin,actionFullReg,actionFullLogin,actionReg} from "./login-reg-actions";
|
|
|
+export {actionCreatePlaylist,actionUploadTrackToPlaylist,actionFullUploadPlaylists,actionSetTrackToPlaylist} from "./my-playlist-tracks-actions";
|
|
|
+export {actionUploadImage,actionSetAvatar} from "./set-avatar-actions";
|
|
|
+export {actionFullUploadTrack,actionUploadTrack,fullUploadTrackWatcher} from "./no-playlist-track-actions";
|
|
|
|
|
|
|
|
|
-const getGQL = url =>
|
|
|
- async (query, variables = {}) => {
|
|
|
- let obj = await fetch(url, {
|
|
|
- method: 'POST',
|
|
|
- headers: {
|
|
|
- "Content-Type": "application/json",
|
|
|
- Authorization: localStorage.authToken ? 'Bearer ' + localStorage.authToken : {},
|
|
|
- },
|
|
|
- body: JSON.stringify({ query, variables })
|
|
|
- })
|
|
|
- let a = await obj.json()
|
|
|
- if (!a.data && a.errors)
|
|
|
- throw new Error(JSON.stringify(a.errors))
|
|
|
- return a.data[Object.keys(a.data)[0]]
|
|
|
- }
|
|
|
|
|
|
-export const backURL = 'http://player.asmer.fs.a-level.com.ua'
|
|
|
|
|
|
-export const gql = getGQL(backURL + '/graphql');
|
|
|
|
|
|
|
|
|
-
|
|
|
-export const actionLogin = (login, password) => {
|
|
|
- return actionPromise(
|
|
|
- "login",
|
|
|
- gql(
|
|
|
- `query log($login:String!, $password:String!) {
|
|
|
- login(login:$login, password:$password)
|
|
|
- }`,
|
|
|
- {login, password }
|
|
|
- )
|
|
|
- );
|
|
|
-};
|
|
|
-
|
|
|
-
|
|
|
-export const actionFullLogin = (login, password) =>
|
|
|
- ({type:'FULL_LOGIN', login, password})
|
|
|
-
|
|
|
-export function* loginWorker (action) {
|
|
|
- const {login, password} = action
|
|
|
-
|
|
|
- let token = yield call(promiseWorker, actionLogin(login, password)) //dispatch(actionLogin(login, password));
|
|
|
- if (token) {
|
|
|
- yield put(actionAuthLogin(token));
|
|
|
- yield put(actionUserFind())
|
|
|
- yield put(actionFindMyTracks())
|
|
|
- yield put(actionAboutMe())
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-export function* loginWatcher() {
|
|
|
- yield takeEvery ('FULL_LOGIN',loginWorker)
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-export const actionFullReg = (login, password) =>
|
|
|
- ({type:'FULL_REG', login, password})
|
|
|
-
|
|
|
-function* regWorker(action) {
|
|
|
- const {login, password} = action
|
|
|
- let regId = yield call(promiseWorker, actionReg(login, password))
|
|
|
- if (regId) {
|
|
|
- yield put(actionFullLogin(login, password))
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-export function* regWatcher() {
|
|
|
- yield takeEvery('FULL_REG', regWorker)
|
|
|
-}
|
|
|
-
|
|
|
-export const actionReg = (login, password) => {
|
|
|
- return actionPromise(
|
|
|
- "reg",
|
|
|
- gql(
|
|
|
- `mutation reg($l: String!, $p: String!){
|
|
|
- createUser(login: $l, password:$p){
|
|
|
- _id login
|
|
|
- }
|
|
|
- }`,
|
|
|
- { l: login, p: password }
|
|
|
- )
|
|
|
- );
|
|
|
-};
|
|
|
-
|
|
|
-
|
|
|
-export const actionUserFind = () => actionPromise('userFind', gql(`query {
|
|
|
- UserFind(query: "[{}]"){
|
|
|
- _id login
|
|
|
- }
|
|
|
- }`))
|
|
|
-
|
|
|
-// export const actionTrackFindByOwner = (myid) => actionPromise('trackFindByOwner1', gql(`query trackFindByOwner($q: String){
|
|
|
-// TrackFind(query: $q){
|
|
|
-// _id url originalFileName
|
|
|
-// }
|
|
|
-// }`, { q: JSON.stringify([{ ___owner: myid }]) }))
|
|
|
-//
|
|
|
-
|
|
|
-
|
|
|
-export const actionFindMyTracks = () =>
|
|
|
- ({type:'FIND_MY_TRACKS'})
|
|
|
-
|
|
|
-export function* findMyTracksWorker () {
|
|
|
- let { auth } = yield select();
|
|
|
- let userId = auth?.payload?.sub?.id;
|
|
|
- yield call(
|
|
|
- promiseWorker, actionPromise('findMyTracks1', gql(`query findMyTracks($q: String){
|
|
|
- TrackFind(query: $q){
|
|
|
- _id url originalFileName
|
|
|
- }
|
|
|
- }`, { q: JSON.stringify([{ ___owner: userId }]) }))
|
|
|
- );
|
|
|
-
|
|
|
-}
|
|
|
-
|
|
|
-export function* findMyTracksWatcher() {
|
|
|
- yield takeEvery ('FIND_MY_TRACKS',findMyTracksWorker)
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-export const actionCreatePlaylist = (name) =>
|
|
|
- ({type:'CREATE_PLAYLIST', name})
|
|
|
-
|
|
|
-export function* createPlaylistWorker (action) {
|
|
|
- let {name} = action
|
|
|
- let {auth} = yield select()
|
|
|
- yield call(promiseWorker,actionPromise('createPlaylist', gql(`mutation p($playlist:PlaylistInput) {
|
|
|
- PlaylistUpsert(playlist:$playlist) {
|
|
|
- _id
|
|
|
- }
|
|
|
- }`, {playlist: {name}})))
|
|
|
- yield put (actionPlaylistFindByOwner())
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-export function* createPlaylistWatcher() {
|
|
|
- yield takeEvery ('CREATE_PLAYLIST',createPlaylistWorker)
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-export const actionTracksFind = () =>
|
|
|
- actionPromise('findAllTracks1', gql(`query findAllTracks {
|
|
|
- TrackFind (query: "[{}]"){
|
|
|
- _id url originalFileName
|
|
|
- }
|
|
|
-}`))
|
|
|
-
|
|
|
-
|
|
|
-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)
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-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)
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-export const actionUploadTrack = (file) => {
|
|
|
- let fd = new FormData();
|
|
|
- fd.append("track", file);
|
|
|
- return actionPromise(
|
|
|
- "uploadFile",
|
|
|
- fetch(backURL+'/track', {
|
|
|
- method: "POST",
|
|
|
- headers: localStorage.authToken
|
|
|
- ? { Authorization: "Bearer " + localStorage.authToken }
|
|
|
- : {},
|
|
|
- body: fd,
|
|
|
- }).then((res) => res.json())
|
|
|
- );
|
|
|
-};
|
|
|
-
|
|
|
-
|
|
|
-export const actionUploadImage = (file) => {
|
|
|
- let fd = new FormData();
|
|
|
- fd.append("photo", file);
|
|
|
- return actionPromise(
|
|
|
- "uploadFile",
|
|
|
- fetch(backURL+'/upload', {
|
|
|
- method: "POST",
|
|
|
- headers: localStorage.authToken
|
|
|
- ? { Authorization: "Bearer " + localStorage.authToken }
|
|
|
- : {},
|
|
|
- body: fd,
|
|
|
- }).then((res) => res.json())
|
|
|
- );
|
|
|
-};
|
|
|
-
|
|
|
-
|
|
|
-export const actionFullUploadTracks = (file) =>
|
|
|
- ({type:'FULL_UPLOAD', file})
|
|
|
-
|
|
|
-export function* fullUploadTracksWorker (action) {
|
|
|
- const {file} = action
|
|
|
- let { route } = yield select()
|
|
|
- let playlistId = route.params?._id;
|
|
|
- if (file) {
|
|
|
- let result = yield call(promiseWorker,actionUploadTrack(file));
|
|
|
- let trackId = result?._id
|
|
|
- yield put(actionSetTrackToPlaylist(playlistId, trackId))
|
|
|
- }
|
|
|
- yield put(actionAboutMe())
|
|
|
-}
|
|
|
-
|
|
|
-export function* fullUploadTracksWatcher () {
|
|
|
- yield takeEvery ('FULL_UPLOAD',fullUploadTracksWorker)
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-export const actionSetTrackToPlaylist = (playlistId,trackId) =>
|
|
|
- ({type:'SET_TRACK_TO_PLAYLIST', playlistId,trackId})
|
|
|
-
|
|
|
-
|
|
|
-export function* setTrackToPlaylistWorker (action) {
|
|
|
- const {playlistId,trackId} = action
|
|
|
- yield call(
|
|
|
- promiseWorker,actionPromise('createPlaylist', gql(`mutation p($playlist:PlaylistInput) {
|
|
|
- PlaylistUpsert(playlist:$playlist) {
|
|
|
- _id
|
|
|
- }
|
|
|
- }`, {playlist: {_id: playlistId,tracks:{_id: trackId }}}))
|
|
|
- );
|
|
|
- yield put(actionAboutMe());
|
|
|
-};
|
|
|
-
|
|
|
-
|
|
|
-export function* setTrackToPlaylistWatcher() {
|
|
|
- yield takeEvery ('SET_TRACK_TO_PLAYLIST', setTrackToPlaylistWorker)
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-// export function* setTrackToPlaylistWorker (action) {
|
|
|
-// const {file} = action
|
|
|
-// let result = yield call(promiseWorker,actionUploadTrack(file));
|
|
|
-// let trackId = result._id;
|
|
|
-// let { route } = yield select();
|
|
|
-// let playlistId = route.params?._id
|
|
|
-// yield call(
|
|
|
-// promiseWorker,actionPromise('createPlaylist', gql(`mutation p($playlist:PlaylistInput) {
|
|
|
-// PlaylistUpsert(playlist:$playlist) {
|
|
|
-// _id
|
|
|
-// }
|
|
|
-// }`, {playlist: {_id: playlistId,tracks:{_id: trackId }}}))
|
|
|
-// );
|
|
|
-// yield put(actionAboutMe());
|
|
|
-// };
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-export const actionSetAvatar = (file) =>
|
|
|
- ({type:'SET_AVATAR', file})
|
|
|
-
|
|
|
-export function* setAvatarWorker (action) {
|
|
|
- const {file} = action
|
|
|
- let result = yield call(promiseWorker,actionUploadImage(file));
|
|
|
- let { auth } = yield select();
|
|
|
- let imageId = result._id;
|
|
|
- let userId = auth?.payload?.sub?.id;
|
|
|
- yield call(
|
|
|
- promiseWorker,actionPromise(
|
|
|
- "setAvatar",
|
|
|
- gql(
|
|
|
- `mutation setAva($userId: String, $imageId: ID){
|
|
|
- UserUpsert(user:{_id: $userId, avatar: {_id: $imageId}}){_id}}`,
|
|
|
- { userId: userId, imageId: imageId }
|
|
|
- )
|
|
|
- ));
|
|
|
- yield put(actionAboutMe());
|
|
|
-};
|
|
|
-
|
|
|
-export function* setAvatarWatcher() {
|
|
|
- yield takeEvery ('SET_AVATAR',setAvatarWorker)
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-export const queries = {
|
|
|
- "/user/:_id": match => ({name: 'trackFindByOwner1',
|
|
|
- query: `query trackFindByOwner($q: String){
|
|
|
- TrackFind(query: $q){ _id url originalFileName}}`,
|
|
|
- variables: { q: JSON.stringify([{ ___owner: match.params._id }]) } }),
|
|
|
-
|
|
|
- "/myPlaylist/:_id": match => ({name: 'playlistById',
|
|
|
- query: `query playlistById($q: String){
|
|
|
- PlaylistFindOne(query: $q){
|
|
|
- _id name owner {login} tracks {_id url originalFileName}
|
|
|
- }
|
|
|
-}`, variables:{ q: JSON.stringify([{ _id: match.params._id }]) } }),
|
|
|
-}
|