|
@@ -0,0 +1,160 @@
|
|
|
+import { gql } from "../helpers";
|
|
|
+import { getState } from "./../App";
|
|
|
+
|
|
|
+export const actionPending = (name) => ({
|
|
|
+ type: "PROMISE",
|
|
|
+ status: "PENDING",
|
|
|
+ name,
|
|
|
+});
|
|
|
+
|
|
|
+export const actionResolved = (name, payload) => ({
|
|
|
+ type: "PROMISE",
|
|
|
+ status: "RESOLVED",
|
|
|
+ name,
|
|
|
+ payload,
|
|
|
+});
|
|
|
+
|
|
|
+export const actionRejected = (name, error) => ({
|
|
|
+ type: "PROMISE",
|
|
|
+ status: "REJECTED",
|
|
|
+ name,
|
|
|
+ error,
|
|
|
+});
|
|
|
+
|
|
|
+export const actionAuthLogin = (token) => ({ type: "AUTH_LOGIN", token });
|
|
|
+
|
|
|
+export const actionAboutMe = (id, login, nick, avatar) => ({
|
|
|
+ type: "ABOUT_ME",
|
|
|
+ id,
|
|
|
+ login,
|
|
|
+ nick,
|
|
|
+ avatar,
|
|
|
+});
|
|
|
+
|
|
|
+export const actionAuthLogout = () => ({ type: "AUTH_LOGOUT" });
|
|
|
+
|
|
|
+export const actionPromise = (name, promise) => async (dispatch) => {
|
|
|
+ dispatch(actionPending(name));
|
|
|
+ try {
|
|
|
+ let data = await promise;
|
|
|
+ dispatch(actionResolved(name, data));
|
|
|
+ return data;
|
|
|
+ } catch (error) {
|
|
|
+ dispatch(actionRejected(name, error));
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+const actionLogin = (login, password) =>
|
|
|
+ actionPromise(
|
|
|
+ "login",
|
|
|
+ gql(
|
|
|
+ `query log($login:String!, $password:String!){
|
|
|
+ login(login:$login, password: $password)
|
|
|
+ }`,
|
|
|
+ { login, password }
|
|
|
+ )
|
|
|
+ );
|
|
|
+
|
|
|
+const actionRegister = (login, password) =>
|
|
|
+ actionPromise(
|
|
|
+ "registration",
|
|
|
+ gql(
|
|
|
+ `mutation reg($login:String!, $password:String!) {
|
|
|
+ createUser(login:$login, password: $password) {
|
|
|
+ _id login nick
|
|
|
+ }
|
|
|
+ }
|
|
|
+ `,
|
|
|
+ { login, password }
|
|
|
+ )
|
|
|
+ );
|
|
|
+
|
|
|
+export const actionFindUser = (_id) =>
|
|
|
+ actionPromise(
|
|
|
+ "user",
|
|
|
+ gql(
|
|
|
+ `query findUser($q:String){
|
|
|
+ UserFindOne(query:$q){
|
|
|
+ _id login nick createdAt avatar {
|
|
|
+ _id url
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ `,
|
|
|
+ { q: JSON.stringify([{ _id }]) }
|
|
|
+ )
|
|
|
+ );
|
|
|
+
|
|
|
+export const actionFullLogin =
|
|
|
+ (login, password) => async (dispatch, getState) => {
|
|
|
+ let token = await dispatch(actionLogin(login, password));
|
|
|
+ if (token) {
|
|
|
+ dispatch(actionAuthLogin(token));
|
|
|
+ const currentState = getState();
|
|
|
+ let id = currentState.auth.payload.sub.id;
|
|
|
+ let user = await dispatch(actionFindUser(id));
|
|
|
+ if (user._id) {
|
|
|
+ dispatch(actionAboutMe(user._id, user.login, user.nick, user.avatar));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+export const actionFullRegister = (login, password) => async (dispatch) => {
|
|
|
+ let check = await dispatch(actionRegister(login, password));
|
|
|
+ if (check) {
|
|
|
+ dispatch(actionFullLogin(login, password));
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+export const actionFindTracks = () =>
|
|
|
+ actionPromise(
|
|
|
+ "tracks",
|
|
|
+ gql(
|
|
|
+ `query findTracks($q:String){
|
|
|
+ TrackFind(query:$q){
|
|
|
+ _id url owner {
|
|
|
+ _id login nick
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ `,
|
|
|
+ { q: "[{}]" }
|
|
|
+ )
|
|
|
+ );
|
|
|
+
|
|
|
+export const actionFindUsers = () =>
|
|
|
+ actionPromise(
|
|
|
+ "users",
|
|
|
+ gql(
|
|
|
+ `query findUsers($q:String){
|
|
|
+ UserFind(query: $q){
|
|
|
+ _id login nick avatar {
|
|
|
+ _id url
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ `,
|
|
|
+ { q: "[{}]" }
|
|
|
+ )
|
|
|
+ );
|
|
|
+
|
|
|
+export const actionUserUpdate = (_id, nick) =>
|
|
|
+ actionPromise(
|
|
|
+ "userUpdate",
|
|
|
+ gql(
|
|
|
+ `mutation userUpdate($user:UserInput){
|
|
|
+ UserUpsert(user: $user){
|
|
|
+ _id login nick
|
|
|
+ }
|
|
|
+ }
|
|
|
+ `,
|
|
|
+ {
|
|
|
+ user: {
|
|
|
+ _id: "61d45a16e9472933a6785f04",
|
|
|
+ login: "",
|
|
|
+ password: "",
|
|
|
+ nick: "",
|
|
|
+ },
|
|
|
+ }
|
|
|
+ )
|
|
|
+ );
|