Skip to content

Commit

Permalink
Merge pull request #997 from cityremade/main
Browse files Browse the repository at this point in the history
Locations/entries/date - now takes user local time;
  • Loading branch information
dbauszus-glx authored Nov 10, 2023
2 parents 9f6efb2 + 2330fe4 commit ca5330a
Showing 1 changed file with 26 additions and 17 deletions.
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

0 comments on commit ca5330a

Please sign in to comment.