Skip to content

Commit

Permalink
set -> update; _set -> set
Browse files Browse the repository at this point in the history
  • Loading branch information
Konrad Jamrozik committed Jul 9, 2024
1 parent 748296d commit 0e31f13
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 38 deletions.
6 changes: 3 additions & 3 deletions web/src/components/SettingsPanel/SettingsPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ export function SettingsPanel(): React.JSX.Element {
function handleIntroEnabledChange(
event: React.ChangeEvent<HTMLInputElement>,
): void {
settings.setIntroEnabled(event.target.checked)
settings.updateIntroEnabled(event.target.checked)
}

function handleOutroEnabledChange(
event: React.ChangeEvent<HTMLInputElement>,
): void {
settings.setOutroEnabled(event.target.checked)
settings.updateOutroEnabled(event.target.checked)
}

function handleChartsEnabledChange(
event: React.ChangeEvent<HTMLInputElement>,
): void {
settings.setChartsEnabled(event.target.checked)
settings.updateChartsEnabled(event.target.checked)
}

// MUI components usage based on: https://mui.com/material-ui/react-list/#switch
Expand Down
1 change: 0 additions & 1 deletion web/src/lib/gameSession/GameSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ export function useGameSession(storedData: StoredData): GameSession {
)
}, [loading, error])

