Skip to content

Commit 39887d3

Browse files
1000396: resolved the latest issues and improvements
1 parent 28b936d commit 39887d3

File tree

5 files changed

+639
-89
lines changed

5 files changed

+639
-89
lines changed

Employee_Managment_App/src/components/EmployeeInfo.tsx

Lines changed: 133 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,117 @@
11
import * as React from 'react';
22
import { TabComponent, TabItemDirective, TabItemsDirective } from '@syncfusion/ej2-react-navigations';
3-
import { Internationalization } from '@syncfusion/ej2-base';
43
import { useLocation } from 'react-router-dom';
5-
import { EmployeeDetails } from '../../interface';
4+
import { EmployeeDetails } from '../interface';
65
import EmployeeLeave from './EmployeeLeave';
76
import EmployeePayStub from './EmployeePayStub';
87
import EmployeePayRoll from './EmployeePayRoll';
9-
import Employees from './Employees';
8+
import { DataManager, UrlAdaptor, Query } from '@syncfusion/ej2-data';
9+
10+
1011

1112
const EmployeeInfo = (props: { employeeData?: EmployeeDetails; userInfo?: EmployeeDetails }) => {
1213
const location = useLocation();
13-
const employeeID = location.state?.employeeID;
14-
let employeeData: EmployeeDetails = props.employeeData
15-
? props.employeeData
16-
: employeeID
17-
? employeeID
18-
: {};
19-
const userInfo: EmployeeDetails = location.state?.userInfo
20-
? location.state?.userInfo
21-
: props.userInfo
22-
? props.userInfo
23-
: {};
24-
let intl: Internationalization = new Internationalization();
14+
const [defaultEmployee, setDefaultEmployee] = React.useState<EmployeeDetails | null>(null);
15+
const [loading, setLoading] = React.useState(true);
16+
17+
// Fetch default employee on component mount if no employee is selected
18+
React.useEffect(() => {
19+
const fetchDefaultEmployee = async () => {
20+
setLoading(true);
21+
try {
22+
const dataManager = new DataManager({
23+
url: 'https://ej2services.syncfusion.com/aspnet/development/api/EmployeesData',
24+
adaptor: new UrlAdaptor(),
25+
crossDomain: true,
26+
});
27+
28+
const query = new Query().take(1);
29+
const result: any = await dataManager.executeQuery(query);
30+
31+
console.log('Fetched employee data:', result);
32+
33+
// Handle different response formats
34+
let employeeArray: any[] = [];
35+
if (Array.isArray(result)) {
36+
employeeArray = result;
37+
} else if (result && Array.isArray(result.result)) {
38+
employeeArray = result.result;
39+
}
40+
41+
if (employeeArray.length > 0) {
42+
setDefaultEmployee(employeeArray[0] as EmployeeDetails);
43+
}
44+
} catch (error) {
45+
console.error('Error fetching default employee:', error);
46+
} finally {
47+
setLoading(false);
48+
}
49+
};
50+
51+
// Only fetch if no employee is already provided
52+
const routeEmployee = (location.state as any)?.employeeID;
53+
if (!routeEmployee && !props.employeeData && !props.userInfo) {
54+
fetchDefaultEmployee();
55+
}
56+
// Always set loading to false after checking - we have defaults
57+
setLoading(false);
58+
}, []);
59+
60+
// Default employee - used if API fetch fails or takes time
61+
const defaultEmployeeData: EmployeeDetails = {
62+
Name: 'Michael Anderson',
63+
EmployeeCode: 'EMP100001',
64+
Branch: 'Tower 1',
65+
Team: 'Management',
66+
Designation: 'General Manager',
67+
TeamLead: 'Christopher Anderson',
68+
ManagerName: 'Christopher Anderson',
69+
70+
DateOfJoining: new Date(new Date().getFullYear() - 20, 2, 1),
71+
FirstName: 'Michael',
72+
LastName: 'Anderson',
73+
FatherName: 'David Anderson',
74+
MotherName: 'Pamela Anderson',
75+
Gender: 'Male',
76+
BloodGroup: 'O+ve',
77+
MaritalStatus: 'Married',
78+
DOB: new Date(new Date().getFullYear() - 42, 3, 20),
79+
};
80+
81+
82+
83+
// Prefer explicit employee from route state, then prop, then fallback to defaultEmployee or hardcoded default
84+
const routeEmployee = (location.state as any)?.employeeID as any;
85+
const routeUser = (location.state as any)?.userInfo as any;
86+
const userInfo: EmployeeDetails = (routeUser as any) ?? (props.userInfo as any) ?? {} as any;
87+
let employeeData: EmployeeDetails = (routeEmployee as any) ?? (props.employeeData as any) ?? (userInfo as any) ?? (defaultEmployee as any) ?? defaultEmployeeData;
2588
// Format the date to the desired output
26-
const custom = {
27-
day: "numeric", // Displays day as a number (e.g., 1)
28-
month: "short", // Displays the short month name (e.g., Feb)
29-
year: "numeric" // Displays the full year (e.g., 2005)
89+
const custom: Intl.DateTimeFormatOptions = {
90+
day: 'numeric', // Displays day as a number (e.g., 1)
91+
month: 'short', // Displays the short month name (e.g., Feb)
92+
year: 'numeric' // Displays the full year (e.g., 2005)
3093
};
31-
let dateOfJoining = employeeData && employeeData.DateOfJoining.toLocaleDateString("en-GB", custom);
32-
let dob: string = employeeData && employeeData.DOB.toLocaleDateString("en-GB", custom);
33-
let experience: number = new Date().getFullYear() - employeeData.DateOfJoining.getFullYear();
34-
let experienceMonth: number = new Date().getMonth() - employeeData.DateOfJoining.getMonth();
94+
95+
// Normalize possible string dates to Date objects and guard for missing values
96+
const dojDate: Date | null = employeeData && (employeeData as any).DateOfJoining
97+
? new Date((employeeData as any).DateOfJoining)
98+
: null;
99+
const dobDate: Date | null = employeeData && (employeeData as any).DOB
100+
? new Date((employeeData as any).DOB)
101+
: null;
102+
103+
const dateOfJoining: string = dojDate ? dojDate.toLocaleDateString('en-GB', custom) : '-';
104+
const dob: string = dobDate ? dobDate.toLocaleDateString('en-GB', custom) : '-';
105+
106+
const now = new Date();
107+
let experienceYears = 0;
108+
let experienceMonths = 0;
109+
if (dojDate) {
110+
let months = (now.getFullYear() - dojDate.getFullYear()) * 12 + (now.getMonth() - dojDate.getMonth());
111+
if (months < 0) months = 0;
112+
experienceYears = Math.floor(months / 12);
113+
experienceMonths = months % 12;
114+
}
35115
let headerText: Object[] = [
36116
{ text: 'Official' },
37117
{ text: 'Personal' },
@@ -95,7 +175,7 @@ const EmployeeInfo = (props: { employeeData?: EmployeeDetails; userInfo?: Employ
95175
<div className="detail">
96176
<span className="sub-heading">Experience</span>
97177
<span className="gap">:</span>
98-
<span className="information">{experience} Years {experienceMonth} Months</span>
178+
<span className="information">{experienceYears} Years {experienceMonths} Months</span>
99179
</div>
100180
<div className="detail">
101181
<span className="sub-heading">User Work Shift</span>
@@ -200,7 +280,35 @@ const EmployeeInfo = (props: { employeeData?: EmployeeDetails; userInfo?: Employ
200280
);
201281
};
202282

283+
const hasEmployee = employeeData && Object.keys(employeeData as any).length > 0;
284+
285+
// Determine if private tabs should be visible
286+
// Only show when viewing the logged-in user's own profile.
287+
// With TopNav now passing EMP100001 as userInfo, this enables private tabs only when the selected
288+
// employee is EMP100001.
289+
const canSeePrivateTabs =
290+
!!employeeData?.EmployeeCode &&
291+
!!userInfo?.EmployeeCode &&
292+
userInfo.EmployeeCode === employeeData.EmployeeCode;
293+
294+
<TabComponent heightAdjustMode="Auto" swipeMode="None" overflowMode='Scrollable'>
295+
<TabItemsDirective>
296+
<TabItemDirective header={headerText[0]} content={content0} />
297+
{canSeePrivateTabs && <TabItemDirective header={headerText[1]} content={content1} />}
298+
{canSeePrivateTabs && <TabItemDirective header={headerText[2]} content={content2} />}
299+
{canSeePrivateTabs && <TabItemDirective header={headerText[3]} content={content3} />}
300+
{canSeePrivateTabs && <TabItemDirective header={headerText[4]} content={content4} />}
301+
</TabItemsDirective>
302+
</TabComponent>
303+
203304
const overview = () => {
305+
if (loading) {
306+
return (
307+
<div className="tab-content">
308+
<div>Loading employee data...</div>
309+
</div>
310+
);
311+
}
204312
return (
205313
<div>
206314
<div className="overview-header">
@@ -227,7 +335,6 @@ const EmployeeInfo = (props: { employeeData?: EmployeeDetails; userInfo?: Employ
227335
<div className="profile-data-designation">{employeeData.Designation}</div>
228336
<div className='profile-data-Teamname'>{employeeData.Team}</div>
229337
<div className="profile-data-supervisor">Supervisor: {employeeData.TeamLead}</div>
230-
<div className="profile-data-branch">Branch: {employeeData.Branch}</div>
231338
<div className="Profile-data-availability e-badge"> Available - {employeeData.Branch}</div>
232339
</div>
233340
</div>
@@ -277,4 +384,4 @@ const EmployeeInfo = (props: { employeeData?: EmployeeDetails; userInfo?: Employ
277384
);
278385
};
279386

280-
export default EmployeeInfo;
387+
export default EmployeeInfo;

Employee_Managment_App/src/components/EmployeeLeave.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,10 +298,10 @@ const statusTemplate = (args: any) => {
298298
actionComplete={actionComplete}
299299
>
300300
<ColumnsDirective>
301-
<ColumnDirective field="EmployeeCode" headerText="ID" visible={false} width="120" />
302-
<ColumnDirective field="AttendanceID" headerText="Leave ID" isPrimaryKey={true} width="140" />
303-
<ColumnDirective field="AbsenceType" headerText="Leave Type" width="120" template={leaveTypeTemplate} />
304-
<ColumnDirective field="ShiftName" headerText="Shift Name" width="120" />
301+
<ColumnDirective field="EmployeeCode" headerText="ID" visible={false} clipMode="EllipsisWithTooltip" width="120" />
302+
<ColumnDirective field="AttendanceID" headerText="Leave ID" isPrimaryKey={true} clipMode="EllipsisWithTooltip" width="140" />
303+
<ColumnDirective field="AbsenceType" headerText="Leave Type" width="120" clipMode="EllipsisWithTooltip" template={leaveTypeTemplate} />
304+
<ColumnDirective field="ShiftName" headerText="Shift Name" clipMode="EllipsisWithTooltip" width="120" />
305305
<ColumnDirective field="From" type="date" format="MMM d yyyy" textAlign="Right" width="120" />
306306
<ColumnDirective field="To" type="date" format="MMM d yyyy" textAlign="Right" width="120" />
307307
<ColumnDirective field="Days" headerText="Day(s)" textAlign="Right" width="120" />

0 commit comments

Comments
 (0)