-
Notifications
You must be signed in to change notification settings - Fork 7
Miryam/google drive #177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Miryam/google drive #177
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| REACT_APP_BASE_URL='http://localhost:3000' | ||
| REACT_APP_SECRET_CODE_CAPVAL='6Ld5uBoqAAAAAKwPXqo5eanm9ZFSuOoBBSdl00pE' | ||
| REACT_APP_SERVER_URL='http://localhost:5000' | ||
| REACT_APP_GOOGLE_CLIENT_ID='1074410346984-b9bsnokpb84s4afiim9t9d797k6orsvk.apps.googleusercontent.com' |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import axios from 'axios'; | ||
|
|
||
| const axiosInstance = axios.create({ | ||
| baseURL: process.env.REACT_APP_SERVER_URL, | ||
| }); | ||
|
|
||
| axiosInstance.interceptors.request.use( | ||
| (config) => { | ||
| const token = localStorage.getItem('authToken'); | ||
| if (token) { | ||
| config.headers['Authorization'] = `Bearer ${token}`; | ||
| } | ||
| return config; | ||
| }, | ||
| (error) => { | ||
| return Promise.reject(error); | ||
| } | ||
| ); | ||
|
|
||
| axiosInstance.interceptors.response.use( | ||
| (response) => { | ||
| return response; | ||
| }, | ||
| (error) => { | ||
| if (error.response.status === 401) { | ||
| window.location.href = '/login'; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what means this window.location? |
||
| } | ||
| return Promise.reject(error); | ||
| } | ||
| ); | ||
|
|
||
| export default axiosInstance; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,21 @@ | ||
| import axios from 'axios'; | ||
| import axiosInstance from './axiosInstance'; | ||
|
|
||
| const url = process.env.REACT_APP_SERVER_URL; | ||
|
|
||
| export async function handleGet(path) { | ||
| const response = await axios.create({ baseURL: url }).get(path); | ||
| export async function handleGet(path, config = {}) { | ||
| const response = await axiosInstance.get(path, config); | ||
| return response; | ||
|
|
||
| }; | ||
|
|
||
| export async function handlePost(path, data) { | ||
| const response = await axios.create({ baseURL: url }).post(path, data); | ||
| export async function handlePost(path, data, config = {}) { | ||
| const response = await axiosInstance.post(path, data, config); | ||
| return response; | ||
|
|
||
| }; | ||
|
|
||
| export async function handlePut(path, data) { | ||
| const response = await axios.create({ baseURL: url }).put(path, data); | ||
| export async function handlePut(path, data, config = {}) { | ||
| const response = await axiosInstance.put(path, data, config); | ||
| return response; | ||
|
|
||
| }; | ||
|
|
||
| export async function handleDelete(path) { | ||
| const response = await axios.create({ baseURL: url }).delete(path); | ||
| export async function handleDelete(path, config = {}) { | ||
| const response = await axiosInstance.delete(path, config); | ||
| return response; | ||
|
|
||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import React, { useEffect } from 'react'; | ||
| import { gapi } from 'gapi-script'; | ||
| import GenericButton from '../../stories/Button/GenericButton'; | ||
| import { GOOGLE_API, MESSAGES, BUTTON_LABELS } from '../../constants/googleDriveConstants'; | ||
|
|
||
| const GoogleDriveUploader = () => { | ||
| const CLIENT_ID = process.env.REACT_APP_GOOGLE_CLIENT_ID; | ||
|
|
||
| useEffect(() => { | ||
| function start() { | ||
| gapi.client.init({ | ||
| clientId: CLIENT_ID, | ||
| scope: GOOGLE_API.scopes, | ||
| }).then(() => { | ||
| console.log(MESSAGES.gapiClientInitialized); | ||
| }).catch(error => { | ||
| console.error(MESSAGES.gapiInitError, error); | ||
| }); | ||
| } | ||
|
|
||
| gapi.load('client:auth2', start); | ||
| }, [CLIENT_ID]); | ||
|
|
||
| const handleAuthClick = async () => { | ||
| try { | ||
| await gapi.auth2.getAuthInstance().signIn({ | ||
| prompt: GOOGLE_API.prompt, | ||
| }); | ||
| console.log(MESSAGES.gapiSignInSuccess); | ||
| } catch (error) { | ||
| console.error(MESSAGES.signInError, error); | ||
| } | ||
| }; | ||
|
|
||
| const handleSignoutClick = () => { | ||
| gapi.auth2.getAuthInstance().signOut(); | ||
| }; | ||
|
|
||
| return ( | ||
| <div> | ||
| <GenericButton | ||
| label={BUTTON_LABELS.signIn} | ||
| onClick={handleAuthClick} | ||
| className="signInButton" | ||
| size="large" | ||
| /> | ||
| <GenericButton | ||
| label={BUTTON_LABELS.signOut} | ||
| onClick={handleSignoutClick} | ||
| className="signOutButton" | ||
| size="large" | ||
| /> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export default GoogleDriveUploader; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import React from 'react'; | ||
| import GoogleDriveUploader from './googleDriveUploader'; | ||
| import UploadToGoogleDrive from './uploadToGoogleDrive'; | ||
|
|
||
| const ManagerGoogleDrive = () => { | ||
| const fileContent = "Hello, world!"; | ||
| const fileName = 'yourfile.txt'; | ||
| const fileMimeType = 'text/plain'; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is this hard-coded values? |
||
|
|
||
| return ( | ||
| <div> | ||
| <UploadToGoogleDrive | ||
| fileContent={fileContent} | ||
| fileName={fileName} | ||
| fileMimeType={fileMimeType} | ||
| /> | ||
| <GoogleDriveUploader /> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export default ManagerGoogleDrive; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import { useGoogleLogin } from '@react-oauth/google'; | ||
| import GenericButton from '../../stories/Button/GenericButton'; | ||
| import { FILE_DETAILS, URLS, MESSAGES, BUTTON_LABELS } from '../../constants/googleDriveConstants'; | ||
| import { handlePost } from '../../axios/middleware'; | ||
|
|
||
| const UploadToGoogleDrive = () => { | ||
| const CLIENT_ID = process.env.REACT_APP_GOOGLE_CLIENT_ID; | ||
|
|
||
| const login = useGoogleLogin({ | ||
| clientId: CLIENT_ID, | ||
| onSuccess: async (tokenResponse) => { | ||
| const accessToken = tokenResponse.access_token; | ||
|
|
||
| const fileData = new Blob([FILE_DETAILS.content], { type: FILE_DETAILS.mimeType }); | ||
|
|
||
| const form = new FormData(); | ||
| form.append('metadata', new Blob([JSON.stringify({ name: FILE_DETAILS.name, mimeType: FILE_DETAILS.mimeType })], { type: 'application/json' })); | ||
| form.append('file', fileData); | ||
|
|
||
| try { | ||
| const response = await handlePost( | ||
| URLS.uploadUrl, | ||
| form, | ||
| { | ||
| headers: { | ||
| Authorization: `Bearer ${accessToken}`, | ||
| 'Content-Type': 'multipart/related', | ||
| }, | ||
| } | ||
| ); | ||
| console.log('File uploaded successfully:', response); | ||
| } catch (error) { | ||
| console.error(MESSAGES.uploadError, error); | ||
| } | ||
| }, | ||
| onError: error => console.error(MESSAGES.loginFailed, error) | ||
| }); | ||
|
|
||
| return ( | ||
| <div> | ||
| <GenericButton | ||
| label={BUTTON_LABELS.upload} | ||
| onClick={login} | ||
| className="uploadButton" | ||
| size="large" | ||
| /> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export default UploadToGoogleDrive; | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| export const BUTTON_LABELS = { | ||
| upload: 'Upload to Google Drive', | ||
| signIn: 'Sign in with Google', | ||
| signOut: 'Sign out', | ||
| }; | ||
|
|
||
| export const FILE_DETAILS = { | ||
| name: 'yourfile.txt', | ||
| mimeType: 'text/plain', | ||
| content: 'Hello, world!', | ||
| }; | ||
|
|
||
| export const URLS = { | ||
| uploadUrl: 'https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart', | ||
| }; | ||
|
|
||
| export const MESSAGES = { | ||
| loginFailed: 'Login failed:', | ||
| uploadError: 'Error uploading file:', | ||
| signInError: 'Error signing in:', | ||
| signOutError: 'Error signing out:', | ||
| gapiInitError: 'Error initializing GAPI client:', | ||
| gapiSignInSuccess: 'Successfully signed in', | ||
| gapiClientInitialized: 'GAPI client initialized', | ||
| }; | ||
|
|
||
| export const GOOGLE_API = { | ||
| clientId: '1074410346984-b9bsnokpb84s4afiim9t9d797k6orsvk.apps.googleusercontent.com', | ||
| scopes: 'https://www.googleapis.com/auth/drive.file', | ||
| prompt: 'select_account', | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,8 @@ | ||
| import React from "react"; | ||
| import {createBrowserRouter } from "react-router-dom"; | ||
| import ProfileList from "../components/profileComponent.jsx"; | ||
| import Layout from "./layout.jsx"; | ||
| import Login from "../login/Login.jsx"; | ||
| import ManagerGoogleDrive from "../components/Report/managerGoogleDrive.jsx"; | ||
| export const router = createBrowserRouter([ | ||
| { | ||
| path: '', | ||
|
|
@@ -16,13 +16,13 @@ export const router = createBrowserRouter([ | |
| path: '/home', | ||
| element: <h1>home</h1> | ||
| }, | ||
| { | ||
| path: '/profiles', | ||
| element:<ProfileList/> | ||
| }, | ||
| { | ||
| path: '/login', | ||
| element: <Login/> | ||
| }, | ||
| { | ||
| path: '/reports', | ||
| element: <ManagerGoogleDrive/> | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is not the reports UI, this should be only a button inside the reports page |
||
| } | ||
| ] | ||
| }, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
re-order the imports