diff --git a/src/utils.js b/src/utils.js index 1a5d274..93feba9 100644 --- a/src/utils.js +++ b/src/utils.js @@ -20,6 +20,7 @@ const classItems = [ let notificationEl; let loadingEl; +let dialogEl; function getHeaderSpace(className, isDetailPage) { const headerSpace = (isDetailPage ? kintone.app.record : kintone.app).getHeaderMenuSpaceElement(); @@ -231,16 +232,16 @@ function convertToWesternYear(year, era) { function getFormatDateString(dateObjOrYear, month, date) { let year = dateObjOrYear; - if (typeof dateObjOrYear === "object") { + if (typeof dateObjOrYear === "object" && !month && !date) { year = dateObjOrYear.getFullYear(); month = dateObjOrYear.getMonth() + 1; date = dateObjOrYear.getDate(); } const formatY = year; const formatM = String(month).padStart(2, '0'); - const formatD = String(date).padStart(2, '0'); + const formatD = date ? String(date).padStart(2, '0') : ''; - return `${formatY}-${formatM}-${formatD}`; + return formatY + '-' + formatM + (formatD && ('-' + formatD)); } function loading(show, text) { @@ -286,6 +287,70 @@ function buildNotification(type, text, duration = -1) { if (!notificationEl) { notificationEl = new Kuc.Notification(param); } else { - Object.assign(notificationEl, param) + Object.assign(notificationEl, param); + } +} + +/** +options: { + icon: '' | 'info' | 'success' | 'error' | 'warning' | 'question', + title: string, + content: string|HTMLElement, + header: string|HTMLElement, + footer: string|HTMLElement, + + dataHolder: {}, + // ↓ if not footer ↓ + ok: string = 'OK', + cancel: boolean|string = 'キャンセル', + onOk: (dataHolder, e) => void, + onCancel: (dataHolder, e) => void, + onClose: (dataHolder, e) => void, +} +*/ +function showDialog(options) { + if (!dialogEl) { + createDialogEl(options); + } else { + Object.assign(dialogEl, options); + } + dialogEl.open(); +} + +function createDialogEl({ ok, cancel, onOk, onCancel, onClose, ...options }) { + if (!options.footer) { + const divEl = document.createElement('div'); + divEl.style.textAlign = 'right'; + + if (cancel !== false) { + const cancelButton = new Kuc.Button({ + text: cancel || 'Cancel', + type: 'normal' + }); + cancelButton.style.paddingRight = '8px'; + cancelButton.addEventListener('click', (event) => { + onCancel && onCancel(dialogEl.dataHolder, event); + dialogEl.close(); + }); + divEl.appendChild(cancelButton); + } + + const okButton = new Kuc.Button({ + text: ok || 'OK', + type: 'submit' + }); + okButton.addEventListener('click', (event) => { + onOk && onOk(dialogEl.dataHolder, event); + dialogEl.close(); + }); + divEl.appendChild(okButton); + options.footer = divEl; + } + + dialogEl = new Kuc.Dialog(options); + if (onClose) { + dialogEl.addEventListener('close', (event) => { + onClose(dialogEl.dataHolder, event); + }); } } \ No newline at end of file diff --git a/src/園児別出欠簿入力/BatchCreateHandler.js b/src/園児別出欠簿入力/BatchCreateHandler.js index 08cb2b9..e097f26 100644 --- a/src/園児別出欠簿入力/BatchCreateHandler.js +++ b/src/園児別出欠簿入力/BatchCreateHandler.js @@ -29,11 +29,29 @@ class BatchCreateHandler { // console.log(records); const createResult = await this.createData(api, records, todayString); - showSuccess(true, "出欠簿一括作成完了"); - location.reload(); + // showSuccess(true, "出欠簿一括作成完了"); + + this.showSuccessDialog(createResult); loading(false); } + showSuccessDialog(result) { + const contentEl = document.createElement('div'); + contentEl.style.fontSize = '16px'; + if (!result) { + contentEl.innerHTML = "データの更新はありません。既に最新の状態です。"; + } else { + contentEl.innerHTML = `${result.records.length}件のデータを挿入しました。`; + } + showDialog({ + title: '出欠簿一括作成完了', + content: contentEl, + ok: '更新して確認', + cancel: false, + onClose: () => { location.reload() } + }); + } + generateRecords = (today, todayString, masterRecords) => { return masterRecords.reduce((acc, masterRecord) => { if (this.needCreateData(masterRecord, today)) { diff --git a/src/園児別出欠簿入力/Link1Handler.js b/src/園児別出欠簿入力/Link1Handler.js index 1a41dea..6a62b3d 100644 --- a/src/園児別出欠簿入力/Link1Handler.js +++ b/src/園児別出欠簿入力/Link1Handler.js @@ -12,7 +12,29 @@ class Link1Handler { headerSpace.appendChild(elements['link-1-action-area']); } - handleLink(e, { year, month }) { + handleLink = async (e, { year, month }) => { + loading(true, '日誌データ連携中...'); + showError(false); + const api = new KintoneRestAPIClient(); + + const dateString = getFormatDateString(year, month); + + // 本アプリからデータを読み取る + // const currentAppRecords = await this.getRecords(api, dateString); + // if (!currentAppRecords) { + // // エラー + // loading(false); + // return; + // } + + // const record = this.generateRecord(dateString, currentAppRecords); + // console.log(record); + + // const result = await this.insertOrUpdateData(api, record, dateString); + // if (result) { + // showSuccess(true, "日誌データ連携作成完了"); + // } + // loading(false); } static getInstance(headerSpace) { diff --git a/src/園児別出欠簿入力/Link2Handler.js b/src/園児別出欠簿入力/Link2Handler.js index 1eefb4c..a9d9f25 100644 --- a/src/園児別出欠簿入力/Link2Handler.js +++ b/src/園児別出欠簿入力/Link2Handler.js @@ -34,17 +34,38 @@ class Link2Handler { const result = await this.insertOrUpdateData(api, record, dateString); if (result) { - showSuccess(true, "日誌データ連携作成完了"); + // showSuccess(true, "日誌データ連携作成完了"); + this.showSuccessDialog(result); } loading(false); } + showSuccessDialog(result) { + const contentEl = document.createElement('div'); + contentEl.style.fontSize = '16px'; + if (!result.size) { + contentEl.innerHTML = `${result.dateString}に園児別テーブルに追加するデータはありませんでした。`; + } else if (result.type === 'update') { + contentEl.innerHTML = `${result.dateString}の園児別テーブルに${result.size}件のデータを再作成しました。`; + } else { + contentEl.innerHTML = `${result.dateString}の日誌は生成しましたが、園児別テーブルに${result.size}件のデータを追加しました。`; + } + showDialog({ + title: '日誌データ連携作成完了', + content: contentEl, + ok: 'アプリへ行く', + cancel: '閉じる', + dataHolder: result, + onOk: (dataHolder) => { window.open(`${location.origin}/k/${env["2歳以上日誌出力用"].appId}/show#record=` + dataHolder.id) }, + }); + } + getRecords = async (api, dateString) => { try { return await api.record.getAllRecordsWithId({ app: env["園児別出欠簿入力"].appId, fields: ['クラス', "園児名", "園での様子_伝達事項", "評価反省"], - condition: `学年 in ("2歳児", "3歳児", "4歳児", "5歳児") and 登園日 = "${dateString}"` + condition: `学年 not in ("", "0歳児", "1歳児") and 登園日 = "${dateString}"` }); } catch (e) { showError(true, '園児台帳アプリのデータ読み取りエラー\n - ' + e); @@ -83,16 +104,24 @@ class Link2Handler { app: env["2歳以上日誌出力用"].appId, record } + const result = { + id: generatedRecordId, + type: generatedRecordId ? 'update' : 'insert', + size: record["園児別テーブル"].value.length, + dateString + }; - let addResult; - if (generatedRecordId) { + let awaitResult; + if (result.type === 'update') { param.id = generatedRecordId; // 本アプリの当日データでテーブルを上書きし、対応する園児データがない行を【削除】します - addResult = await api.record.updateRecord(param); + awaitResult = await api.record.updateRecord(param); } else { - addResult = await api.record.addRecord(param); + awaitResult = await api.record.addRecord(param); } - return addResult; + Object.assign(result, awaitResult); + + return result; } catch (e) { showError(true, '日誌データ連携作成失敗\n - ' + e); }