Reputation: 1157
I've a requirement for leave management system where user can apply for leaves depending upon their allotment and using Angular
with ASP.NET Web Api
. Example:
UserId - Leave - Allotment - Year
1001 - Casual - 20 - 2019
1001 - Sick - 20 - 2019
1002 - Annual - 10 - 2019
This is pretty basic. So what I am doing whenever a user tries to apply for leave, the system gets total days from start and end date, finally checks the allotment with respect to the total days. In the front-end, user has to select a type, for which the specific allotment no should be deducted as follows:
<label for="leaveType">Leave Type</label>
<select [(ngModel)]="leaveType" [ngModelOptions]="{standalone: true}" class="form-control">
<option value="">Please select leave</option>
<option *ngFor="let value of leaveType" [ngValue]="value.LeaveType">
{{value.LeaveType}}
</option>
</select>
I am getting the leave allotment details using ASP.NET Web Api
for a specific user perfectly and this is what I did:
leaveType: string;
chkLeave: number = 0;
userLeave: any;
this.dataservice.GetLeaveInfo(selectedValue).subscribe(result => { //selectedValue is another drop down value where users are being selected with user id
this.userLeave = result;
}, error => console.error(error));
Then finally in another method using Angular
, I try to verify whether there is available leave allotment for that specific leave type like this:
chkAllotment() {
var startDate = new Date(Result[0]);
var endDate = new Date(Result[1]);
const diff = endDate.getDate() - startDate.getDate();
debugger;
for(let i = 0; i < this.userLeave.length; i++) {
if(userLeave[i].LeaveName = this.leaveType && userLeave[i].LeaveRemains >= diff)
this.chkLeave = 1;
}
};
Finally I did this to check if the leave type allotment is available for the user:
if(this.chkLeave == 1) {
alert("You can have the leave.");
} else {
alert("Oops! You can't have the leave.");
}
Now the issue is for multiple leave allotment, the method works perfectly and verifies the allotment for single allotment like Annual. Say for user 1001, it has two leave type allotments. So when I select casual leave, for the first time it checks perfectly whether the user is eligible for the leave depending upon allotment but it stuck when I select the second leave type, say sick and the leave days exceeds the allotment no (Basically allotments are total enjoyable days). It keeps the state of the first leave and the variable chkLeave holds the number 0 or 1 depending upon the first chosen leave type. In the inspect element of the browser, I can see the details in an array for leave allotment but while verifying, the loop somehow doesn't give the expected output. Is there anything wrong with the for loop?
Upvotes: 2
Views: 105
Reputation: 26372
Try with this change, as I noticed that you have hte chkLeave, which is not again set to 0, so it retains the 1 from the previous iteration.
chkAllotment() {
var startDate = new Date(Result[0]);
var endDate = new Date(Result[1]);
const diff = endDate.getDate() - startDate.getDate();
debugger;
// Re initialize the chkLeave to 0
this.chkLeave = 0;
for(let i = 0; i < this.userLeave.length; i++) {
if(this.userLeave[i].LeaveName = this.leaveType && userLeave[i].LeaveRemains >= diff)
this.chkLeave = 1;
}
};
As an extra, the way you handle the diff might cause problems, if the leave spans more than one month.
The getDate() method returns the day of the month (from 1 to 31) for the specified date.
This means that 2019-12-01 with 2020-01-01 have 0 days diff.
Basically it could be any of the two reasons.
userLeave[i].LeaveName == this.leaveType
Should most probably be
userLeave[i].LeaveType == this.leaveType
To calculate the diff between two days, see the guide here. Be carefull though, as you want to have the days, without and seconds/milliseconds as it could create rounding problems.
Upvotes: 1