修复考核调研评分进度回显

This commit is contained in:
chenpei 2026-07-13 18:39:56 +08:00
parent aadea673d6
commit 9ad759c694
3 changed files with 85 additions and 6 deletions

View File

@ -8,6 +8,7 @@ import { useSession } from 'next-auth/react'
import { pinyin } from 'pinyin-pro'
import { createPortal } from 'react-dom'
import { sortPerformanceSurveyImagesByName } from '@/lib/performance-survey-image-order'
import { calculatePerformanceSurveyResponseProgress } from '@/lib/performance-survey-response-progress'
type ApiState<T> = {
loading: boolean
@ -3850,9 +3851,13 @@ function SurveySubmit({ surveyId, session }: { surveyId: string; session: any })
const activeTarget = detail.data.targets.find((target: any) => target.id === activeTargetId)
const submitted = activeTarget?.currentAssignment?.status === 'submitted'
const scoreValues = questions.map((question) => Number(scores[question.id] ?? 0))
const scoreTotal = scoreValues.reduce((sum, value) => sum + value, 0)
const ratedCount = questions.filter((question) => Number.isFinite(scores[question.id])).length
const submittedScores = activeTarget?.currentAssignment?.response?.scores
const progress = calculatePerformanceSurveyResponseProgress({
questions,
draftScores: scores,
submittedScores: submittedScores && typeof submittedScores === 'object' ? submittedScores : {},
submitted,
})
const surveyStatusLabel = getSurveyStatusLabel(detail.data.status)
const dueLabel = detail.data.dueAt ? `截止 ${formatDate(detail.data.dueAt)}` : '无截止时间'
const viewer = session?.user || {}
@ -3913,12 +3918,12 @@ function SurveySubmit({ surveyId, session }: { surveyId: string; session: any })
<div className="mt-3 rounded-2xl bg-[#f6f8fb] px-3.5 py-2.5">
<div className="flex items-center justify-between text-[12px] font-semibold text-[#667085]">
<span>填写进度</span>
<span className="text-[#101828]">{ratedCount}/{questions.length || 0} · {scoreTotal} 分</span>
<span className="text-[#101828]">{progress.ratedCount}/{progress.totalCount} · {progress.totalScore} 分</span>
</div>
<div className="mt-2 h-1.5 overflow-hidden rounded-full bg-[#e5edf6]">
<div
className="h-full rounded-full bg-[#1677ff] transition-all"
style={{ width: `${questions.length > 0 ? Math.min(100, (ratedCount / questions.length) * 100) : 0}%` }}
style={{ width: `${progress.percent}%` }}
/>
</div>
</div>
@ -4078,7 +4083,7 @@ function SurveySubmit({ surveyId, session }: { surveyId: string; session: any })
</button>
</div>
<button
disabled={submitting || !activeTargetId || questions.length === 0 || ratedCount !== questions.length}
disabled={submitting || !activeTargetId || questions.length === 0 || progress.ratedCount !== questions.length}
onClick={submit}
className="min-h-[52px] w-full rounded-[16px] bg-[#1677ff] px-4 py-2.5 text-[17px] font-semibold text-white shadow-[0_12px_24px_rgba(22,119,255,0.28)] transition active:scale-[0.99] disabled:bg-[#9ec9ff] disabled:shadow-none"
>

View File

@ -0,0 +1,44 @@
import { calculatePerformanceSurveyResponseProgress } from '@/lib/performance-survey-response-progress'
const questions = [{ id: 'q1' }, { id: 'q2' }, { id: 'q3' }]
describe('performance survey response progress', () => {
it('填写过程中读取尚未提交的本地评分', () => {
const result = calculatePerformanceSurveyResponseProgress({
questions,
draftScores: { q1: 12, q2: 0 },
submitted: false,
})
expect(result).toMatchObject({
ratedCount: 2,
totalCount: 3,
totalScore: 12,
})
expect(result.percent).toBeCloseTo(200 / 3)
})
it('提交后改读数据库响应,不受本地评分清空影响', () => {
expect(calculatePerformanceSurveyResponseProgress({
questions,
draftScores: {},
submittedScores: { q1: 18, q2: 17, q3: 16 },
submitted: true,
})).toEqual({
ratedCount: 3,
totalCount: 3,
totalScore: 51,
percent: 100,
})
})
it('零分算已填写,缺失值和空值不算', () => {
expect(calculatePerformanceSurveyResponseProgress({
questions,
draftScores: { q1: 0, q2: null, q3: '' },
submitted: false,
})).toMatchObject({
ratedCount: 1,
totalScore: 0,
})
})
})

View File

@ -0,0 +1,30 @@
type PerformanceSurveyProgressQuestion = {
id: string
}
export function calculatePerformanceSurveyResponseProgress(input: {
questions: PerformanceSurveyProgressQuestion[]
draftScores?: Record<string, unknown> | null
submittedScores?: Record<string, unknown> | null
submitted: boolean
}) {
const scoreSource = input.submitted
? input.submittedScores || {}
: input.draftScores || {}
const values = input.questions.map((question) => {
const rawValue = scoreSource[question.id]
if (rawValue === null || rawValue === undefined || rawValue === '') return null
const numericValue = Number(rawValue)
return Number.isFinite(numericValue) ? numericValue : null
})
const ratedValues = values.filter((value): value is number => value !== null)
const totalCount = input.questions.length
const ratedCount = ratedValues.length
return {
ratedCount,
totalCount,
totalScore: ratedValues.reduce((sum, value) => sum + value, 0),
percent: totalCount > 0 ? Math.min(100, (ratedCount / totalCount) * 100) : 0,
}
}