Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ファイルピッカーでのインポート機能を追加 #10

Merged
merged 3 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/components/ControlUI.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<template #loading>クリップボードを読み取り中……</template>
<template #completed>クリップボードからインポートしました</template>
</ButtonWithState>
<button @click="importFromFile">ファイルからインポート</button>
<button
@click="
confirm('リセットすると入力中のすべてのデータが破棄されます。') &&
Expand Down Expand Up @@ -36,6 +37,24 @@ const importFromClipboard = async () => {
.map((line) => line.split('\t'))
book.value = b
}

const importFromFile = async (event: Event) => {
const input = event.target as HTMLInputElement
if (!input.files?.length) return

const file = input.files[0]
const reader = new FileReader()
reader.onload = (e) => {
const text = (e.target?.result as string) || ''
try {
dataStore.importCSVString(text)
} catch (error) {
alert('ファイルを正しく読み取ることができませんでした。正しいファイルが選択されているか確認してください。');
console.error('Error importing CSV:', error)
}
}
reader.readAsText(file)
}
</script>

<style scoped>
Expand Down
6 changes: 1 addition & 5 deletions src/components/DragDropImporter.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@
<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue'
import { useDataStore } from '../store/data'
import { storeToRefs } from 'pinia'
import { parse as parseCSV } from 'papaparse'

const dataStore = useDataStore()
const { book } = storeToRefs(dataStore)

const isDragging = ref(false)

Expand All @@ -27,8 +24,7 @@ const onDrop = (e: DragEvent) => {
const reader = new FileReader()
reader.addEventListener('load', (e) => {
const text = e.target?.result as string
const { data } = parseCSV<string[]>(text)
book.value = data
dataStore.importCSVString(text)
})
reader.readAsText(files[0])
}
Expand Down
9 changes: 9 additions & 0 deletions src/store/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { defineStore } from 'pinia'
import { computed, ref } from 'vue'
import { Transaction, TransactionCategory } from '../types/transaction'
import { replaceFullWidthWithHalfWidth } from '../lib/string'
import { parse as parseCSV } from 'papaparse'

/** 仕訳に応じて収支のセルの値を選択し返す
*
Expand Down Expand Up @@ -172,6 +173,11 @@ export const useDataStore = defineStore('data', () => {
)
}

const importCSVString = (csvString: string) => {
const { data } = parseCSV<string[]>(csvString)
book.value = data
}

return {
/** 団体名 */
orgName,
Expand Down Expand Up @@ -226,5 +232,8 @@ export const useDataStore = defineStore('data', () => {

/** ストアを初期状態を戻す */
reset,

/** CSV文字列をパースしてbookに設定する */
importCSVString,
}
})
Loading