Skip to content

Commit ee5a196

Browse files
committed
fix: If the word is already in the Wortschatz move it to first position
1 parent af36e26 commit ee5a196

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/app/components/WordForm.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export default function WordForm({ onAddWordAction }: WordFormProps) {
2020
<Input
2121
className="px-4"
2222
name="word"
23-
placeholder="Deutsches Wort eingeben"
23+
placeholder="Deutsches Wort eingeben, z.B. Brief"
2424
required
2525
/>
2626
<Button className="px-4 py-2" type="submit">

src/app/components/Wortilizer.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,21 @@ export default function Wortilizer() {
3737
return
3838
}
3939

40-
if (wortschatz.find((wortschatzEntry) => wortschatzEntry.word === word)) {
41-
console.error('Word exists in vocabulary')
40+
// If a word is already in the vocab, move it to first position
41+
const foundWortschatzEntry = wortschatz.find(
42+
(wortschatzEntry) => wortschatzEntry.word === word
43+
)
44+
if (foundWortschatzEntry) {
45+
saveWortschatz((prevWortschatz) => {
46+
const restPrevWortschatz = prevWortschatz.filter(
47+
(wortschatzEntry) => wortschatzEntry.word !== word
48+
)
49+
return [foundWortschatzEntry, ...restPrevWortschatz]
50+
})
4251
return
4352
}
4453

54+
// All good, just add the word
4555
const wortschatzEntry: WortschatzEntry = {
4656
word,
4757
article,

0 commit comments

Comments
 (0)