diff --git a/FIX_PROPOSAL.md b/FIX_PROPOSAL.md new file mode 100644 index 0000000..715624f --- /dev/null +++ b/FIX_PROPOSAL.md @@ -0,0 +1,93 @@ +To address the issue of dismissing the modal during save not cancelling persistence, we can implement a solution that checks if a save is in progress before closing the modal. Here's a possible implementation: + +```typescript +// Add a variable to track if a save is in progress +let saveInProgress = false; + +// Update the save path to set saveInProgress to true +setSaving(true); +saveInProgress = true; + +try { + const promptData = { + title: title().trim(), + content: content().trim(), + description: description().trim(), + category: category(), + tags: tags(), + isFavorite: isFavorite(), + }; + + if (isEditing()) { + await promptStore.updatePrompt(promptStore.state.editingPrompt!.id, promptData); + } else { + await promptStore.createPrompt(promptData); + } + + // Only close the editor if save is not in progress + if (!saveInProgress) { + promptStore.closeEditor(); + } +} catch (e) { + setErrors([{ field: "general", message: String(e) }]); +} finally { + saveInProgress = false; + setSaving(false); +} + +// Update the dismiss paths to check if a save is in progress +const handleClose = () => { + if (saveInProgress) { + // Show a confirmation dialog or disable dismiss affordances + // For example: + // alert("Save is in progress. Please wait for it to complete."); + return; + } + promptStore.closeEditor(); +}; +``` + +Alternatively, you can use a save generation id to check if the save has been cancelled: + +```typescript +// Add a variable to track the save generation id +let saveGenerationId = 0; + +// Update the save path to increment the save generation id +setSaving(true); +saveGenerationId++; + +try { + const promptData = { + title: title().trim(), + content: content().trim(), + description: description().trim(), + category: category(), + tags: tags(), + isFavorite: isFavorite(), + }; + + if (isEditing()) { + await promptStore.updatePrompt(promptStore.state.editingPrompt!.id, promptData); + } else { + await promptStore.createPrompt(promptData); + } + + // Only close the editor if the save generation id matches + if (saveGenerationId === currentSaveGenerationId) { + promptStore.closeEditor(); + } +} catch (e) { + setErrors([{ field: "general", message: String(e) }]); +} finally { + setSaving(false); +} + +// Update the dismiss paths to increment the save generation id +const handleClose = () => { + saveGenerationId++; + promptStore.closeEditor(); +}; +``` + +In this implementation, the `saveGenerationId` is incremented every time a save is initiated, and the `handleClose` function increments the `saveGenerationId` when the modal is dismissed. The save path checks if the `saveGenerationId` matches the current `saveGenerationId` before closing the editor. If the `saveGenerationId` does not match, it means the save has been cancelled, and the editor is not closed. \ No newline at end of file