EDIT: For complete reference, Im trying to do this with Redux
https://codesandbox.io/s/github/SenBoyer/codm-tracker-unfinished
For quick visual reference:
https://jsfiddle.net/59r31Lmt/
Im trying to make it so when you click a camo square it becomes active, when you click all the camo squares, the gold unlocks, and when you unlock gold for ALL of the guns, the diamond "unlocks"/becomes active for all of them.
This is the slice I'm using
import { createSlice } from "@reduxjs/toolkit";
const initialState = {
sandIsActive: false,
jungleIsActive: false,
splinterIsActive: false,
tigerIsActive: false,
dragonIsActive: false,
reptileIsActive: false,
goldIsActive: false,
diamondIsActive: false,
};
const camoBarSlice = createSlice({
name: "camoBar",
initialState,
reducers: {
setSandActive: (state, action) => {
return {
...state,
sandIsActive: !state.isActive,
};
},
setJungleActive: (state, action) => {
return {
...state,
jungleIsActive: !state.isActive,
};
},
setSplinterActive: (state, action) => {
return {
...state,
splinterIsActive: !state.isActive,
};
},
setTigerActive: (state, action) => {
return {
...state,
tigerIsActive: !state.isActive,
};
},
setDragonActive: (state, action) => {
return {
...state,
dragonIsActive: !state.isActive,
};
},
setReptileActive: (state, action) => {
return {
...state,
reptileIsActive: !state.isActive,
};
},
setGoldActive: (state, action) => {
return {
...state,
goldIsActive: !state.isActive,
};
},
},
});
So when
sandIsActive: true,
jungleIsActive: true,
splinterIsActive: true,
tigerIsActive: true,
dragonIsActive: true,
reptileIsActive: true,
setGoldActive() runs and:
goldIsActive: true
then when goldIsActive is true for all 20ish guns, setDiamondActive() runs and:
diamondIsActive: true
for every gun.
How do I share the state logic between all the guns so that it will know to run the function setDiamondActive when they all become active?
Do I have to make an individual slice for each gun with the same logic? Then what though? How do I pass the logic?
Do I have to add a state value for every single square on the whole website because it's not possible to share state values across other slices? You need to keep it all in one slice? So it would have to be something like:
const initialState = {
ak47SandIsActive: false,
ak47JungleIsActive: false,
ak47SplinterIsActive: false,
ak47TigerIsActive: false,
ak47DragonIsActive: false,
ak47ReptileIsActive: false,
ak47GoldIsActive: false,
ak47DiamondIsActive: false,
m13SandIsActive: false,
m13JungleIsActive: false,
m13SplinterIsActive: false,
m13TigerIsActive: false,
m13DragonIsActive: false,
m13ReptileIsActive: false,
m13GoldIsActive: false,
m13DiamondIsActive: false,
};
then set an onClick on each and every square with the relevant dispatch action?
Can I put a slice inside another slice? So that I can have an "outer reducer" that controls the diamond square, while an inside reducer controls the entirety of the gun logic? Is there anything close to that or that could accomplish the same thing?
Basically how do I use redux to keep track of the state here? The logic in the slice I posted works for one gun at a time but how do I use it to control the logic of 20+ guns here?