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

Locations/entries/date - now takes user local time; #997

Merged
merged 2 commits into from
Nov 10, 2023
Merged
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
43 changes: 26 additions & 17 deletions lib/ui/locations/entries/date.mjs
Original file line number Diff line number Diff line change
@@ -1,32 +1,41 @@
export default entry => {

let val
const dateString = new Date(entry.newValue || entry.value * 1000)
.toLocaleDateString(entry.locale, entry.options); // day only as configured in workspace

const timeString = new Date(entry.newValue || entry.value * 1000)
.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }); // time only as 00:00

if (entry.edit) {

val = mapp.utils.html.node`
// formatted value for input element.
const formats = {
datetime: `${new Date(entry.newValue || entry.value * 1000).toLocaleDateString("fr-CA")}T${timeString}`, // YYYY-MM-DDT00:00
date: `${new Date(entry.newValue || entry.value * 1000).toLocaleDateString("fr-CA")}` // YYYY-MM-DD
}

// return date/time input
return mapp.utils.html.node`
<input
type=${entry.type === 'datetime' && 'datetime-local' || 'date'}
value=${entry.value &&
(entry.type === 'datetime' && new Date(entry.value * 1000).toISOString().split('Z')[0]
|| new Date(entry.value * 1000).toISOString().split('T')[0])}
type=${entry.type === 'datetime' ? 'datetime-local' : 'date'}
value="${formats[entry.type]}"
onchange=${e => {

entry.newValue = new Date(e.target.value).getTime() / 1000
entry.newValue = new Date(e.target.value).getTime() / 1000;

entry.location.view?.dispatchEvent(
new CustomEvent('valChange', {
detail: entry
})
)
entry.location.view?.dispatchEvent(
new CustomEvent('valChange', {
detail: entry
}))
}}>`;

}}>`;

} else {

val = entry.value && new Date(entry.value * 1000).toLocaleString(entry.locale, entry.options)
}

// Assign val for non-editable entry.
const val = entry.value
&& (entry.type === 'datetime' ? `${dateString} ${timeString}` : dateString)
|| 'null'

const node = mapp.utils.html.node`
<div
class="val"
Expand Down