Skip to content

Commit

Permalink
Merge pull request #482 from GatherPress/fix-event-past-issue
Browse files Browse the repository at this point in the history
Fix event start and end picker to use datetime format in settings.
  • Loading branch information
mauteri authored Jan 6, 2024
2 parents 6452ad8 + a1ce2a6 commit f29263c
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 27 deletions.
2 changes: 1 addition & 1 deletion build/blocks/event-date/index.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('moment', 'react', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-data', 'wp-date', 'wp-element', 'wp-i18n'), 'version' => '083cc7b917e8e9e4ec2e');
<?php return array('dependencies' => array('moment', 'react', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-data', 'wp-date', 'wp-element', 'wp-i18n'), 'version' => '938fba5894732d634c1d');
4 changes: 2 additions & 2 deletions build/blocks/event-date/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/panels.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('moment', 'react', 'wp-api-fetch', 'wp-components', 'wp-data', 'wp-date', 'wp-edit-post', 'wp-element', 'wp-i18n', 'wp-plugins'), 'version' => 'e6fa24339463aecad895');
<?php return array('dependencies' => array('moment', 'react', 'wp-api-fetch', 'wp-components', 'wp-data', 'wp-date', 'wp-edit-post', 'wp-element', 'wp-i18n', 'wp-plugins'), 'version' => '4a6e361c06e539664af7');
2 changes: 1 addition & 1 deletion build/panels.js

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions src/components/DateTime.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ import {
export const DateTimeStartLabel = (props) => {
const { dateTimeStart } = props;

return moment.tz(dateTimeStart, getTimeZone()).format(dateTimeLabelFormat);
return moment
.tz(dateTimeStart, getTimeZone())
.format(dateTimeLabelFormat());
};

/**
Expand All @@ -46,7 +48,7 @@ export const DateTimeStartLabel = (props) => {
export const DateTimeEndLabel = (props) => {
const { dateTimeEnd } = props;

return moment.tz(dateTimeEnd, getTimeZone()).format(dateTimeLabelFormat);
return moment.tz(dateTimeEnd, getTimeZone()).format(dateTimeLabelFormat());
};

export const DateTimeStartPicker = (props) => {
Expand Down
9 changes: 7 additions & 2 deletions src/components/DateTimeEnd.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,18 @@ const DateTimeEnd = (props) => {

return (
<PanelRow>
<Flex>
<FlexItem>{__('End', 'gatherpress')}</FlexItem>
<Flex direction="column" gap="0">
<FlexItem>
<label htmlFor="gp-datetime-end">
{__('End', 'gatherpress')}
</label>
</FlexItem>
<FlexItem>
<Dropdown
popoverProps={{ placement: 'bottom-end' }}
renderToggle={({ isOpen, onToggle }) => (
<Button
id="gp-datetime-end"
onClick={onToggle}
aria-expanded={isOpen}
isLink
Expand Down
24 changes: 10 additions & 14 deletions src/components/DateTimeRange.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,16 @@ const DateTimeRange = () => {

return (
<>
<section>
<h3>{__('Date & time', 'gatherpress')}</h3>
<DateTimeStart
dateTimeStart={dateTimeStart}
setDateTimeStart={setDateTimeStart}
/>
<DateTimeEnd
dateTimeEnd={dateTimeEnd}
setDateTimeEnd={setDateTimeEnd}
/>
</section>
<section>
<TimeZone timezone={timezone} setTimezone={setTimezone} />
</section>
<h3>{__('Date & time', 'gatherpress')}</h3>
<DateTimeStart
dateTimeStart={dateTimeStart}
setDateTimeStart={setDateTimeStart}
/>
<DateTimeEnd
dateTimeEnd={dateTimeEnd}
setDateTimeEnd={setDateTimeEnd}
/>
<TimeZone timezone={timezone} setTimezone={setTimezone} />
</>
);
};
Expand Down
9 changes: 7 additions & 2 deletions src/components/DateTimeStart.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,18 @@ const DateTimeStart = (props) => {

return (
<PanelRow>
<Flex>
<FlexItem>{__('Start', 'gatherpress')}</FlexItem>
<Flex direction="column" gap="0">
<FlexItem>
<label htmlFor="gp-datetime-start">
{__('Start', 'gatherpress')}
</label>
</FlexItem>
<FlexItem>
<Dropdown
popoverProps={{ placement: 'bottom-end' }}
renderToggle={({ isOpen, onToggle }) => (
<Button
id="gp-datetime-start"
onClick={onToggle}
aria-expanded={isOpen}
isLink
Expand Down
24 changes: 22 additions & 2 deletions src/helpers/datetime.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,27 @@ import { isEventPostType, triggerEventCommuncation } from './event';

export const dateTimeMomentFormat = 'YYYY-MM-DDTHH:mm:ss';
export const dateTimeDatabaseFormat = 'YYYY-MM-DD HH:mm:ss';
export const dateTimeLabelFormat = 'MMMM D, YYYY h:mm a';

/**
* Get the combined date and time format for event labels.
*
* This function retrieves the date and time formats from global settings
* and combines them to create a formatted label for event start and end times.
*
* @since 1.0.0
*
* @return {string} The combined date and time format for event labels.
*/
export const dateTimeLabelFormat = () => {
const dateFormat = convertPHPToMomentFormat(
getFromGlobal('settings.date_format')
);
const timeFormat = convertPHPToMomentFormat(
getFromGlobal('settings.time_format')
);

return dateFormat + ' ' + timeFormat;
};

/**
* Retrieves the timezone for the application based on the provided timezone or the global setting.
Expand Down Expand Up @@ -366,7 +386,7 @@ export function convertPHPToMomentFormat(format) {
U: 'X',
};

return format
return String(format)
.split('')
.map((chr) => (chr in replacements ? replacements[chr] : chr))
.join('');
Expand Down
14 changes: 14 additions & 0 deletions test/unit/js/src/components/DateTime.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ import {
* Coverage for DateTimeStartLabel.
*/
test('DateTimeStartLabel displays correct format', () => {
global.GatherPress = {
settings: {
date_format: 'F j, Y',
time_format: 'g:i a',
},
};

const props = {
dateTimeStart: '2023-12-28T10:26:00',
};
Expand All @@ -30,6 +37,13 @@ test('DateTimeStartLabel displays correct format', () => {
* Coverage for DateTimeEndLabel.
*/
test('DateTimeEndLabel displays correct format', () => {
global.GatherPress = {
settings: {
date_format: 'F j, Y',
time_format: 'g:i a',
},
};

const props = {
dateTimeEnd: '2023-12-28T10:26:00',
};
Expand Down
15 changes: 15 additions & 0 deletions test/unit/js/src/helpers/datetime.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'moment-timezone';
*/
import {
convertPHPToMomentFormat,
dateTimeLabelFormat,
defaultDateTimeEnd,
defaultDateTimeStart,
getDateTimeEnd,
Expand All @@ -22,6 +23,20 @@ import {
updateDateTimeStart,
} from '../../../../../src/helpers/datetime';

/**
* Coverage for dateTimeLabelFormat.
*/
test('dateTimeLabelFormat returns correct format', () => {
global.GatherPress = {
settings: {
date_format: 'F j, Y',
time_format: 'g:i a',
},
};

expect(dateTimeLabelFormat()).toBe('MMMM D, YYYY h:mm a');
});

/**
* Coverage for getTimeZone.
*/
Expand Down

0 comments on commit f29263c

Please sign in to comment.