Skip to content

Commit 452bae0

Browse files
authored
Merge pull request #8130 from nextcloud/refactor/pinia-migration-trashbinStore
refactor(pinia): refactor trashbin store to pinia
2 parents 4a2d6d4 + 80f6630 commit 452bae0

9 files changed

Lines changed: 94 additions & 98 deletions

File tree

src/components/board/DeletedTabSidebar.vue

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
</div>
1515
<button :title="t('settings', 'Undo')"
1616
class="app-navigation-entry-deleted-button icon-history"
17-
@click="stackUndoDelete(deletedStack)" />
17+
@click="stackUndoDeleteLocal(deletedStack)" />
1818
</li>
1919
</ul>
2020

@@ -28,14 +28,15 @@
2828
</div>
2929
<button :title="t('settings', 'Undo')"
3030
class="app-navigation-entry-deleted-button icon-history"
31-
@click="cardUndoDelete(deletedCard)" />
31+
@click="cardUndoDeleteLocal(deletedCard)" />
3232
</li>
3333
</ul>
3434
</div>
3535
</template>
3636

3737
<script>
38-
import { mapState } from 'vuex'
38+
import { mapActions, mapState } from 'pinia'
39+
import { useTrashbinStore } from '../../stores/trashbin.js'
3940
import relativeDate from '../../mixins/relativeDate.js'
4041
4142
export default {
@@ -55,30 +56,30 @@ export default {
5556
}
5657
},
5758
computed: {
58-
...mapState({
59-
deletedStacks: state => [...state.trashbin.deletedStacks].sort((a, b) => (a.deletedAt > b.deletedAt) ? -1 : 1),
60-
deletedCards: state => [...state.trashbin.deletedCards].sort((a, b) => (a.deletedAt > b.deletedAt) ? -1 : 1),
59+
...mapState(useTrashbinStore, {
60+
deletedStacks: state => [...state.deletedStacks].sort((a, b) => (a.deletedAt > b.deletedAt) ? -1 : 1),
61+
deletedCards: state => [...state.deletedCards].sort((a, b) => (a.deletedAt > b.deletedAt) ? -1 : 1),
6162
}),
62-
6363
},
6464
created() {
6565
this.getData()
6666
},
6767
methods: {
68+
...mapActions(useTrashbinStore, ['fetchDeletedItems', 'stackUndoDelete', 'cardUndoDelete']),
6869
async getData() {
6970
this.isLoading = true
70-
await this.$store.dispatch('fetchDeletedItems', this.board.id)
71+
this.fetchDeletedItems(this.board.id)
7172
this.isLoading = false
7273
},
73-
stackUndoDelete(deletedStack) {
74+
stackUndoDeleteLocal(deletedStack) {
7475
const copiedDeletedStack = Object.assign({}, deletedStack)
7576
copiedDeletedStack.deletedAt = 0
76-
this.$store.dispatch('stackUndoDelete', copiedDeletedStack)
77+
this.stackUndoDelete(copiedDeletedStack)
7778
},
78-
cardUndoDelete(deletedCard) {
79+
cardUndoDeleteLocal(deletedCard) {
7980
const copiedDeletedCard = Object.assign({}, deletedCard)
8081
copiedDeletedCard.deletedAt = 0
81-
this.$store.dispatch('cardUndoDelete', copiedDeletedCard)
82+
this.cardUndoDelete(copiedDeletedCard)
8283
},
8384
},
8485
}

src/components/board/Stack.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ import { showError, showUndo } from '@nextcloud/dialogs'
160160
import CardItem from '../cards/CardItem.vue'
161161
162162
import '@nextcloud/dialogs/style.css'
163+
import { mapActions } from 'pinia'
164+
import { useTrashbinStore } from '../../stores/trashbin.js'
163165
164166
export default {
165167
name: 'Stack',
@@ -252,6 +254,7 @@ export default {
252254
},
253255
254256
methods: {
257+
...mapActions(useTrashbinStore, ['stackUndoDelete']),
255258
stopCardCreation(e) {
256259
// For some reason the submit event triggers a MouseEvent that is bubbling to the outside
257260
// so we have to ignore it
@@ -294,7 +297,7 @@ export default {
294297
},
295298
deleteStack(stack) {
296299
this.$store.dispatch('deleteStack', stack)
297-
showUndo(t('deck', 'List deleted'), () => this.$store.dispatch('stackUndoDelete', stack))
300+
showUndo(t('deck', 'List deleted'), () => this.stackUndoDelete(stack))
298301
},
299302
setArchivedToAllCardsFromStack(stack, isArchived) {
300303

src/components/cards/CardMenuEntries.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ import { showUndo } from '@nextcloud/dialogs'
9898
import '@nextcloud/dialogs/style.css'
9999
import { emit } from '@nextcloud/event-bus'
100100
import { useActionsStore } from '../../stores/actions.js'
101+
import { useTrashbinStore } from '../../stores/trashbin.js'
101102
102103
export default {
103104
name: 'CardMenuEntries',
@@ -188,7 +189,7 @@ export default {
188189
deleteCard() {
189190
this.$store.dispatch('deleteCard', this.card)
190191
const undoCard = { ...this.card, deletedAt: 0 }
191-
showUndo(t('deck', 'Card deleted'), () => this.$store.dispatch('cardUndoDelete', undoCard))
192+
showUndo(t('deck', 'Card deleted'), () => useTrashbinStore().cardUndoDelete(undoCard))
192193
if (this.$router.currentRoute.name === 'card') {
193194
this.$router.push({ name: 'board' })
194195
}

src/main.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Vue.config.errorHandler = (err, vm, info) => {
4545

4646
const pinia = createPinia()
4747
Vue.use(PiniaVuePlugin)
48+
pinia.use(() => ({ $vuex: store }))
4849

4950
/* eslint-disable-next-line no-new */
5051
new Vue({

src/store/card.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import { CardApi } from './../services/CardApi.js'
77
import moment from 'moment'
88
import Vue from 'vue'
9+
import { useTrashbinStore } from '../stores/trashbin.js'
910

1011
const apiClient = new CardApi()
1112

@@ -323,7 +324,7 @@ export default function cardModuleFactory() {
323324
async deleteCard({ commit }, card) {
324325
await apiClient.deleteCard(card.id)
325326
commit('deleteCard', card)
326-
commit('moveCardToTrash', card)
327+
useTrashbinStore().moveCardToTrash(card)
327328
},
328329
async archiveUnarchiveCard({ commit }, card) {
329330
let call = 'archiveCard'

src/store/main.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import { generateOcsUrl, generateUrl } from '@nextcloud/router'
1313
import { BoardApi } from '../services/BoardApi.js'
1414
import stackModuleFactory from './stack.js'
1515
import cardModuleFactory from './card.js'
16-
import trashbin from './trashbin.js'
1716
import attachment from './attachment.js'
1817
import overview from './overview.js'
1918
Vue.use(Vuex)
@@ -35,7 +34,6 @@ export default function storeFactory() {
3534
modules: {
3635
stack: stackModuleFactory(),
3736
card: cardModuleFactory(),
38-
trashbin,
3937
attachment,
4038
overview,
4139
},

src/store/stack.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import Vue from 'vue'
77
import { StackApi } from './../services/StackApi.js'
88
import applyOrderToArray from './../helpers/applyOrderToArray.js'
9+
import { useTrashbinStore } from '../stores/trashbin.js'
910

1011
const apiClient = new StackApi()
1112

@@ -108,7 +109,7 @@ export default function stackModuleFactory() {
108109
apiClient.deleteStack(stack.id, stack.boardId)
109110
.then((stack) => {
110111
commit('deleteStack', stack)
111-
commit('moveStackToTrash', stack)
112+
useTrashbinStore().moveStackToTrash(stack)
112113
})
113114
},
114115
updateStack({ commit }, stack) {

src/store/trashbin.js

Lines changed: 0 additions & 80 deletions
This file was deleted.

src/stores/trashbin.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
3+
* SPDX-License-Identifier: AGPL-3.0-or-later
4+
*/
5+
6+
import { defineStore } from 'pinia'
7+
import { StackApi } from '../services/StackApi.js'
8+
import { CardApi } from '../services/CardApi.js'
9+
10+
const stackApi = new StackApi()
11+
const cardApi = new CardApi()
12+
13+
export const useTrashbinStore = defineStore('trashbin', {
14+
state: () => ({
15+
deletedStacks: [],
16+
deletedCards: [],
17+
}),
18+
actions: {
19+
setDeletedStacks(delStacks) {
20+
this.deletedStacks = []
21+
if (delStacks.length > 0) {
22+
this.deletedStacks = delStacks
23+
}
24+
},
25+
moveStackToTrash(stack) {
26+
stack.deletedAt = Math.floor(Date.now() / 1000)
27+
this.deletedStacks.push(stack)
28+
},
29+
removeStackFromTrash(stack) {
30+
const existingIndex = this.deletedStacks.findIndex(_stack => _stack.id === stack.id)
31+
if (existingIndex !== -1) {
32+
this.deletedStacks.splice(existingIndex, 1)
33+
}
34+
},
35+
setDeletedCards(delCards) {
36+
this.deletedCards = []
37+
this.deletedCards = delCards
38+
},
39+
moveCardToTrash(card) {
40+
card.deletedAt = Math.floor(Date.now() / 1000)
41+
this.deletedCards.push(card)
42+
},
43+
removeCardFromTrash(card) {
44+
const existingIndex = this.deletedCards.findIndex(_card => _card.id === card.id)
45+
if (existingIndex !== -1) {
46+
this.deletedCards.splice(existingIndex, 1)
47+
}
48+
},
49+
fetchDeletedItems(boardId) {
50+
stackApi.deletedStacks(boardId).then((deletedStacks) => {
51+
this.setDeletedStacks(deletedStacks)
52+
})
53+
cardApi.deletedCards(boardId).then((deletedCards) => {
54+
this.setDeletedCards(deletedCards)
55+
})
56+
},
57+
stackUndoDelete(stack) {
58+
stackApi.updateStack(stack).then((restoredStack) => {
59+
this.$vuex.commit('addStack', restoredStack)
60+
this.removeStackFromTrash(restoredStack)
61+
})
62+
},
63+
cardUndoDelete(card) {
64+
cardApi.updateCard(card).then((restoredCard) => {
65+
this.removeCardFromTrash(restoredCard)
66+
this.$vuex.commit('addCard', restoredCard)
67+
})
68+
},
69+
},
70+
})

0 commit comments

Comments
 (0)