enter the new updux

This commit is contained in:
Yanick Champoux 2023-03-21 15:42:45 -04:00
parent 42b81e2128
commit cdb7e8ee35
8 changed files with 90 additions and 3 deletions

View File

@ -0,0 +1,18 @@
<Hst.Story>
<ShipEdit {ship} />
</Hst.Story>
<script>
import { setContext } from "svelte";
import ShipEdit from "./ShipEdit.svelte";
import { createApi } from "$lib/store/api";
export let Hst;
const api = createApi();
setContext("api", api);
let ship = api.getState();
api.subscribe(() => (ship = api.getState()));
</script>

View File

@ -0,0 +1,19 @@
<main>
<Identification {...ship.identification} />
</main>
<script>
import Identification from "./ShipEdit/Identification.svelte";
export let ship = {
identification: {},
};
</script>
<style>
main {
width: var(--main-width);
margin-left: auto;
margin-right: auto;
}
</style>

View File

@ -33,8 +33,7 @@
$: if (shipTypes.length > 0 && !shipTypes.includes(shipType))
shipType = shipTypes[0];
$: api?.dispatch?.setShipType?.(shipType);
$: api?.dispatch?.setShipClass?.(shipClass);
$: api?.dispatch?.updateIdentification?.({ shipType, shipClass });
</script>
<style>

View File

@ -33,7 +33,9 @@
$: massUnused = mass - usedMass;
$: withinBudget = massUnused >= 0;
$: api?.dispatch?.setShipMass?.(mass);
$: api?.dispatch?.updateIdentification?.({
reqs: { mass },
});
/* const change_tonnage = ({ target: { value } }) => */
/* ship.dispatch(ship.actions.set_ship_mass(parseInt(value))); */

9
src/lib/store/api.ts Normal file
View File

@ -0,0 +1,9 @@
import ship from "./ship";
export type Api = ReturnType<typeof ship.createStore>;
export const createApi = () => {
const api = ship.createStore();
console.log(api);
return api;
};

11
src/lib/store/ship.ts Normal file
View File

@ -0,0 +1,11 @@
import Updux from "updux";
import identification from "./ship/identification";
const shipDux = new Updux({
subduxes: {
identification,
},
});
export default shipDux;

View File

@ -0,0 +1,29 @@
import Updux, { createAction, withPayload } from "updux";
import u from "@yanick/updeep-remeda";
const initial = {
shipType: "",
shipClass: "",
isCarrier: false,
reqs: {
mass: 10,
cost: 0,
usedMass: 0,
},
};
const setShipClass = createAction("setShipClass", withPayload<string>());
const updateIdentification = createAction("updateIdentification");
export const dux = new Updux({
initial,
actions: {
setShipClass,
updateIdentification,
},
});
dux.addMutation(setShipClass, (shipClass) => u({ shipClass }));
dux.addMutation(updateIdentification, (update) => u(update));
export default dux;