Adding Redux to project, updating to version 0.1.0

This commit is contained in:
2023-10-06 23:13:57 +00:00
parent d4f2474436
commit ec020257e0
18 changed files with 16724 additions and 48 deletions

View File

@ -0,0 +1,27 @@
import React from 'react';
import { Provider } from 'react-redux';
import { Router } from 'react-router-dom';
import { createMemoryHistory } from 'history';
import initialStateReducer from '../frontend/reducers/initialState';
import setStore from '../frontend/setStore';
export const ProviderMock = ({ children, initialState }: { children: unknown, initialState?: unknown}) => {
let initialStateMock = initialStateReducer;
if(initialState !== undefined){
initialStateMock = initialState as unknown as typeof initialStateReducer;
}
const history = createMemoryHistory();
const store = setStore({ initialState: initialStateMock });
return(
<Provider store={store}>
<Router location={history.location} navigator={history}>
{children as JSX.Element}
</Router>
</Provider>
);
};
export default ProviderMock;

View File

@ -0,0 +1,2 @@
export const fileMock = '';
module.exports = fileMock;

1
src/__mocks__/index.ts Normal file
View File

@ -0,0 +1 @@
export * from './ProviderMock';

View File

@ -3,7 +3,7 @@ import PrincipalRoutes from './PrincipalRoutes';
const App = () => {
useEffect(() => {
const ws = new WebSocket('wss://xs70kvlc-3000.use.devtunnels.ms/ws');
const ws = new WebSocket('wss://nmr4jbx8-3000.use.devtunnels.ms/ws');
ws.onmessage = (event) => {
if (event.data === 'reload') {
window.location.reload();

View File

@ -1,25 +1,36 @@
import React from 'react';
import './InitialComponent.scss';
import { connect } from 'react-redux';
import { Link } from 'react-router-dom';
const InitialComponent = () => (
<div className="App">
<header className="App-header">
<img src="assets/img/logo.svg" className="App-logo" alt="logo" />
<p>
Edit <code>src/frontend/InitialComponent.jsx</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
<Link className="App-link" to="/other-component">Other Component</Link>
</header>
</div>
);
const InitialComponent = ({ hello }: { hello: string }) => {
export default InitialComponent;
return(
<div className="App">
<header className="App-header">
<img src="assets/img/logo.svg" className="App-logo" alt="logo" />
<p>This is the text from the store of redux: <strong>{hello}</strong></p>
<p>
Edit <code>src/frontend/InitialComponent.jsx</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
<Link className="App-link" to="/other-component">Other Component</Link>
</header>
</div>
);
};
const mapStateToProps = (state) => {
return {
hello: state.testReducer.hello
};
};
export default connect(mapStateToProps)(InitialComponent);

View File

@ -1,15 +1,23 @@
import React from 'react';
//Redux
import { Provider } from 'react-redux';
import setStore from '../setStore';
import initialState from '../reducers/initialState';
import { renderToString } from 'react-dom/server';
import { StaticRouter } from 'react-router-dom/server';
import App from '../components/App';
const url = process.argv[2];
const store = setStore({ initialState });
const render = () => {
return renderToString(
<StaticRouter location={`${url}`} >
<App />
</StaticRouter>
<Provider store={store}>
<StaticRouter location={`${url}`} >
<App />
</StaticRouter>
</Provider>
);
};

View File

@ -0,0 +1,5 @@
import setStore from '../setStore';
import initialState from '../reducers/initialState';
const store = setStore({ initialState });
const preloadedState = store.getState();
console.log(preloadedState);

View File

@ -10,7 +10,7 @@ import { config } from '../../config';
import './styles/global.scss';
import App from './components/App';
// import serviceWorkerRegistration from '../../serviceWorkerRegistration';
import serviceWorkerRegistration from '../../serviceWorkerRegistration';
declare global {
interface Window {
@ -72,10 +72,10 @@ ENV === 'production' && hydrateRoot(container,
</Provider>
); */
/* if((ENV) && (ENV === 'production')){
if((ENV) && (ENV === 'production')){
serviceWorkerRegistration();
} */
}
/* if(module.hot){
/*if(module.hot){
module.hot.accept();
} */
}*/

View File

@ -1,2 +1,3 @@
const initialState = {};
import { IInitialState } from './';
const initialState: IInitialState = {};
export default initialState;

View File

@ -6,7 +6,7 @@ import (
)
func LoadEnv() {
err := godotenv.Load(".env")
err := godotenv.Load("../../.env")
if err != nil {
log.Fatal("Error loading .env file")
}

View File

@ -0,0 +1,37 @@
package utils
import (
"fmt"
"os/exec"
)
func GetPreloadedState() string {
// Set the path to the jsxToString.js file
jsFilePath := "./web/getstate/getPreloadedState.js"
// Create the command to run Node.js with the jsxToString.js file
cmd := exec.Command("node", jsFilePath)
// Get a pipe to the standard input of the Node.js process
stdin, err := cmd.StdinPipe()
if err != nil {
fmt.Println(err)
return ""
}
// Write the URL parameter to the standard input of the Node.js process
//fmt.Fprintf(stdin, "%s\n", url)
// Close the standard input pipe
stdin.Close()
// Get the output and error of the Node.js process
output, err := cmd.CombinedOutput()
if err != nil {
fmt.Println(err)
return string(output)
}
// Return the output of the Node.js process
return string(output)
}

View File

@ -7,7 +7,7 @@ import (
func JsxToString(url string) string {
// Set the path to the jsxToString.js file
jsFilePath := "./web/utils/tsxToString.js"
jsFilePath := "./web/conversion/tsxToString.js"
// Create the command to run Node.js with the jsxToString.js file
cmd := exec.Command("node", jsFilePath, url)

View File

@ -32,6 +32,7 @@ func RegisterHandlers(e *echo.Echo, paths []string) {
//return c.File(filePath)
url := c.Request().URL.String()
component := utils.JsxToString(url)
preloadedState := utils.GetPreloadedState();
html := `<!DOCTYPE html>
<html lang="es">
<head>
@ -47,9 +48,9 @@ func RegisterHandlers(e *echo.Echo, paths []string) {
</head>
<body>
<div id="app">`+ component +`</div>
<!-- <script>
window.__PRELOADED_STATE__ = ${JSON.stringify(preloadedState).replace(/</g, '\\u003c')}
</script> -->
<script>
window.__PRELOADED_STATE__ = JSON.stringify(`+ preloadedState+`).replace(/</g, '\\u003c')
</script>
<script src="assets/app-frontend.js" type="text/javascript"></script>
<script src="assets/vendor-vendors.js" type="text/javascript"></script>
</body>