1- import React , { Component } from 'react' ;
2- import { Container , Paper , Typography , Button , Grid , TextField } from '@material-ui/core' ;
1+ import React , {
2+ Component
3+ } from 'react' ;
4+ import {
5+ Container ,
6+ Paper ,
7+ Typography ,
8+ Button ,
9+ Grid ,
10+ TextField
11+ } from '@material-ui/core' ;
312import axios from 'axios' ;
4- import { MathJax , MathJaxContext } from 'better-react-mathjax' ;
13+ import {
14+ MathJax ,
15+ MathJaxContext
16+ } from 'better-react-mathjax' ;
517import Plot from 'react-plotly.js' ; // Import Plotly component
618import * as TransferFuncVar from './TransferFuncVar.js' ;
719import HomeIcon from "@material-ui/icons/Home" ;
@@ -14,7 +26,7 @@ class TransferFunc extends Component {
1426 A : TransferFuncVar . A ,
1527 B : TransferFuncVar . B ,
1628 C : TransferFuncVar . C ,
17- version : packagejson . version ,
29+ version : packagejson . version ,
1830 token : '' ,
1931 errorA : false ,
2032 errorB : false ,
@@ -27,48 +39,80 @@ class TransferFunc extends Component {
2739 // Validation function
2840 validateInputs = ( ) => {
2941 let valid = true ;
30- const { A, B, C, token, version } = this . state ;
42+ const {
43+ A,
44+ B,
45+ C,
46+ token,
47+ version
48+ } = this . state ;
3149
3250 if ( ! A || isNaN ( A ) ) {
33- this . setState ( { errorA : true } ) ;
51+ this . setState ( {
52+ errorA : true
53+ } ) ;
3454 valid = false ;
3555 } else {
36- this . setState ( { errorA : false } ) ;
56+ this . setState ( {
57+ errorA : false
58+ } ) ;
3759 }
3860
3961 if ( ! B || isNaN ( B ) ) {
40- this . setState ( { errorB : true } ) ;
62+ this . setState ( {
63+ errorB : true
64+ } ) ;
4165 valid = false ;
4266 } else {
43- this . setState ( { errorB : false } ) ;
67+ this . setState ( {
68+ errorB : false
69+ } ) ;
4470 }
4571
4672 if ( ! C || isNaN ( C ) ) {
47- this . setState ( { errorC : true } ) ;
73+ this . setState ( {
74+ errorC : true
75+ } ) ;
4876 valid = false ;
4977 } else {
50- this . setState ( { errorC : false } ) ;
78+ this . setState ( {
79+ errorC : false
80+ } ) ;
5181 }
5282
5383 if ( ! version ) {
54- this . setState ( { errorVersion : true } ) ;
84+ this . setState ( {
85+ errorVersion : true
86+ } ) ;
5587 valid = false ;
5688 } else {
57- this . setState ( { errorVersion : false } ) ;
89+ this . setState ( {
90+ errorVersion : false
91+ } ) ;
5892 }
5993
6094 if ( ! token ) {
61- this . setState ( { errorToken : true } ) ;
95+ this . setState ( {
96+ errorToken : true
97+ } ) ;
6298 valid = false ;
6399 } else {
64- this . setState ( { errorToken : false } ) ;
100+ this . setState ( {
101+ errorToken : false
102+ } ) ;
65103 }
66104
67105 return valid ;
68106 } ;
69107
70108 uploadVar = async ( ) => {
71- const { A, B, C, token } = this . state ;
109+ const {
110+ A,
111+ B,
112+ C,
113+ token,
114+ version
115+ } = this . state ;
72116
73117 if ( ! this . validateInputs ( ) ) return ;
74118
@@ -80,8 +124,7 @@ class TransferFunc extends Component {
80124 const getFileSHA = async ( ) => {
81125 try {
82126 const res = await axios . get (
83- `https://api.github.com/repos/${ repoOwner } /${ repoName } /contents/${ filePath } ?ref=${ branch } ` ,
84- {
127+ `https://api.github.com/repos/${ repoOwner } /${ repoName } /contents/${ filePath } ?ref=${ branch } ` , {
85128 headers : {
86129 Authorization : `token ${ token } ` ,
87130 } ,
@@ -94,28 +137,48 @@ class TransferFunc extends Component {
94137 }
95138 } ;
96139
97- this . setState ( { loading : true } ) ; // Set loading to true before making the request
140+ this . setState ( {
141+ loading : true
142+ } ) ;
98143 const sha = await getFileSHA ( ) ;
99- console . log ( 'File SHA:' , sha ) ;
100- this . setState ( { loading : false } ) ; // Reset loading after the request is done
144+ this . setState ( {
145+ loading : false
146+ } ) ; // Reset loading after the request is done
101147 const updatedContent = `
102- export const A = ${ A } ;
103- export const B = ${ B } ;
104- export const C = ${ C } ;
105- ` ;
148+ export const A = ${ A } ;
149+ export const B = ${ B } ;
150+ export const C = ${ C } ;
151+ `;
106152
107153 const content = btoa ( unescape ( encodeURIComponent ( updatedContent ) ) ) ; // Convert text to base64
154+ const updatedPackageJson = {
155+ ...packagejson , // Current content
156+ version : version , // New version
157+ } ;
158+
159+ const updatedPackageJsonContent = JSON . stringify ( updatedPackageJson , null , 2 ) ; // Format the JSON
160+
161+ const contentPackageJson = btoa ( unescape ( encodeURIComponent ( updatedPackageJsonContent ) ) ) ; // Base64 encode
108162
109163 try {
110164 const response = await axios . put (
111- `https://api.github.com/repos/${ repoOwner } /${ repoName } /contents/${ filePath } ` ,
112- {
165+ `https://api.github.com/repos/${ repoOwner } /${ repoName } /contents/${ filePath } ` , {
113166 message : sha ? 'Update TransferFuncVar.js file' : 'Create TransferFuncVar.js file' ,
114167 content : content ,
115- sha : sha ,
116168 branch : branch ,
117- } ,
118- {
169+ } , {
170+ headers : {
171+ Authorization : `token ${ token } ` ,
172+ } ,
173+ }
174+ ) ;
175+
176+ await axios . put (
177+ `https://api.github.com/repos/${ repoOwner } /${ repoName } /contents/package.json` , {
178+ message : 'Update package.json version' ,
179+ content : contentPackageJson ,
180+ branch : branch ,
181+ } , {
119182 headers : {
120183 Authorization : `token ${ token } ` ,
121184 } ,
0 commit comments