Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ONI-125: Cookie for calendar type added. Date display added for all searches. #149

Merged
merged 1 commit into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/components/RequireAuth.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import FormControlLabel from '@material-ui/core/FormControlLabel';
import { Switch } from "@material-ui/core";
import { toggleCurrentCalendarType } from "../actions"
import { useDispatch } from 'react-redux';
import { useTranslations } from "../helpers/i18n";

export const APP_BAR_CONTRIBUTION_KEY = "core.AppBar";
export const MAIN_MENU_CONTRIBUTION_KEY = "core.MainMenu";
Expand Down Expand Up @@ -215,10 +216,14 @@ const RequireAuth = (props) => {
);

const isAppBarMenu = useMemo(() => theme.menu.variant.toUpperCase() === "APPBAR", [theme.menu.variant]);
const [isSecondaryCalendar, setSecondaryCalendar] = useBoolean(false);
const [isSecondaryCalendar, setSecondaryCalendar] = useBoolean(true);
const dispatch = useDispatch();

useEffect(() => dispatch(toggleCurrentCalendarType(!isSecondaryCalendar)), [isSecondaryCalendar])
useEffect(() =>{
localStorage.setItem("isSecondaryCalendarEnabled", JSON.stringify(!isSecondaryCalendar));
dispatch(toggleCurrentCalendarType(!isSecondaryCalendar)), [isSecondaryCalendar]
}
)

if (!auth.isAuthenticated) {
return <Redirect to={redirectTo} />;
Expand Down Expand Up @@ -274,7 +279,7 @@ const RequireAuth = (props) => {
</>
)
}

const { formatMessage } = useTranslations(module, modulesManager);
return (
<>
<AppBar
Expand Down Expand Up @@ -324,7 +329,7 @@ const RequireAuth = (props) => {
checked={isSecondaryCalendar}
onChange={setSecondaryCalendar.toggle}
/>}
label= {"AD/BS"}
label={formatMessage("core.calendarSwitcher")}
labelPlacement="start"
/>
}
Expand Down
2 changes: 2 additions & 0 deletions src/components/generics/Searcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,8 @@ const mapStateToProps = (state) => ({
paginationPage: state.core?.savedPagination?.paginationPage,
afterCursor: state.core?.savedPagination?.afterCursor,
beforeCursor: state.core?.savedPagination?.beforeCursor,
// This is not used directly, but is needed for Searcher component to be rerendered on change of calendar type
isSecondaryCalendarEnabled: state.core.isSecondaryCalendarEnabled ?? false
});

const mapDispatchToProps = (dispatch) => {
Expand Down
3 changes: 2 additions & 1 deletion src/helpers/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export function formatAmount(intl, amount) {
return `${intl.formatMessage({ id: "currency" })} ${amount || 0}`;
}

export function formatDateFromISO(mm, intl, date, isSecondaryCalendar=false) {
export function formatDateFromISO(mm, intl, date) {
let isSecondaryCalendar = JSON.parse(localStorage.getItem('isSecondaryCalendarEnabled'));
if (isSecondaryCalendar) {
return neFormateDateFromISO(date);
}
Expand Down
35 changes: 18 additions & 17 deletions src/pickers/DatePicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,24 @@ class DatePicker extends Component {
isSecondaryCalendarEnabled,
...otherProps
} = this.props;
if (isSecondaryCalendarEnabled){

if (isSecondaryCalendarEnabled){
const nepaliDate = (!!this.state.value ? new NepaliDate(new Date(this.state.value)).format('YYYY-MM-DD'): new NepaliDate().format('YYYY-MM-DD'))
return (
<FormControl fullWidth={fullWidth}>
<label>{!!label ? formatMessage(intl, module, label) : null}</label>
<Calendar
onChange={this.onChangeNepal}
defaultDate={nepaliDate}
language="en"
style={{width:"100%", display: "flex", position:"static"}}
dateFormat="DD/MM/YYYY"
placeholder="Select date"
/>
</FormControl>
);
}
else{
return (
<FormControl fullWidth={fullWidth}>
<MUIDatePicker
Expand All @@ -85,22 +102,6 @@ class DatePicker extends Component {
</FormControl>
);
}
else{
const nepaliDate = (!!this.state.value ? new NepaliDate(new Date(this.state.value)).format('YYYY-MM-DD'): new NepaliDate().format('YYYY-MM-DD'))
return (
<FormControl fullWidth={fullWidth}>
<label>{!!label ? formatMessage(intl, module, label) : null}</label>
<Calendar
onChange={this.onChangeNepal}
defaultDate={nepaliDate}
language="en"
style={{width:"100%", display: "flex", position:"static"}}
dateFormat="DD/MM/YYYY"
placeholder="Select date"
/>
</FormControl>
);
}
}
}

Expand Down
14 changes: 10 additions & 4 deletions src/pickers/NeDateFormatter.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import moment from "moment";
import NepaliDate from 'nepali-date-converter'

export function formatDateFromISO(date) {
const secondCalendarFormat = ['DD MM YYYY', 'np']
if (!date) return "";
let dte = moment(date);
let bsDate = calendarFunctions.getBsDateByAdDate(dte.year(), dte.month() + 1, dte.date());
return calendarFunctions.bsDateFormat("%y-%m-%d", bsDate.bsYear, bsDate.bsMonth, bsDate.bsDate);
// this nepali-date-converter only supports dates up to this date, it will crash whole site if this date is exceeded
// added as a safeguard, other solution is probably needed
// TODO remove after nepal component change
if (date > new Date("2090/12/30")){
date = new Date("2090/12/30");
}
let bsDate = new NepaliDate(new Date(date)).format(...secondCalendarFormat);
return bsDate
}
3 changes: 2 additions & 1 deletion src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,6 @@
"core.advancedFilters.button.filter": "Filter",
"core.RegistersStatusReport.region": "Region",
"core.RegistersStatusReport.district": "District",
"core.Table.ordinalNumberHeader": "Number"
"core.Table.ordinalNumberHeader": "Number",
"core.calendarSwitcher": "BS/AD"
}