11import { defineStore } from 'pinia' ;
22
3- function apiCacheName ( apiUrl , params ) {
4- return apiUrl
5- return apiUrl + new URLSearchParams ( params ) . toString ( )
6- }
7-
83export function authHeaders ( ) {
94 const token = useCookie ( 'token' ) . value
105 return {
@@ -14,7 +9,6 @@ export function authHeaders() {
149
1510export const useItemsStore = defineStore ( 'items' , {
1611 state : ( ) => ( {
17- items : { } ,
1812 paths : { } ,
1913 filters : { } ,
2014 schema : undefined ,
@@ -27,12 +21,12 @@ export const useItemsStore = defineStore('items', {
2721 currentPage : 1 ,
2822 ordering : undefined ,
2923 itemsChanged : 0 ,
24+ total : 0
3025 } ) ,
3126 actions : {
32- async retrieveItems ( apiUrl = undefined , params = { } , cache = true , one = false ) {
27+ async retrieveItems ( apiUrl = undefined , params = { } , one = false ) {
3328 const { $axios} = useNuxtApp ( ) ;
3429
35- const cacheName = apiCacheName ( apiUrl , params )
3630 // Would be nice to amke ordering dynamic as a parameter, perhaps one day
3731 if ( ! ( "limit" in params ) )
3832 params . limit = this . pageSize
@@ -52,18 +46,10 @@ export const useItemsStore = defineStore('items', {
5246 if ( Array . isArray ( res ) ) { // When the endpoint is not paginated
5347 res = { results : res }
5448 }
55- if ( ! cache ) {
56- if ( one ) {
57- return res . results [ 0 ]
58- }
59- return res
60- }
61-
62- this . items [ cacheName ] = res
6349 if ( one ) {
64- return this . items [ cacheName ] [ 0 ]
50+ return res . results [ 0 ]
6551 }
66- return this . items [ cacheName ]
52+ return res
6753 } ,
6854 async deleteItem ( apiUrl , id , refresh = true ) {
6955 const { $axios} = useNuxtApp ( ) ;
@@ -90,22 +76,18 @@ export const useItemsStore = defineStore('items', {
9076 return await this . _resolveRefs ( this . schema [ schemaName ] )
9177 return this . schema [ schemaName ]
9278 } ,
93- async getNextItem ( apiUrl , itemId , direction = 1 , params = { } , force = false ) {
94- const { $axios} = useNuxtApp ( ) ;
95-
96- const cacheName = apiCacheName ( apiUrl , params )
97-
98- if ( force || ! this . items [ cacheName ] ) {
99- await this . retrieveItems ( apiUrl )
79+ async getNextItem ( items , apiUrl , itemId , direction = 1 , params = { } , force = false ) {
80+ if ( force || ! items ) {
81+ items = await this . retrieveItems ( apiUrl )
10082 }
10183 // It takes the next item after currentItem
102- let index = this . items [ cacheName ] . results . findIndex ( item => item . id === itemId )
84+ let index = items . results . findIndex ( item => item . id === itemId )
10385 if ( index === - 1 )
10486 return undefined
10587 index += direction
106- if ( index < 0 || index >= this . items [ cacheName ] . results . length )
88+ if ( index < 0 || index >= items . results . length )
10789 return undefined
108- return this . items [ cacheName ] . results [ index ]
90+ return items . results [ index ]
10991 } ,
11092 async _resolveRefs ( schema ) {
11193 if ( ! schema . properties && schema . oneOf ) {
0 commit comments