fix: date
This commit is contained in:
@@ -4,5 +4,7 @@ import PostScheme from './post/PostScheme.vue';
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<PostScheme />
|
||||
</template>
|
||||
<main class="w-screen h-screen">
|
||||
<PostScheme />
|
||||
</main>
|
||||
</template>
|
||||
40
frontend/src/author/AuthorScheme.vue
Normal file
40
frontend/src/author/AuthorScheme.vue
Normal file
@@ -0,0 +1,40 @@
|
||||
<script setup lang="ts">
|
||||
import Table from '../table/Table.vue'
|
||||
import { onMounted, reactive } from 'vue'
|
||||
import { getDefaultValues } from '../utils/structs/defaults.util.ts'
|
||||
import S from './author.service.ts'
|
||||
import type { Scheme } from '../types/scheme.type'
|
||||
import { Author } from '../../bindings/app/internal/services/models.ts'
|
||||
|
||||
const service = new S
|
||||
|
||||
onMounted(async () => {
|
||||
|
||||
})
|
||||
|
||||
|
||||
const scheme: Scheme<Author> = reactive({
|
||||
Id:{
|
||||
hidden: true,
|
||||
type: {
|
||||
primitive: "number",
|
||||
},
|
||||
},
|
||||
Name:{
|
||||
russian: "Имя",
|
||||
type: {
|
||||
primitive: "string",
|
||||
},
|
||||
},
|
||||
|
||||
})
|
||||
|
||||
const getDefaults = () => getDefaultValues(scheme)
|
||||
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<main class="w-screen h-screen">
|
||||
<Table :scheme :service :getDefaults></Table>
|
||||
</main>
|
||||
</template>
|
||||
29
frontend/src/author/author.service.ts
Normal file
29
frontend/src/author/author.service.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { GetAll, Create, Delete, GetById, Update, Count } from "../../bindings/app/internal/services/authorservice.ts"
|
||||
import type { Author } from "../../bindings/app/internal/services/models.ts"
|
||||
import type { IService } from "../types/service.type.ts"
|
||||
|
||||
export default class AuthorService implements IService<Author> {
|
||||
async read(id: number) {
|
||||
return await GetById(id) as Author
|
||||
}
|
||||
|
||||
async readAll() {
|
||||
return await GetAll() as Author[]
|
||||
}
|
||||
|
||||
async create(item: Author) {
|
||||
await Create(item)
|
||||
}
|
||||
|
||||
async delete(id: number) {
|
||||
return await Delete(id)
|
||||
}
|
||||
|
||||
async update(item: Author) {
|
||||
await Update(item)
|
||||
}
|
||||
|
||||
async count() {
|
||||
return await Count()
|
||||
}
|
||||
}
|
||||
@@ -4,9 +4,11 @@ import App from './App.vue'
|
||||
import { Config } from 'primevue'
|
||||
import Aura from '@primevue/themes/aura'
|
||||
import 'primeicons/primeicons.css'
|
||||
import { ru } from 'primelocale/js/ru.js'
|
||||
|
||||
createApp(App).use(Config, {
|
||||
theme: {
|
||||
preset: Aura
|
||||
}
|
||||
preset: Aura,
|
||||
},
|
||||
locale: ru
|
||||
}).mount('#app')
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<script setup lang="ts">
|
||||
import Table from '../table/Table.vue'
|
||||
import { onMounted, reactive } from 'vue'
|
||||
import { getDefaultValues } from '../utils/structs/defaults.util'
|
||||
import { getDefaultValues } from '../utils/structs/defaults.util.ts'
|
||||
import S from './post.service.ts'
|
||||
import type { Scheme } from '../types/scheme.type'
|
||||
import { Post } from '../../bindings/app/internal/services/models.ts'
|
||||
@@ -9,36 +9,37 @@ import { Post } from '../../bindings/app/internal/services/models.ts'
|
||||
const service = new S
|
||||
|
||||
onMounted(async () => {
|
||||
|
||||
|
||||
})
|
||||
|
||||
|
||||
const scheme: Scheme<Post> = reactive({
|
||||
Id:{
|
||||
type: {
|
||||
primitive: "number",
|
||||
Id: {
|
||||
hidden: true,
|
||||
type: {
|
||||
primitive: "number",
|
||||
},
|
||||
},
|
||||
hidden: true,
|
||||
},
|
||||
Text:{
|
||||
type: {
|
||||
primitive: "string",
|
||||
Text: {
|
||||
russian: "Текст",
|
||||
type: {
|
||||
primitive: "string",
|
||||
},
|
||||
},
|
||||
},
|
||||
CreatedAt:{
|
||||
type: {
|
||||
primitive: "number",
|
||||
CreatedAt: {
|
||||
hidden: true,
|
||||
type: {
|
||||
primitive: "number",
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
})
|
||||
|
||||
const getDefaults = () => getDefaultValues(scheme)
|
||||
const getDefaults = () => getDefaultValues(scheme)
|
||||
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<main class="w-screen h-screen">
|
||||
<Table :scheme :service :getDefaults></Table>
|
||||
</main>
|
||||
<main class="w-screen h-screen">
|
||||
<Table :scheme :service :getDefaults></Table>
|
||||
</main>
|
||||
</template>
|
||||
|
||||
@@ -4,7 +4,7 @@ import type { IService } from "../types/service.type.ts"
|
||||
|
||||
export default class PostService implements IService<Post> {
|
||||
async read(id: number) {
|
||||
return await GetById(id)
|
||||
return await GetById(id) as Post
|
||||
}
|
||||
|
||||
async readAll() {
|
||||
@@ -17,10 +17,12 @@ export default class PostService implements IService<Post> {
|
||||
|
||||
async delete(id: number) {
|
||||
return await Delete(id)
|
||||
}
|
||||
}
|
||||
|
||||
async update(item: Post) {
|
||||
await Update(item)
|
||||
}
|
||||
|
||||
async count() {
|
||||
return await Count()
|
||||
}
|
||||
|
||||
@@ -1,3 +1,14 @@
|
||||
@tailwind base;
|
||||
@tailwind components;
|
||||
@tailwind utilities;
|
||||
@tailwind utilities;
|
||||
|
||||
|
||||
html, body {
|
||||
background: white;
|
||||
}
|
||||
|
||||
@media (prefers-color-scheme: dark) {
|
||||
html, body {
|
||||
background: #121212;
|
||||
}
|
||||
}
|
||||
@@ -5,10 +5,10 @@ import type { Scheme } from '../types/scheme.type';
|
||||
import type { IService } from '../types/service.type';
|
||||
import { manyStructsView } from '../utils/structs/structs-view.util';
|
||||
import { type UnwrapRef } from 'vue';
|
||||
import { toDate, toTimestamp } from '../utils/date/converters.util';
|
||||
|
||||
const showCreate = defineModel<boolean>('show')
|
||||
const createItem = defineModel<T>('item')
|
||||
|
||||
const items = defineModel<UnwrapRef<T[]>>('items')
|
||||
|
||||
const props = defineProps<{
|
||||
@@ -45,7 +45,10 @@ const emits = defineEmits<{
|
||||
v-else-if="props.scheme[key]?.type?.primitive === 'number'" />
|
||||
<InputText class="w-[300px]" v-model:model-value="<string>createItem![key]"
|
||||
v-else-if="props.scheme[key].type?.primitive === 'string'" />
|
||||
<DatePicker class="w-[300px]" v-model:model-value="<Date>createItem![key]"
|
||||
<DatePicker class="w-[300px]" :default-value="toDate(createItem![key] as number)" @value-change="v => {
|
||||
createItem![key] = toTimestamp(v as Date) as any
|
||||
console.log(createItem![key])
|
||||
}" show-time
|
||||
v-else-if="props.scheme[key].type?.primitive === 'date'" />
|
||||
<Textarea class="w-[300px]" v-model="<string>createItem![key]"
|
||||
v-else-if="props.scheme[key].type?.primitive === 'multiple'" />
|
||||
@@ -76,12 +79,12 @@ const emits = defineEmits<{
|
||||
</div>
|
||||
<template #footer>
|
||||
<Button severity="success" @click="async () => {
|
||||
console.log(createItem)
|
||||
if (props.updateMode) {
|
||||
await props.service.update(createItem as T)
|
||||
await emits('onSaveUpdate', createItem as T)
|
||||
await emits('onSave', createItem as T)
|
||||
} else {
|
||||
if (createItem) createItem.Id = 0;
|
||||
await props.service.create(createItem as T)
|
||||
await emits('onSaveCreate', createItem as T)
|
||||
await emits('onSave', createItem as T)
|
||||
|
||||
@@ -7,6 +7,8 @@ import type { TableEmits } from "../types/table-emits.type";
|
||||
import FloatingButton from "../components/buttons/FloatingButton.vue";
|
||||
import type { IEntity } from "../types/entity.type";
|
||||
import DialogWindow from "./DialogWindow.vue";
|
||||
import { timestampToDate } from "../utils/date/converters.util";
|
||||
import { viewDate } from "../utils/date/view.util";
|
||||
|
||||
const props = defineProps<TableProps<T>>();
|
||||
|
||||
@@ -116,10 +118,10 @@ const updateSlotName = (key: any) => key + "Update";
|
||||
<template #body="{ data }">
|
||||
<p>
|
||||
{{
|
||||
manyStructsView(
|
||||
viewDate(manyStructsView(
|
||||
data[key],
|
||||
props.scheme[key]?.type?.nested?.field,
|
||||
)
|
||||
), scheme[key]?.type?.primitive)
|
||||
}}
|
||||
</p>
|
||||
</template>
|
||||
@@ -131,9 +133,9 @@ const updateSlotName = (key: any) => key + "Update";
|
||||
<Button
|
||||
severity="secondary"
|
||||
icon="pi pi-pencil"
|
||||
@click="() => {
|
||||
emits('onUpdateOpen')
|
||||
emits('onOpen')
|
||||
@click="async () => {
|
||||
await emits('onUpdateOpen')
|
||||
await emits('onOpen')
|
||||
updateItem = data
|
||||
}"
|
||||
></Button>
|
||||
@@ -141,7 +143,7 @@ const updateSlotName = (key: any) => key + "Update";
|
||||
severity="danger"
|
||||
icon="pi pi-trash"
|
||||
@click="async () => {
|
||||
emits('onDelete', data)
|
||||
await emits('onDelete', data)
|
||||
await props.service.delete(data.Id)
|
||||
items = await props.service.readAll() as UnwrapRef<T[]>
|
||||
}"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
const getFullTimestamp = (n: number): number => {
|
||||
export const getFullTimestamp = (n: number): number => {
|
||||
const length = String(n).length
|
||||
let str = ''
|
||||
while (str.length + length < 13) {
|
||||
@@ -6,5 +6,34 @@ const getFullTimestamp = (n: number): number => {
|
||||
}
|
||||
return parseInt(`${n}${str}`)
|
||||
}
|
||||
export const toDate = (n: number) => new Date(getFullTimestamp(n))
|
||||
export const toDate = (n: number | Date) => {
|
||||
if (n instanceof Date) return n
|
||||
return new Date(getFullTimestamp(n))
|
||||
}
|
||||
export const toTimestamp = (d: Date) => d.getTime()
|
||||
|
||||
export const dateToTimestamp = (obj: any) => {
|
||||
if (typeof obj == 'object') {
|
||||
for (let key in obj) {
|
||||
if (obj[key] instanceof Date) {
|
||||
obj[key] = toTimestamp(obj[key])
|
||||
} else if (typeof obj[key] == 'object') {
|
||||
dateToTimestamp(obj[key])
|
||||
}
|
||||
}
|
||||
}
|
||||
return obj
|
||||
}
|
||||
|
||||
export const timestampToDate = (obj: any) => {
|
||||
if (typeof obj == 'object') {
|
||||
for (let key in obj) {
|
||||
if (typeof obj[key] == 'number') {
|
||||
obj[key] = toDate(obj[key])
|
||||
} else if (typeof obj[key] == 'object') {
|
||||
timestampToDate(obj[key])
|
||||
}
|
||||
}
|
||||
}
|
||||
return obj
|
||||
}
|
||||
14
frontend/src/utils/date/view.util.ts
Normal file
14
frontend/src/utils/date/view.util.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import type { PrimitiveFieldType } from "../../types/primitive-field-type.type"
|
||||
import { toDate } from "./converters.util";
|
||||
|
||||
export const viewDate = (data: unknown, type: PrimitiveFieldType) => {
|
||||
if (type !== 'date') return data;
|
||||
return toDate(data as number | Date).toLocaleDateString('ru-RU', {
|
||||
year: 'numeric',
|
||||
month: 'long',
|
||||
day: 'numeric',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
second: '2-digit'
|
||||
})
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { IEntity } from "../../types/entity.type";
|
||||
import type { Scheme } from "../../types/scheme.type";
|
||||
import { getTomorrow } from "../date/getters";
|
||||
import { toTimestamp } from "../date/converters.util";
|
||||
|
||||
export const getDefaultValues = <T extends IEntity>(scheme: Scheme<T>) => {
|
||||
const keys = Object.keys(scheme) as (keyof typeof scheme)[]
|
||||
@@ -8,10 +8,11 @@ export const getDefaultValues = <T extends IEntity>(scheme: Scheme<T>) => {
|
||||
|
||||
for (let key of keys) {
|
||||
const primitive = scheme[key]?.type?.primitive
|
||||
if (primitive == 'string' || primitive == 'multiple') {
|
||||
if (scheme[key].hidden) continue;
|
||||
if ((primitive == 'string' || primitive == 'multiple')) {
|
||||
obj[key] = ''
|
||||
} else if (primitive == 'date') {
|
||||
obj[key] = getTomorrow()
|
||||
obj[key] = toTimestamp(new Date)
|
||||
} else if (primitive == 'boolean') {
|
||||
obj[key] = false
|
||||
} else if (primitive == 'number') {
|
||||
|
||||
Reference in New Issue
Block a user