import React, { useState, useEffect } from 'react';
import { useRouter } from 'next/router';
import { makeStyles } from "@material-ui/core/styles";
import Button from "@material-ui/core/Button";
import School from "@material-ui/icons/School";
import PermIdentity from '@material-ui/icons/PermIdentity';
import Description from '@material-ui/icons/Description';
import Work from "@material-ui/icons/Work";
import Code from "@material-ui/icons/Code";
import GitHub from "@material-ui/icons/GitHub";
import GridContainer from "/components/Grid/GridContainer.js";
import GridItem from "/components/Grid/GridItem.js";
import CustomTabs from "/components/CustomTabs/CustomTabs.js";
import styles from "/styles/jss/nextjs-material-kit/pages/componentsSections/tabsStyle.js";
import Education from '../../components/Profile/Education';
import Experience from '../../components/Profile/Experience';
import Leetcode from '../../components/Profile/Leetcode';
import Github from '../../components/Profile/Github';
import Extras from '../../components/Profile/Extras';
import { getServerAuthSession } from "../../lib/auth";
import { fetchUserProfile, updateUserProfile, getVerifiedUser } from '../../utils/api';
import CustomLinearProgress from "/components/CustomLinearProgress/CustomLinearProgress.js";
import BasicInfo from '../../components/Profile/BasicInfo';
setCachedProfile(updatedProfile);
// Handle successful save (e.g., show a success message)
} catch (error) {
console.error('Error saving profile:', error);
// Handle error (e.g., show an error message to the user)
}
export async function getServerSideProps(context) {
const session = await getServerAuthSession(context);
if (!session) {
return {
redirect: {
destination: '/login',
permanent: false,
},
};
}
// Ensure the role is defined or set to null
const role = session.user.role || null;
import React, { useState } from 'react';
import { makeStyles } from "@material-ui/core/styles";
import styles from "/styles/jss/nextjs-material-kit/pages/componentsSections/tabsStyle.js";
import CustomInput from "/components/CustomInput/CustomInput";
import Button from "@material-ui/core/Button";
import Card from "@material-ui/core/Card";
import CardContent from "@material-ui/core/CardContent";
import Grid from "@material-ui/core/Grid";
import Select from "@material-ui/core/Select";
import MenuItem from "@material-ui/core/MenuItem";
import FormControl from "@material-ui/core/FormControl";
import InputLabel from "@material-ui/core/InputLabel";
import TextField from "@material-ui/core/TextField";
import Chip from "@material-ui/core/Chip";
</Grid>
<TextField
label="Tech Stack (Enter tech stack separated by commas) (The more you change the tech stack, the more points you lose)"
variant="outlined"
fullWidth
value={experience.techStack}
onChange={(e) => handleTechStackChange(index, e)}
margin="normal"
id={`techStack-${index}`}
/>
<div className={classes.chipContainer}>
{experience.techStack.split(',').map((tech, i) => (
<Chip key={i} label={tech.trim()} className={classes.chip} />
))}
</div>
<div className={classes.pointContainer}>
{Array.from({ length: getPointCount(experience) }, (_, i) => (
<div key={i} className={classes.pointInput}>
<span className={classes.bulletPoint}>•</span>
<CustomInput
labelText="What you did(sentence must end with a full stop)"
id={`what-${index}-${i}`}
formControlProps={{ fullWidth: true }}
inputProps={{
name: `what-${i}`,
value: experience.points?.[i]?.what || '',
onChange: (event) => handlePointChange(index, i, 'what', event.target.value),
}}
/>
<CustomInput
labelText="Impact it created(sentence must end with a full stop)"
id={`impact-${index}-${i}`}
formControlProps={{ fullWidth: true }}
inputProps={{
name: `impact-${i}`,
value: experience.points?.[i]?.impact || '',
onChange: (event) => handlePointChange(index, i, 'impact', event.target.value),
}}
/>
</div>
))}
</div>
</CardContent>
</Card>
))}
{experiences.length < 8 && (
<Button
color="primary"
onClick={addExperience}
className={classes.addButton}
>
Add Experience
</Button>
)}
</div>
);
}
The exprience and project tabs both have dates and points.
However, for experience tab, the points are dynamic depending on some logic.
What is happening is, the percentage completion is breaking apart because of this dynamic calculation of points. Projects also have the same kind of structure but the number of points in projects are fixed(2). In experience however, it's dynamic. And percentage completion probably assumes the data before hand, and doesn't wait for the dynamic calculation of points.
You're encountering an issue where the dynamic calculation of points in the Experience tab isn't reflected in your profile completion percentage. This happens because your calculateCompletionPercentage function doesn't consider the dynamic number of fields introduced due to the logic in getPointCount for experiences.
To fix this, you need to adjust the calculateCompletionPercentage function to account for the dynamically expected fields in the experiences. Here's how you can do it:
Bring getPointCount and getDuration into the Profile Component
Since getPointCount and getDuration are used to determine the expected number of points in an experience, you'll need them in the Profile component where calculateCompletionPercentage resides.
Modify calculateCompletionPercentage to Use Expected Fields in Experiences
Update your calculateCompletionPercentage function to account for the expected number of points in each experience based on getPointCount. Here's the revised function:
jsx
const calculateCompletionPercentage = (data) => {
let totalFields = 0;
let filledFields = 0;
const countFields = (obj, parentKey = '') => {
for (const key in obj) {
if (key === 'twitterUrl') continue;
if (key === 'countryOfOrigin' && obj['international'] === 'no') continue;
if (key === 'tier') continue;
const value = obj[key];
const isObject = typeof value === 'object' && value !== null && !Array.isArray(value);
if (isObject && key !== 'points') {
countFields(value, key);
} else if (Array.isArray(value)) {
if (key === 'experiences') {
value.forEach((experience) => countExperienceFields(experience));
} else if (key === 'projects') {
value.forEach((project) => countFields(project, key));
} else {
value.forEach((item) => countFields(item));
}
} else if (key !== 'points') {
totalFields++;
if (value !== '' && value !== null && value !== undefined) {
filledFields++;
}
}
}
};
const countExperienceFields = (experience) => {
for (const key in experience) {
if (key === 'points' || key === 'tier') continue;
const value = experience[key];
const isObject = typeof value === 'object' && value !== null && !Array.isArray(value);
if (isObject) {
countFields(value);
} else {
totalFields++;
if (value !== '' && value !== null && value !== undefined) {
filledFields++;
}
}
}
const expectedPointCount = getPointCount(experience);
totalFields += expectedPointCount * 2; // Each point has 'what' and 'impact'
if (experience.points && Array.isArray(experience.points)) {
for (let i = 0; i < expectedPointCount; i++) {
const point = experience.points[i] || {};
['what', 'impact'].forEach((field) => {
totalFields++;
if (point[field] !== '' && point[field] !== null && point[field] !== undefined) {
filledFields++;
}
});
}
} else {
totalFields += expectedPointCount * 2;
}
};
countFields(data);
return totalFields > 0 ? (filledFields / totalFields) * 100 : 0;
};
Explanation:
Recursive Counting: The countFields function is modified to pass down a parentKey to identify when it's processing experiences.
Handling Experiences Separately: When countFields encounters the experiences key, it calls countExperienceFields for each experience.
Counting Expected Points in Experiences: Within countExperienceFields, it calculates expectedPointCount using getPointCount(experience) and adjusts totalFields and filledFields accordingly.
Accounting for Missing Points: If experience.points doesn't have all expected points, the missing fields are still added to totalFields but not to filledFields.
Ensure Consistency Between Components
Make sure the getPointCount function in the Experience component and the one in the Profile component are identical. Consider moving it to a utility file if necessary to maintain consistency.
Adjust Experience Component if Needed
If your Experience component modifies the points array or relies on side effects, ensure it's synchronized with the logic in the Profile component.
Test Thoroughly
After making these changes, test the profile completion percentage by adding experiences with varying types and durations to confirm that the percentage updates correctly.
By accounting for the dynamic number of points in your profile completion calculation, you ensure that the completion percentage accurately reflects the user's progress, even when the number of required fields changes based on certain conditions.