Skip to content

Commit

Permalink
Merge pull request #7 from OldStarchy/OldStarchy/issue4
Browse files Browse the repository at this point in the history
  • Loading branch information
OldStarchy authored Aug 17, 2024
2 parents 4a52f99 + 85c9035 commit 0e44689
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ import { cities } from './data/cities';
import { useDeck } from './hooks/useDeck';
import { createSave, loadSave } from './lib/SaveFormat';
import { getAutosave, saveToAutosave } from './lib/autosave';
import { DISCARD_DECK, INFECTION_DECK, TEMP_DECK } from './lib/consts';
import { DISCARD_DECK, INFECTION_DECK } from './lib/consts';

function App() {
const [appState, dispatchAppState] = useAppState();
Expand Down Expand Up @@ -116,11 +116,11 @@ function App() {
} else {
dispatch(createDeck(INFECTION_DECK));
dispatch(createDeck(DISCARD_DECK));
dispatch(createDeck(TEMP_DECK));

dispatch(
createCards(
INFECTION_DECK,
0,
...Object.entries(cities).flatMap(([city, count]) =>
new Array(count).fill(city),
),
Expand Down
8 changes: 4 additions & 4 deletions src/components/form/CreateCardForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import {
useUniverse,
} from '../../context/universe/UniverseContext';
import { createCards } from '../../context/universe/actions/CardActions';
import { moveCard } from '../../context/universe/actions/DeckActions';
import { setKeyframe } from '../../context/withUndoReducer';
import { INFECTION_DECK, TEMP_DECK } from '../../lib/consts';
import { INFECTION_DECK } from '../../lib/consts';
import { Button } from '../common/Button';
import { Input } from '../common/Input';

Expand Down Expand Up @@ -44,8 +43,9 @@ const CreateCardFormImpl = memo(function CreateCardFormImpl({
const handleSubmit = (e: FormEvent) => {
e.preventDefault();

dispatch(createCards(TEMP_DECK, ...new Array(count).fill(name)));
dispatch(moveCard(TEMP_DECK, 0, INFECTION_DECK, 0, -1));
dispatch(
createCards(INFECTION_DECK, 0, ...new Array(count).fill(name)),
);
dispatch(setKeyframe());
setName('');
setCount(1);
Expand Down
12 changes: 10 additions & 2 deletions src/context/universe/actions/CardActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@ interface CreateCardAction {
type: typeof ACTION_CREATE_CARD;
names: string[];
targetDeckId: Deck['id'];
targetIndex: number;
}

export function createCards(
targetDeck: Deck['id'],
targetIndex: number,
...names: string[]
): CreateCardAction {
return { type: ACTION_CREATE_CARD, names, targetDeckId: targetDeck };
return {
type: ACTION_CREATE_CARD,
names,
targetDeckId: targetDeck,
targetIndex,
};
}

export function createCardReducer(
Expand All @@ -31,13 +38,14 @@ export function createCardReducer(
const newDeck = {
...deck,
items: [
...deck.items,
...deck.items.slice(0, action.targetIndex),
...newCards.map(
(card): DeckItem => ({
type: 'card',
cardId: card.id,
}),
),
...deck.items.slice(action.targetIndex),
],
};

Expand Down
5 changes: 3 additions & 2 deletions src/lib/SaveFormat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Card } from '../context/universe/Card';
import { Deck } from '../context/universe/Deck';
import { Group } from '../context/universe/Group';
import { Universe } from '../context/universe/Universe';
import { DISCARD_DECK, INFECTION_DECK } from './consts';

interface SaveFormatV0 {
infectionDeck: DeckSaveFormatV0;
Expand Down Expand Up @@ -150,11 +151,11 @@ export function loadSave(data: SaveFormatV0 | SaveFormatV1): {
}

const infectionDeck: WDeck = {
id: 'Infection Deck',
id: INFECTION_DECK,
items: [],
};
const discardDeck: WDeck = {
id: 'Discard Deck',
id: DISCARD_DECK,
items: [],
};

Expand Down
1 change: 0 additions & 1 deletion src/lib/consts.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export const INFECTION_DECK = 'Infection Deck';
export const DISCARD_DECK = 'Discard Deck';
export const TEMP_DECK = 'TEMP';

0 comments on commit 0e44689

Please sign in to comment.