-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
UI: Date time local transform #24694
Merged
kiannaquach
merged 12 commits into
ui/VAULT-19096/customizable-banners
from
ui/datetime-local-transform
Jan 8, 2024
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a3d99f2
Date time local
kiannaquach c8d1332
Add deserialize
kiannaquach 32f6f24
Add copyright header
kiannaquach 626e6a5
check if date exists
kiannaquach e9e1403
Use parseISO for date strings since datefns requires this in new update
kiannaquach f69bf13
Update tests
kiannaquach a69c2c6
Ensure we cehck for an ISOString
kiannaquach b4b556c
Merge branch 'ui/VAULT-19096/customizable-banners' into ui/datetime-l…
kiannaquach 97ae559
Add checks so tests wont fail
kiannaquach 0416e75
Update parseISO
kiannaquach 9d8e2f9
Address feedback
kiannaquach 4556450
Merge branch 'ui/VAULT-19096/customizable-banners' into ui/datetime-l…
kiannaquach File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,34 @@ | ||
/** | ||
* Copyright (c) HashiCorp, Inc. | ||
* SPDX-License-Identifier: BUSL-1.1 | ||
*/ | ||
|
||
import Transform from '@ember-data/serializer/transform'; | ||
import { datetimeLocalStringFormat } from 'core/utils/date-formatters'; | ||
import { format } from 'date-fns'; | ||
|
||
export default class DateTimeLocalTransform extends Transform { | ||
getISODateFormat(deserializedDate) { | ||
if (!deserializedDate) return null; | ||
|
||
// if the date is a date object or in local date time format ("yyyy-MM-dd'T'HH:mm"), we want to ensure | ||
// it gets converted to an ISOString | ||
if ( | ||
typeof deserializedDate === 'object' || | ||
(typeof deserializedDate === 'string' && !deserializedDate.includes('Z')) | ||
) { | ||
return new Date(deserializedDate).toISOString(); | ||
} | ||
|
||
return deserializedDate; | ||
} | ||
|
||
deserialize(serialized) { | ||
if (!serialized) return null; | ||
return format(new Date(serialized), datetimeLocalStringFormat); | ||
} | ||
|
||
serialize(deserialized) { | ||
return this.getISODateFormat(deserialized); | ||
} | ||
} |
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,30 @@ | ||
/** | ||
* Copyright (c) HashiCorp, Inc. | ||
* SPDX-License-Identifier: BUSL-1.1 | ||
*/ | ||
|
||
import { module, test } from 'qunit'; | ||
|
||
import { setupTest } from 'vault/tests/helpers'; | ||
|
||
module('Unit | Transform | date time local', function (hooks) { | ||
setupTest(hooks); | ||
|
||
hooks.beforeEach(function () { | ||
this.transform = this.owner.lookup('transform:date-time-local'); | ||
}); | ||
|
||
test('it serializes correctly for the API', function (assert) { | ||
assert.ok(this.transform); | ||
let serialized = this.transform.serialize('2024-01-31T00:00'); | ||
assert.strictEqual( | ||
serialized, | ||
new Date('2024-01-31T00:00').toISOString(), | ||
'should serialize a string that is not in ISO format' | ||
); | ||
serialized = this.transform.serialize(new Date('2024-03-30T17:11:00Z')); | ||
assert.strictEqual(serialized, '2024-03-30T17:11:00.000Z', 'should serialize a date object'); | ||
serialized = this.transform.serialize('2024-03-30T17:11:00.000Z'); | ||
assert.strictEqual(serialized, '2024-03-30T17:11:00.000Z', 'should always show an ISO string'); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated to use parseISO since date-fns doesn't accept strings as arguments.
