Ability to save and load calculator states into two slots
This commit is contained in:
parent
6df04527dd
commit
38cd0b0cf8
@ -5,6 +5,7 @@ import BuyInSelector from "@/components/BuyInSelector";
|
||||
import ChipsSelector from "@/components/ChipsSelector";
|
||||
import ChipDistributionSummary from "@/components/ChipDistributionSummary";
|
||||
import DarkModeToggle from "@/components/DarkModeToggle";
|
||||
import { saveState, loadState } from "../components/CalculatorState";
|
||||
|
||||
const IndexScreen = () => {
|
||||
const [playerCount, setPlayerCount] = useState(2);
|
||||
@ -13,14 +14,26 @@ const IndexScreen = () => {
|
||||
const [totalChipsCount, setTotalChipsCount] = useState<number[]>([]);
|
||||
const [darkMode, setDarkMode] = useState(false);
|
||||
|
||||
const handleSave = () => {
|
||||
const handleSave = async (slot: "SLOT1" | "SLOT2") => {
|
||||
if (buyInAmount === null) {
|
||||
Alert.alert("Error", "Please select a valid buy-in amount");
|
||||
return;
|
||||
}
|
||||
const state = { playerCount, buyInAmount, numberOfChips, totalChipsCount };
|
||||
const result = await saveState(slot, state);
|
||||
Alert.alert(result.success ? "Success" : "Error", result.message);
|
||||
};
|
||||
|
||||
const handleLoad = async (slot: "SLOT1" | "SLOT2") => {
|
||||
const loadedState = await loadState(slot);
|
||||
if (loadedState) {
|
||||
setPlayerCount(loadedState.playerCount);
|
||||
setBuyInAmount(loadedState.buyInAmount);
|
||||
setNumberOfChips(loadedState.numberOfChips);
|
||||
setTotalChipsCount(loadedState.totalChipsCount);
|
||||
Alert.alert("Success", `State loaded from ${slot}`);
|
||||
} else {
|
||||
Alert.alert(
|
||||
"Success",
|
||||
`Buy-in amount set to ${buyInAmount} for ${playerCount} players`
|
||||
);
|
||||
Alert.alert("Info", "No saved state found");
|
||||
}
|
||||
};
|
||||
|
||||
@ -44,7 +57,10 @@ const IndexScreen = () => {
|
||||
buyInAmount={buyInAmount}
|
||||
totalChipsCount={totalChipsCount}
|
||||
/>
|
||||
<Button title="Save" onPress={handleSave} disabled={buyInAmount === null} />
|
||||
<Button title="Save to Slot 1" onPress={() => handleSave("SLOT1")} disabled={buyInAmount === null} />
|
||||
<Button title="Save to Slot 2" onPress={() => handleSave("SLOT2")} disabled={buyInAmount === null} />
|
||||
<Button title="Load from Slot 1" onPress={() => handleLoad("SLOT1")} />
|
||||
<Button title="Load from Slot 2" onPress={() => handleLoad("SLOT2")} />
|
||||
</ScrollView>
|
||||
</View>
|
||||
);
|
||||
@ -75,3 +91,4 @@ const styles = StyleSheet.create({
|
||||
});
|
||||
|
||||
export default IndexScreen;
|
||||
|
||||
|
65
components/CalculatorState.tsx
Normal file
65
components/CalculatorState.tsx
Normal file
@ -0,0 +1,65 @@
|
||||
/*import AsyncStorage from "@react-native-async-storage/async-storage";
|
||||
|
||||
const STORAGE_KEYS = {
|
||||
SLOT1: "@poker_state_slot1",
|
||||
SLOT2: "@poker_state_slot2",
|
||||
};
|
||||
|
||||
export interface PokerState {
|
||||
playerCount: number;
|
||||
buyInAmount: number | null;
|
||||
numberOfChips: number;
|
||||
totalChipsCount: number[];
|
||||
}
|
||||
|
||||
export const saveState = async (slot: keyof typeof STORAGE_KEYS, state: PokerState) => {
|
||||
try {
|
||||
await AsyncStorage.setItem(STORAGE_KEYS[slot], JSON.stringify(state));
|
||||
return { success: true, message: `State saved to ${slot}` };
|
||||
} catch (error) {
|
||||
return { success: false, message: "Failed to save state" };
|
||||
}
|
||||
};
|
||||
|
||||
export const loadState = async (slot: keyof typeof STORAGE_KEYS): Promise<PokerState | null> => {
|
||||
try {
|
||||
const storedState = await AsyncStorage.getItem(STORAGE_KEYS[slot]);
|
||||
return storedState ? JSON.parse(storedState) : null;
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
};*/
|
||||
|
||||
|
||||
import AsyncStorage from "@react-native-async-storage/async-storage";
|
||||
|
||||
const STORAGE_KEYS = {
|
||||
SLOT1: "@poker_state_slot1",
|
||||
SLOT2: "@poker_state_slot2",
|
||||
};
|
||||
|
||||
export interface PokerState {
|
||||
playerCount: number;
|
||||
buyInAmount: number | null;
|
||||
numberOfChips: number;
|
||||
totalChipsCount: number[];
|
||||
}
|
||||
|
||||
export const saveState = async (slot: keyof typeof STORAGE_KEYS, state: PokerState) => {
|
||||
try {
|
||||
await AsyncStorage.setItem(STORAGE_KEYS[slot], JSON.stringify(state));
|
||||
return { success: true, message: `State saved to ${slot}` };
|
||||
} catch (error) {
|
||||
return { success: false, message: "Failed to save state" };
|
||||
}
|
||||
};
|
||||
|
||||
export const loadState = async (slot: keyof typeof STORAGE_KEYS): Promise<PokerState | null> => {
|
||||
try {
|
||||
const storedState = await AsyncStorage.getItem(STORAGE_KEYS[slot]);
|
||||
return storedState ? JSON.parse(storedState) : null;
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
52
components/__tests__/CalculatorState.test.tsx
Normal file
52
components/__tests__/CalculatorState.test.tsx
Normal file
@ -0,0 +1,52 @@
|
||||
import AsyncStorage from "@react-native-async-storage/async-storage";
|
||||
import { saveState, loadState, PokerState } from "../CalculatorState";
|
||||
|
||||
jest.mock("@react-native-async-storage/async-storage", () =>
|
||||
require("@react-native-async-storage/async-storage/jest/async-storage-mock")
|
||||
);
|
||||
|
||||
describe("CalculatorState.tsx", () => {
|
||||
const mockState: PokerState = {
|
||||
playerCount: 4,
|
||||
buyInAmount: 50,
|
||||
numberOfChips: 5,
|
||||
totalChipsCount: [100, 200, 150, 50, 75],
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it("should save state successfully", async () => {
|
||||
await saveState("SLOT1", mockState);
|
||||
expect(AsyncStorage.setItem).toHaveBeenCalledWith(
|
||||
"@poker_state_slot1",
|
||||
JSON.stringify(mockState)
|
||||
);
|
||||
});
|
||||
|
||||
it("should fail to save state if an error occurs", async () => {
|
||||
jest.spyOn(AsyncStorage, "setItem").mockRejectedValueOnce(new Error("Failed to save"));
|
||||
const result = await saveState("SLOT1", mockState);
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.message).toBe("Failed to save state");
|
||||
});
|
||||
|
||||
it("should load state successfully", async () => {
|
||||
await AsyncStorage.setItem("@poker_state_slot1", JSON.stringify(mockState));
|
||||
const result = await loadState("SLOT1");
|
||||
expect(result).toEqual(mockState);
|
||||
});
|
||||
|
||||
it("should return null if no state is found", async () => {
|
||||
jest.spyOn(AsyncStorage, "getItem").mockResolvedValueOnce(null);
|
||||
const result = await loadState("SLOT1");
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it("should return null if an error occurs while loading state", async () => {
|
||||
jest.spyOn(AsyncStorage, "getItem").mockRejectedValueOnce(new Error("Failed to load"));
|
||||
const result = await loadState("SLOT1");
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user