Skip to content

Commit

Permalink
bs date bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrlx-altf4 committed Jul 24, 2021
1 parent 5877026 commit a235af8
Show file tree
Hide file tree
Showing 20 changed files with 186 additions and 233 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,8 @@ Last 45 days 2020-08-28 - 2020-10-12
Next week 2020-10-12 - 2020-10-19
///
```

## Terms:

-parse => Convert date string to date object using the date format for example: "2020-10-12" => {year: 2020, month:10, date:12} given format ("YYYY-MM-DD");
-format => Opposite of parse. Converts date object to date string. for example: {year: 2020, month:10, date:12}=>"2020-10-12" => given format ("YYYY-MM-DD");
27 changes: 6 additions & 21 deletions example/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,14 @@ const App = () => {
<h1>AD Calendar</h1>
<p>Selected Date AD: {JSON.stringify(selectedDate)};</p>
<div style={{ marginBottom: 150 }}>
asdfasdf
<NepaliCalendar
defaultValue="2021-07-09"
showExtra={true}
calendarType={"AD"}
dateFormat="yyyy-mm-dd"
value={selectedDate}
disablePast
showMonthDropdown={true}
// showMonthDropdown={true}
// showMonthDropdown={true}
// showYearDropdown={true}
// maxDate="2021-07-10"
Expand All @@ -93,18 +92,18 @@ const App = () => {
<NepaliCalendar
// defaultValue="2021-07-09"
showExtra={true}
calendarType={"BS"}
calendarType="BS"
dateFormat="yyyy-mm-dd"
value={selectedDateBS}
// showMonthDropdown={true}
// showYearDropdown={true}
// disablePast
// maxDate="2021-07-10"
disablePast
// disableFuture
maxDate="2078-10-27"
// minDate="07-03-2021"
// disablePast
// disableDate={(date) => date === "07-03-2021"}
onSelect={(formattedDate, adDate, bsDate, date) => {
// alert(formattedDate);
setSelectedDateBS(formattedDate);
}}
/>
Expand Down Expand Up @@ -174,18 +173,4 @@ const App = () => {
);
};

export default App;

// import React from "react";
// import { Calendar } from "nepalicalendar";
// import "./App.css";
// const App = () => {
// return (
// <h1>
// {" "}
// <Calendar />
// App
// </h1>
// );
// };
// export default App;
export default App;
5 changes: 3 additions & 2 deletions src/AdBsDateRenderer.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ad2bs } from "./CalendarData";
import { parseDate, isDateValid } from "./date-fns";
import { isDateValidWithFormat } from "./CalendarData/validator";
import { parseDate } from "./date-fns";
import { useCalendarType } from "./hooks";

type AdBsDateRendererProps = {
Expand All @@ -25,7 +26,7 @@ const AdBsDateRenderer = ({
if (adDate == null || adDate === "") {
return "";
}
if (isDateValid(adDate, adDateFormat)) {
if (isDateValidWithFormat(adDate, adDateFormat)) {
return calendarType === "AD"
? adDate
: `${padDateMonth(bsDateObj.date)}-${padDateMonth(bsDateObj.month)}-${
Expand Down
1 change: 0 additions & 1 deletion src/Calendar/DateRender.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from "react";
// import { getFormattedDateFromObject } from "../date-fns";
import { DateRange, DisableProps, IDateObject } from "./types";
import useCalendarData from "./useCalendarData";
import { checkIsInRange, checkIsSelected, checkIsDisabled } from "./util";
Expand Down
1 change: 0 additions & 1 deletion src/Calendar/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
getNepaliNumber,
getValidYears,
} from "../CalendarData";
// import { getTotalDaysInAdMonth } from "../date-fns";

type OffsetChange = (a: number) => void;

Expand Down
11 changes: 7 additions & 4 deletions src/Calendar/RangeRender.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from "react";
// import { getFormattedDateFromObject } from "../date-fns";
import { DateRange, DisableProps, IDateObject } from "./types";
import useCalendarData from "./useCalendarData";
import {
Expand Down Expand Up @@ -58,16 +57,20 @@ const DateRenderer = (props: DateRendererProps) => {
minDate,
},
dateFormat,
d.bsDate
d.bsDate,
isAD
);

const isSelected =
d.isCurrentMonth && checkIsSelected(selectedData, d.adDate);
const isInRange = range ? checkIsInRange(d.adDate, range) : false;
const isInRange = range
? checkIsInRange(isAD ? d.adDate : d.bsDate, range)
: false;

