-
Notifications
You must be signed in to change notification settings - Fork 639
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: now datepicker shows either date or datetime
Before the change datepicker showed always datetime fixes #463
- Loading branch information
1 parent
ab1e4f8
commit 961d67d
Showing
13 changed files
with
179 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/// <reference types="cypress" /> | ||
/// <reference types="../../support" /> | ||
|
||
const dateRegex = /^[\d]{4}-[\d]{2}-[\d]{2}$/ | ||
const dateTimeRegex = /^[\d]{4}-[\d]{2}-[\d]{2} [\d]{2}:[\d]{2}$/ | ||
|
||
context('resources/Profession/actions/new', () => { | ||
before(() => { | ||
cy.login() | ||
}) | ||
|
||
beforeEach(() => { | ||
Cypress.Cookies.preserveOnce(Cypress.env('COOKIE_NAME')) | ||
cy.visit('resources/Page/actions/new') | ||
}) | ||
|
||
it('creates new page', () => { | ||
const data = { | ||
title: 'Some title', | ||
content: 'hello world', | ||
createdDate: '2011-01-01', | ||
createdDateTime: '2011-01-01 11:00', | ||
} | ||
|
||
cy.get('[data-testid="property-edit-title"] input').type(data.title) | ||
cy.get('[data-testid="property-edit-content"] .ql-editor') | ||
.click() | ||
.type(data.content) | ||
cy.get('[data-testid="property-edit-createdDate"] input').type(data.createdDate) | ||
cy.get('[class^="date-picker__Overlay"].visible').click() | ||
cy.get('[data-testid="property-edit-createdDateTime"] input').type(data.createdDateTime) | ||
cy.get('[class^="date-picker__Overlay"].visible').click() | ||
|
||
cy.get('button[type=submit]').contains('Save').click() | ||
|
||
cy.location('pathname').should('eq', '/admin/resources/Page') | ||
|
||
cy.get('td[data-property-name="title"]') | ||
.contains(data.title) | ||
.parents('tr') | ||
.then((parent) => { | ||
expect(parent.find('[data-property-name="title"]')).to.have.text(data.title) | ||
expect(parent.find('[data-property-name="content"]')).to.have.text(`<p>${data.content}<...`) | ||
expect(parent.find('[data-property-name="createdDate"]').text()).to.match(dateRegex) | ||
expect(parent.find('[data-property-name="createdDateTime"]').text()).to.match(dateTimeRegex) | ||
}) | ||
}) | ||
|
||
it('shows regular date picker for date property', () => { | ||
cy.get('[data-testid="property-edit-createdDate"] input').click() | ||
cy.get('[data-testid="property-edit-createdDate"] .react-datepicker-time__input').should('not.exist') | ||
cy.get('[data-testid="property-edit-createdDate"] .react-datepicker__day.react-datepicker__day--001') | ||
.not('.react-datepicker__day--outside-month').click() | ||
cy.get('[data-testid="property-edit-createdDate"] input').then(($el) => { | ||
expect($el.val()).to.match(dateRegex) | ||
}) | ||
}) | ||
|
||
it('shows datetime picker for date property', () => { | ||
cy.get('[data-testid="property-edit-createdDateTime"] input').click() | ||
cy.get('[data-testid="property-edit-createdDateTime"] .react-datepicker-time__input').should('exist') | ||
cy.get('[data-testid="property-edit-createdDateTime"] .react-datepicker__day.react-datepicker__day--001') | ||
.not('.react-datepicker__day--outside-month').click() | ||
|
||
cy.get('[data-testid="property-edit-createdDateTime"] input').then(($el) => { | ||
expect($el.val()).to.match(dateTimeRegex) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { PropertyType } from '../../../../backend/adapters/base-property' | ||
|
||
/** | ||
* adds leading 0 to the number when it is lower than 10 | ||
* @private | ||
*/ | ||
const pad = (n: number): string => (n < 10 ? `0${n.toString()}` : n.toString()) | ||
|
||
/** | ||
* Formats date to YYYY-MM-DD | ||
* | ||
* @param {Date} date | ||
* @return {string} | ||
* @private | ||
*/ | ||
const formatDate = (date: Date): string => `${date.getFullYear()}-${pad(date.getMonth() + 1) | ||
}-${pad(date.getDate())}` | ||
|
||
/** | ||
* Formats date to HH:mm | ||
* | ||
* @param {Date} date | ||
* @return {string} | ||
* @private | ||
*/ | ||
const formatTime = (date: Date): string => `${pad(date.getHours())}:${pad(date.getMinutes())}` | ||
|
||
/** | ||
* Formats date to YYYY-MM-DD HH:mm | ||
* | ||
* @param {Date} date | ||
* @return {string} | ||
* @private | ||
*/ | ||
const formatDateTime = (date: Date): string => `${formatDate(date)} ${formatTime(date)}` | ||
|
||
/** | ||
* Based on the property type formats date to either YYYY-MM-DD HH:mm or YYYY-MM-DD | ||
* | ||
* @param {Date} date | ||
* @return {string} | ||
* @private | ||
*/ | ||
const formatDateProperty = (date: Date, propertyType?: PropertyType): string => { | ||
if (propertyType === 'date') { | ||
return formatDate(date) | ||
} | ||
return formatDateTime(date) | ||
} | ||
|
||
export { | ||
formatDateProperty, | ||
formatDate, | ||
formatDateTime, | ||
pad, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 8 additions & 2 deletions
10
src/frontend/components/property-type/datetime/map-value.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,13 @@ | ||
export default (value: Date): string => { | ||
import { formatDateProperty } from '../../design-system' | ||
import { PropertyType } from '../../../../backend/adapters/base-property' | ||
|
||
export default (value: Date, propertyType: PropertyType): string => { | ||
if (!value) { | ||
return '' | ||
} | ||
const date = new Date(value) | ||
return date.toLocaleString() | ||
if (date) { | ||
return formatDateProperty(date, propertyType) | ||
} | ||
return '' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters