refactor(webui): convert dates using axios interceptor

This commit is contained in:
Gauthier Roebroeck 2023-07-11 14:46:43 +08:00
parent 438c40d4d0
commit 58c8187a41
14 changed files with 56 additions and 36 deletions

View file

@ -24,7 +24,7 @@
new Intl.DateTimeFormat($i18n.locale, {
dateStyle: 'medium',
timeStyle: 'short'
}).format(new Date(item.dateTime))
}).format(item.dateTime)
}}
</template>
</v-data-table>

View file

@ -30,7 +30,7 @@
new Intl.DateTimeFormat($i18n.locale, {
dateStyle: 'medium',
timeStyle: 'short'
}).format(new Date(usersLastActivity[u.id]))
}).format(usersLastActivity[u.id])
})
}}
</v-list-item-subtitle>

View file

@ -1,17 +1,40 @@
import urls from '@/functions/urls'
import axios, { AxiosInstance, AxiosRequestConfig } from 'axios'
import axios, {AxiosInstance, AxiosRequestConfig} from 'axios'
import _Vue from 'vue'
import {parseISO} from 'date-fns'
export default {
install (Vue: typeof _Vue) {
Vue.prototype.$http = axios.create({
install(Vue: typeof _Vue) {
const client = axios.create({
baseURL: urls.origin,
withCredentials: true,
headers: { 'X-Requested-With': 'XMLHttpRequest' },
headers: {'X-Requested-With': 'XMLHttpRequest'},
} as AxiosRequestConfig)
client.interceptors.response.use(originalResponse => {
handleDates(originalResponse.data)
return originalResponse
})
Vue.prototype.$http = client
},
}
const isoDateFormat = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d*)?(?:[-+]\d{2}:?\d{2}|Z)?$/
function isIsoDateString(value: any): boolean {
return value && typeof value === 'string' && isoDateFormat.test(value)
}
export function handleDates(body: any) {
if (body === null || body === undefined || typeof body !== 'object')
return body
for (const key of Object.keys(body)) {
const value = body[key]
if (isIsoDateString(value)) body[key] = parseISO(value)
else if (typeof value === 'object') handleDates(value)
}
}
declare module 'vue/types/vue' {
interface Vue {
$http: AxiosInstance;

View file

@ -110,15 +110,13 @@ export class BookItem extends Item<BookDto> {
text = this.item.metadata.releaseDate ? new Intl.DateTimeFormat(i18n.locale, {dateStyle: 'medium', timeZone: 'UTC'} as Intl.DateTimeFormatOptions).format(new Date(this.item.metadata.releaseDate)) : i18n.t('book_card.no_release_date')
else if (context.includes(ItemContext.DATE_ADDED))
{
const date = new Date(this.item.created)
text = new Intl.DateTimeFormat(i18n.locale, {dateStyle: 'medium'} as Intl.DateTimeFormatOptions).format(date)
title = new Intl.DateTimeFormat(i18n.locale, {dateStyle: 'long', timeStyle: 'medium'} as Intl.DateTimeFormatOptions).format(date)
text = new Intl.DateTimeFormat(i18n.locale, {dateStyle: 'medium'} as Intl.DateTimeFormatOptions).format(this.item.created)
title = new Intl.DateTimeFormat(i18n.locale, {dateStyle: 'long', timeStyle: 'medium'} as Intl.DateTimeFormatOptions).format(this.item.created)
}
else if (context.includes(ItemContext.READ_DATE)) {
if (this.item.readProgress?.lastModified) {
const date = new Date(this.item.readProgress?.lastModified)
text = new Intl.DateTimeFormat(i18n.locale, {dateStyle: 'medium'} as Intl.DateTimeFormatOptions).format(date)
title = new Intl.DateTimeFormat(i18n.locale, {dateStyle: 'long', timeStyle: 'medium'} as Intl.DateTimeFormatOptions).format(date)
if (this.item.readProgress?.readDate) {
text = new Intl.DateTimeFormat(i18n.locale, {dateStyle: 'medium'} as Intl.DateTimeFormatOptions).format(this.item.readProgress?.readDate)
title = new Intl.DateTimeFormat(i18n.locale, {dateStyle: 'long', timeStyle: 'medium'} as Intl.DateTimeFormatOptions).format(this.item.readProgress?.readDate)
} else {
text = i18n.t('book_card.unread')
}
@ -179,15 +177,13 @@ export class SeriesItem extends Item<SeriesDto> {
text = this.item.booksMetadata.releaseDate ? new Intl.DateTimeFormat(i18n.locale, {dateStyle: 'medium', timeZone: 'UTC'} as Intl.DateTimeFormatOptions).format(new Date(this.item.booksMetadata.releaseDate)) : i18n.t('book_card.no_release_date')
else if (context.includes(ItemContext.DATE_ADDED))
{
const date = new Date(this.item.created)
text = new Intl.DateTimeFormat(i18n.locale, {dateStyle: 'medium'} as Intl.DateTimeFormatOptions).format(date)
title = new Intl.DateTimeFormat(i18n.locale, {dateStyle: 'long', timeStyle: 'medium'} as Intl.DateTimeFormatOptions).format(date)
text = new Intl.DateTimeFormat(i18n.locale, {dateStyle: 'medium'} as Intl.DateTimeFormatOptions).format(this.item.created)
title = new Intl.DateTimeFormat(i18n.locale, {dateStyle: 'long', timeStyle: 'medium'} as Intl.DateTimeFormatOptions).format(this.item.created)
}
else if (context.includes(ItemContext.DATE_UPDATED))
{
const date = new Date(this.item.lastModified)
text = new Intl.DateTimeFormat(i18n.locale, {dateStyle: 'medium'} as Intl.DateTimeFormatOptions).format(date)
title = new Intl.DateTimeFormat(i18n.locale, {dateStyle: 'long', timeStyle: 'medium'} as Intl.DateTimeFormatOptions).format(date)
text = new Intl.DateTimeFormat(i18n.locale, {dateStyle: 'medium'} as Intl.DateTimeFormatOptions).format(this.item.lastModified)
title = new Intl.DateTimeFormat(i18n.locale, {dateStyle: 'long', timeStyle: 'medium'} as Intl.DateTimeFormatOptions).format(this.item.lastModified)
}
else
text = i18n.tc('common.books_n', this.item.booksCount)

View file

@ -9,8 +9,8 @@ export interface BookDto {
name: string,
url: string,
number: number,
created: string,
lastModified: string,
created: Date,
lastModified: Date,
sizeBytes: number,
size: string,
media: MediaDto,
@ -76,8 +76,9 @@ export interface BookMetadataDto {
export interface ReadProgressDto {
page: number,
completed: boolean,
created: string,
lastModified: string
readDate: Date,
created: Date,
lastModified: Date
}
export interface BookMetadataUpdateDto {

View file

@ -4,8 +4,8 @@ interface CollectionDto {
ordered: boolean,
filtered: boolean,
seriesIds: string[],
createdDate: string,
lastModifiedDate: string
createdDate: Date,
lastModifiedDate: Date
}
interface CollectionCreationDto {

View file

@ -1,6 +1,6 @@
export interface HistoricalEventDto {
type: string,
timestamp: string,
timestamp: Date,
bookId?: string,
seriesId?: string,
properties: Record<string, string>[],

View file

@ -27,6 +27,6 @@ export interface PageHashCreationDto {
export interface PageHashKnownDto extends PageHashDto {
action: PageHashAction
deleteCount: number,
createdDate: string,
lastModifiedDate: string,
createdDate: Date,
lastModifiedDate: Date,
}

View file

@ -5,8 +5,8 @@ export interface ReadListDto {
ordered: boolean,
filtered: boolean,
bookIds: string[],
createdDate: string,
lastModifiedDate: string
createdDate: Date,
lastModifiedDate: Date
}
export interface ReadListCreationDto {

View file

@ -6,8 +6,8 @@ export interface SeriesDto {
libraryId: string,
name: string,
url: string,
created: string,
lastModified: string,
created: Date,
lastModified: Date,
booksCount: number,
booksReadCount: number,
booksUnreadCount: number,

View file

@ -4,7 +4,7 @@ export interface TransientBookDto {
id: string,
name: string,
url: string,
fileLastModified: string,
fileLastModified: Date,
sizeBytes: number,
size: string,
status: string,

View file

@ -44,6 +44,6 @@ export interface AuthenticationActivityDto {
userAgent?: string,
success: Boolean,
error?: string,
dateTime: string,
dateTime: Date,
source?: string,
}

View file

@ -527,7 +527,7 @@ export default Vue.extend({
return new Intl.DateTimeFormat(this.$i18n.locale, {
dateStyle: 'medium',
timeStyle: 'short',
} as Intl.DateTimeFormatOptions).format(new Date(this.book.readProgress.lastModified))
} as Intl.DateTimeFormatOptions).format(this.book.readProgress.readDate)
return undefined
},
previousId(): string {

View file

@ -41,7 +41,7 @@
new Intl.DateTimeFormat($i18n.locale, {
dateStyle: 'medium',
timeStyle: 'short'
}).format(new Date(item.timestamp))
}).format(item.timestamp)
}}
</template>