Skip to content

Commit

Permalink
🔨 refactor: Dock 栏目使用查询方案获取预约;不再使用 reservation 对象提供数据
Browse files Browse the repository at this point in the history
- dock 栏点击刷新没有查询到最新的 reservation close #193
- 重构 reservation #197
  • Loading branch information
frostime committed Sep 13, 2024
1 parent 475eee3 commit 9f22172
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 8 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"rollup-plugin-livereload": "^2.0.5",
"sass": "^1.72.0",
"siyuan": "1.0.2",
"siyuan-plugin-cli": "^1.2.0",
"svelte": "^4.2.19",
"sy-plugin-changelog": "^0.0.7",
"ts-node": "^10.9.2",
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 25 additions & 5 deletions src/components/dock-reserve.svelte
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<script lang="ts">
import { onMount } from "svelte";
import ListItem from "./list-item.svelte";
import { reservation } from "@/global-status";
import * as api from "@/serverApi";
import { i18n } from "@/utils";
import { retrieveResvFromBlocks } from "@/func";
let expandStatus = [];
let allResvs = [];
let allResvs: { date: string; blocks: Block[] }[] = [];
let _i18n = i18n.DockReserve;
function allExpand() {
Expand All @@ -23,9 +23,28 @@
}
}
type DateString = string;
async function updateWithReservations() {
//[ [date, [blockIds]] ]
let entries = Object.entries(reservation.reservations.OnDate);
let reservations: {
id: BlockId;
content: string;
date: DateString;
}[] = await retrieveResvFromBlocks("datetime", "-2 day");
const resvDates: Record<DateString, BlockId[]> = reservations.reduce(
(acc, resv) => {
if (!acc[resv.date]) {
acc[resv.date] = [];
}
acc[resv.date].push(resv.id);
return acc;
},
{} as Record<DateString, BlockId[]>,
);
let entries = Object.entries(resvDates);
let dateCnt = entries.length;
let allBlockIds = [];
for (let i = 0; i < dateCnt; i++) {
Expand Down Expand Up @@ -68,7 +87,7 @@
newResvs.push({
date: `${entry[0].slice(0, 4)}-${entry[0].slice(
4,
6
6,
)}-${entry[0].slice(6, 8)}`,
blocks: blocks,
});
Expand All @@ -86,7 +105,8 @@
<div class="fn__flex-1 fn__flex-column file-tree layout__tab">
<div class="block__icons">
<div class="block__logo">
<svg class="block__logoicon"><use xlink:href="#iconBookmark" /></svg>
<svg class="block__logoicon"><use xlink:href="#iconBookmark" /></svg
>
{_i18n.title}
</div>
<span class="fn__flex-1" />
Expand Down
16 changes: 13 additions & 3 deletions src/func/reserve/retrieve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @Author : Yp Z
* @Date : 2023-06-17 13:55:54
* @FilePath : /src/func/reserve/retrieve.ts
* @LastEditTime : 2024-04-06 18:02:54
* @LastEditTime : 2024-09-13 11:48:53
* @Description :
*/
import * as serverApi from '@/serverApi';
Expand Down Expand Up @@ -172,16 +172,26 @@ export function RetvFactory(type: RetvType, position: RetvPosition, resvBlockIds
}


export async function retrieveResvFromBlocks(time: 'today' | 'future'): Promise<Reservation[]> {
/**
* 查询所有的预约块,并返回预约内容
* @param time 'today' | 'future' | TAfterDateTime
* - 'today' 查询今天的预约
* - 'future' 查询未来的预约
* - 'datetime': 将 datetimeArgs 传入 `strftime` 函数的日期字符串,查询该日期之后的预约
* `A.value > strftime('%Y%m%d', datetime('now', ...datetimeArgs))`;
* @returns Promise<Reservation[]>
*/
export async function retrieveResvFromBlocks(time: 'today' | 'future' | 'datetime', ...datetimeArgs: string[]): Promise<Reservation[]> {
let cond = '';
if (time === 'today') {
cond = `A.value = strftime('%Y%m%d', datetime('now'))`;
} else if (time === 'future') {
cond = `A.value >= strftime('%Y%m%d', datetime('now'))`;
} else if (time === 'datetime') {
cond = `A.value >= strftime('%Y%m%d', datetime('now', ${datetimeArgs.map((arg) => `'${arg}'`).join(',')}))`;
}
let sql = `select B.id, B.content, A.value as date from blocks as B inner join attributes as A
on(A.block_id = B.id and A.name = 'custom-reservation' and ${cond}) order by A.value;`;
let results: Reservation[] = await serverApi.sql(sql);
console.debug(results);
return results;
}

0 comments on commit 9f22172

Please sign in to comment.