Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

121 fix undo button in tagger #252

Merged
merged 3 commits into from
Dec 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 15 additions & 27 deletions app/(interactive)/tag-match/[slug]/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,24 +229,15 @@ export default function TagMatch() {
}

const saveToHistory = () => {
setTaggerHistory((taggerHistory) => {
// Add the new state to the history
const updatedHistory = [
...taggerHistory,
{
table: tableState.rows,
page: currentPage,
activeRowIndex: tableState.activeRowIndex
}
]

// Check if the history exceeds the maximum length
if (updatedHistory.length > 30) {
// Remove the oldest entry (at the beginning of the array)
return updatedHistory.slice(-30)
setTaggerHistory((prevHistory) => {
// Create a deepy copy of the current state using JSON parse/stringify
const newHistoryEntry = {
table: JSON.parse(JSON.stringify(tableState.rows)),
page: currentPage,
activeRowIndex: tableState.activeRowIndex,
popUp
}

return updatedHistory
return [...prevHistory, newHistoryEntry].slice(-30)
})
}

Expand Down Expand Up @@ -369,20 +360,17 @@ export default function TagMatch() {
const undoLastAction = () => {
if (taggerHistory.length === 0) return

// Get the last state from the history
// Get the last state and restore it
const lastState = taggerHistory[taggerHistory.length - 1]

// Update the current state to the last state from the history
setTableState((oldTableState) => {
return { ...oldTableState, rows: lastState.table }
setTableState({
rows: JSON.parse(JSON.stringify(lastState.table)),
activeRowIndex: lastState.activeRowIndex
})
setCurrentPage(lastState.page)
setTableState((oldTableState) => {
return { ...oldTableState, activeRowIndex: lastState.activeRowIndex }
})
setPopUp(lastState.popUp)

// Remove the last state from the history
setTaggerHistory(taggerHistory.slice(0, -1))
// Remove the used state from history
setTaggerHistory((prev) => prev.slice(0, -1))
}

// This pulls the button data from the taggerButtonData.js file
Expand Down