You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.3 KiB
58 lines
1.3 KiB
export interface QuickNoteModalState {
|
|
savedContent: string
|
|
draftContent: string
|
|
isDirty: boolean
|
|
lastSaveFailed: boolean
|
|
}
|
|
|
|
export interface ComputeIsDirtyInput {
|
|
savedContent: string
|
|
draftContent: string
|
|
}
|
|
|
|
export function computeIsDirty(input: ComputeIsDirtyInput): boolean {
|
|
return input.savedContent !== input.draftContent
|
|
}
|
|
|
|
export function createQuickNoteModalState(savedContent: string): QuickNoteModalState {
|
|
return {
|
|
savedContent,
|
|
draftContent: savedContent,
|
|
isDirty: false,
|
|
lastSaveFailed: false,
|
|
}
|
|
}
|
|
|
|
export function updateDraftContent(state: QuickNoteModalState, draftContent: string): QuickNoteModalState {
|
|
return {
|
|
...state,
|
|
draftContent,
|
|
isDirty: computeIsDirty({
|
|
savedContent: state.savedContent,
|
|
draftContent,
|
|
}),
|
|
lastSaveFailed: false,
|
|
}
|
|
}
|
|
|
|
export function markSaveSucceeded(state: QuickNoteModalState): QuickNoteModalState {
|
|
const nextSavedContent = state.draftContent
|
|
return {
|
|
...state,
|
|
savedContent: nextSavedContent,
|
|
draftContent: nextSavedContent,
|
|
isDirty: false,
|
|
lastSaveFailed: false,
|
|
}
|
|
}
|
|
|
|
export function markSaveFailed(state: QuickNoteModalState): QuickNoteModalState {
|
|
return {
|
|
...state,
|
|
isDirty: computeIsDirty({
|
|
savedContent: state.savedContent,
|
|
draftContent: state.draftContent,
|
|
}),
|
|
lastSaveFailed: true,
|
|
}
|
|
}
|
|
|