Update backend and frontend components

This commit is contained in:
poignatov
2025-12-30 18:27:12 +03:00
parent 6473622977
commit a76252a198
4 changed files with 190 additions and 97 deletions

View File

@@ -228,7 +228,7 @@ function TestConfigSelection({ onNavigate, refreshTrigger = 0 }) {
</div>
))}
<button
onClick={() => onNavigate?.('words', { isNewDictionary: true })}
onClick={() => onNavigate?.('words', { dictionaryId: null, isNewDictionary: true })}
className="add-dictionary-button"
>
<div className="add-config-icon">+</div>

View File

@@ -11,36 +11,45 @@ function WordList({ onNavigate, dictionaryId, isNewDictionary, refreshTrigger =
const [dictionaryName, setDictionaryName] = useState('')
const [originalDictionaryName, setOriginalDictionaryName] = useState('')
const [isSavingName, setIsSavingName] = useState(false)
const [currentDictionaryId, setCurrentDictionaryId] = useState(dictionaryId)
const [isNewDict, setIsNewDict] = useState(isNewDictionary)
// Normalize undefined to null for clarity: new dictionary if dictionaryId is null or undefined
const [currentDictionaryId, setCurrentDictionaryId] = useState(dictionaryId ?? null)
// isNewDict is computed from currentDictionaryId: new dictionary if currentDictionaryId == null
const isNewDict = currentDictionaryId == null
// Helper function to check if dictionary exists and is not new
const hasValidDictionary = (dictId) => {
return dictId !== undefined && dictId !== null
}
useEffect(() => {
setCurrentDictionaryId(dictionaryId)
setIsNewDict(isNewDictionary)
// Normalize undefined to null: if dictionaryId is undefined, treat it as null (new dictionary)
const normalizedDictionaryId = dictionaryId ?? null
setCurrentDictionaryId(normalizedDictionaryId)
if (isNewDictionary) {
if (normalizedDictionaryId == null) {
setLoading(false)
setDictionary(null)
setDictionaryName('')
setOriginalDictionaryName('')
setWords([])
} else if (dictionaryId !== undefined && dictionaryId !== null) {
fetchDictionary()
fetchWords()
} else if (hasValidDictionary(normalizedDictionaryId)) {
fetchDictionary(normalizedDictionaryId)
fetchWords(normalizedDictionaryId)
} else {
setLoading(false)
setWords([])
}
}, [dictionaryId, isNewDictionary, refreshTrigger])
}, [dictionaryId, refreshTrigger])
const fetchDictionary = async () => {
const fetchDictionary = async (dictId) => {
try {
const response = await fetch(`${API_URL}/dictionaries`)
if (!response.ok) {
throw new Error('Ошибка при загрузке словарей')
}
const dictionaries = await response.json()
const dict = dictionaries.find(d => d.id === dictionaryId)
const dict = dictionaries.find(d => d.id === dictId)
if (dict) {
setDictionary(dict)
setDictionaryName(dict.name)
@@ -51,14 +60,14 @@ function WordList({ onNavigate, dictionaryId, isNewDictionary, refreshTrigger =
}
}
const fetchWords = async () => {
if (isNewDictionary || dictionaryId === undefined || dictionaryId === null) {
const fetchWords = async (dictId) => {
if (!hasValidDictionary(dictId)) {
setWords([])
setLoading(false)
return
}
await fetchWordsForDictionary(dictionaryId)
await fetchWordsForDictionary(dictId)
}
const fetchWordsForDictionary = async (dictId) => {
@@ -91,7 +100,7 @@ function WordList({ onNavigate, dictionaryId, isNewDictionary, refreshTrigger =
setIsSavingName(true)
try {
if (isNewDictionary) {
if (!hasValidDictionary(currentDictionaryId)) {
// Create new dictionary
const response = await fetch(`${API_URL}/dictionaries`, {
method: 'POST',
@@ -108,21 +117,21 @@ function WordList({ onNavigate, dictionaryId, isNewDictionary, refreshTrigger =
const newDict = await response.json()
const newDictionaryId = newDict.id
// Update local state immediately
// Update local state
setOriginalDictionaryName(newDict.name)
setDictionaryName(newDict.name)
setDictionary(newDict)
setCurrentDictionaryId(newDictionaryId)
setIsNewDict(false)
// Fetch words for the new dictionary
// Reinitialize screen: fetch dictionary info and words for the new dictionary
await fetchDictionary(newDictionaryId)
await fetchWordsForDictionary(newDictionaryId)
// Update navigation to use the new dictionary ID and remove isNewDictionary flag
onNavigate?.('words', { dictionaryId: newDictionaryId, isNewDictionary: false })
} else if (dictionaryId !== undefined && dictionaryId !== null) {
// Update existing dictionary
const response = await fetch(`${API_URL}/dictionaries/${dictionaryId}`, {
// Update navigation to use the new dictionary ID
onNavigate?.('words', { dictionaryId: newDictionaryId })
} else if (hasValidDictionary(currentDictionaryId)) {
// Update existing dictionary (rename)
const response = await fetch(`${API_URL}/dictionaries/${currentDictionaryId}`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
@@ -146,6 +155,7 @@ function WordList({ onNavigate, dictionaryId, isNewDictionary, refreshTrigger =
}
}
// Show save button only if name is not empty and has changed
const showSaveButton = dictionaryName.trim() !== '' && dictionaryName.trim() !== originalDictionaryName
if (loading) {
@@ -195,10 +205,8 @@ function WordList({ onNavigate, dictionaryId, isNewDictionary, refreshTrigger =
)}
</div>
{/* Show add button and words list:
- If dictionary exists (has dictionaryId), show regardless of name
- If new dictionary (no dictionaryId), show only if name is set */}
{((currentDictionaryId !== undefined && currentDictionaryId !== null && !isNewDict) || (isNewDict && dictionaryName.trim())) && (
{/* Show add button and words list only if dictionaryId exists and is not new */}
{hasValidDictionary(currentDictionaryId) && (
<>
{(!words || words.length === 0) ? (
<>