From 3e453e73f336e99234f9354da58952698453eaa1 Mon Sep 17 00:00:00 2001 From: Sam Burdick Date: Tue, 27 Feb 2024 00:44:34 -0800 Subject: [PATCH] Update reducer for set unit cell, need to fix infinite loop --- frontend/src/actions.js | 14 ++++---------- frontend/src/control-flow.js | 17 +++++++++++------ frontend/src/reducers.js | 30 +++++++++++++----------------- frontend/src/store.js | 4 ---- 4 files changed, 28 insertions(+), 37 deletions(-) diff --git a/frontend/src/actions.js b/frontend/src/actions.js index dc75a1a2..9bfe4281 100644 --- a/frontend/src/actions.js +++ b/frontend/src/actions.js @@ -1,12 +1,6 @@ -export const ADD_QUBIT = 'ADD_QUBIT'; -export const REMOVE_QUBIT = 'REMOVE_QUBIT'; +export const SET_UNIT_CELL = 'SET_UNIT_CELL'; -export const addQubit = (qubit) => ({ - type: ADD_QUBIT, - payload: qubit, -}); - -export const remoteQubit = (qubit) => ({ - type: REMOVE_QUBIT, - payload: qubit, +export const setUnitCell = (unitCell) => ({ + type: SET_UNIT_CELL, + payload: unitCell, }); diff --git a/frontend/src/control-flow.js b/frontend/src/control-flow.js index b562e516..4dd4205e 100644 --- a/frontend/src/control-flow.js +++ b/frontend/src/control-flow.js @@ -12,7 +12,6 @@ import QubitLattice from './qubits/QubitLattice'; import Button from './components/Button'; import DownloadButton from './components/download/DownloadButton'; import store from './store'; -import { addQubit } from './actions'; /** * Defines how the app behaves (button and feature placement) upon initialization @@ -25,7 +24,8 @@ export default function InitializeControlFlow() { const workspace = new Container(); workspace.name = 'workspace'; const grid = new Grid(gridSize, workspace, app); - + // Add qubits from redux store + // const storedUnitCell = store.getState().unitCell; workspace.addChild(grid); grid.units.forEach((row) => { row.forEach((unit) => { @@ -143,10 +143,17 @@ export default function InitializeControlFlow() { }); }); + // Commit unit cell to redux store + store.dispatch({ + type: 'SET_UNIT_CELL', + payload: { + qubits: lattice.constellation, + gridSquares: grid.visibleUnits() + }, + }); + // Add qubits to the workspace - // eslint-disable-next-line max-len for (let horiz = 0; horiz < app.renderer.width; horiz += grid.physicalWidth) { - // eslint-disable-next-line max-len for (let vertic = 0; vertic < app.renderer.height; vertic += grid.physicalHeight) { for (const qubit of lattice.constellation) { const newQubit = new Qubit( @@ -156,8 +163,6 @@ export default function InitializeControlFlow() { workspace.gridSize ); workspace.addChild(newQubit); - // Add qubit to redux store - store.dispatch(addQubit(newQubit.serialized())); } } } diff --git a/frontend/src/reducers.js b/frontend/src/reducers.js index 68d5f614..a60fa0eb 100644 --- a/frontend/src/reducers.js +++ b/frontend/src/reducers.js @@ -1,29 +1,25 @@ /* eslint-disable default-param-last */ -// reducers/workspaceReducer.js -import { ADD_QUBIT, REMOVE_QUBIT } from './actions'; +import { SET_UNIT_CELL } from './actions'; -const initialState = { - qubits: [], - plaquettes: [], +export const initialState = { + untiCell: { + qubits: [], + gridSquares: [], + }, }; -const workspaceReducer = (state = initialState, action) => { +const rootReducer = (state = initialState, action) => { switch (action.type) { - case ADD_QUBIT: - state.qubits.push(action.payload); - return state; - case REMOVE_QUBIT: - state.qubits.forEach((qubit, index) => { - if (qubit === action.payload) { - state.qubits.splice(index, 1); + case SET_UNIT_CELL: + return { + unitCell: { + qubits: action.payload.qubits, + gridSquares: action.payload.gridSquares, } - }); - return state; + }; default: return state; } }; -const rootReducer = workspaceReducer; - export default rootReducer; diff --git a/frontend/src/store.js b/frontend/src/store.js index 7550c186..afa52157 100644 --- a/frontend/src/store.js +++ b/frontend/src/store.js @@ -1,10 +1,6 @@ import { configureStore } from '@reduxjs/toolkit'; import rootReducer from './reducers'; -export const initialState = { - qubits: [], -}; - // Load state from localStorage const loadState = () => { try {