Skip to content

Commit b484218

Browse files
committed
refactor(resume): Improve resume section content update logic
- Enhance section content update mechanism to handle array and object content types - Modify updateSectionContent method to support direct array replacement - Refactor type handling for section content updates - Improve type safety and flexibility in section content modifications - Optimize random ID generation for better uniqueness
1 parent fb7d38f commit b484218

1 file changed

Lines changed: 20 additions & 14 deletions

File tree

contexts/ResumeContext.tsx

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,14 @@ import {
1515
DEFAULT_METADATA,
1616
DEFAULT_PERSONAL_INFO,
1717
SECTION_TYPES,
18-
DeepPartial,
1918
ResumeInsert,
2019
ResumeUpdate,
2120
} from '@/types/resume';
2221
import { Profile } from '@/types/profile';
2322
import { ResumeImportService, ImportResult } from '@/lib/services/resume-import';
2423
import { updateResumeMetadata } from '@/lib/services/resume-metadata';
2524
import { ResumeScoringService, ScoringResult } from '@/lib/services/resume-scoring';
26-
import { saveToLocalStorage, loadFromLocalStorage, removeFromLocalStorage } from '@/lib/storage/offline-storage';
25+
import { saveToLocalStorage, removeFromLocalStorage } from '@/lib/storage/offline-storage';
2726

2827
// Context Type Definition
2928
export interface ResumeContextType {
@@ -84,7 +83,7 @@ interface ResumeProviderProps {
8483
}
8584

8685
// Helper function to generate unique IDs
87-
const generateId = () => `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
86+
const generateId = () => `${Date.now()}-${Math.random().toString(36).substring(2, 11)}`;
8887

8988
// Helper function to create default section
9089
const createDefaultSection = (type: SectionType, order: number): ResumeSection => {
@@ -299,12 +298,12 @@ export function ResumeProvider({ children, initialResumes = [], userProfile }: R
299298
const errorMessage = err instanceof ResumeError ? err.message : 'Failed to save resume';
300299
setError(errorMessage);
301300
setSaveStatus('error');
302-
301+
303302
// Keep in local storage if save failed
304303
if (resume) {
305304
saveToLocalStorage(resume);
306305
}
307-
306+
308307
throw err;
309308
} finally {
310309
setSaving(false);
@@ -402,10 +401,10 @@ export function ResumeProvider({ children, initialResumes = [], userProfile }: R
402401
const updateResumeTitle = useCallback((title: string) => {
403402
setResume((prev) => {
404403
if (!prev) return null;
405-
404+
406405
// Store previous state for potential rollback
407406
optimisticStateRef.current = prev;
408-
407+
409408
// Apply optimistic update immediately
410409
return { ...prev, title };
411410
});
@@ -423,10 +422,10 @@ export function ResumeProvider({ children, initialResumes = [], userProfile }: R
423422
const sectionInfo = SECTION_TYPES[type];
424423
setAnnouncement(`${sectionInfo.defaultTitle} section added`);
425424
setLastAddedSectionId(newSection.id);
426-
425+
427426
// Clear the lastAddedSectionId after a short delay
428427
setTimeout(() => setLastAddedSectionId(null), 500);
429-
428+
430429
// Apply optimistic update immediately
431430
return {
432431
...prev,
@@ -492,7 +491,14 @@ export function ResumeProvider({ children, initialResumes = [], userProfile }: R
492491
return {
493492
...prev,
494493
sections: prev.sections.map((s) =>
495-
s.id === sectionId ? { ...s, content: { ...s.content, ...content } as SectionContent } : s
494+
s.id === sectionId
495+
? {
496+
...s,
497+
content: Array.isArray(s.content) && Array.isArray(content)
498+
? (content as SectionContent) // If both are arrays, replace directly with proper type
499+
: { ...(s.content as object), ...(content as object) } as SectionContent, // Otherwise, merge objects
500+
}
501+
: s
496502
),
497503
};
498504
});
@@ -721,7 +727,7 @@ export function ResumeProvider({ children, initialResumes = [], userProfile }: R
721727
if (isSavingRef.current || saveQueueRef.current.length === 0) return;
722728

723729
isSavingRef.current = true;
724-
730+
725731
// Get the latest resume from the queue (discard intermediate states)
726732
const latestResume = saveQueueRef.current[saveQueueRef.current.length - 1];
727733
saveQueueRef.current = [];
@@ -774,21 +780,21 @@ export function ResumeProvider({ children, initialResumes = [], userProfile }: R
774780
const errorMessage = err instanceof ResumeError ? err.message : 'Failed to save resume';
775781
setError(errorMessage);
776782
setSaveStatus('error');
777-
783+
778784
// Rollback to previous state if available
779785
if (optimisticStateRef.current) {
780786
setResume(optimisticStateRef.current);
781787
setAnnouncement('Changes reverted due to save error');
782788
}
783-
789+
784790
// Keep in local storage if save failed
785791
if (latestResume) {
786792
saveToLocalStorage(latestResume);
787793
}
788794
} finally {
789795
setSaving(false);
790796
isSavingRef.current = false;
791-
797+
792798
// Process any new items that were added to the queue while saving
793799
if (saveQueueRef.current.length > 0) {
794800
processSaveQueue();

0 commit comments

Comments
 (0)