parent
8c7f509bc8
commit
d5b3ee3c53
File diff suppressed because it is too large
Load Diff
@ -1,11 +1,16 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
|
||||||
function App() {
|
import AppContextProvider from './ContextProvider';
|
||||||
return (
|
|
||||||
<div>
|
import Pool from './Pool';
|
||||||
just wow.
|
|
||||||
</div>
|
import '../static/css/main.css';
|
||||||
);
|
|
||||||
}
|
|
||||||
|
const App = () => (
|
||||||
|
<AppContextProvider>
|
||||||
|
<Pool />
|
||||||
|
</AppContextProvider>
|
||||||
|
);
|
||||||
|
|
||||||
export default App;
|
export default App;
|
||||||
|
@ -0,0 +1,38 @@
|
|||||||
|
import React from 'react';
|
||||||
|
|
||||||
|
import useAppState from './useAppState';
|
||||||
|
import ApiHelper from '../helpers/ApiHelper';
|
||||||
|
import { useEffectOnce } from '../helpers/Hooks';
|
||||||
|
|
||||||
|
|
||||||
|
export const AppContext = React.createContext();
|
||||||
|
|
||||||
|
const AppContextProvider = props => {
|
||||||
|
const [state, dispatch] = useAppState();
|
||||||
|
const Api = new ApiHelper({ state });
|
||||||
|
|
||||||
|
|
||||||
|
const getPoolConfig = () => {
|
||||||
|
Api.getPoolConfig()
|
||||||
|
.then(config => {
|
||||||
|
dispatch({ type: 'UPDATE_POOL_CONFIG', config });
|
||||||
|
dispatch({ type: 'APP_LOADED', appLoaded: true });
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const actions = {
|
||||||
|
getPoolConfig,
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffectOnce(() => {
|
||||||
|
getPoolConfig();
|
||||||
|
})
|
||||||
|
|
||||||
|
return (
|
||||||
|
<AppContext.Provider value={{ state, actions }}>
|
||||||
|
{state.appSettings.appLoaded && props.children}
|
||||||
|
</AppContext.Provider>
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
export default AppContextProvider;
|
@ -0,0 +1,10 @@
|
|||||||
|
import React from 'react';
|
||||||
|
|
||||||
|
|
||||||
|
const Pool = () => (
|
||||||
|
<div>
|
||||||
|
<p>welcome good friend <3 thank so much for mining!</p>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
|
||||||
|
export default Pool;
|
@ -0,0 +1,67 @@
|
|||||||
|
import { useReducer, useRef } from 'react';
|
||||||
|
|
||||||
|
|
||||||
|
const initialState = () => ({
|
||||||
|
appSettings: {
|
||||||
|
apiURL: 'https://pool.wowne.ro/api',
|
||||||
|
appLoaded: false,
|
||||||
|
coinDecimals: 11,
|
||||||
|
},
|
||||||
|
pool: {
|
||||||
|
config: null,
|
||||||
|
},
|
||||||
|
user: {
|
||||||
|
loggedIn: false,
|
||||||
|
},
|
||||||
|
messages: {},
|
||||||
|
});
|
||||||
|
|
||||||
|
let updatedState;
|
||||||
|
|
||||||
|
const reducer = (state, action) => {
|
||||||
|
let result = {};
|
||||||
|
const {
|
||||||
|
appLoaded,
|
||||||
|
config,
|
||||||
|
loggedIn,
|
||||||
|
} = action;
|
||||||
|
|
||||||
|
switch (action.type) {
|
||||||
|
case 'APP_LOADED':
|
||||||
|
result = {
|
||||||
|
...state,
|
||||||
|
appSettings: {
|
||||||
|
...state.appSettings,
|
||||||
|
appLoaded,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
case 'CLEAR_MESSAGES':
|
||||||
|
result = { ...state, messages: {} };
|
||||||
|
break;
|
||||||
|
case 'USER_LOGGED_IN':
|
||||||
|
result = { ...state, user: { loggedIn } };
|
||||||
|
break;
|
||||||
|
case 'UPDATE_POOL_CONFIG':
|
||||||
|
result = {
|
||||||
|
...state,
|
||||||
|
pool: {
|
||||||
|
...state.pool,
|
||||||
|
config,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new Error();
|
||||||
|
}
|
||||||
|
updatedState.current = result;
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
|
||||||
|
const useAppState = () => {
|
||||||
|
const state = initialState();
|
||||||
|
updatedState = useRef(state);
|
||||||
|
return [...useReducer(reducer, state), updatedState];
|
||||||
|
};
|
||||||
|
|
||||||
|
export default useAppState;
|
@ -0,0 +1,31 @@
|
|||||||
|
export default class ApiHelper {
|
||||||
|
constructor(options) {
|
||||||
|
this.apiURL = options.state.appSettings.apiURL;
|
||||||
|
}
|
||||||
|
|
||||||
|
getPoolConfig = () => {
|
||||||
|
return this.fetch(`${this.apiURL}/config`, { method: 'GET' })
|
||||||
|
.then(res => Promise.resolve(res));
|
||||||
|
}
|
||||||
|
|
||||||
|
fetch = (url, options) => {
|
||||||
|
const headers = options.headers || {
|
||||||
|
Accept: 'application/json',
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
};
|
||||||
|
|
||||||
|
return fetch(url, { headers, ...options })
|
||||||
|
.then(this._checkStatus)
|
||||||
|
.then(response => response.json());
|
||||||
|
};
|
||||||
|
|
||||||
|
_checkStatus = response => {
|
||||||
|
if (response.status >= 200 && response.status < 300) {
|
||||||
|
return response;
|
||||||
|
} else {
|
||||||
|
const error = new Error(response.statusText);
|
||||||
|
error.response = response;
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
@ -0,0 +1,4 @@
|
|||||||
|
import { useEffect } from 'react';
|
||||||
|
|
||||||
|
|
||||||
|
export const useEffectOnce = fn => useEffect(fn, []);
|
Loading…
Reference in new issue