Skip to content

Commit

Permalink
download logs as txt, dropbox or gdrive
Browse files Browse the repository at this point in the history
  • Loading branch information
bandinopla committed Jan 17, 2024
1 parent 34270cc commit 0a630c1
Show file tree
Hide file tree
Showing 14 changed files with 536 additions and 57 deletions.
70 changes: 70 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@types/react-dom": "^17.0.9",
"apollo-upload-client": "^16.0.0",
"codemirror": "^5.62.3",
"dropbox": "^10.34.0",
"firebase": "^9.6.11",
"firebaseui": "^6.0.1",
"fireworks": "^2.2.7",
Expand Down
4 changes: 4 additions & 0 deletions public/changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
- 2.20.0 : 2024-01-17
+ Added posibility to download the logs directly to Dropbox and Google Drive.
* Changed the position of the "download logs" box in the settings to the top of the settings.

- 2.19.5 : 2024-01-11
* Bugfix in SBD Stats section: unit convertion from KG to LB: https://github.com/bandinopla/weightxreps-client/issues/20
* Updated lifts data
Expand Down
Binary file added public/dropbox.webp
Binary file not shown.
Binary file added public/gdrive.webp
Binary file not shown.
Binary file added public/txt.webp
Binary file not shown.
4 changes: 2 additions & 2 deletions src/codemirror/LogTextEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -1549,7 +1549,7 @@ const __convertJEditorDataToText = (value, usekg, utags) => {
break;

case "JEditorBWTag": // viene en KILOS
console.log("BW TAG!!!", usekg, datum.bw)
//console.log("BW TAG!!!", usekg, datum.bw)
out.push( "@ "+ ( !usekg? kg2lb(datum.bw) : datum.bw ) +" bw" );
break;

Expand Down Expand Up @@ -1657,7 +1657,7 @@ const __convertJEditorDataToText = (value, usekg, utags) => {
.forEach( set=> {


console.log("********",set)
//console.log("********",set)

//
// nos fijamos si es una version "W,W,W" o solo "W"
Expand Down
127 changes: 127 additions & 0 deletions src/componentes/download-to-dropbox.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import { useEffect, useState } from "react";
import { Dropbox } from "dropbox";
import { AsciiSpinner } from "./ascii-spinner";
import { useGetSession } from "../session/session-handler";
import Alert from '@material-ui/lab/Alert';

const CLIENT_ID = 'g2a1ufuwx67ivex';

const getAuthenticationUrl = async () => {
var dbx = new Dropbox({ clientId: CLIENT_ID });
return dbx.auth.getAuthenticationUrl( window.location.origin+window.location.pathname, "dropbox" ) ;
}

const readAccessToken = ()=> {

const regex = /access_token=([^&]+).*state=dropbox/;
let m;
if ((m = regex.exec( window.location.hash )) !== null) {
return m[1];
}
}

const sendToDropbox = async (accessToken, filename, contents)=>{

var dbx = new Dropbox({ accessToken });
let op = await dbx.filesUpload({path: '/'+filename, contents, mode:"overwrite" });

if( op.status==200 )
{
return op.result.path_display;
}
else
{
throw new Error( op.result.error_summary );
}
}

export const DownloadToDropbox = ({selfFocus, txt, isFocused, close})=>{

const {session} = useGetSession();
const [error, setError] = useState(null);
const [loading, setLoading] = useState(false);
const [data, setData] = useState();
const [sent, setSent] = useState();
let token = readAccessToken();

useEffect(()=>{

if( token && !isFocused )
{
selfFocus(false);
}

},[]);

const onClose = ()=>{
setSent(null);
close();
}


if( loading )
{
if( data===true && txt )
{
setLoading(false);
}

return <AsciiSpinner label={data===true?"Packing your logs...":loading}/>
}

if( sent === false )
{
return <Alert severity="error" onClose={onClose}>{error}</Alert> ;
}

if( sent )
{
return <Alert onClose={onClose}>Logs uploaded to your dropbox folder named "weightxreps.net{sent}"</Alert> ;
}

if( isFocused )
{
if( token )
{
if( txt )
{
setData(
txt().then(
logsAsText=> sendToDropbox(token, session.user.uname+"--logs.txt", logsAsText)
)

.then( path=>{
setSent(path);
}, e=>{
setSent(false);
setError(e.message);
})

.finally(()=>{
setLoading(false);
setData(null)
})
);

setLoading("Sending to dropbox...");
}
else
{
setData(true); //<--- flag to indicate we are waiting for the logs
setLoading("Downloading your logs...");
selfFocus(true);
}

}
else
{
// pedir token
setLoading("Connecting to your Dropbox...");

getAuthenticationUrl()
.then( url=>window.open(url,"_self") );
}
}

return null;
};
Loading

0 comments on commit 0a630c1

Please sign in to comment.