Sfoglia il codice sorgente

hw done.Upload avatar now working. To be implemented in player-project

miskson 2 anni fa
parent
commit
65a4885024
1 ha cambiato i file con 33 aggiunte e 4 eliminazioni
  1. 33 4
      hw22-dropzone-uploads/src/App.js

+ 33 - 4
hw22-dropzone-uploads/src/App.js

@@ -73,6 +73,36 @@ const actionLoadFile = (file) => {
   )
 }
 
+const actionAboutMe = () => {
+  let _id = jwtDecode(localStorage.authToken).sub.id
+  return (
+    actionPromise('aboutUser', gql(`
+    query($userId: String!) {
+      UserFindOne(query: $userId){
+        login, _id, avatar {_id, url, originalFileName}
+      }
+    }
+    `, { userId: JSON.stringify([{_id}]) }))
+  )  
+}
+
+const actionSetAvatar = (file) =>
+  async (dispatch, getState) => {
+    await dispatch(actionLoadFile(file))
+    let picId = getState().promise?.loadFile?.payload?._id
+    let userId = jwtDecode(localStorage.authToken).sub.id
+    await dispatch(actionPromise('setAvatar', gql(`
+      mutation {
+        UserUpsert(user:{_id: "${userId}", avatar: {_id: "${picId}"}}){
+          _id, login, avatar{
+              _id, url
+          }
+        }
+      }
+    `)))
+    dispatch(actionAboutMe())
+  }
+
 function promiseReducer(state={}, {type, name, status, payload, error}){
   if (type === 'PROMISE'){
       return {
@@ -92,10 +122,9 @@ const store = createStore(
 )
 store.subscribe(() => console.log(store.getState()))
 
-function MyDropzone({onLoad}) {
+function MyDropzone({uploadAvatar, userData, onLoad}) {
   const onDrop = useCallback(acceptedFiles => {
-    //console.log(acceptedFiles)
-    onLoad(acceptedFiles[0])
+    uploadAvatar(acceptedFiles[0])
   }, [])
   const {getRootProps, getInputProps, isDragActive} = useDropzone({onDrop})
 
@@ -111,7 +140,7 @@ function MyDropzone({onLoad}) {
   )
 }
 
-const ConnectDropzone = connect(null, {onLoad: actionLoadFile})(MyDropzone)
+const ConnectDropzone = connect(null, {uploadAvatar: actionSetAvatar, onLoad: actionLoadFile, userData: actionAboutMe})(MyDropzone)
 
 function App() {
   return (