|
@@ -0,0 +1,170 @@
|
|
|
|
+import { gql, store } from './reducers';
|
|
|
|
+import { uploadFile } from '../basic/uploadFilesFunc';
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+const actionPending = (name) => ({ type: "PROMISE", name, status: "PENDING" });
|
|
|
|
+const actionFulfilled = (name, payload) => ({
|
|
|
|
+ type: "PROMISE",
|
|
|
|
+ name,
|
|
|
|
+ status: "FULFILLED",
|
|
|
|
+ payload,
|
|
|
|
+});
|
|
|
|
+
|
|
|
|
+const actionRejected = (name, error) => ({
|
|
|
|
+ type: "PROMISE",
|
|
|
|
+ name,
|
|
|
|
+ status: "REJECTED",
|
|
|
|
+ error,
|
|
|
|
+});
|
|
|
|
+
|
|
|
|
+const actionPromise = (name, promise) =>
|
|
|
|
+ async (dispatch) => {
|
|
|
|
+ dispatch(actionPending(name));
|
|
|
|
+ try {
|
|
|
|
+ let payload = await promise;
|
|
|
|
+ dispatch(actionFulfilled(name, payload));
|
|
|
|
+ return payload;
|
|
|
|
+ } catch (error) {
|
|
|
|
+ dispatch(actionRejected(name, error));
|
|
|
|
+ }
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+//Регистрация
|
|
|
|
+
|
|
|
|
+async function gqlRegistration(login, password) {
|
|
|
|
+ try {
|
|
|
|
+ let result = await gql(`mutation register ($login:String!, $password:String!){
|
|
|
|
+ createUser(login:$login password:$password){
|
|
|
|
+ _id login
|
|
|
|
+ }
|
|
|
|
+}`,
|
|
|
|
+ { "login": login, "password": password });
|
|
|
|
+ return (result.errors?result.errors[0].message:result.data.UserUpsert._id);
|
|
|
|
+ }
|
|
|
|
+ catch (e) {return e}
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+const actionRegister = (login, password) =>
|
|
|
|
+ async dispatch => {
|
|
|
|
+ let user = await dispatch(actionPromise('login', gqlRegistration(login, password)));
|
|
|
|
+ if (user) { await dispatch(actionLogin(login, password)) }
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+//Авторизация
|
|
|
|
+// async function gqlLogin(login, password) {
|
|
|
|
+// try {
|
|
|
|
+// let result = await gql(` query log($login:String!, $password:String!){
|
|
|
|
+// login(login:$login, password:$password)
|
|
|
|
+// }`,
|
|
|
|
+// { "login": login, "password": password });
|
|
|
|
+
|
|
|
|
+// return (result.data.login?result.data.login:'Пользователь не найден')
|
|
|
|
+// }
|
|
|
|
+// catch(e) {return e}
|
|
|
|
+// };
|
|
|
|
+
|
|
|
|
+const actionLogin = (login, password) =>
|
|
|
|
+ async dispatch => {
|
|
|
|
+ let token = await dispatch(actionPromise("login", gql(` query login($login:String!, $password:String!){
|
|
|
|
+ login(login:$login, password:$password)
|
|
|
|
+ }`,
|
|
|
|
+ { "login": login, "password": password })));
|
|
|
|
+ if (token) { await dispatch(actionAuthLogin(token)) }
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+const actionAuthLogin = (token) => ({ type: "AUTH_LOGIN", token });
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+const actionFullLogin = (login, password) => ({ type: 'FULL_LOGIN', login, password });
|
|
|
|
+
|
|
|
|
+const actionAuthLogout = () => ({ type: "AUTH_LOGOUT" });
|
|
|
|
+
|
|
|
|
+//Занрузка всех объявлений
|
|
|
|
+const actionAllAds = () =>
|
|
|
|
+ actionPromise('allAds', gql(`query ads($query: String){
|
|
|
|
+ AdFind(query: $query) {
|
|
|
|
+ _id,
|
|
|
|
+ owner {
|
|
|
|
+ login
|
|
|
|
+ },
|
|
|
|
+ images {
|
|
|
|
+ url
|
|
|
|
+ },
|
|
|
|
+ title,
|
|
|
|
+ description,
|
|
|
|
+ price
|
|
|
|
+ }
|
|
|
|
+ }`, { query: JSON.stringify([ {},{ sort: [{ _id: -1 }] }]) }
|
|
|
|
+ ))
|
|
|
|
+
|
|
|
|
+//Загрузка объявления по id
|
|
|
|
+const actionAdById = (_id) =>
|
|
|
|
+ actionPromise('adById', gql(`query adById($query: String){
|
|
|
|
+ AdFindOne(query:$query){
|
|
|
|
+ _id,
|
|
|
|
+ owner {
|
|
|
|
+ login
|
|
|
|
+ },
|
|
|
|
+ images {
|
|
|
|
+ _id,
|
|
|
|
+ url
|
|
|
|
+ },
|
|
|
|
+ comments {
|
|
|
|
+ _id,
|
|
|
|
+ text,
|
|
|
|
+ owner {
|
|
|
|
+ login
|
|
|
|
+ }
|
|
|
|
+ },
|
|
|
|
+ createdAt,
|
|
|
|
+ title,
|
|
|
|
+ description,
|
|
|
|
+ tags,
|
|
|
|
+ address,
|
|
|
|
+ price,
|
|
|
|
+ }
|
|
|
|
+ }`,
|
|
|
|
+ {query: JSON.stringify([{ _id }])
|
|
|
|
+ }
|
|
|
|
+ ))
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+//Загрузка картинки аватара
|
|
|
|
+const actionAvatar = (imageId) =>
|
|
|
|
+ async (dispatch, getState) => {
|
|
|
|
+ await dispatch(actionPromise('setAvatar', gql(`mutation setAvatar ( $imageId:ID, $user_id:String) {
|
|
|
|
+ UserUpsert(user:{_id: $user_id, avatar: {_id: $imageId}}){
|
|
|
|
+ _id, avatar{
|
|
|
|
+ _id
|
|
|
|
+ }
|
|
|
|
+ }}`, { imageId, user_id: getState().auth.payload?.sub?.id })))
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+const actionSetAvatar = file =>
|
|
|
|
+ async (dispatch) => {
|
|
|
|
+ let result = await dispatch(actionUploadFile(file));
|
|
|
|
+ if (result) {
|
|
|
|
+ await dispatch(actionAvatar(result._id));
|
|
|
|
+ // dispatch(actionAboutMe(store.state.auth.payload));
|
|
|
|
+ }
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ //Загрузка изображений к объявлению Ad
|
|
|
|
+const actionUploadFile = file => actionPromise('uploadFile', uploadFile(file));
|
|
|
|
+const actionUploadFiles = files => actionPromise('uploadFiles', Promise.all(files=>files.map(file=>uploadFile(file))));
|
|
|
|
+
|
|
|
|
+const actionAdUpsert = (ad) =>
|
|
|
|
+ actionPromise(
|
|
|
|
+ 'adUpsert',
|
|
|
|
+ gql(
|
|
|
|
+ `
|
|
|
|
+mutation AdUpsert($ad:AdInput){
|
|
|
|
+ AdUpsert(ad:$ad){
|
|
|
|
+ _id title
|
|
|
|
+ }
|
|
|
|
+}`,
|
|
|
|
+ { ad: { ...ad, images: ad.images.map(({ _id }) => ({ _id })) } },
|
|
|
|
+ ),
|
|
|
|
+ );
|
|
|
|
+
|
|
|
|
+export {actionPending, actionFulfilled, actionRejected, actionPromise, actionRegister, actionLogin, actionAuthLogin, actionFullLogin, actionAuthLogout, actionAdById, actionAvatar, actionSetAvatar, actionAllAds, actionUploadFile, actionUploadFiles, actionAdUpsert };
|