Fixed careerSuggestions on refresh/login

This commit is contained in:
Josh 2025-05-14 15:20:55 +00:00
parent b71920f68e
commit 7bb176c12a
2 changed files with 74 additions and 55 deletions

View File

@ -151,47 +151,63 @@ function CareerExplorer({ handleCareerClick, userState, areaTitle }) {
}; };
useEffect(() => { useEffect(() => {
const fetchUserProfile = async () => { const fetchUserProfile = async () => {
try { try {
const token = localStorage.getItem('token'); const token = localStorage.getItem('token');
const res = await axios.get(`${apiUrl}/user-profile`, { const res = await axios.get(`${apiUrl}/user-profile`, {
headers: { Authorization: `Bearer ${token}` }, headers: { Authorization: `Bearer ${token}` },
}); });
if (res.status === 200) { if (res.status === 200) {
const profileData = res.data; const profileData = res.data;
setUserProfile(profileData); setUserProfile(profileData);
if (profileData.career_list) { // Explicitly set careerList from saved data
try { if (profileData.career_list) {
setCareerList(JSON.parse(profileData.career_list)); setCareerList(JSON.parse(profileData.career_list));
} catch (err) {
console.error('Error parsing career_list:', err);
} }
}
// Check explicitly for Interest Inventory answers
const priorities = profileData.career_priorities if (profileData.interest_inventory_answers) {
? JSON.parse(profileData.career_priorities) const answers = profileData.interest_inventory_answers;
: {}; const careerSuggestionsRes = await axios.post(`${apiUrl}/onet/submit_answers`, {
answers,
const allAnswered = ['interests', 'meaning', 'stability', 'growth', 'balance', 'recognition'].every( state: profileData.state,
(key) => priorities[key] area: profileData.area,
); });
if (!allAnswered) { // Destructure `careers` from the server response
setShowModal(true); const { careers = [] } = careerSuggestionsRes.data || {};
}
} else { // Flatten in case it's a nested array (or just a no-op if already flat)
setShowModal(true); // profile not found, show modal by default setCareerSuggestions(careers.flat());
} else {
// No interest inventory answers: fallback to an empty list
setCareerSuggestions([]);
}
const priorities = profileData.career_priorities
? JSON.parse(profileData.career_priorities)
: {};
const allAnswered = ['interests', 'meaning', 'stability', 'growth', 'balance', 'recognition'].every(
(key) => priorities[key]
);
if (!allAnswered) {
setShowModal(true);
} }
} catch (err) { } else {
console.error('Error fetching user profile:', err); setShowModal(true);
setShowModal(true); // on error, default to showing modal
} }
}; } catch (err) {
console.error('Error fetching user profile:', err);
fetchUserProfile(); setShowModal(true);
}, [apiUrl]); }
};
fetchUserProfile();
}, [apiUrl]);
// Load suggestions from Interest Inventory if provided (optional) // Load suggestions from Interest Inventory if provided (optional)
useEffect(() => { useEffect(() => {
@ -202,26 +218,29 @@ function CareerExplorer({ handleCareerClick, userState, areaTitle }) {
// Fetch Job Zones if suggestions are provided // Fetch Job Zones if suggestions are provided
useEffect(() => { useEffect(() => {
const fetchJobZones = async () => { const fetchJobZones = async () => {
if (!careerSuggestions.length) return; if (!careerSuggestions.length) return;
const flatSuggestions = careerSuggestions.flat(); const flatSuggestions = careerSuggestions.flat();
const socCodes = flatSuggestions.map((career) => career.code); const socCodes = flatSuggestions.map((career) => career.code);
try {
const response = await axios.post(`${apiUrl}/job-zones`, { socCodes });
const jobZoneData = response.data;
const updatedCareers = flatSuggestions.map((career) => ({
...career,
job_zone: jobZoneData[career.code.slice(0, -3)]?.job_zone || null,
}));
setCareersWithJobZone(updatedCareers);
} catch (error) {
console.error('Error fetching job zone information:', error);
}
};
fetchJobZones(); try {
}, [careerSuggestions, apiUrl]); const response = await axios.post(`${apiUrl}/job-zones`, { socCodes });
const jobZoneData = response.data;
const updatedCareers = flatSuggestions.map((career) => ({
...career,
job_zone: jobZoneData[career.code.slice(0, -3)]?.job_zone || null,
}));
// IMPORTANT: Ensure this actually sets a new array
setCareersWithJobZone([...updatedCareers]);
} catch (error) {
console.error('Error fetching job zone information:', error);
}
};
fetchJobZones();
}, [careerSuggestions, apiUrl]);
// Filtering logic (Job Zone and Fit) // Filtering logic (Job Zone and Fit)
const filteredCareers = useMemo(() => { const filteredCareers = useMemo(() => {

Binary file not shown.