use Redux for tulajdonsagok

This commit is contained in:
2024-03-31 11:55:56 +02:00
parent edbe8934e4
commit a922b006bc
12 changed files with 212 additions and 71 deletions
@@ -1,10 +0,0 @@
import { Fetched } from "../models/Fetched";
import React from "react";
export const userDefaults: Fetched<string | null> = {
state: "not-started",
data: null,
}
export const UserContext = React.createContext(userDefaults)
UserContext.displayName = "UserEmailContext";
@@ -0,0 +1,31 @@
import {RollAbility} from "./rollAbility";
describe('RollAbility', () => {
it('returns 3 if all rolls are 1', () => {
const actual = RollAbility({
d6: () => 1
})
expect(actual).toEqual(3)
})
it('returns 18 if at least 3 rolls are 6', () => {
let counter = 0;
const actual = RollAbility({
d6: () => {
return ++counter > 1 ? 6 : 1;
}
})
expect(actual).toEqual(18)
})
it('sums up rolls are removes the smallest roll', () => {
let counter = 1;
const actual = RollAbility({
d6: () => {
if (counter++ > 4) {
throw "Cheater! You can only roll 4 times!"
}
return counter
}
})
expect(actual).toEqual(12)
})
})
@@ -0,0 +1,21 @@
import {d6} from "./kockak";
export function RollAbility(dependencies: {
d6: () => number
} = {d6}) {
const { d6: roll6 } = dependencies;
let min = 6;
let sum = 0;
let rolls: number[] = []
for (let i = 0; i < 4; i++) {
const roll = roll6();
if (roll < min) {
min = roll;
}
sum += roll;
rolls.push(roll)
}
const result = sum - min;
console.log("Rolled: " + rolls + " got: " + result)
return result;
}
@@ -6,19 +6,22 @@ export const userSlice = createSlice({
name: 'user',
initialState: {
state: "not-started" as LoadingState,
email: null,
email: null as string | null,
},
reducers: {
load: (state) => {
state.state = "loading"
return state
},
setUser: (state, action) => {
setUser: (state, action: {payload: string}) => {
state.state = "finished"
state.email = action.payload
return state
},
unsetUser: (state) => {
state.state = "finished"
state.email = null
return state
}
},
})