// kja why pass here storedData, data and setData? Instead, pass storedData and obtain the remained data and setData from it.
return new GameSession(
new GameSessionData(storedData, data, setData),
loading,
Expand Down
39 changes: 18 additions & 21 deletions web/src/lib/gameSession/GameSessionData.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable max-statements */
/* eslint-disable sonarjs/no-inverted-boolean-check */
/* eslint-disable no-underscore-dangle */
/* eslint-disable @typescript-eslint/parameter-properties */
import _ from 'lodash'
import {
Expand Down Expand Up @@ -30,8 +29,8 @@ export const initialGameSessionData: GameSessionDataType = {
export class GameSessionData {
public constructor(
private readonly storedData: StoredData,
private _data: GameSessionDataType,
private readonly _setData: React.Dispatch<
private readonly data: GameSessionDataType,
private readonly setData: React.Dispatch<
React.SetStateAction<GameSessionDataType>
>,
) {}
Expand Down Expand Up @@ -117,7 +116,7 @@ export class GameSessionData {
}

public getTurns(): readonly GameSessionTurn[] {
return this._data.turns
return this.data.turns
}

public getCurrentTurn(): GameSessionTurn {
Expand All @@ -126,7 +125,7 @@ export class GameSessionData {

public getTurnAtUnsafe(turnToFind: number): GameSessionTurn | undefined {
return _.find(
this._data.turns,
this.data.turns,
(gameTurn) => getTurnNo(gameTurn) === turnToFind,
)
}
Expand All @@ -136,7 +135,7 @@ export class GameSessionData {
}

public getGameStates(): readonly GameState[] {
return _.flatMap(this._data.turns, (turn) => [
return _.flatMap(this.data.turns, (turn) => [
turn.StartState,
turn.EndState,
])
Expand Down Expand Up @@ -164,7 +163,7 @@ export class GameSessionData {
const newData: GameSessionDataType = {
turns: [...turnsBeforeCurrentTurn.slice(0, -1), turnBeforeCurrentTurn],
}
this.setData(newData)
this.updateData(newData)
}

public resetCurrentTurn(): void {
Expand All @@ -174,7 +173,7 @@ export class GameSessionData {
const newData: GameSessionDataType = {
turns: [...this.getTurnsBeforeCurrentTurn(), currentTurnAfterReset],
}
this.setData(newData)
this.updateData(newData)
}

public getTurnsBeforeCurrentTurn(): readonly GameSessionTurn[] {
Expand All @@ -189,43 +188,41 @@ export class GameSessionData {
const newData: GameSessionDataType = {
turns,
}
this.setData(newData)
this.updateData(newData)
}

public resetData(): void {
this.storedData.resetGameSessionData()
this._setData(initialGameSessionData)
this.setData(initialGameSessionData)
}

public save(): void {
console.log('GameSessionData.save()')
this.storedData.saveGameSessionData(this._data)
this.storedData.saveGameSessionData(this.data)
}

public getSize(): number {
return JSON.stringify(this._data).length
return JSON.stringify(this.data).length
}

public getSaveOnExitEnabled(): boolean {
return this.storedData.getSaveOnExitEnabled(JSON.stringify(this._data))
return this.storedData.getSaveOnExitEnabled(JSON.stringify(this.data))
}

public getCompressionEnabled(): boolean {
return this.storedData.getCompressionEnabled(JSON.stringify(this._data))
return this.storedData.getCompressionEnabled(JSON.stringify(this.data))
}

private setData(data: GameSessionDataType): void {
private updateData(data: GameSessionDataType): void {
GameSessionData.verify(data.turns)
// Uncomment to save game session data on every update.
// Now it is saved only on exit.
// // Uncomment to save game session data on every update.
// // Now it is saved only on exit.
// this.storedData.saveGameSessionData(data)
this._setData(data)
// kja is this necessary?
this._data = data
this.setData(data)
}
}

// future work: Consider for later: using a reducer to manage game sates:
// future work: Consider for later: using a reducer to manage game states:
// https://react.dev/learn/extracting-state-logic-into-a-reducer
// using immer:
// https://react.dev/learn/extracting-state-logic-into-a-reducer#writing-concise-reducers-with-immer
25 changes: 12 additions & 13 deletions web/src/lib/settings/Settings.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable no-underscore-dangle */
/* eslint-disable @typescript-eslint/max-params */
/* eslint-disable @typescript-eslint/parameter-properties */
import _ from 'lodash'
Expand Down Expand Up @@ -49,32 +48,32 @@ export class Settings {
public constructor(
private readonly storedData: StoredData,
public readonly introEnabled: boolean,
private readonly _setIntroEnabled: React.Dispatch<
private readonly setIntroEnabled: React.Dispatch<
React.SetStateAction<boolean>
>,
public readonly outroEnabled: boolean,
private readonly _setOutroEnabled: React.Dispatch<
private readonly setOutroEnabled: React.Dispatch<
React.SetStateAction<boolean>
>,
public readonly chartsEnabled: boolean,
private readonly _setChartsEnabled: React.Dispatch<
private readonly setChartsEnabled: React.Dispatch<
React.SetStateAction<boolean>
>,
) {}

public setIntroEnabled(value: boolean): void {
public updateIntroEnabled(value: boolean): void {
this.saveSettings({ introEnabled: value })
this._setIntroEnabled(value)
this.setIntroEnabled(value)
}

public setOutroEnabled(value: boolean): void {
public updateOutroEnabled(value: boolean): void {
this.saveSettings({ outroEnabled: value })
this._setOutroEnabled(value)
this.setOutroEnabled(value)
}

public setChartsEnabled(value: boolean): void {
public updateChartsEnabled(value: boolean): void {
this.saveSettings({ chartsEnabled: value })
this._setChartsEnabled(value)
this.setChartsEnabled(value)
}

public saveOnExit(): void {
Expand All @@ -85,9 +84,9 @@ export class Settings {
public reset(): void {
console.log('Settings.reset()')
this.storedData.resetSettingsData()
this._setIntroEnabled(defaultSettingsData.introEnabled)
this._setOutroEnabled(defaultSettingsData.outroEnabled)
this._setChartsEnabled(defaultSettingsData.chartsEnabled)
this.setIntroEnabled(defaultSettingsData.introEnabled)
this.setOutroEnabled(defaultSettingsData.outroEnabled)
this.setChartsEnabled(defaultSettingsData.chartsEnabled)
}

private saveSettings({
Expand Down

0 comments on commit 0e31f13

Please sign in to comment.