|
1 |
| -import React, { useState, useEffect } from 'react'; |
2 |
| -import { PublicClientApplication } from '@azure/msal-browser'; |
| 1 | +import React from 'react'; |
3 | 2 | import { Buffer } from 'buffer';
|
| 3 | +import { AuthProvider } from './components/AuthProvider'; |
| 4 | +import CsvUploader from './components/CsvUploader'; |
4 | 5 |
|
5 | 6 | window.Buffer = Buffer;
|
6 | 7 |
|
7 |
| -// MSAL Configuration |
8 |
| -const msalConfig = { |
9 |
| - auth: { |
10 |
| - clientId: process.env.REACT_APP_STATIC_WEB_APP_CLIENT_ID, |
11 |
| - authority: `https://login.microsoftonline.com/${process.env.REACT_APP_AZURE_TENANT_ID}`, |
12 |
| - redirectUri: window.location.origin, |
13 |
| - }, |
14 |
| -}; |
15 |
| - |
16 |
| -const msalInstance = new PublicClientApplication(msalConfig); |
17 |
| - |
18 | 8 | const App = () => {
|
19 |
| - const [file, setFile] = useState(null); |
20 |
| - const [error, setError] = useState(''); |
21 |
| - const [modalVisible, setModalVisible] = useState(false); |
22 |
| - const [user, setUser] = useState(null); |
23 |
| - const [isInitialized, setIsInitialized] = useState(false); |
24 |
| - |
25 |
| - const handleFileChange = (e) => { |
26 |
| - setError(''); |
27 |
| - setFile(e.target.files[0]); |
28 |
| - }; |
29 |
| - |
30 |
| - const uploadFile = async (file) => { |
31 |
| - if (!file) { |
32 |
| - setError('Please select a file to upload'); |
33 |
| - return; |
34 |
| - } |
35 |
| - |
36 |
| - try { |
37 |
| - console.log('Authenticating and uploading file...'); |
38 |
| - |
39 |
| - // Acquire access token |
40 |
| - const account = msalInstance.getAllAccounts()[0]; |
41 |
| - if (!account) { |
42 |
| - throw new Error('User is not signed in'); |
43 |
| - } |
44 |
| - |
45 |
| - const tokenResponse = await msalInstance.acquireTokenSilent({ |
46 |
| - scopes: ['api://hvalfangst-function-app/Csv.Writer'], |
47 |
| - account |
48 |
| - }); |
49 |
| - |
50 |
| - console.log('Token response:', tokenResponse); |
51 |
| - |
52 |
| - const token = tokenResponse.accessToken; |
53 |
| - |
54 |
| - const endpoint = 'https://hvalfangstlinuxfunctionapp.azurewebsites.net/api/upload_csv'; |
55 |
| - |
56 |
| - // Read the file as a text string |
57 |
| - const fileContent = await file.text(); |
58 |
| - |
59 |
| - const response = await fetch(endpoint, { |
60 |
| - method: 'POST', |
61 |
| - headers: { |
62 |
| - 'Content-Type': 'text/csv', // Set content type to CSV |
63 |
| - Authorization: `Bearer ${token}`, // Include OAuth token |
64 |
| - }, |
65 |
| - body: fileContent, // Send the file content as the body |
66 |
| - }); |
67 |
| - |
68 |
| - if (!response.ok) { |
69 |
| - const errorMessage = await response.text(); |
70 |
| - throw new Error(`Failed to upload: ${errorMessage}`); |
71 |
| - } |
72 |
| - |
73 |
| - console.log('Response:', response) |
74 |
| - |
75 |
| - console.log('File uploaded successfully'); |
76 |
| - setModalVisible(true); // Show success modal |
77 |
| - } catch (error) { |
78 |
| - console.error('Error uploading file:', error); |
79 |
| - setError(`Error uploading file: ${error.message}`); |
80 |
| - } |
81 |
| - }; |
82 |
| - |
83 |
| - const handleFileUpload = () => { |
84 |
| - if (file) { |
85 |
| - uploadFile(file); |
86 |
| - } else { |
87 |
| - setError('Please select a file to upload'); |
88 |
| - } |
89 |
| - }; |
90 |
| - |
91 |
| - const closeModal = () => { |
92 |
| - setModalVisible(false); |
93 |
| - }; |
94 |
| - |
95 |
| - const handleSignIn = async () => { |
96 |
| - try { |
97 |
| - await msalInstance.initialize(); |
98 |
| - setIsInitialized(true); |
99 |
| - const response = await msalInstance.loginPopup({ |
100 |
| - scopes: ['openid'], |
101 |
| - }); |
102 |
| - console.log('Sign in response:', response); |
103 |
| - console.log('User signed in successfully'); |
104 |
| - setUser(response.account); |
105 |
| - } catch (error) { |
106 |
| - console.error('Error signing in:', error); |
107 |
| - setError(`Error signing in: ${error.message}`); |
108 |
| - } |
109 |
| - }; |
110 |
| - |
111 |
| - const handleSignOut = async () => { |
112 |
| - if (!isInitialized) { |
113 |
| - console.error('MSAL instance is not initialized.'); |
114 |
| - return; |
115 |
| - } |
116 |
| - await msalInstance.logoutPopup(); |
117 |
| - setUser(null); |
118 |
| - }; |
119 |
| - |
120 |
| - useEffect(() => { |
121 |
| - const initializeMsal = async () => { |
122 |
| - await msalInstance.initialize(); |
123 |
| - setIsInitialized(true); |
124 |
| - const accounts = msalInstance.getAllAccounts(); |
125 |
| - if (accounts.length > 0) { |
126 |
| - setUser(accounts[0]); |
127 |
| - } |
128 |
| - }; |
129 |
| - |
130 |
| - initializeMsal(); |
131 |
| - }, []); |
132 |
| - |
133 | 9 | return (
|
134 |
| - <div className="csv-uploader"> |
135 |
| - <h1>CSV Uploader</h1> |
136 |
| - {user ? ( |
137 |
| - <div> |
138 |
| - <p>Welcome, {user.name}</p> |
139 |
| - <button onClick={handleSignOut}>Sign Out</button> |
140 |
| - </div> |
141 |
| - ) : ( |
142 |
| - <button onClick={handleSignIn}>Sign In</button> |
143 |
| - )} |
144 |
| - |
145 |
| - <input type="file" accept=".csv" onChange={handleFileChange} /> |
146 |
| - <button onClick={handleFileUpload}>Upload</button> |
147 |
| - {error && <p className="error">{error}</p>} |
148 |
| - |
149 |
| - {modalVisible && ( |
150 |
| - <div className="modal"> |
151 |
| - <div className="modal-content"> |
152 |
| - <h2>Upload Successful!</h2> |
153 |
| - <p>Your file has been uploaded to Azure Blob Storage via Azure Function.</p> |
154 |
| - <button onClick={closeModal}>Close</button> |
155 |
| - </div> |
156 |
| - </div> |
157 |
| - )} |
158 |
| - </div> |
| 10 | + <AuthProvider> |
| 11 | + <CsvUploader /> |
| 12 | + </AuthProvider> |
159 | 13 | );
|
160 | 14 | };
|
161 | 15 |
|
|
0 commit comments