|
@@ -1,157 +1,249 @@
|
|
-import store from "../reducers"
|
|
|
|
-const getGQL = url =>
|
|
|
|
- (query, variables={}) => fetch(url, {
|
|
|
|
- method: 'POST',
|
|
|
|
- headers: {
|
|
|
|
- //Accept: "application/json",
|
|
|
|
- "Content-Type": "application/json",
|
|
|
|
- ...(localStorage.authToken ? {Authorization: "Bearer " + localStorage.authToken } : {})
|
|
|
|
- },
|
|
|
|
- body: JSON.stringify({query, variables})
|
|
|
|
- }).then(res => res.json())
|
|
|
|
-
|
|
|
|
- let gql = getGQL("/graphql")
|
|
|
|
-
|
|
|
|
-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 actionPromise = (name, promise) =>
|
|
|
|
- async dispatch => {
|
|
|
|
- dispatch(actionPending(name))
|
|
|
|
- try {
|
|
|
|
- let payload = await promise
|
|
|
|
- dispatch(actionResolved(name , payload))
|
|
|
|
- return payload
|
|
|
|
- }
|
|
|
|
- catch(error){
|
|
|
|
- dispatch(actionRejected(name , error))
|
|
|
|
- }
|
|
|
|
|
|
+const getGQL =
|
|
|
|
+ (url) =>
|
|
|
|
+ (query, variables = {}) =>
|
|
|
|
+ fetch(url, {
|
|
|
|
+ method: "POST",
|
|
|
|
+ headers: {
|
|
|
|
+ //Accept: "application/json",
|
|
|
|
+ "Content-Type": "application/json",
|
|
|
|
+ ...(localStorage.authToken
|
|
|
|
+ ? { Authorization: "Bearer " + localStorage.authToken }
|
|
|
|
+ : {}),
|
|
|
|
+ },
|
|
|
|
+ body: JSON.stringify({ query, variables }),
|
|
|
|
+ }).then((res) => res.json());
|
|
|
|
+
|
|
|
|
+let gql = getGQL("/graphql");
|
|
|
|
+
|
|
|
|
+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 actionPromise = (name, promise) => async (dispatch) => {
|
|
|
|
+ dispatch(actionPending(name));
|
|
|
|
+ try {
|
|
|
|
+ let payload = await promise;
|
|
|
|
+ dispatch(actionResolved(name, payload));
|
|
|
|
+ return payload;
|
|
|
|
+ } catch (error) {
|
|
|
|
+ dispatch(actionRejected(name, error));
|
|
}
|
|
}
|
|
|
|
+};
|
|
|
|
|
|
- let log = async(login , password) => {
|
|
|
|
- let query = ` query log($l:String!,$p:String!) {
|
|
|
|
|
|
+let log = async (login, password) => {
|
|
|
|
+ let query = ` query log($l:String!,$p:String!) {
|
|
login(login:$l,password:$p)
|
|
login(login:$l,password:$p)
|
|
- }`
|
|
|
|
-let qVariables = {
|
|
|
|
- "l": login,
|
|
|
|
- "p": password
|
|
|
|
-}
|
|
|
|
-let result = await gql(query,qVariables)
|
|
|
|
-return result
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-let reg = async(login,password) => {
|
|
|
|
|
|
+ }`;
|
|
|
|
+ let qVariables = {
|
|
|
|
+ l: login,
|
|
|
|
+ p: password,
|
|
|
|
+ };
|
|
|
|
+ let result = await gql(query, qVariables);
|
|
|
|
+ return result;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+let reg = async (login, password) => {
|
|
let query = `mutation reg($l:String! , $p:String!) {
|
|
let query = `mutation reg($l:String! , $p:String!) {
|
|
createUser(login:$l,password:$p){
|
|
createUser(login:$l,password:$p){
|
|
_id login
|
|
_id login
|
|
}
|
|
}
|
|
-}`
|
|
|
|
- let qVariables = {
|
|
|
|
- "l": login,
|
|
|
|
- "p": password
|
|
|
|
- }
|
|
|
|
- let result = await gql(query,qVariables)
|
|
|
|
- return result
|
|
|
|
|
|
+}`;
|
|
|
|
+ let qVariables = {
|
|
|
|
+ l: login,
|
|
|
|
+ p: password,
|
|
|
|
+ };
|
|
|
|
+ let result = await gql(query, qVariables);
|
|
|
|
+ return result;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+export const actionAuthLogin = (token) => ({ type: "LOGIN", token });
|
|
|
|
+export const actionAuthLogout = () => ({ type: "LOGOUT" });
|
|
|
|
+export const actionFullLogin = (login, password) => async (dispatch) => {
|
|
|
|
+ let result = await dispatch(actionPromise("login", log(login, password)));
|
|
|
|
+ if (result?.data?.login !== null) {
|
|
|
|
+ dispatch(actionAuthLogin(result.data.login));
|
|
|
|
+ } else {
|
|
|
|
+ alert("такого пользователя не существует");
|
|
}
|
|
}
|
|
|
|
+ await dispatch(actionFindUser());
|
|
|
|
+};
|
|
|
|
|
|
|
|
+export const actionRegister = (login, password) => async (dispatch) => {
|
|
|
|
+ return await dispatch(actionPromise("register", reg(login, password)));
|
|
|
|
+};
|
|
|
|
|
|
-export const actionAuthLogin = token => ({type:'LOGIN', token})
|
|
|
|
-export const actionAuthLogout = () => ({type:'LOGOUT'})
|
|
|
|
-export const actionFullLogin = (login , password) => async dispatch => {
|
|
|
|
- let result = await dispatch(actionPromise("login",log(login,password)))
|
|
|
|
- console.log(result)
|
|
|
|
- if (result?.data?.login !== null){
|
|
|
|
- dispatch(actionAuthLogin(result.data.login))
|
|
|
|
- }
|
|
|
|
- else {
|
|
|
|
- alert ('такого пользователя не существует')
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
|
|
+export const actionFullRegister = (login, password) => async (dispatch) => {
|
|
|
|
+ let result = await dispatch(actionRegister(login, password));
|
|
|
|
|
|
-export const actionRegister = (login,password) => async dispatch => {
|
|
|
|
- return await dispatch (actionPromise('register' , reg(login,password)))
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-export const actionFullRegister = (login,password) => async dispatch => {
|
|
|
|
- let result = await dispatch (actionRegister(login,password))
|
|
|
|
-
|
|
|
|
if (result?.data?.createUser !== null) {
|
|
if (result?.data?.createUser !== null) {
|
|
- await dispatch (actionFullLogin(login,password))
|
|
|
|
- }
|
|
|
|
- else {
|
|
|
|
- alert("Такой пользователь уже есть")
|
|
|
|
-
|
|
|
|
|
|
+ await dispatch(actionFullLogin(login, password));
|
|
|
|
+ } else {
|
|
|
|
+ alert("Такой пользователь уже есть");
|
|
}
|
|
}
|
|
-}
|
|
|
|
|
|
+};
|
|
|
|
|
|
-export const imgFind = async() => {
|
|
|
|
- return await gql(`query fhbg{
|
|
|
|
|
|
+export const imgFind = async () => {
|
|
|
|
+ return await gql(`query fhbg{
|
|
ImageFind(query:"[{}]"){
|
|
ImageFind(query:"[{}]"){
|
|
url owner{
|
|
url owner{
|
|
nick
|
|
nick
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- }`)
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-const userFind = async(id) => {
|
|
|
|
- let query = `query userOne {
|
|
|
|
- UserFindOne(query:"[{\"_id\":\"6151892f68554b3e33cd8134\"}]"){
|
|
|
|
- _id avatar{
|
|
|
|
- url
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }`
|
|
|
|
-
|
|
|
|
- let qVariables = {id}
|
|
|
|
-
|
|
|
|
- let res = await gql(query,qVariables)
|
|
|
|
- console.log(res)
|
|
|
|
- return res
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-let ava = async (idUser , id) => {
|
|
|
|
- let query = `mutation setAvatar{
|
|
|
|
- UserUpsert(user:{_id: "6151892f68554b3e33cd8134", avatar: {_id: "61518b0b68554b3e33cd813c"}}){
|
|
|
|
- _id, avatar{
|
|
|
|
- _id
|
|
|
|
|
|
+ }`);
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+const userFind = (_id) => {
|
|
|
|
+ return gql(
|
|
|
|
+ `query userOne($query:String) {
|
|
|
|
+ UserFindOne(query:$query){
|
|
|
|
+ _id avatar{
|
|
|
|
+ url
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}`,
|
|
|
|
+ { query: JSON.stringify([{ _id }]) }
|
|
|
|
+ );
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+const snippetByOwner = async (id) => {
|
|
|
|
+ let query = `query snippetFind($query:String){
|
|
|
|
+ SnippetFind(query:$query){
|
|
|
|
+ owner{
|
|
|
|
+ _id
|
|
|
|
+ }
|
|
|
|
+ title description _id files {
|
|
|
|
+ type text name
|
|
|
|
+ }
|
|
}
|
|
}
|
|
- }
|
|
|
|
-}`
|
|
|
|
-let qVariables = {idUser , id}
|
|
|
|
-
|
|
|
|
-let res = await gql(query, qVariables)
|
|
|
|
-return res
|
|
|
|
-}
|
|
|
|
|
|
+}`;
|
|
|
|
+ let variables = {
|
|
|
|
+ query: JSON.stringify([{___owner: id } , {sort:[{_id: -1}]}])
|
|
|
|
+ };
|
|
|
|
|
|
-export const actionImgFind = () => async dispatch => {
|
|
|
|
- return await dispatch(actionPromise('img'),imgFind())
|
|
|
|
|
|
+ let res = gql(query, variables);
|
|
|
|
+ return res;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+const snippetById = async(id) => {
|
|
|
|
+ let query = `query snippetFind($query:String){
|
|
|
|
+ SnippetFind(query:$query){
|
|
|
|
+ owner{
|
|
|
|
+ _id
|
|
|
|
+ }
|
|
|
|
+ title description _id files {
|
|
|
|
+ type text name
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}`;
|
|
|
|
+let variables = {
|
|
|
|
+ query: JSON.stringify([{_id: id }])
|
|
|
|
+};
|
|
|
|
|
|
-export const actionFindUser = () => async dispatch => {
|
|
|
|
- return await dispatch (actionPromise('findUser'), userFind(store.getState().auth.payload.sub.id) )
|
|
|
|
|
|
+let res = gql(query, variables);
|
|
|
|
+return res;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
-export const actionSetAvatar = (idUser , id) => async dispatch => {
|
|
|
|
- return await dispatch (actionPromise('setAvatar') , ava(idUser , id))
|
|
|
|
-}
|
|
|
|
|
|
|
|
-export const actionUploadFile = (files) => async dispatch => {
|
|
|
|
- let fd = new FormData()
|
|
|
|
- fd.append('photo' , files)
|
|
|
|
- return await dispatch (actionPromise('upload', fetch('/upload', {
|
|
|
|
- method: "POST",
|
|
|
|
- headers: localStorage.authToken ? {Authorization: 'Bearer ' + localStorage.authToken} : {},
|
|
|
|
- body: fd
|
|
|
|
-}).then(res => res.json())))
|
|
|
|
-}
|
|
|
|
|
|
+let ava = async (idUser, id) => {
|
|
|
|
+ let query = `mutation setAvatar($idUser:String , $idAvatar:ID){
|
|
|
|
+ UserUpsert(user:{_id: $idUser, avatar: {_id: $idAvatar}}){
|
|
|
|
+ _id, avatar{
|
|
|
|
+ url
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}`;
|
|
|
|
+ let qVariables = { idUser: idUser, idAvatar: id };
|
|
|
|
|
|
-export const actionFullAvatar = (idUser , id) => async dispatch => {
|
|
|
|
- let result = await dispatch (actionUploadFile())
|
|
|
|
- await dispatch (actionSetAvatar(idUser,id))
|
|
|
|
-}
|
|
|
|
|
|
+ let res = await gql(query, qVariables);
|
|
|
|
+ return res;
|
|
|
|
+};
|
|
|
|
|
|
|
|
+let snippetAdd = async (title, description, files , idSnippet) => {
|
|
|
|
+ let query = `mutation newSnippet($snippet:SnippetInput) {
|
|
|
|
+ SnippetUpsert(snippet:$snippet){
|
|
|
|
+ _id
|
|
|
|
+ }
|
|
|
|
+ }`;
|
|
|
|
+
|
|
|
|
+ let qVariables = { snippet: { title, description, files } ,_id: idSnippet };
|
|
|
|
+
|
|
|
|
+ let res = await gql(query, qVariables);
|
|
|
|
+ return res;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+export const actionImgFind = () => async (dispatch) => {
|
|
|
|
+ return await dispatch(actionPromise("img", imgFind()));
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+export const actionFindUser = () => async (dispatch, getState) => {
|
|
|
|
+ return await dispatch(
|
|
|
|
+ actionPromise("findUser", userFind(getState().auth.payload.sub.id))
|
|
|
|
+ );
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+export const actionSetAvatar = (id) => async (dispatch, getState) => {
|
|
|
|
+ return await dispatch(
|
|
|
|
+ actionPromise("setAvatar", ava(getState().auth.payload.sub.id, id))
|
|
|
|
+ );
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+export const actionUploadFile = (files) => async (dispatch) => {
|
|
|
|
+ let fd = new FormData();
|
|
|
|
+ fd.append("photo", files);
|
|
|
|
+ return await dispatch(
|
|
|
|
+ actionPromise(
|
|
|
|
+ "upload",
|
|
|
|
+ fetch("/upload", {
|
|
|
|
+ method: "POST",
|
|
|
|
+ headers: localStorage.authToken
|
|
|
|
+ ? { Authorization: "Bearer " + localStorage.authToken }
|
|
|
|
+ : {},
|
|
|
|
+ body: fd,
|
|
|
|
+ }).then((res) => res.json())
|
|
|
|
+ )
|
|
|
|
+ );
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+export const actionFullAvatar = (file) => async (dispatch) => {
|
|
|
|
+ let result = await dispatch(actionUploadFile(file));
|
|
|
|
+ await dispatch(actionSetAvatar(result._id));
|
|
|
|
+ await dispatch(actionFindUser());
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+export const actionSnippetAdd =
|
|
|
|
+ (title, description, files , idSnippet) => async (dispatch) => {
|
|
|
|
+ return await dispatch(
|
|
|
|
+ actionPromise("addSnippet",
|
|
|
|
+ snippetAdd(title, description, files , idSnippet))
|
|
|
|
+ );
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ export const actionSnippetFindByOwner =
|
|
|
|
+ (id) => async (dispatch) => {
|
|
|
|
+ return await dispatch(
|
|
|
|
+ actionPromise("findSnippet",
|
|
|
|
+ snippetByOwner(id))
|
|
|
|
+ );
|
|
|
|
+ };
|
|
|
|
+ export const actionSnippetById =
|
|
|
|
+ (id) => async (dispatch) => {
|
|
|
|
+ return await dispatch(
|
|
|
|
+ actionPromise("findSnippetById",
|
|
|
|
+ snippetById(id))
|
|
|
|
+ );
|
|
|
|
+ };
|