Implement final answer management in submission hook and storage utilities. Add functions to save, load, and clear final answers in localStorage. Update useSubmission hook to prioritize final answers over drafts, enhancing user experience during task submissions.
Some checks failed
platform/bro-js/challenge-pl/pipeline/head There was a failure building this commit
Some checks failed
platform/bro-js/challenge-pl/pipeline/head There was a failure building this commit
This commit is contained in:
@@ -6,6 +6,7 @@ import {
|
|||||||
} from '../__data__/api/api'
|
} from '../__data__/api/api'
|
||||||
import type { ChallengeSubmission, QueueStatus } from '../__data__/types'
|
import type { ChallengeSubmission, QueueStatus } from '../__data__/types'
|
||||||
import { useChallenge } from '../context/ChallengeContext'
|
import { useChallenge } from '../context/ChallengeContext'
|
||||||
|
import { loadFinalAnswer, saveFinalAnswer } from '../utils/drafts'
|
||||||
|
|
||||||
interface UseSubmissionArgs {
|
interface UseSubmissionArgs {
|
||||||
taskId: string
|
taskId: string
|
||||||
@@ -43,13 +44,24 @@ export const useSubmission = ({ taskId }: UseSubmissionArgs): SubmissionResult =
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
behaviorTracker.reset()
|
behaviorTracker.reset()
|
||||||
const draft = loadDraft(taskId)
|
|
||||||
if (draft) {
|
// Сначала проверяем финальный ответ (если задание уже решалось)
|
||||||
setResultState(draft)
|
const finalAnswer = loadFinalAnswer(taskId)
|
||||||
|
if (finalAnswer) {
|
||||||
|
setResultState(finalAnswer)
|
||||||
behaviorTracker.markDraftUsed()
|
behaviorTracker.markDraftUsed()
|
||||||
} else {
|
} else {
|
||||||
setResultState('')
|
// Если финального ответа нет, проверяем черновик
|
||||||
|
const draft = loadDraft(taskId)
|
||||||
|
if (draft) {
|
||||||
|
setResultState(draft)
|
||||||
|
behaviorTracker.markDraftUsed()
|
||||||
|
} else {
|
||||||
|
// Если ничего нет - пустое поле
|
||||||
|
setResultState('')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pollingManager.stop()
|
pollingManager.stop()
|
||||||
setQueueId(null)
|
setQueueId(null)
|
||||||
setQueueStatus(null)
|
setQueueStatus(null)
|
||||||
@@ -114,6 +126,9 @@ export const useSubmission = ({ taskId }: UseSubmissionArgs): SubmissionResult =
|
|||||||
})
|
})
|
||||||
|
|
||||||
setFinalSubmission(status.submission)
|
setFinalSubmission(status.submission)
|
||||||
|
// Сохраняем финальный ответ для восстановления при возврате
|
||||||
|
saveFinalAnswer(taskId, result)
|
||||||
|
// Очищаем черновик, так как теперь есть финальный ответ
|
||||||
clearDraft(taskId)
|
clearDraft(taskId)
|
||||||
pollingManager.stop()
|
pollingManager.stop()
|
||||||
return false
|
return false
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
const STORAGE_PREFIX = 'challenge_draft_'
|
const STORAGE_PREFIX = 'challenge_draft_'
|
||||||
|
const FINAL_ANSWER_PREFIX = 'challenge_final_answer_'
|
||||||
|
|
||||||
const isBrowser = () => typeof window !== 'undefined' && typeof window.localStorage !== 'undefined'
|
const isBrowser = () => typeof window !== 'undefined' && typeof window.localStorage !== 'undefined'
|
||||||
|
|
||||||
@@ -17,6 +18,24 @@ export function clearDraft(taskId: string) {
|
|||||||
window.localStorage.removeItem(`${STORAGE_PREFIX}${taskId}`)
|
window.localStorage.removeItem(`${STORAGE_PREFIX}${taskId}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Сохранение финального ответа (после успешной отправки)
|
||||||
|
export function saveFinalAnswer(taskId: string, result: string) {
|
||||||
|
if (!isBrowser()) return
|
||||||
|
window.localStorage.setItem(`${FINAL_ANSWER_PREFIX}${taskId}`, result)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Загрузка финального ответа
|
||||||
|
export function loadFinalAnswer(taskId: string): string | null {
|
||||||
|
if (!isBrowser()) return null
|
||||||
|
return window.localStorage.getItem(`${FINAL_ANSWER_PREFIX}${taskId}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Очистка финального ответа
|
||||||
|
export function clearFinalAnswer(taskId: string) {
|
||||||
|
if (!isBrowser()) return
|
||||||
|
window.localStorage.removeItem(`${FINAL_ANSWER_PREFIX}${taskId}`)
|
||||||
|
}
|
||||||
|
|
||||||
export function listDrafts() {
|
export function listDrafts() {
|
||||||
if (!isBrowser()) return [] as string[]
|
if (!isBrowser()) return [] as string[]
|
||||||
|
|
||||||
@@ -30,3 +49,16 @@ export function listDrafts() {
|
|||||||
return keys
|
return keys
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Очистка всех финальных ответов (при выходе)
|
||||||
|
export function clearAllFinalAnswers() {
|
||||||
|
if (!isBrowser()) return
|
||||||
|
const keysToRemove: string[] = []
|
||||||
|
for (let i = 0; i < window.localStorage.length; i += 1) {
|
||||||
|
const key = window.localStorage.key(i)
|
||||||
|
if (key?.startsWith(FINAL_ANSWER_PREFIX)) {
|
||||||
|
keysToRemove.push(key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
keysToRemove.forEach(key => window.localStorage.removeItem(key))
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,8 @@
|
|||||||
* Все ключи и операции в одном месте
|
* Все ключи и операции в одном месте
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import { clearAllFinalAnswers, listDrafts, clearDraft } from './drafts'
|
||||||
|
|
||||||
const isBrowser = () => typeof window !== 'undefined'
|
const isBrowser = () => typeof window !== 'undefined'
|
||||||
|
|
||||||
// Ключи localStorage
|
// Ключи localStorage
|
||||||
@@ -107,6 +109,13 @@ export const storage = {
|
|||||||
|
|
||||||
// Очищаем все прогрессы по цепочкам
|
// Очищаем все прогрессы по цепочкам
|
||||||
storage.clearAllChainProgress()
|
storage.clearAllChainProgress()
|
||||||
|
|
||||||
|
// Очищаем все финальные ответы
|
||||||
|
clearAllFinalAnswers()
|
||||||
|
|
||||||
|
// Очищаем все черновики
|
||||||
|
const drafts = listDrafts()
|
||||||
|
drafts.forEach(taskId => clearDraft(taskId))
|
||||||
},
|
},
|
||||||
|
|
||||||
// Очистка всех прогрессов по цепочкам
|
// Очистка всех прогрессов по цепочкам
|
||||||
|
|||||||
Reference in New Issue
Block a user