|
@@ -19,6 +19,19 @@ const backendURL = 'http://hipstagram.asmer.fs.a-level.com.ua';
|
|
|
const gql = getGQL(backendURL + '/graphql');
|
|
|
|
|
|
|
|
|
+const uploadFile = file => {
|
|
|
+ const fd = new FormData;
|
|
|
+ fd.append('photo', file);
|
|
|
+ return fetch(backendURL+'/upload', {
|
|
|
+ method: "POST",
|
|
|
+ headers: localStorage.authToken ? {Authorization: 'Bearer ' + localStorage.authToken} : {},
|
|
|
+ body: fd
|
|
|
+ }).then(res => res.json());
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
export const actionPromise = (name,promise) =>{
|
|
|
const actionPending = name => ({name,type:"PROMISE",status:'PENDING'})
|
|
@@ -39,16 +52,28 @@ export const actionPromise = (name,promise) =>{
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+export const actionUploadFile = file =>
|
|
|
+ actionPromise('fileUpload', uploadFile(file));
|
|
|
+export const actionAvatar = (userrId, fileeId) =>
|
|
|
+ actionPromise("userAvatar", gql(`mutation setAvatar($userId: String, $fileId: ID){
|
|
|
+ UserUpsert(user:{_id: $userId, avatar: {_id: $fileId}}){
|
|
|
+ _id, avatar{
|
|
|
+ _id
|
|
|
+ }
|
|
|
+ }
|
|
|
+}`, {userId: userrId,
|
|
|
+ fileId: fileeId}))
|
|
|
+
|
|
|
export const actionAuthLogin = (tokennn) =>({type:'AUTH_LOGIN',token:tokennn})
|
|
|
export const actionAuthLogout= () => ({type: 'AUTH_LOGOUT'})
|
|
|
|
|
|
|
|
|
export const actionFullRegister = (Login,password) =>
|
|
|
- actionPromise('fullRegister', gql(`mutation register($Login:String,$password:String){
|
|
|
- UserUpsert(user:{login:$Login,password:$password}){
|
|
|
+ actionPromise('fullRegister', gql(`mutation register($login:String!,$password:String!){
|
|
|
+ UserUpsert(user:{login:$login,password:$password}){
|
|
|
_id login
|
|
|
}
|
|
|
- }`,{Login: Login, password: password}))
|
|
|
+ }`,{login: Login, password: password}))
|
|
|
|
|
|
export const actionFullLogin = (login,password) => async (dispatch) => {
|
|
|
const tokennn = await dispatch(
|
|
@@ -58,3 +83,26 @@ export const actionFullLogin = (login,password) => async (dispatch) => {
|
|
|
);
|
|
|
await dispatch(actionAuthLogin(tokennn));
|
|
|
}
|
|
|
+
|
|
|
+export const actionAboutMe = (_id) =>
|
|
|
+ actionPromise("userInfo", gql(`query AboutMe ($Id: String){
|
|
|
+ UserFindOne(query:$Id){
|
|
|
+ _id,nick, avatar{
|
|
|
+ url
|
|
|
+ },
|
|
|
+ following{
|
|
|
+ _id
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }`,{
|
|
|
+ Id: JSON.stringify([{_id}])
|
|
|
+ }))
|
|
|
+
|
|
|
+export const actionSetAvatar = file =>
|
|
|
+ async(dispatch, getState) => {
|
|
|
+ await dispatch(actionUploadFile(file))
|
|
|
+ const {auth:{payload:{sub:{id}}},promise:{fileUpload:{payload:{_id}}}} = getState()
|
|
|
+ await dispatch(actionAvatar(id,_id))
|
|
|
+ await dispatch(actionAboutMe(id))
|
|
|
+ }
|
|
|
+
|