aotds-docks/src/lib/store/ship/identification.ts

49 lines
1.3 KiB
TypeScript
Raw Normal View History

2023-03-21 19:42:45 +00:00
import Updux, { createAction, withPayload } from "updux";
import u from "@yanick/updeep-remeda";
2023-03-24 15:01:04 +00:00
import { carrierDux } from "./carrier";
2023-03-21 19:42:45 +00:00
2023-03-24 15:01:04 +00:00
const initialState = {
2023-03-26 17:02:22 +00:00
shipType: "",
shipClass: "",
isCarrier: false,
reqs: {
mass: 10,
cost: 0,
usedMass: 0,
},
2023-03-21 19:42:45 +00:00
};
const setShipClass = createAction("setShipClass", withPayload<string>());
const updateIdentification = createAction("updateIdentification");
2023-03-22 17:04:47 +00:00
const setShipReqs = createAction("setShipReqs", withPayload());
2023-04-21 19:35:46 +00:00
const setCarrier = createAction("setCarrier", withPayload<boolean>());
2023-03-21 19:42:45 +00:00
2023-03-26 17:02:22 +00:00
export const identificationDux = new Updux({
initialState,
actions: {
setShipClass,
updateIdentification,
setShipReqs,
2023-04-21 19:35:46 +00:00
setCarrier,
2023-03-26 17:02:22 +00:00
},
selectors: {
getShipMass: (state) => state.reqs.mass,
isCarrier: ({ isCarrier }) => isCarrier,
},
2023-03-21 19:42:45 +00:00
});
2023-03-26 17:02:22 +00:00
identificationDux.addMutation(setShipClass, (shipClass) => u({ shipClass }));
identificationDux.addMutation(updateIdentification, (update) => u(update));
2023-03-21 19:42:45 +00:00
2023-04-21 19:35:46 +00:00
identificationDux.addMutation(setCarrier, (isCarrier) => u({ isCarrier }));
identificationDux.addEffect(setCarrier, (api) => (next) => (action) => {
next(action);
if (!action.payload) {
api.dispatch(carrierDux.actions.setNbrCarrierBays(0));
}
});
identificationDux.addMutation(setShipReqs, (reqs) => u({ reqs }));
2023-03-24 15:01:04 +00:00
2023-03-26 17:02:22 +00:00
export default identificationDux;