Skip to content

Commit aa6fc5f

Browse files
ayothealokobabblebeyCopilot
authored
feat: add comparison check to word-editor (#191)
## Description Currently, when creating or editing a word, every request is submitted regardless of whether any content has actually changed. This PR introduces a **content comparison check** so that submissions only proceed when there are real changes. - Added a `hasWordChanged` check in the `handleSubmit` function to compare current editor values with their initial values. - Display a short, user-friendly message when the user tries to submit without making any changes. ## Related Issue Fixes #38 ## Screenshots/Screencasts <img width="1588" height="784" alt="image" src="https://github.com/user-attachments/assets/9240bfe5-adc7-45d1-b3b5-27c501164189" /> ## Notes to Reviewer - Added `initialContent` to the store. - Extended `useWordEditor` to expose these values along with their corresponding setters. --------- Co-authored-by: Olabode Lawal-Shittabey <babblebey@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 7aa51ea commit aa6fc5f

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

src/components/islands/word-editor.jsx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,21 @@ function Editor({ eTitle, eContent, eMetadata, className, action, ...props }) {
104104
const router = useRouter();
105105
const isSubmitted = useStore($isWordSubmitted);
106106
const isSubmitLoading = useStore($isWordSubmitLoading);
107-
const { title, setTitle, content, setContent } = useWordEditor();
107+
const {
108+
title,
109+
setTitle,
110+
content,
111+
setContent,
112+
initialContent,
113+
setInitialContent,
114+
} = useWordEditor();
108115

109116
const isDone = isSubmitLoading || isSubmitted;
110117

111118
useEffect(() => {
112119
setTitle(eTitle);
113120
setContent(eContent);
121+
setInitialContent(eContent);
114122
}, []);
115123

116124
/**
@@ -120,6 +128,14 @@ function Editor({ eTitle, eContent, eMetadata, className, action, ...props }) {
120128
* @todo handle error for when submission isn't successful
121129
*/
122130
async function handleSubmit(e) {
131+
const hasWordChanged = content !== initialContent;
132+
if (!hasWordChanged) {
133+
alert(
134+
"No changes detected. Please update the content before submitting.",
135+
);
136+
return;
137+
}
138+
123139
$isWordSubmitLoading.set(true);
124140
const formData = new FormData(e.target);
125141
const response = await fetch("/api/dictionary", {

src/lib/hooks/use-word-editor.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,16 @@ export default function useWordEditor() {
1616
$wordEditor.setKey("content", content);
1717
}
1818

19+
function setInitialContent(content) {
20+
$wordEditor.setKey("inititalContent", content);
21+
}
22+
1923
return {
2024
title: word.title,
2125
content: word.content,
26+
initialContent: word.inititalContent,
2227
setTitle,
2328
setContent,
29+
setInitialContent,
2430
};
2531
}

src/lib/stores/dictionary.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ import { atom, map } from "nanostores";
1010
* @typedef {Object} Word
1111
* @property {string} title
1212
* @property {string} content
13+
* @property {string} initialContent
1314
* @type {import('nanostores').MapStore<Record<string, Word>>}
1415
*/
1516
export const $wordEditor = map({
1617
title: "",
1718
content: "",
19+
initialContent: "",
1820
});
1921

2022
export const $isWordSubmitLoading = atom(false);

0 commit comments

Comments
 (0)