|
@@ -1,4 +1,77 @@
|
|
|
-import store, {actionPromise, gql} from "../reducers";
|
|
|
+import {actionPromise} from '../reducers'
|
|
|
+import {actionAuthLogin, actionAuthLogout} from '../reducers'
|
|
|
+import {store} from "../reducers";
|
|
|
+import {connect} from "react-redux";
|
|
|
+
|
|
|
+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) =>
|
|
|
+ async function i(dispatch) {
|
|
|
+ let token = await dispatch(actionLogin(login, password));
|
|
|
+ if (token) {
|
|
|
+ dispatch(actionAuthLogin(token));
|
|
|
+ dispatch(actionAboutMe())
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+export const actionFullReg = (login, password) =>
|
|
|
+ async function a(dispatch) {
|
|
|
+ try {
|
|
|
+ await dispatch(actionReg(login, password));
|
|
|
+ } catch (e) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ await dispatch(actionFullLogin(login, password));
|
|
|
+ }
|
|
|
+
|
|
|
+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: "[{}]"){
|
|
@@ -14,28 +87,59 @@ export const actionTrackFindByOwner = (_id) => actionPromise('trackFindByOwner1'
|
|
|
}`, { q: JSON.stringify([{ ___owner: _id }]) }))
|
|
|
|
|
|
|
|
|
-export const actionUserFindOne = (_id, name='userFindOne') => actionPromise(name, gql(`query userFindOne($myid: String) {
|
|
|
- UserFindOne(query: $myid) {
|
|
|
- _id createdAt login nick avatar {_id url}
|
|
|
- }
|
|
|
- }`,{ myid: JSON.stringify([{ _id }]) }));
|
|
|
-
|
|
|
-export const actionAboutMe = () =>
|
|
|
- async (dispatch, getState) => {
|
|
|
- let { auth } = getState();
|
|
|
- let id = auth?.payload?.sub.id;
|
|
|
- if (id) {
|
|
|
- await dispatch(actionUserFindOne(id, 'aboutMe'))
|
|
|
+export const actionFindMyTracks = () =>
|
|
|
+ actionPromise('findMyTracks1', gql(`query findMyTracks($q: String){
|
|
|
+ TrackFind(query: $q){
|
|
|
+ _id url originalFileName
|
|
|
}
|
|
|
- }
|
|
|
+ }`, { q: JSON.stringify([{ ___owner: "61cda244e9472933a6785efc" }]) }))
|
|
|
+
|
|
|
+
|
|
|
+// const CactionFindMyTracks = connect (state => ({myId: state.auth?.payload.sub.id}))(actionFindMyTracks)
|
|
|
|
|
|
|
|
|
+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 = () => async (dispatch, getState) => {
|
|
|
+ let { auth } = getState();
|
|
|
+ let id = auth?.payload?.sub.id;
|
|
|
+ if (id) {
|
|
|
+ await dispatch(actionUserFindOne(id, "aboutMe"));
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
export const actionUploadImage = (file) => {
|
|
|
let fd = new FormData();
|
|
|
fd.append("photo", file);
|
|
|
return actionPromise(
|
|
|
"uploadFile",
|
|
|
- fetch("http://shop-roles.asmer.fs.a-level.com.ua/upload", {
|
|
|
+ fetch('http://player.asmer.fs.a-level.com.ua/upload', {
|
|
|
+ method: "POST",
|
|
|
+ headers: localStorage.authToken
|
|
|
+ ? { Authorization: "Bearer " + localStorage.authToken }
|
|
|
+ : {},
|
|
|
+ body: fd,
|
|
|
+ }).then((res) => res.json())
|
|
|
+ );
|
|
|
+};
|
|
|
+
|
|
|
+export const actionUploadTrack = (file) => {
|
|
|
+ let fd = new FormData();
|
|
|
+ fd.append("track", file);
|
|
|
+ return actionPromise(
|
|
|
+ "uploadFile",
|
|
|
+ fetch('http://player.asmer.fs.a-level.com.ua/track', {
|
|
|
method: "POST",
|
|
|
headers: localStorage.authToken
|
|
|
? { Authorization: "Bearer " + localStorage.authToken }
|
|
@@ -54,7 +158,7 @@ export const actionSetAvatar = (file) => async (dispatch, getState) => {
|
|
|
actionPromise(
|
|
|
"setAvatar",
|
|
|
gql(
|
|
|
- `mutation setAva($userId: String, $imageId: ID){
|
|
|
+ `mutation setAva($userId: String, $imageId: ID){
|
|
|
UserUpsert(user:{_id: $userId, avatar: {_id: $imageId}}){_id}}`,
|
|
|
{ userId: userId, imageId: imageId }
|
|
|
)
|
|
@@ -62,8 +166,5 @@ export const actionSetAvatar = (file) => async (dispatch, getState) => {
|
|
|
);
|
|
|
await dispatch(actionAboutMe());
|
|
|
};
|
|
|
+store.dispatch(actionAboutMe())
|
|
|
|
|
|
-
|
|
|
-store.dispatch(actionUserFind())
|
|
|
-
|
|
|
-export const actionPlayTrack = () => console.log('kak dela kak dela');
|