|
@@ -1,255 +0,0 @@
|
|
|
-const getGQL = url =>
|
|
|
- (query, variables) =>
|
|
|
- fetch(url , {
|
|
|
- method: 'POST',
|
|
|
- headers: {
|
|
|
- "content-type": "application/json",
|
|
|
- ...(localStorage.authToken ? { Authorization: "Bearer " + localStorage.authToken } : {})
|
|
|
- },
|
|
|
- body: JSON.stringify({query, variables})
|
|
|
- }).then(res => res.json())
|
|
|
-
|
|
|
-let gql = getGQL("/graphql")
|
|
|
-
|
|
|
-let log = async(login, password) => {
|
|
|
- let query = `query login($login:String!, $password:String!) {
|
|
|
- login(login: $login, password: $password)
|
|
|
- }`
|
|
|
-
|
|
|
- let variables = {"login":login, "password":password}
|
|
|
-
|
|
|
- let token = await gql(query, variables)
|
|
|
- localStorage.authToken = token.data.login
|
|
|
- console.log(token)
|
|
|
- return token.data.login
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-let reg = async(login, password) => {
|
|
|
- let query = `mutation reg($login:String!, $password:String!) {
|
|
|
- createUser(
|
|
|
- login: $login,
|
|
|
- password: $password
|
|
|
-
|
|
|
- ){
|
|
|
- _id
|
|
|
- }}`
|
|
|
-
|
|
|
- let variables = {"login":login, "password":password}
|
|
|
-
|
|
|
- let res = await gql(query, variables)
|
|
|
- return res
|
|
|
-}
|
|
|
-
|
|
|
-export const actionAuthLogout = () => ({type: 'LOGOUT'})
|
|
|
-const actionAuthLogin = token => ({type: 'LOGIN', token})
|
|
|
-const actionLogin = (login, password) => actionPromise("login", log(login, password))
|
|
|
-const actionReg = (login, password) => actionPromise("reg", reg(login, password))
|
|
|
-
|
|
|
-export const actionFullLogin = (login, password) => async(dispatch) => {
|
|
|
-let result = await dispatch(actionLogin(login, password))
|
|
|
- if (result !== null){
|
|
|
- dispatch(actionAuthLogin(result))
|
|
|
- } else {
|
|
|
- alert ('That user doesn’t exist!')
|
|
|
- localStorage.clear()
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-export const actionFullRegister = (login, password) => async(dispatch) => {
|
|
|
- let result = await dispatch(actionReg(login, password))
|
|
|
- if(result?.data?.createUser !== null) {
|
|
|
- dispatch(actionFullLogin(login, password))
|
|
|
- } else {
|
|
|
- alert('Such a user already exists!')
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-// let ChangePass = async(login, password, newPassword) => {
|
|
|
-// let query = `mutation changePass($login:String!, $password:String!, $newPassword:String!) {
|
|
|
-// changePassword(
|
|
|
-// login: $login,
|
|
|
-// password: $password,
|
|
|
-// newPassword: $newPassword
|
|
|
-
|
|
|
-// ){
|
|
|
-// _id
|
|
|
-// }`
|
|
|
-
|
|
|
-// let variables = {"login":login, "password":password, "newPassword":newPassword}
|
|
|
-
|
|
|
-// let res = await gql(query, variables)
|
|
|
-// return res
|
|
|
-// }
|
|
|
-
|
|
|
-// export const actionchangePass = (login, password, newPassword) => actionPromise("changePass", ChangePass(login, password, newPassword))
|
|
|
-
|
|
|
-let SnippetUpsert = async (title, description, files) => {
|
|
|
- let query = `mutation SnippetUpsert($snippet:SnippetInput) {
|
|
|
- SnippetUpsert(snippet:$snippet){
|
|
|
- _id
|
|
|
- }
|
|
|
- }`
|
|
|
-
|
|
|
- let variables = { snippet: { title, description, files:files }};
|
|
|
-
|
|
|
- let res = await gql(query, variables)
|
|
|
- return res
|
|
|
-}
|
|
|
-
|
|
|
-export const actionSnippetUpsert = (title, description, files) => actionPromise("SnippetUpsert", SnippetUpsert(title, description, files))
|
|
|
-
|
|
|
-const UserFind = async (_id) => {
|
|
|
- let query = `UserFind(query:'[{}])' {
|
|
|
- UserFind(query:$query) {
|
|
|
- _id login avatar{
|
|
|
- url
|
|
|
- }
|
|
|
- }
|
|
|
- }`
|
|
|
- let variables = { query: JSON.stringify([{ _id }])}
|
|
|
-
|
|
|
- let res = await gql(query, variables)
|
|
|
- return res
|
|
|
-}
|
|
|
-
|
|
|
-export const actionUserFind = (_id) => actionPromise("UserFind", UserFind(_id))
|
|
|
-
|
|
|
-export const imgFind = async () => {
|
|
|
- return await gql(`query ImageFind{
|
|
|
- ImageFind(query:"[{}]"){
|
|
|
- url owner{
|
|
|
- nick
|
|
|
- }
|
|
|
- }
|
|
|
- }`)
|
|
|
-}
|
|
|
-
|
|
|
-export const actionImgFind = () => async (dispatch) => {
|
|
|
- return await dispatch(actionPromise("img", imgFind()));
|
|
|
-}
|
|
|
-
|
|
|
-const setAva = async (idUser, id) => {
|
|
|
- let query = `mutation setAvatar($idUser:String , $idAvatar:ID){
|
|
|
- UserUpsert(user:{_id: $idUser, avatar: {_id: $idAvatar}}){
|
|
|
- _id, avatar{
|
|
|
- url
|
|
|
- }
|
|
|
- }
|
|
|
- }`
|
|
|
- let variables = { idUser: idUser, idAvatar: id }
|
|
|
-
|
|
|
- let res = await gql(query, variables)
|
|
|
- return res
|
|
|
-}
|
|
|
-
|
|
|
-export const actionSetAva = (idUser, id) => actionPromise("setAva", setAva(idUser, id))
|
|
|
-
|
|
|
-const actionPending = name => ({type: 'PROMISE', status: 'PENDING', name})
|
|
|
-const actionResolved = (name, payload) => ({type: 'PROMISE', status: 'RESOLVED', name, payload})
|
|
|
-const actionRejected = (name, error) => ({type: 'PROMISE', status: 'REJECTED', name, error})
|
|
|
-
|
|
|
-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 up = async(file) => {
|
|
|
- let formData = new FormData()
|
|
|
- formData.append('photo', file)
|
|
|
- return fetch('/upload', {
|
|
|
- method: "POST",
|
|
|
- headers: localStorage.authToken ? {Authorization: 'Bearer ' + localStorage.authToken} : {},
|
|
|
- body: formData
|
|
|
- }).then(res => res.json())
|
|
|
- }
|
|
|
-
|
|
|
-export const actionUploadFile = (file) => actionPromise("upload", up(file))
|
|
|
-
|
|
|
-export const actionFullAvatar = (file) => async (dispatch) => {
|
|
|
- let result = await dispatch(actionUploadFile(file));
|
|
|
- await dispatch(actionSetAva(result._id));
|
|
|
- await dispatch(actionUserFind());
|
|
|
-}
|
|
|
-
|
|
|
-// const snippetByOwner = 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([{___owner: id } , {sort:[{_id: -1}]}])
|
|
|
-// };
|
|
|
-
|
|
|
-// 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 }])
|
|
|
-// };
|
|
|
-
|
|
|
-// let res = gql(query, variables);
|
|
|
-// 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 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))
|
|
|
-// );
|
|
|
-// };
|