const isRangeBoundary = range
? checkIsRangeBoundary(d.adDate, range)
? checkIsRangeBoundary(isAD ? d.adDate : d.bsDate, range)
: false;

return (
<td
key={i}
Expand Down
9 changes: 0 additions & 9 deletions src/Calendar/index.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import React, { useState, useEffect } from "react";

import Header from "./Header";
// import DateRender from "./DateRender";
import RangeRender from "./RangeRender";
import useSelectedData from "./useSelectedData";

import { getMonthOffset, checkDatePropsValidity } from "./util";
// getTodaysDate
import { dateFormatter, getDateFromObject } from "../date-fns";
import { getWeekNames, formatBsDate } from "../CalendarData";

import { DateRange, INepaliCalendar, IDateObject } from "./types";

// import "../nepali_date_picker.css";

const NepaliCalendar = (props: INepaliCalendar) => {
const {
// showToday = true,
Expand Down Expand Up @@ -108,11 +104,6 @@ const NepaliCalendar = (props: INepaliCalendar) => {
? { from: range?.from, to: range?.to, format: range?.format ?? dateFormat }
: null;

console.log("just trying this");
console.log("dateRange", dateRange);
const x = 100;
console.log("x", x);

return (
<div className="rl-nepali-date-panel-wrapper">
<div className="rl-nepali-date-panel">
Expand Down
5 changes: 3 additions & 2 deletions src/Calendar/useSelectedData.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { useEffect, useState } from "react";
import { IDateObject } from "./types";
import { isDateValid, parseDate } from "../date-fns";
import { parseDate } from "../date-fns";

import { getDateObj } from "../date-fns";
import { parseBsDate, bs2ad } from "../CalendarData";
import { isDateValidWithFormat } from "../CalendarData/validator";

//Regardless of isAD value the data should always contain Ad data by converting bs Data to ad if isAD = false
const useSelectedData = (
Expand All @@ -19,7 +20,7 @@ const useSelectedData = (
month: currentDate.getMonth() + 1,
year: currentDate.getFullYear(),
};
if (initialDate && isDateValid(initialDate, dateFormat)) {
if (initialDate && isDateValidWithFormat(initialDate, dateFormat)) {
currentDate = parseDate(initialDate, dateFormat);

selectedData = {
Expand Down
82 changes: 15 additions & 67 deletions src/Calendar/util.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import {
ad2bs,
bs2ad,
formatBsDate,
isInValidRange,
parseBsDate,
} from "./../CalendarData/index";
import { isInBetween } from "../utils";
import { CalendarType, DateRange, IDateObject } from "./types";
import {
parseDate,
getDateFromObject,
isDateValid,
dateFormatter,
} from "../date-fns";
import { parseDate, getDateFromObject, dateFormatter } from "../date-fns";
import { DisableProps } from "./types";
import { isDateValidWithFormat } from "../CalendarData/validator";
import { isAfter, isBefore } from "../CalendarData/calculation";

type ADBSDateType = {
ad: IDateObject;
Expand Down Expand Up @@ -76,7 +75,8 @@ export function checkIsDisabled(
adDateObj: IDateObject,
disableConfig: DisableProps,
dateFormat: string,
bsDateObj: IDateObject
bsDateObj: IDateObject,
isAD = true
): boolean {
const { disableDate, maxDate, minDate, disablePast, disableFuture } =
disableConfig;
Expand All @@ -87,52 +87,11 @@ export function checkIsDisabled(
date: adDateObj.date,
});

const providedDateString = formatBsDate(
isAD ? adDateObj : bsDateObj,
dateFormat
);
const formattedAdDate = dateFormatter(adDate, dateFormat);
const maxDateString = maxDate ? parseDate(maxDate, dateFormat) : null;
const minDateString = minDate ? parseDate(minDate, dateFormat) : null;

const today = new Date();
if (
typeof disableDate === "function" &&
disableDate(formattedAdDate, adDateObj, bsDateObj, adDate)
)
return true;
if (adDate > today && disableFuture) {
return true;
}
if (adDate < today && disablePast) {
return true;
}

if (maxDateString && adDate > maxDateString) {
return true;
}

if (minDateString && adDate < minDateString) {
return true;
}

return false;
}

export function checkIsBsDisabled(
adDateObj: IDateObject,
disableConfig: DisableProps,
dateFormat: string,
bsDateObj: IDateObject
): boolean {
const { disableDate, maxDate, minDate, disablePast, disableFuture } =
disableConfig;

const adDate = getDateFromObject({
year: adDateObj.year,
month: adDateObj.month,
date: adDateObj.date,
});

const formattedAdDate = dateFormatter(adDate, dateFormat);
const maxDateString = maxDate ? parseDate(maxDate, dateFormat) : null;
const minDateString = minDate ? parseDate(minDate, dateFormat) : null;

const today = new Date();
if (
Expand All @@ -147,23 +106,16 @@ export function checkIsBsDisabled(
return true;
}

if (maxDateString && adDate > maxDateString) {
if (maxDate && isAfter(providedDateString, maxDate, dateFormat)) {
return true;
}

if (minDateString && adDate < minDateString) {
if (minDate && isBefore(providedDateString, minDate, dateFormat)) {
return true;
}

return false;
}
//TODO may be generic???
// inclusive of first and last date;
export function isInBetween(data: any, first: any, last: any): boolean {
if (data >= first && data <= last && first < last) {
return true;
} else return false;
}

export function checkIsInRange(
adDateObj: IDateObject,
Expand Down Expand Up @@ -199,6 +151,7 @@ const isYearMonthDateSame = (dateLeft: Date, dateRight: Date): boolean => {
return true;
return false;
};

export function checkIsRangeBoundary(
adDateObj: IDateObject,
range?: DateRange
Expand Down Expand Up @@ -320,7 +273,7 @@ export const checkDatePropsValidity = (
checker.forEach((prop) => {
const value = allDateProps[prop];
if (value) {
if (!isDateValid(value, dateFormat)) {
if (!isDateValidWithFormat(value, dateFormat)) {
throwTypeError(value, prop, dateFormat);
return;
} else {
Expand All @@ -330,10 +283,5 @@ export const checkDatePropsValidity = (
}
});
};
export const padDateMonth = (val: string | number) => {
return `${val}`.padStart(2, "0");
};

// export { padDateMonth };

export { getTodaysDate, checkIsToday, checkIsSelected, getMonthOffset };
31 changes: 28 additions & 3 deletions src/CalendarData/calculation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function getNewBsDate<T extends string | undefined>(
return bsObj;
} else
throw new Error(
`If string type is passed as first argument then dateFormat should be passed compulsarily to parse the string. Or pass the date Object {year,month,date} but received dateFormat as ${dateFormat} for first argument ${JSON.stringify(
`If string type is passed as second argument then dateFormat should be passed compulsarily to parse the string. Or pass the date Object {year,month,date} but received dateFormat as ${dateFormat} for second argument ${JSON.stringify(
dateObjString
)}`
);
Expand Down Expand Up @@ -61,7 +61,7 @@ export function getNewAdDate<T extends string | undefined>(
return adObj;
} else
throw new Error(
`If string type is passed as first argument then dateFormat should be passed compulsarily to parse the string. Or pass the date Object {year,month,date} but received dateFormat as ${dateFormat} for first argument ${JSON.stringify(
`If string type is passed as second argument then dateFormat should be passed compulsarily to parse the string. Or pass the date Object {year,month,date} but received dateFormat as ${dateFormat} for first argument ${JSON.stringify(
dateObjString
)}`
);
Expand Down Expand Up @@ -102,11 +102,36 @@ export function compareDates(
if (date1Obj.year > date2Obj.year) return 1;
if (date2Obj.year > date1Obj.year) return -1;

if (date1Obj.month > date2Obj.year) return 1;
if (date1Obj.month > date2Obj.month) return 1;
if (date2Obj.month > date1Obj.month) return -1;

if (date1Obj.date > date2Obj.date) return 1;
if (date2Obj.date > date1Obj.date) return -1;

return 0;
}

export function areDateEqual(
date1: string,
date2: string,
dateFormat: string
): boolean {
const compareBoolean = compareDates(date1, date2, dateFormat);
return compareBoolean === 0;
}
export function isAfter(
date1: string,
date2: string,
dateFormat: string
): boolean {
const compareBoolean = compareDates(date1, date2, dateFormat);
return compareBoolean === 1;
}
export function isBefore(
date1: string,
date2: string,
dateFormat: string
): boolean {
const compareBoolean = compareDates(date1, date2, dateFormat);
return compareBoolean === -1;
}
Loading

0 comments on commit a235af8

Please sign in to comment.