-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
680 additions
and
90 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
frontend/src/components/AutoSaveIndicator/AutosaveIndicator.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import { cva } from 'class-variance-authority'; | ||
|
||
const indicatorStyles = cva( | ||
'fixed left-0 right-0 transition-all duration-300 flex items-center justify-center py-1 text-sm font-medium z-40 border-b', | ||
{ | ||
variants: { | ||
status: { | ||
idle: 'bg-gray-50 text-gray-600', | ||
saving: 'bg-blue-50 text-blue-700', | ||
success: 'bg-green-50 text-green-700', | ||
error: 'bg-red-50 text-red-700' | ||
} | ||
}, | ||
defaultVariants: { | ||
status: 'idle' | ||
} | ||
} | ||
); | ||
|
||
export interface AutosaveIndicatorProps { | ||
status: 'idle' | 'saving' | 'success' | 'error'; | ||
message?: string; | ||
} | ||
|
||
export const AutosaveIndicator: React.FC<AutosaveIndicatorProps> = ({ | ||
status, | ||
message | ||
}) => { | ||
const [showSuccess, setShowSuccess] = useState(false); | ||
|
||
useEffect(() => { | ||
let timeout: NodeJS.Timeout; | ||
|
||
if (status === 'success') { | ||
setShowSuccess(true); | ||
timeout = setTimeout(() => { | ||
setShowSuccess(false); | ||
}, 2000); | ||
} | ||
|
||
return () => { | ||
if (timeout) clearTimeout(timeout); | ||
}; | ||
}, [status]); | ||
|
||
const indicatorStatus = | ||
status === 'saving' ? 'saving' : | ||
status === 'error' ? 'error' : | ||
showSuccess ? 'success' : 'idle'; | ||
|
||
const defaultMessages = { | ||
idle: 'Your answers will be autosaved as you complete your application', | ||
saving: 'Autosaving...', | ||
success: 'All changes saved', | ||
error: 'Failed to save changes' | ||
}; | ||
|
||
const displayMessage = message || defaultMessages[indicatorStatus]; | ||
|
||
return ( | ||
<div style={{ top: '96px' }} className={indicatorStyles({ status: indicatorStatus })}> | ||
<div className="flex items-center gap-2"> | ||
{status === 'saving' && ( | ||
<div className="w-4 h-4 relative"> | ||
<div className="absolute inset-0 border-2 border-blue-700 border-solid rounded-full border-r-transparent animate-spin" /> | ||
</div> | ||
)} | ||
<span>{displayMessage}</span> | ||
</div> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { AutosaveIndicator } from './AutoSaveIndicator'; | ||
export type { AutosaveIndicatorProps } from './AutoSaveIndicator'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import React, { useState } from 'react'; | ||
import { MdKeyboardArrowDown, MdKeyboardArrowUp } from 'react-icons/md'; | ||
import { sanitizeHtmlId } from '@/utils/html'; | ||
|
||
export interface ValidationError { | ||
section: string; | ||
subsection: string; | ||
questionText: string; | ||
inputType: string; | ||
required: boolean; | ||
value: any; | ||
reason: string; | ||
} | ||
|
||
interface ErrorsBySection { | ||
[section: string]: { | ||
count: number; | ||
errors: ValidationError[]; | ||
}; | ||
} | ||
|
||
export interface ProjectErrorProps { | ||
errors: ValidationError[]; | ||
onErrorClick: (section: string, subsectionId: string) => void; | ||
} | ||
|
||
export const ProjectError: React.FC<ProjectErrorProps> = ({ errors, onErrorClick }) => { | ||
const [expandedSections, setExpandedSections] = useState<Set<string>>(new Set()); | ||
|
||
if (errors.length === 0) return null; | ||
|
||
const errorsBySection: ErrorsBySection = errors.reduce((acc, error) => { | ||
if (!acc[error.section]) { | ||
acc[error.section] = { | ||
count: 0, | ||
errors: [] | ||
}; | ||
} | ||
|
||
acc[error.section].count += 1; | ||
acc[error.section].errors.push(error); | ||
|
||
return acc; | ||
}, {} as ErrorsBySection); | ||
|
||
const toggleSection = (section: string) => { | ||
setExpandedSections(prev => { | ||
const newSet = new Set(prev); | ||
|
||
if (newSet.has(section)) { | ||
newSet.delete(section); | ||
} else { | ||
newSet.add(section); | ||
} | ||
|
||
return newSet; | ||
}); | ||
}; | ||
|
||
const handleErrorClick = (error: ValidationError, e: React.MouseEvent) => { | ||
e.preventDefault(); | ||
onErrorClick(error.section, sanitizeHtmlId(error.subsection)); | ||
}; | ||
|
||
return ( | ||
<div className="fixed top-32 right-8 w-80 bg-white border border-dashed border-red-600 rounded-lg overflow-hidden"> | ||
<div className="bg-red-50 p-4 border-b border-red-100"> | ||
<div className="text-red-600 text-lg font-semibold"> | ||
Oops! You're missing information | ||
</div> | ||
</div> | ||
|
||
<div className="p-4 max-h-[calc(50vh-100px)] overflow-y-auto"> | ||
{Object.entries(errorsBySection).map(([section, { count, errors }]) => ( | ||
<div key={section} className="mb-4 last:mb-0"> | ||
<button | ||
onClick={() => toggleSection(section)} | ||
className="w-full flex items-center justify-between text-left mb-2 group" | ||
> | ||
<div> | ||
<h3 className="font-medium text-gray-900">{section}</h3> | ||
<p className="text-gray-600 text-sm"> | ||
{count} unfilled required field{count !== 1 ? 's' : ''} | ||
</p> | ||
</div> | ||
{expandedSections.has(section) ? ( | ||
<MdKeyboardArrowUp className="h-5 w-5 text-gray-500 group-hover:text-gray-700" /> | ||
) : ( | ||
<MdKeyboardArrowDown className="h-5 w-5 text-gray-500 group-hover:text-gray-700" /> | ||
)} | ||
</button> | ||
|
||
{expandedSections.has(section) && ( | ||
<div className="pl-4 space-y-2"> | ||
{errors.map((error, idx) => ( | ||
<div key={idx} className="border-l-2 border-red-200 pl-3"> | ||
<button | ||
onClick={(e) => handleErrorClick(error, e)} | ||
className="block w-full text-left hover:bg-red-50 p-2 rounded transition-colors" | ||
> | ||
<p className="text-sm font-medium text-gray-900"> | ||
{error.questionText} | ||
</p> | ||
<p className="text-xs text-gray-500 mt-1"> | ||
{error.reason} in {error.subsection} | ||
</p> | ||
</button> | ||
</div> | ||
))} | ||
</div> | ||
)} | ||
</div> | ||
))} | ||
|
||
<p className="text-sm text-gray-600 mt-4"> | ||
Please review these sections before submitting. Click on each error to go to the relevant question. | ||
</p> | ||
</div> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { ProjectError } from './ProjectError'; | ||
export type { ProjectErrorProps, ValidationError } from './ProjectError'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.