Skip to content
Merged
Show file tree
Hide file tree
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
11 changes: 3 additions & 8 deletions src/components/map-projects/MapProject.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ import { getPreviewEligibleRowIndexes, getRowsToProcess, countAIOnlyRows, getAut
import { aiOnByDefault, getMaxRunRows, getMapperSettings } from '@openconceptlab/oclmap-extensions'
import { createAutosaveScheduler, createLatestSender, saveOnLeave, trackSave, whenSaved, installUnloadGuard } from './autosave'
import { blockLeaving, continueLeaving, createRunClosers, isUnsavedNewProject, shouldAskBeforeLeaving, shouldWarnBeforeUnload } from './leaveGuard'
import { editProposed, getColumnsSnapshot } from './unsavedEdits'
import MatchSummaryCard from './MatchSummaryCard'
import MappingDecisionResult from './MappingDecisionResult'
import DecisionSelector from './DecisionSelector'
Expand Down Expand Up @@ -2261,13 +2262,7 @@ const MapProject = () => {
prompt_output_locale: promptOutputLocale || '',
input_locale: inputLocale || '',
use_lexical_variants: Boolean(useLexicalVariants),
columns: map(columns, col => ({
dataKey: col.dataKey,
mapped: col.mapped,
hidden: columnVisibilityModel[col.dataKey] === false,
width: columnWidth[col.dataKey] || undefined,
ai_assistant_hidden: AIAssistantColumns[col.dataKey] === false
}))
columns: getColumnsSnapshot(columns, {columnVisibilityModel, columnWidth, AIAssistantColumns})
})

