Skip to content

Commit

Permalink
Merge sheets and groups / add log wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalObi committed Feb 9, 2021
1 parent 6441ed5 commit 7388f3e
Showing 1 changed file with 44 additions and 34 deletions.
78 changes: 44 additions & 34 deletions FE/simple-sheet-fe/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,55 @@ import logo from './logo.svg';
import './App.css';

function App() {
const [error, setError] = useState(null),
[isLoaded, setIsLoaded] = useState(false),
[items, setItems] = useState([]),
handleAjaxError = error => {
const useStateCallbackWrapper = (initilValue, callBack) => {
const [state, setState] = useState(initilValue);

useEffect(() => callBack(state), [state]);
return [state, setState];
};

const log = (key, state) => console.log(`${key}`, state);

const [error, setError] = useStateCallbackWrapper(null, state => log('error', state)),
[isLoaded, setIsLoaded] = useStateCallbackWrapper(false, state => log('isLoaded', state)),
[sheetsAndGroups, setSheetsAndGroups] = useStateCallbackWrapper({}, state => log('sheetsAndGroups', state));

const handleAjaxError = error => {
setIsLoaded(true);
setError(error);

console.log('err', error);
};

useEffect(() => {
fetch('/sheets/1')
.then(res => res.json())
.then(
res => {
console.log('sheets', res);

fetch(`/groups/${res.id}`)
.then(res => res.json())
.then(res => {
setIsLoaded(true);
console.log('groups', res);
}, handleAjaxError)
useEffect(() => {
fetch('/sheets/1')
.then(sheets => sheets.json())
.then(sheets => {
if (sheets) {
fetch(`/groups/${sheets.id}`)
.then(groups => groups.json())
.then(groups => {
const sheetsAndGroups = Object.assign({}, sheets, groups);
delete sheetsAndGroups.id;

setSheetsAndGroups(sheetsAndGroups);
setIsLoaded(true);
}, handleAjaxError)
}, []);

if (error) {
return <p>error</p>;
} else if (!isLoaded) {
return <p>loading</p>;
} else {
return (
<div className="App">
<header className="App-header">
Simple Sheet
</header>
</div>
);
}
}
}, handleAjaxError)
}, []);

if (error) {
return <p>error</p>;
} else if (!isLoaded) {
return <p>loading</p>;
} else {
return (
<div className="App">
<header className="App-header">
Simple Sheet
</header>
</div>
);
}
}

export default App;

0 comments on commit 7388f3e

Please sign in to comment.