|
@@ -1,249 +0,0 @@
|
|
-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!) {
|
|
|
|
- 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 query = `mutation reg($l:String! , $p:String!) {
|
|
|
|
- createUser(login:$l,password:$p){
|
|
|
|
- _id login
|
|
|
|
-}
|
|
|
|
-}`;
|
|
|
|
- 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 actionFullRegister = (login, password) => async (dispatch) => {
|
|
|
|
- let result = await dispatch(actionRegister(login, password));
|
|
|
|
-
|
|
|
|
- if (result?.data?.createUser !== null) {
|
|
|
|
- await dispatch(actionFullLogin(login, password));
|
|
|
|
- } else {
|
|
|
|
- alert("Такой пользователь уже есть");
|
|
|
|
- }
|
|
|
|
-};
|
|
|
|
-
|
|
|
|
-export const imgFind = async () => {
|
|
|
|
- return await gql(`query fhbg{
|
|
|
|
- ImageFind(query:"[{}]"){
|
|
|
|
- url owner{
|
|
|
|
- nick
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }`);
|
|
|
|
-};
|
|
|
|
-
|
|
|
|
-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 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 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 };
|
|
|
|
-
|
|
|
|
- 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))
|
|
|
|
- );
|
|
|
|
- };
|
|
|