浏览代码

show pdf information

amenpunk 2 年之前
父节点
当前提交
7ad9c7549e
共有 3 个文件被更改,包括 206 次插入125 次删除
  1. 1 0
      package.json
  2. 69 0
      src/Pages/Resultados.jsx
  3. 136 125
      src/Utils/HTTP.js

+ 1 - 0
package.json

@@ -30,6 +30,7 @@
30 30
         "react-dom": "^17.0.2",
31 31
         "react-hook-form": "^7.39.4",
32 32
         "react-hot-toast": "^2.2.0",
33
+        "react-pdf": "^6.2.2",
33 34
         "react-query": "^3.34.12",
34 35
         "react-redux": "^8.0.4",
35 36
         "react-router": "6.2.1",

+ 69 - 0
src/Pages/Resultados.jsx

@@ -1,10 +1,79 @@
1
+import { useEffect, useState } from 'react';
1 2
 import { useParams } from 'react-router-dom'
3
+import { Service } from '../Utils/HTTP';
4
+import { useSelector } from 'react-redux';
5
+import { Button  } from '@mui/material'
6
+import { Document, Page, pdfjs } from 'react-pdf/dist/esm/entry.webpack';
7
+// import 'react-pdf/dist/esm/Page/AnnotationLayer.css';
8
+import 'react-pdf/dist/esm/Page/AnnotationLayer.css';
9
+import 'react-pdf/dist/esm/Page/TextLayer.css';
10
+
11
+const Cargando = () => <div><h2>loading...</h2></div>
12
+
13
+// Create Document Component
14
+const MyDocument = (props) => {
15
+
16
+  let { pdf } = props;
17
+  const [numPages, setNumPages] = useState(null);
18
+  const [pageNumber, setPageNumber] = useState(1);
19
+
20
+  function onDocumentLoadSuccess({ numPages }) {
21
+    console.log('numero de pagians',numPages)
22
+    setNumPages(numPages);
23
+  }
24
+
25
+  function onLoadError(error){
26
+    console.log('error: ', error)
27
+  }
28
+
29
+  return (
30
+    <div>
31
+      <div>
32
+        <Button  onClick={() =>setPageNumber(pageNumber + 1)} >
33
+          Siguiente
34
+        </Button>
35
+      </div>
36
+      <Document renderMode="canvas" file={pdf.data} onLoadSuccess={onDocumentLoadSuccess} onLoadError={onLoadError}>
37
+        <Page  pageNumber={pageNumber} loading={<Cargando/>} />
38
+      </Document>
39
+      <p>
40
+        Page {pageNumber} of {numPages}
41
+      </p>
42
+    </div>
43
+  )
44
+};
45
+
46
+
2 47
 export function Resultados() {
48
+
3 49
   let { id } = useParams();
50
+  let auth = useSelector((state) => state.token)
51
+  let [pdf, setPdf] = useState(null);
52
+
53
+  useEffect(() => {
54
+
55
+    let url = `/report/cleaverResult/${id}?output=pdf`
56
+    let rest = new Service(url);
57
+
58
+    pdfjs.GlobalWorkerOptions.workerSrc =  `https://cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.js`;
59
+
60
+    rest
61
+      .getBlob(auth.token)
62
+      .then(blob => setPdf(blob))
63
+      .catch(error => console.log({ error }))
64
+
65
+  }, [auth, id])
66
+
4 67
   return (
5 68
     <div className="content-section">
6 69
       <div className="main">
7 70
         <h1>Resultados {id}</h1>
71
+        <hr />
72
+        {
73
+          pdf ?
74
+            <MyDocument pdf={pdf} />
75
+            : <div> <h5> loading...</h5></div>
76
+        }
8 77
       </div>
9 78
     </div>
10 79
   )

+ 136 - 125
src/Utils/HTTP.js

@@ -2,153 +2,164 @@ import axios from 'axios';
2 2
 
3 3
 export class Service {
4 4
 
5
-    constructor(path) {
6
-        this.base_url = 'http://204.48.25.93:8081'
7
-        this.url = this.base_url + path
8
-        // this.base_url = 'http://psicoadmin.ditca.org:8081'
9
-        // this.api =  'http://204.48.25.93:8081/user?user=patrik&password=12345'
10
-        // this.api =  'http://psicoadmin.ditca.org:8081/user?user=patrik&password=12345'
5
+  constructor(path) {
6
+    this.base_url = 'http://204.48.25.93:8081'
7
+    this.url = this.base_url + path
8
+    // this.base_url = 'http://psicoadmin.ditca.org:8081'
9
+    // this.api =  'http://204.48.25.93:8081/user?user=patrik&password=12345'
10
+    // this.api =  'http://psicoadmin.ditca.org:8081/user?user=patrik&password=12345'
11
+  }
12
+
13
+  async getBlob(token) {
14
+    return await axios({
15
+      url: this.url,
16
+      method: 'get',
17
+      responseType: 'blob',
18
+      headers: {
19
+        'Authorization': `Bearer ${token}`
20
+      }
21
+    })
22
+  }
23
+
24
+
25
+  async getQuery(token) {
26
+    return await axios.get(this.url, {
27
+      headers: {
28
+        'Authorization': `Bearer ${token}`
29
+      }
30
+    })
31
+  }
32
+
33
+  async get(token) {
34
+    return axios.get(this.url, {
35
+      headers: {
36
+        'Authorization': `Bearer ${token}`
37
+      }
38
+    })
39
+      .then((res) => {
40
+        return res;
41
+      })
42
+      .catch((error) => {
43
+        console.log("ERROR :: ", error)
44
+        return new Error("GG");
45
+      })
46
+  }
47
+
48
+  async post(body, token) {
49
+
50
+    if (!token) {
51
+      let response = await axios({
52
+        method: "POST",
53
+        url: this.url,
54
+        headers: {
55
+          'Content-Type': 'application/json',
56
+        },
57
+        data: body
58
+      })
59
+      return await response.data;
11 60
     }
12 61
 
13 62
 
14
-    async getQuery(token) {
15
-        return await axios.get(this.url, {
16
-            headers: {
17
-                'Authorization': `Bearer ${token}`
18
-            }
19
-        })
63
+    const MyHeaders = {
64
+      'Authorization': 'Bearer ' + token,
20 65
     }
21 66
 
22
-    async get(token) {
23
-        return axios.get(this.url, {
24
-            headers: {
25
-                'Authorization': `Bearer ${token}`
26
-            }
27
-        })
28
-            .then((res) => {
29
-                return res;
30
-            })
31
-            .catch((error) => {
32
-                console.log("ERROR :: ", error)
33
-                return new Error("GG");
34
-            })
67
+    let response = await axios({
68
+      method: "POST",
69
+      url: this.url,
70
+      headers: MyHeaders,
71
+      data: body
72
+    })
73
+    console.log('response', response)
74
+
75
+    return await response.data;
76
+
77
+  }
78
+
79
+  async postQuery(body, token) {
80
+
81
+    if (!token) {
82
+      return await axios({
83
+        method: "POST",
84
+        url: this.url,
85
+        headers: {
86
+          'Content-Type': 'application/json',
87
+        },
88
+        data: body
89
+      })
35 90
     }
36 91
 
37
-    async post(body, token) {
38
-
39
-        if (!token) {
40
-            let response = await axios({
41
-                method: "POST",
42
-                url: this.url,
43
-                headers: {
44
-                    'Content-Type': 'application/json',
45
-                },
46
-                data: body
47
-            })
48
-            return await response.data;
49
-        }
50
-
51
-
52
-        const MyHeaders = {
53
-            'Authorization': 'Bearer ' + token,
54
-        }
55
-
56
-        let response = await axios({
57
-            method: "POST",
58
-            url: this.url,
59
-            headers: MyHeaders,
60
-            data: body
61
-        })
62
-        console.log('response', response)
63
-
64
-        return await response.data;
65 92
 
93
+    const MyHeaders = {
94
+      'Authorization': 'Bearer ' + token,
66 95
     }
67 96
 
68
-    async postQuery(body, token) {
69
-
70
-        if (!token) {
71
-            return await axios({
72
-                method: "POST",
73
-                url: this.url,
74
-                headers: {
75
-                    'Content-Type': 'application/json',
76
-                },
77
-                data: body
78
-            })
79
-        }
80
-
97
+    return await axios({
98
+      method: "POST",
99
+      url: this.url,
100
+      headers: MyHeaders,
101
+      data: body
102
+    })
81 103
 
82
-        const MyHeaders = {
83
-            'Authorization': 'Bearer ' + token,
84
-        }
85 104
 
86
-        return await axios({
87
-            method: "POST",
88
-            url: this.url,
89
-            headers: MyHeaders,
90
-            data: body
91
-        })
105
+  }
92 106
 
107
+  async put(body, token) {
93 108
 
109
+    if (!token) {
110
+      let response = await axios({
111
+        method: "PUT",
112
+        url: this.url,
113
+        headers: {
114
+          'Content-Type': 'application/json',
115
+        },
116
+        data: body
117
+      })
118
+      return await response.data;
94 119
     }
95 120
 
96
-    async put(body, token) {
97
-
98
-        if (!token) {
99
-            let response = await axios({
100
-                method: "PUT",
101
-                url: this.url,
102
-                headers: {
103
-                    'Content-Type': 'application/json',
104
-                },
105
-                data: body
106
-            })
107
-            return await response.data;
108
-        }
109
-
110
-
111
-        const MyHeaders = {
112
-            'Authorization': 'Bearer ' + token,
113
-        }
114
-
115
-        let response = await axios({
116
-            method: "PUT",
117
-            url: this.url,
118
-            headers: MyHeaders,
119
-            data: body
120
-        })
121
-        console.log('response', response)
122
-
123
-        return await response.data;
124 121
 
122
+    const MyHeaders = {
123
+      'Authorization': 'Bearer ' + token,
125 124
     }
126 125
 
126
+    let response = await axios({
127
+      method: "PUT",
128
+      url: this.url,
129
+      headers: MyHeaders,
130
+      data: body
131
+    })
132
+    console.log('response', response)
133
+
134
+    return await response.data;
135
+
136
+  }
137
+
138
+
139
+  async putQuery(body, token) {
140
+    if (!token) {
141
+      return await axios({
142
+        method: "PUT",
143
+        url: this.url,
144
+        headers: {
145
+          'Content-Type': 'application/json',
146
+        },
147
+        data: body
148
+      })
149
+    }
127 150
 
128
-    async putQuery(body, token) {
129
-        if (!token) {
130
-            return await axios({
131
-                method: "PUT",
132
-                url: this.url,
133
-                headers: {
134
-                    'Content-Type': 'application/json',
135
-                },
136
-                data: body
137
-            })
138
-        }
139
-
140
-        const MyHeaders = {
141
-            'Authorization': 'Bearer ' + token,
142
-        }
143
-
144
-        return await axios({
145
-            method: "PUT",
146
-            url: this.url,
147
-            headers: MyHeaders,
148
-            data: body
149
-        })
151
+    const MyHeaders = {
152
+      'Authorization': 'Bearer ' + token,
150 153
     }
151 154
 
155
+    return await axios({
156
+      method: "PUT",
157
+      url: this.url,
158
+      headers: MyHeaders,
159
+      data: body
160
+    })
161
+  }
162
+
152 163
 
153 164
 
154 165
 }