Skip to content

Commit

Permalink
input to date
Browse files Browse the repository at this point in the history
  • Loading branch information
Prasidha Karki committed Aug 11, 2020
1 parent 45a0c35 commit d6a3eb3
Show file tree
Hide file tree
Showing 8 changed files with 1,043 additions and 93 deletions.
595 changes: 595 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.3.2",
"@testing-library/user-event": "^7.1.2",
"antd": "^4.5.3",
"moment": "^2.27.0",
"react": "^16.13.1",
"react-dom": "^16.13.1",
Expand Down
91 changes: 91 additions & 0 deletions src/nepali_date_picker/ad_bs_date_render.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import React,{ useState,useEffect, createContext } from 'react';
import moment from 'moment';
import { calendarFunctions } from './helper_bs';


var ad_bs_listener = {};

var calendarType="AD";

var ad_bs_sub_pub = Object.seal({
key: 0,
subscribers: {},
subscribe: function (subscriber) {
console.log("subscribing for",this.key)
this.subscribers[this.key] = subscriber;
return this.key++;
},
publish: function (dateType) {
console.log("publishing", { dateType })
setCalendarType(dateType)
for (var sub in this.subscribers) {
// console.log("publishing for key",sub)
try {
this.subscribers[sub](dateType);
} catch (ignore) {
console.log("running subs error", ignore)
}
}
},
unsubscribe: function (key) {
console.log("unsubscribing",key)
delete this.subscribers[key];
},
});

ad_bs_listener['ad_bs'] = Object.create(ad_bs_sub_pub);

function get_ad_bs_listener() {
return ad_bs_listener;
}

function getCalendarType(){
return calendarType
}
function setCalendarType(val){
calendarType=val
}

const AD_BS_RENDERER = ({ adDate, adDateFormat = "DD-MM-YYYY" }) => {


let adDateObj = moment(adDate, adDateFormat);
let bsDateObj=calendarFunctions.getBsDateByAdDate(adDateObj.year(),adDateObj.month()+1,adDateObj.date());
// const [_key, _setKey] = useState(1);
const [calendarType,setCalendarType]=useState(getCalendarType())
let new_key=null;
useEffect(() => {
let _k=ad_bs_listener.ad_bs.subscribe((dateType)=>{
setCalendarType(dateType||"AD")
})
console.log("subscrr",_k)
new_key=_k
// _setKey(_k)
// ChatAPI.subscribeToFriendStatus(props.friend.id, handleStatusChange);
// Specify how to clean up after this effect:
},[]);

useEffect(()=>{
return function cleanup() {
console.log("key is",new_key,calendarType)
ad_bs_listener.ad_bs.unsubscribe(new_key)
// ChatAPI.unsubscribeFromFriendStatus(props.friend.id, handleStatusChange);
};
},[]);

if(adDate==null||adDate==""){
return ""
}
if (adDateObj.isValid()) {
return calendarType=="AD"?adDate:(`${padDateMonth(bsDateObj.bsDate)}-${padDateMonth(bsDateObj.bsMonth)}-${(bsDateObj.bsYear)}`)

} else {
return "Invalid Date"
}
}

const padDateMonth=(val)=>{
return `${val}`.padStart(2,"0")
}

export { get_ad_bs_listener,AD_BS_RENDERER,getCalendarType,setCalendarType,padDateMonth }
61 changes: 61 additions & 0 deletions src/nepali_date_picker/assets/calendar.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit d6a3eb3

Please sign in to comment.