mirror of
https://github.com/lana-k/sqliteviz.git
synced 2025-12-06 18:18:53 +08:00
#121 save inquiries in store
This commit is contained in:
23
src/App.vue
23
src/App.vue
@@ -4,6 +4,29 @@
|
|||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
import storedInquiries from '@/lib/storedInquiries'
|
||||||
|
|
||||||
|
export default {
|
||||||
|
created () {
|
||||||
|
this.$store.commit('setInquiries', storedInquiries.getStoredInquiries())
|
||||||
|
},
|
||||||
|
computed: {
|
||||||
|
inquiries () {
|
||||||
|
return this.$store.state.inquiries
|
||||||
|
}
|
||||||
|
},
|
||||||
|
watch: {
|
||||||
|
inquiries: {
|
||||||
|
deep: true,
|
||||||
|
handler () {
|
||||||
|
storedInquiries.updateStorage(this.inquiries)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
@font-face {
|
@font-face {
|
||||||
font-family: "Open Sans";
|
font-family: "Open Sans";
|
||||||
|
|||||||
@@ -36,38 +36,6 @@ export default {
|
|||||||
return inquiryTab.isPredefined || !inquiryTab.name
|
return inquiryTab.isPredefined || !inquiryTab.name
|
||||||
},
|
},
|
||||||
|
|
||||||
save (inquiryTab, newName) {
|
|
||||||
const value = {
|
|
||||||
id: inquiryTab.isPredefined ? nanoid() : inquiryTab.id,
|
|
||||||
query: inquiryTab.query,
|
|
||||||
viewType: inquiryTab.dataView.mode,
|
|
||||||
viewOptions: inquiryTab.dataView.getOptionsForSave(),
|
|
||||||
name: newName || inquiryTab.name
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get inquiries from local storage
|
|
||||||
const myInquiries = this.getStoredInquiries()
|
|
||||||
|
|
||||||
// Set createdAt
|
|
||||||
if (newName) {
|
|
||||||
value.createdAt = new Date()
|
|
||||||
} else {
|
|
||||||
var inquiryIndex = myInquiries.findIndex(oldInquiry => oldInquiry.id === inquiryTab.id)
|
|
||||||
value.createdAt = myInquiries[inquiryIndex].createdAt
|
|
||||||
}
|
|
||||||
|
|
||||||
// Insert in inquiries list
|
|
||||||
if (newName) {
|
|
||||||
myInquiries.push(value)
|
|
||||||
} else {
|
|
||||||
myInquiries[inquiryIndex] = value
|
|
||||||
}
|
|
||||||
|
|
||||||
// Save to local storage
|
|
||||||
this.updateStorage(myInquiries)
|
|
||||||
return value
|
|
||||||
},
|
|
||||||
|
|
||||||
updateStorage (inquiries) {
|
updateStorage (inquiries) {
|
||||||
localStorage.setItem('myInquiries', JSON.stringify({ version: this.version, inquiries }))
|
localStorage.setItem('myInquiries', JSON.stringify({ version: this.version, inquiries }))
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -13,5 +13,34 @@ export default {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return inquiry.id
|
return inquiry.id
|
||||||
|
},
|
||||||
|
async saveInquiry ({ state }, { inquiryTab, newName }) {
|
||||||
|
const value = {
|
||||||
|
id: inquiryTab.isPredefined ? nanoid() : inquiryTab.id,
|
||||||
|
query: inquiryTab.query,
|
||||||
|
viewType: inquiryTab.dataView.mode,
|
||||||
|
viewOptions: inquiryTab.dataView.getOptionsForSave(),
|
||||||
|
name: newName || inquiryTab.name
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get inquiries from local storage
|
||||||
|
const myInquiries = state.inquiries
|
||||||
|
|
||||||
|
// Set createdAt
|
||||||
|
if (newName) {
|
||||||
|
value.createdAt = new Date()
|
||||||
|
} else {
|
||||||
|
var inquiryIndex = myInquiries.findIndex(oldInquiry => oldInquiry.id === inquiryTab.id)
|
||||||
|
value.createdAt = myInquiries[inquiryIndex].createdAt
|
||||||
|
}
|
||||||
|
|
||||||
|
// Insert in inquiries list
|
||||||
|
if (newName) {
|
||||||
|
myInquiries.push(value)
|
||||||
|
} else {
|
||||||
|
myInquiries.splice(inquiryIndex, 1, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
return value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -60,5 +60,43 @@ export default {
|
|||||||
},
|
},
|
||||||
setPredefinedInquiriesLoaded (state, value) {
|
setPredefinedInquiriesLoaded (state, value) {
|
||||||
state.predefinedInquiriesLoaded = value
|
state.predefinedInquiriesLoaded = value
|
||||||
|
},
|
||||||
|
setInquiries (state, value) {
|
||||||
|
state.inquiries = value
|
||||||
|
},
|
||||||
|
addInquiry (state, newInquiry) {
|
||||||
|
state.inquiries.push(newInquiry)
|
||||||
|
},
|
||||||
|
deleteInquiries (state, inquiryIdSet) {
|
||||||
|
state.inquiries = state.inquiries.filter(
|
||||||
|
inquiry => !inquiryIdSet.has(inquiry.id)
|
||||||
|
)
|
||||||
|
|
||||||
|
// Close deleted inquiries if it was opened
|
||||||
|
const tabs = state.tabs
|
||||||
|
let i = tabs.length - 1
|
||||||
|
while (i > -1) {
|
||||||
|
if (inquiryIdSet.has(tabs[i].id)) {
|
||||||
|
this.commit('deleteTab', tabs[i])
|
||||||
|
}
|
||||||
|
i--
|
||||||
|
}
|
||||||
|
},
|
||||||
|
renameInquiry (state, {inquiryId, newName}) {
|
||||||
|
const renamingInquiry = state.inquiries
|
||||||
|
.find(inquiry => inquiry.id === inquiryId)
|
||||||
|
|
||||||
|
renamingInquiry.name = newName
|
||||||
|
|
||||||
|
// update tab, if renamed inquiry is opened
|
||||||
|
const tab = state.tabs.find(tab => tab.id === renamingInquiry.id)
|
||||||
|
if (tab) {
|
||||||
|
this.commit('updateTab', {
|
||||||
|
tab,
|
||||||
|
newValues: {
|
||||||
|
name: newName
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ export default {
|
|||||||
currentTab: null,
|
currentTab: null,
|
||||||
currentTabId: null,
|
currentTabId: null,
|
||||||
untitledLastIndex: 0,
|
untitledLastIndex: 0,
|
||||||
|
inquiries: [],
|
||||||
predefinedInquiries: [],
|
predefinedInquiries: [],
|
||||||
loadingPredefinedInquiries: false,
|
loadingPredefinedInquiries: false,
|
||||||
predefinedInquiriesLoaded: false,
|
predefinedInquiriesLoaded: false,
|
||||||
|
|||||||
@@ -183,7 +183,6 @@ export default {
|
|||||||
mixins: [tooltipMixin],
|
mixins: [tooltipMixin],
|
||||||
data () {
|
data () {
|
||||||
return {
|
return {
|
||||||
inquiries: [],
|
|
||||||
filter: null,
|
filter: null,
|
||||||
newName: null,
|
newName: null,
|
||||||
processedInquiryId: null,
|
processedInquiryId: null,
|
||||||
@@ -198,6 +197,9 @@ export default {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
computed: {
|
computed: {
|
||||||
|
inquiries () {
|
||||||
|
return this.$store.state.inquiries || []
|
||||||
|
},
|
||||||
predefinedInquiries () {
|
predefinedInquiries () {
|
||||||
return this.$store.state.predefinedInquiries.map(inquiry => {
|
return this.$store.state.predefinedInquiries.map(inquiry => {
|
||||||
inquiry.isPredefined = true
|
inquiry.isPredefined = true
|
||||||
@@ -258,7 +260,6 @@ export default {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
async created () {
|
async created () {
|
||||||
this.inquiries = storedInquiries.getStoredInquiries()
|
|
||||||
const loadingPredefinedInquiries = this.$store.state.loadingPredefinedInquiries
|
const loadingPredefinedInquiries = this.$store.state.loadingPredefinedInquiries
|
||||||
const predefinedInquiriesLoaded = this.$store.state.predefinedInquiriesLoaded
|
const predefinedInquiriesLoaded = this.$store.state.predefinedInquiriesLoaded
|
||||||
if (!predefinedInquiriesLoaded && !loadingPredefinedInquiries) {
|
if (!predefinedInquiriesLoaded && !loadingPredefinedInquiries) {
|
||||||
@@ -330,31 +331,17 @@ export default {
|
|||||||
this.errorMsg = "Inquiry name can't be empty"
|
this.errorMsg = "Inquiry name can't be empty"
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
const processedInquiry = this.inquiries[this.processedInquiryIndex]
|
this.$store.commit('renameInquiry', {
|
||||||
processedInquiry.name = this.newName
|
inquiryId: this.processedInquiryId,
|
||||||
this.$set(this.inquiries, this.processedInquiryIndex, processedInquiry)
|
newName: this.newName
|
||||||
|
|
||||||
// update inquiries in local storage
|
|
||||||
storedInquiries.updateStorage(this.inquiries)
|
|
||||||
|
|
||||||
// update tab, if renamed inquiry is opened
|
|
||||||
const tab = this.$store.state.tabs
|
|
||||||
.find(tab => tab.id === processedInquiry.id)
|
|
||||||
if (tab) {
|
|
||||||
this.$store.commit('updateTab', {
|
|
||||||
tab,
|
|
||||||
newValues: {
|
|
||||||
name: this.newName
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
|
||||||
// hide dialog
|
// hide dialog
|
||||||
this.$modal.hide('rename')
|
this.$modal.hide('rename')
|
||||||
},
|
},
|
||||||
duplicateInquiry (index) {
|
duplicateInquiry (index) {
|
||||||
const newInquiry = storedInquiries.duplicateInquiry(this.showedInquiries[index])
|
const newInquiry = storedInquiries.duplicateInquiry(this.showedInquiries[index])
|
||||||
this.inquiries.push(newInquiry)
|
this.$store.commit('addInquiry', newInquiry)
|
||||||
storedInquiries.updateStorage(this.inquiries)
|
|
||||||
},
|
},
|
||||||
showDeleteDialog (idsSet) {
|
showDeleteDialog (idsSet) {
|
||||||
this.deleteGroup = idsSet.size > 1
|
this.deleteGroup = idsSet.size > 1
|
||||||
@@ -366,39 +353,19 @@ export default {
|
|||||||
deleteInquiry () {
|
deleteInquiry () {
|
||||||
this.$modal.hide('delete')
|
this.$modal.hide('delete')
|
||||||
if (!this.deleteGroup) {
|
if (!this.deleteGroup) {
|
||||||
this.inquiries.splice(this.processedInquiryIndex, 1)
|
this.$store.commit('deleteInquiries', new Set().add(this.processedInquiryId))
|
||||||
|
|
||||||
// Close deleted inquiry tab if it was opened
|
|
||||||
const tab = this.$store.state.tabs
|
|
||||||
.find(tab => tab.id === this.processedInquiryId)
|
|
||||||
if (tab) {
|
|
||||||
this.$store.commit('deleteTab', tab)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Clear checkbox
|
// Clear checkbox
|
||||||
if (this.selectedInquiriesIds.has(this.processedInquiryId)) {
|
if (this.selectedInquiriesIds.has(this.processedInquiryId)) {
|
||||||
this.selectedInquiriesIds.delete(this.processedInquiryId)
|
this.selectedInquiriesIds.delete(this.processedInquiryId)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
this.inquiries = this.inquiries.filter(
|
this.$store.commit('deleteInquiries', this.selectedInquiriesIds)
|
||||||
inquiry => !this.selectedInquiriesIds.has(inquiry.id)
|
|
||||||
)
|
|
||||||
|
|
||||||
// Close deleted inquiries if it was opened
|
|
||||||
const tabs = this.$store.state.tabs
|
|
||||||
let i = tabs.length - 1
|
|
||||||
while (i > -1) {
|
|
||||||
if (this.selectedInquiriesIds.has(tabs[i].id)) {
|
|
||||||
this.$store.commit('deleteTab', tabs[i])
|
|
||||||
}
|
|
||||||
i--
|
|
||||||
}
|
|
||||||
|
|
||||||
// Clear checkboxes
|
// Clear checkboxes
|
||||||
this.selectedInquiriesIds.clear()
|
this.selectedInquiriesIds.clear()
|
||||||
}
|
}
|
||||||
this.selectedInquiriesCount = this.selectedInquiriesIds.size
|
this.selectedInquiriesCount = this.selectedInquiriesIds.size
|
||||||
storedInquiries.updateStorage(this.inquiries)
|
|
||||||
},
|
},
|
||||||
exportToFile (inquiryList, fileName) {
|
exportToFile (inquiryList, fileName) {
|
||||||
storedInquiries.export(inquiryList, fileName)
|
storedInquiries.export(inquiryList, fileName)
|
||||||
@@ -414,8 +381,7 @@ export default {
|
|||||||
importInquiries () {
|
importInquiries () {
|
||||||
storedInquiries.importInquiries()
|
storedInquiries.importInquiries()
|
||||||
.then(importedInquiries => {
|
.then(importedInquiries => {
|
||||||
this.inquiries = this.inquiries.concat(importedInquiries)
|
this.$store.commit('setInquiries', this.inquiries.concat(importedInquiries))
|
||||||
storedInquiries.updateStorage(this.inquiries)
|
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
@@ -122,7 +122,7 @@ export default {
|
|||||||
this.saveInquiry()
|
this.saveInquiry()
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
saveInquiry () {
|
async saveInquiry () {
|
||||||
const isNeedName = storedInquiries.isTabNeedName(this.currentInquiry)
|
const isNeedName = storedInquiries.isTabNeedName(this.currentInquiry)
|
||||||
if (isNeedName && !this.name) {
|
if (isNeedName && !this.name) {
|
||||||
this.errorMsg = 'Inquiry name can\'t be empty'
|
this.errorMsg = 'Inquiry name can\'t be empty'
|
||||||
@@ -132,7 +132,10 @@ export default {
|
|||||||
const tabView = this.currentInquiry.view
|
const tabView = this.currentInquiry.view
|
||||||
|
|
||||||
// Save inquiry
|
// Save inquiry
|
||||||
const value = storedInquiries.save(this.currentInquiry, this.name)
|
const value = await this.$store.dispatch('saveInquiry', {
|
||||||
|
inquiryTab:this.currentInquiry,
|
||||||
|
newName:this.name
|
||||||
|
})
|
||||||
|
|
||||||
// Update tab in store
|
// Update tab in store
|
||||||
this.$store.commit('updateTab', {
|
this.$store.commit('updateTab', {
|
||||||
|
|||||||
Reference in New Issue
Block a user