Skip to content

Commit

Permalink
Added context Api in webapp for state managment
Browse files Browse the repository at this point in the history
  • Loading branch information
Shubh942 committed Jul 20, 2024
1 parent d0ca7a9 commit 77dd6c9
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 21 deletions.
69 changes: 58 additions & 11 deletions webapp/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 webapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"coverage": "vitest run --coverage"
},
"dependencies": {
"axios": "^1.7.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-icons": "^5.2.1",
Expand Down
9 changes: 7 additions & 2 deletions webapp/src/components/DebugHeader/DebugHeader.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "react";
import React, { useContext } from "react";
import {
FaArrowLeft,
FaArrowRight,
Expand All @@ -8,10 +8,13 @@ import {
import { IoReload } from "react-icons/io5";
import { MdSkipNext, MdSkipPrevious } from "react-icons/md";
import { BsArrowRightSquareFill } from "react-icons/bs";
import { DataContext } from "../../context/DataContext";

import "./DebugHeader.css";

const DebugHeader = () => {
const { refresh, setRefresh } = useContext(DataContext);

Check failure on line 16 in webapp/src/components/DebugHeader/DebugHeader.jsx

View workflow job for this annotation

GitHub Actions / test-npm

src/App.test.jsx > renders Threads component within Debug route

TypeError: Cannot destructure property 'refresh' of 'useContext(...)' as it is undefined. ❯ DebugHeader src/components/DebugHeader/DebugHeader.jsx:16:11 ❯ renderWithHooks node_modules/react-dom/cjs/react-dom.development.js:15486:18 ❯ mountIndeterminateComponent node_modules/react-dom/cjs/react-dom.development.js:20103:13 ❯ beginWork node_modules/react-dom/cjs/react-dom.development.js:21626:16 ❯ beginWork$1 node_modules/react-dom/cjs/react-dom.development.js:27465:14 ❯ performUnitOfWork node_modules/react-dom/cjs/react-dom.development.js:26599:12 ❯ workLoopSync node_modules/react-dom/cjs/react-dom.development.js:26505:5 ❯ renderRootSync node_modules/react-dom/cjs/react-dom.development.js:26473:7 ❯ recoverFromConcurrentError node_modules/react-dom/cjs/react-dom.development.js:25889:20 ❯ performConcurrentWorkOnRoot node_modules/react-dom/cjs/react-dom.development.js:25789:22

Check failure on line 16 in webapp/src/components/DebugHeader/DebugHeader.jsx

View workflow job for this annotation

GitHub Actions / test-npm

src/App.test.jsx > renders LocalVariable component within Debug route

TypeError: Cannot destructure property 'refresh' of 'useContext(...)' as it is undefined. ❯ DebugHeader src/components/DebugHeader/DebugHeader.jsx:16:11 ❯ renderWithHooks node_modules/react-dom/cjs/react-dom.development.js:15486:18 ❯ mountIndeterminateComponent node_modules/react-dom/cjs/react-dom.development.js:20103:13 ❯ beginWork node_modules/react-dom/cjs/react-dom.development.js:21626:16 ❯ beginWork$1 node_modules/react-dom/cjs/react-dom.development.js:27465:14 ❯ performUnitOfWork node_modules/react-dom/cjs/react-dom.development.js:26599:12 ❯ workLoopSync node_modules/react-dom/cjs/react-dom.development.js:26505:5 ❯ renderRootSync node_modules/react-dom/cjs/react-dom.development.js:26473:7 ❯ recoverFromConcurrentError node_modules/react-dom/cjs/react-dom.development.js:25889:20 ❯ performConcurrentWorkOnRoot node_modules/react-dom/cjs/react-dom.development.js:25789:22

Check failure on line 16 in webapp/src/components/DebugHeader/DebugHeader.jsx

View workflow job for this annotation

GitHub Actions / test-npm

src/App.test.jsx > renders Context component within Debug route

TypeError: Cannot destructure property 'refresh' of 'useContext(...)' as it is undefined. ❯ DebugHeader src/components/DebugHeader/DebugHeader.jsx:16:11 ❯ renderWithHooks node_modules/react-dom/cjs/react-dom.development.js:15486:18 ❯ mountIndeterminateComponent node_modules/react-dom/cjs/react-dom.development.js:20103:13 ❯ beginWork node_modules/react-dom/cjs/react-dom.development.js:21626:16 ❯ beginWork$1 node_modules/react-dom/cjs/react-dom.development.js:27465:14 ❯ performUnitOfWork node_modules/react-dom/cjs/react-dom.development.js:26599:12 ❯ workLoopSync node_modules/react-dom/cjs/react-dom.development.js:26505:5 ❯ renderRootSync node_modules/react-dom/cjs/react-dom.development.js:26473:7 ❯ recoverFromConcurrentError node_modules/react-dom/cjs/react-dom.development.js:25889:20 ❯ performConcurrentWorkOnRoot node_modules/react-dom/cjs/react-dom.development.js:25789:22

Check failure on line 16 in webapp/src/components/DebugHeader/DebugHeader.jsx

View workflow job for this annotation

GitHub Actions / test-npm

src/App.test.jsx > renders MemoryMap component within Debug route

TypeError: Cannot destructure property 'refresh' of 'useContext(...)' as it is undefined. ❯ DebugHeader src/components/DebugHeader/DebugHeader.jsx:16:11 ❯ renderWithHooks node_modules/react-dom/cjs/react-dom.development.js:15486:18 ❯ mountIndeterminateComponent node_modules/react-dom/cjs/react-dom.development.js:20103:13 ❯ beginWork node_modules/react-dom/cjs/react-dom.development.js:21626:16 ❯ beginWork$1 node_modules/react-dom/cjs/react-dom.development.js:27465:14 ❯ performUnitOfWork node_modules/react-dom/cjs/react-dom.development.js:26599:12 ❯ workLoopSync node_modules/react-dom/cjs/react-dom.development.js:26505:5 ❯ renderRootSync node_modules/react-dom/cjs/react-dom.development.js:26473:7 ❯ recoverFromConcurrentError node_modules/react-dom/cjs/react-dom.development.js:25889:20 ❯ performConcurrentWorkOnRoot node_modules/react-dom/cjs/react-dom.development.js:25789:22

Check failure on line 16 in webapp/src/components/DebugHeader/DebugHeader.jsx

View workflow job for this annotation

GitHub Actions / test-npm

src/App.test.jsx > renders BreakPoints component within Debug route

TypeError: Cannot destructure property 'refresh' of 'useContext(...)' as it is undefined. ❯ DebugHeader src/components/DebugHeader/DebugHeader.jsx:16:11 ❯ renderWithHooks node_modules/react-dom/cjs/react-dom.development.js:15486:18 ❯ mountIndeterminateComponent node_modules/react-dom/cjs/react-dom.development.js:20103:13 ❯ beginWork node_modules/react-dom/cjs/react-dom.development.js:21626:16 ❯ beginWork$1 node_modules/react-dom/cjs/react-dom.development.js:27465:14 ❯ performUnitOfWork node_modules/react-dom/cjs/react-dom.development.js:26599:12 ❯ workLoopSync node_modules/react-dom/cjs/react-dom.development.js:26505:5 ❯ renderRootSync node_modules/react-dom/cjs/react-dom.development.js:26473:7 ❯ recoverFromConcurrentError node_modules/react-dom/cjs/react-dom.development.js:25889:20 ❯ performConcurrentWorkOnRoot node_modules/react-dom/cjs/react-dom.development.js:25789:22

Check failure on line 16 in webapp/src/components/DebugHeader/DebugHeader.jsx

View workflow job for this annotation

GitHub Actions / test-npm

src/components/DebugHeader/__tests__/DebugHeader.test.jsx > DebugHeader Component > renders DebugHeader component with icons and filename

TypeError: Cannot destructure property 'refresh' of 'useContext(...)' as it is undefined. ❯ DebugHeader src/components/DebugHeader/DebugHeader.jsx:16:11 ❯ renderWithHooks node_modules/react-dom/cjs/react-dom.development.js:15486:18 ❯ mountIndeterminateComponent node_modules/react-dom/cjs/react-dom.development.js:20103:13 ❯ beginWork node_modules/react-dom/cjs/react-dom.development.js:21626:16 ❯ beginWork$1 node_modules/react-dom/cjs/react-dom.development.js:27465:14 ❯ performUnitOfWork node_modules/react-dom/cjs/react-dom.development.js:26599:12 ❯ workLoopSync node_modules/react-dom/cjs/react-dom.development.js:26505:5 ❯ renderRootSync node_modules/react-dom/cjs/react-dom.development.js:26473:7 ❯ recoverFromConcurrentError node_modules/react-dom/cjs/react-dom.development.js:25889:20 ❯ performConcurrentWorkOnRoot node_modules/react-dom/cjs/react-dom.development.js:25789:22

Check failure on line 16 in webapp/src/components/DebugHeader/DebugHeader.jsx

View workflow job for this annotation

GitHub Actions / test-npm

src/components/DebugHeader/__tests__/DebugHeader.test.jsx > DebugHeader Component > clicking Save button triggers save action

TypeError: Cannot destructure property 'refresh' of 'useContext(...)' as it is undefined. ❯ DebugHeader src/components/DebugHeader/DebugHeader.jsx:16:11 ❯ renderWithHooks node_modules/react-dom/cjs/react-dom.development.js:15486:18 ❯ mountIndeterminateComponent node_modules/react-dom/cjs/react-dom.development.js:20103:13 ❯ beginWork node_modules/react-dom/cjs/react-dom.development.js:21626:16 ❯ beginWork$1 node_modules/react-dom/cjs/react-dom.development.js:27465:14 ❯ performUnitOfWork node_modules/react-dom/cjs/react-dom.development.js:26599:12 ❯ workLoopSync node_modules/react-dom/cjs/react-dom.development.js:26505:5 ❯ renderRootSync node_modules/react-dom/cjs/react-dom.development.js:26473:7 ❯ recoverFromConcurrentError node_modules/react-dom/cjs/react-dom.development.js:25889:20 ❯ performConcurrentWorkOnRoot node_modules/react-dom/cjs/react-dom.development.js:25789:22

return (
<div className="parent-debug-header">
<div className="debug-header">
Expand All @@ -33,7 +36,9 @@ const DebugHeader = () => {
<div className="filename-content">filename</div>
</div>
<div className="save">
<button className="save-button">Save</button>
<button className="save-button" onClick={() => setRefresh(!refresh)}>
{refresh ? "Saving.." : "Save"}
</button>
</div>
</div>
</div>
Expand Down
30 changes: 30 additions & 0 deletions webapp/src/context/DataContext.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React, { createContext, useState, useEffect, useCallback } from "react";
import axios from "axios";

export const DataContext = createContext();

export const DataProvider = ({ children }) => {
const [refresh, setRefresh] = useState(false);
const [stack, setStack] = useState([]);
const [functions, setFunctions] = useState([]);

const fetchData = useCallback(async () => {
if (refresh) {
try {
} catch (error) {
console.error("Error fetching data:", error);
setRefresh(!refresh);
}
}
});

useEffect(() => {
fetchData();
}, [refresh]);

return (
<DataContext.Provider value={{ refresh, setRefresh, stack, functions }}>
{children}
</DataContext.Provider>
);
};
19 changes: 11 additions & 8 deletions webapp/src/main.jsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
// main.jsx
import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import App from "./App";

import { BrowserRouter } from "react-router-dom";
import { DataProvider } from "./context/DataContext";
import App from "./App";
import "./index.css";

ReactDOM.render(
<BrowserRouter>
<React.StrictMode>
<App />
</React.StrictMode>
</BrowserRouter>,
<DataProvider>
<BrowserRouter>
<React.StrictMode>
<App />
</React.StrictMode>
</BrowserRouter>
</DataProvider>,
document.getElementById("root")
);

0 comments on commit 77dd6c9

Please sign in to comment.