const closeConfiguration = () => {
Expand Down Expand Up @@ -5730,7 +5725,7 @@ const MapProject = () => {
return returnSelf ? (cached || concept) : cached
}

const onProposedUpdate = proposedState => setProposed(prev => ({...prev, [rowIndex]: {...proposedState}}))
const onProposedUpdate = proposedState => editProposed({rowIndex, state: proposedState, setProposed, scheduleAutoSave})

const doubleClickCallback = useDoubleClick(onCSVRowSelect, () => {});

Expand Down
4 changes: 3 additions & 1 deletion src/components/map-projects/Propose.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ const Propose = ({onChange, proposed, onSubmit, repo, row, columns}) => {
const idParts = event.target.id.split('.')
const attributeIndex = parseInt(idParts[1])
const attributeType = idParts[2]
// A copy of the edited attribute: the one in state is also the one in
// the project's saved proposal.
const newAttributes = [...state.attributes]
newAttributes[attributeIndex][attributeType] = event.target.value
newAttributes[attributeIndex] = {...newAttributes[attributeIndex], [attributeType]: event.target.value}
newState = {...state, attributes: newAttributes}
setState(newState)
} else {
Expand Down
101 changes: 101 additions & 0 deletions src/components/map-projects/__tests__/unsavedEdits.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* Proposal field edits and Configure column-mapping changes are seen as
* unsaved (ocl_online#786).
*
* Run with: npm test
*/

import test from 'node:test'
import assert from 'node:assert/strict'

import { editProposed, getColumnsSnapshot, PROPOSAL_CHANGE } from '../unsavedEdits.js'
import { createAutosaveScheduler, saveOnLeave } from '../autosave.js'
import { shouldWarnBeforeUnload } from '../leaveGuard.js'

const setupScheduler = () => {
const timers = []
const saves = []
const scheduler = createAutosaveScheduler({
getProjectId: () => 'project-1',
isSaving: () => false,
onFire: reasons => saves.push(reasons),
setTimeoutFn: fn => { timers.push(fn); return timers.length },
clearTimeoutFn: () => {},
})
return { scheduler, saves }
}

const setupProposed = initial => {
let proposed = initial
return {
get: () => proposed,
setProposed: update => { proposed = update(proposed) },
}
}

test('editProposed: sets the row\'s proposal and schedules an autosave', () => {
const { scheduler } = setupScheduler()
const store = setupProposed({ 2: { id: 'A', name: 'Old' } })

editProposed({ rowIndex: 3, state: { id: 'B', name: 'New' }, setProposed: store.setProposed, scheduleAutoSave: reason => scheduler.schedule(reason) })

assert.deepEqual(store.get(), { 2: { id: 'A', name: 'Old' }, 3: { id: 'B', name: 'New' } })
assert.equal(scheduler.hasPending(), true)
assert.deepEqual(scheduler.pendingReasons(), [PROPOSAL_CHANGE])
})

test('editProposed: leaving before the autosave runs is caught', () => {
const { scheduler } = setupScheduler()
const store = setupProposed({ 0: { id: 'A' } })
editProposed({ rowIndex: 0, state: { id: 'A', name: 'Edited' }, setProposed: store.setProposed, scheduleAutoSave: reason => scheduler.schedule(reason) })

assert.equal(shouldWarnBeforeUnload({ pendingAutosave: scheduler.hasPending(), projectId: 'project-1' }), true)

const saved = []
assert.equal(saveOnLeave({ scheduler, inFlightSave: null, save: reasons => saved.push(reasons) }), true)
assert.deepEqual(saved, [[PROPOSAL_CHANGE]])
})

test('editProposed: keeps a copy of the editor\'s top-level fields', () => {
const store = setupProposed({})
const state = { id: 'A' }
editProposed({ rowIndex: 1, state, setProposed: store.setProposed, scheduleAutoSave: () => {} })
state.id = 'changed later'
assert.equal(store.get()[1].id, 'A')
})

const columns = [
{ dataKey: 'name', original: 'name', label: 'Name' },
{ dataKey: 'loinc_code', original: 'loinc_code', label: 'Mapping: Code', targetSource: { loinc: '/orgs/Regenstrief/sources/LOINC/' } },
]
const snapshot = cols => JSON.stringify(getColumnsSnapshot(cols))

test('getColumnsSnapshot: a mapping label change is a change', () => {
const next = [{ ...columns[0], label: 'Synonyms' }, columns[1]]
assert.notEqual(snapshot(next), snapshot(columns))
})

test('getColumnsSnapshot: a target source change is a change', () => {
const next = [columns[0], { ...columns[1], targetSource: { loinc: '/orgs/Other/sources/LOINC/' } }]
assert.notEqual(snapshot(next), snapshot(columns))
})

test('getColumnsSnapshot: clearing a mapping is a change', () => {
const next = [{ ...columns[0], label: '', unmapped: true }, columns[1]]
assert.notEqual(snapshot(next), snapshot(columns))
})

test('getColumnsSnapshot: an unchanged mapping is not a change', () => {
assert.equal(snapshot(columns.map(col => ({ ...col }))), snapshot(columns))
})

test('getColumnsSnapshot: visibility, width and AI Assistant columns still count', () => {
const base = JSON.stringify(getColumnsSnapshot(columns, {}))
assert.notEqual(JSON.stringify(getColumnsSnapshot(columns, { columnVisibilityModel: { name: false } })), base)
assert.notEqual(JSON.stringify(getColumnsSnapshot(columns, { columnWidth: { name: 200 } })), base)
assert.notEqual(JSON.stringify(getColumnsSnapshot(columns, { AIAssistantColumns: { name: false } })), base)
})

test('getColumnsSnapshot: a missing column list is empty', () => {
assert.deepEqual(getColumnsSnapshot(undefined), [])
})
26 changes: 26 additions & 0 deletions src/components/map-projects/unsavedEdits.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Edits the Mapper must see as unsaved (ocl_online#786).

// A proposal's fields are saved with the project, like a review note, so an
// edit schedules an autosave. Without one, leaving right after the edit (the
// save on leave, the beforeunload prompt) would miss it.
export const PROPOSAL_CHANGE = 'proposal_change'

export const editProposed = ({ rowIndex, state, setProposed, scheduleAutoSave }) => {
setProposed(prev => ({ ...prev, [rowIndex]: { ...state } }))
scheduleAutoSave(PROPOSAL_CHANGE)
}

// Configure's snapshot of the columns: what the mapping controls write (the
// column's label, its target source, cleared on purpose) and how it shows.
// A mapping-only change then saves when Configure closes and counts as
// unsaved before that.
export const getColumnsSnapshot = (columns, { columnVisibilityModel = {}, columnWidth = {}, AIAssistantColumns = {} } = {}) =>
(columns || []).map(col => ({
dataKey: col.dataKey,
label: col.label || '',
targetSource: col.targetSource || null,
unmapped: Boolean(col.unmapped),
hidden: columnVisibilityModel[col.dataKey] === false,
width: columnWidth[col.dataKey] || undefined,
ai_assistant_hidden: AIAssistantColumns[col.dataKey] === false,
}))
Loading