Fixed careerSuggestions on refresh/login
This commit is contained in:
parent
b71920f68e
commit
7bb176c12a
@ -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);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
const priorities = profileData.career_priorities
|
// Check explicitly for Interest Inventory answers
|
||||||
? JSON.parse(profileData.career_priorities)
|
if (profileData.interest_inventory_answers) {
|
||||||
: {};
|
const answers = profileData.interest_inventory_answers;
|
||||||
|
const careerSuggestionsRes = await axios.post(`${apiUrl}/onet/submit_answers`, {
|
||||||
|
answers,
|
||||||
|
state: profileData.state,
|
||||||
|
area: profileData.area,
|
||||||
|
});
|
||||||
|
|
||||||
const allAnswered = ['interests', 'meaning', 'stability', 'growth', 'balance', 'recognition'].every(
|
// Destructure `careers` from the server response
|
||||||
(key) => priorities[key]
|
const { careers = [] } = careerSuggestionsRes.data || {};
|
||||||
);
|
|
||||||
|
|
||||||
if (!allAnswered) {
|
// Flatten in case it's a nested array (or just a no-op if already flat)
|
||||||
setShowModal(true);
|
setCareerSuggestions(careers.flat());
|
||||||
}
|
} else {
|
||||||
} else {
|
// No interest inventory answers: fallback to an empty list
|
||||||
setShowModal(true); // profile not found, show modal by default
|
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);
|
||||||
|
setShowModal(true);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
fetchUserProfile();
|
fetchUserProfile();
|
||||||
}, [apiUrl]);
|
}, [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(() => {
|
||||||
|
BIN
user_profile.db
BIN
user_profile.db
Binary file not shown.
Loading…
Reference in New Issue
Block a user