chore: refactor sync actions WIP

This commit is contained in:
chrislicodes 2023-05-23 22:29:17 +02:00
parent 0eee5ed657
commit 71beedae89
3 changed files with 72 additions and 37 deletions

View file

@ -14,7 +14,11 @@ import {
ChessStudyMove,
VariantMove,
} from 'src/lib/storage';
import { displayMoveInHistory, findMoveIndex } from 'src/lib/ui-state';
import {
displayMoveInHistory,
findMoveIndex,
getCurrentMove,
} from 'src/lib/ui-state';
import { useImmerReducer } from 'use-immer';
import { ChessgroundProps, ChessgroundWrapper } from './ChessgroundWrapper';
import { CommentSection } from './CommentSection';
@ -111,46 +115,18 @@ export const ChessStudy = ({
return draft;
}
case 'SYNC_SHAPES': {
const currentMoveId = draft.currentMove?.moveId;
const moves = draft.study.moves;
const move = getCurrentMove(draft);
const { variant, moveIndex } = findMoveIndex(moves, currentMoveId);
if (variant) {
const move =
moves[variant.parentMoveIndex].variants[variant.variantIndex]
.moves[moveIndex];
move.shapes = action.shapes;
draft.currentMove = move;
} else {
const move = moves[moveIndex];
move.shapes = action.shapes;
draft.currentMove = move;
}
move.shapes = action.shapes;
draft.currentMove = move;
return draft;
}
case 'SYNC_COMMENT': {
const currentMoveId = draft.currentMove?.moveId;
const moves = draft.study.moves;
const move = getCurrentMove(draft);
const { variant, moveIndex } = findMoveIndex(moves, currentMoveId);
if (variant) {
const move =
moves[variant.parentMoveIndex].variants[variant.variantIndex]
.moves[moveIndex];
move.comment = action.comment;
draft.currentMove = move;
} else {
const move = moves[moveIndex];
move.comment = action.comment;
draft.currentMove = move;
}
move.comment = action.comment;
draft.currentMove = move;
return draft;
}

View file

@ -93,3 +93,20 @@ export const displayMoveInHistory = (
return draft;
};
export const getCurrentMove = (
draft: Draft<GameState>
): Draft<ChessStudyMove> | Draft<VariantMove> => {
const currentMoveId = draft.currentMove?.moveId;
const moves = draft.study.moves;
const { variant, moveIndex } = findMoveIndex(moves, currentMoveId);
if (variant) {
return moves[variant.parentMoveIndex].variants[variant.variantIndex].moves[
moveIndex
];
} else {
return moves[moveIndex];
}
};

View file

@ -582,8 +582,50 @@
"after": "r1bqkbnr/ppp1pppp/2n5/3p4/3P4/5N2/PPP1PPPP/RNBQKB1R w KQkq - 2 3",
"moveId": "EKWVgjt5Z2Cz8bqD5Ie0k",
"variants": [],
"shapes": [],
"comment": null
"shapes": [
{
"orig": "f3",
"dest": "e5",
"brush": "green"
},
{
"orig": "c3",
"dest": "d5",
"brush": "green"
},
{
"orig": "b3",
"dest": "b5",
"brush": "green"
},
{
"orig": "c6",
"dest": "e6",
"brush": "green"
},
{
"orig": "g6",
"dest": "g4",
"brush": "green"
}
],
"comment": {
"type": "doc",
"content": [
{
"type": "heading",
"attrs": {
"level": 1
},
"content": [
{
"type": "text",
"text": "This is a test"
}
]
}
]
}
}
]
}