Fix changing items
This commit is contained in:
21
src/store.js
21
src/store.js
@@ -1,16 +1,21 @@
|
||||
import { writable } from 'svelte/store';
|
||||
import { v4 as uuid } from 'uuid'
|
||||
import { getNextItemIndexFn, getPrevItemIndexFn } from './utils/item-index'
|
||||
|
||||
|
||||
function createStore() {
|
||||
const { subscribe, set, update } = writable({
|
||||
items: [],
|
||||
currentItemId: null
|
||||
currentItemId: null,
|
||||
currentItemIndex: null,
|
||||
action: 'next'
|
||||
});
|
||||
|
||||
function setItem(id) {
|
||||
function setItem(id = uuid()) {
|
||||
update(store => ({
|
||||
...store,
|
||||
currentItemId: id,
|
||||
currentItemIndex: 0, // store.items.length - 1, TODO: use as a param
|
||||
items: [
|
||||
...store.items,
|
||||
id
|
||||
@@ -27,22 +32,26 @@ function createStore() {
|
||||
|
||||
function next({ infinite }) {
|
||||
update(store => {
|
||||
const currentItemIndex = store.items.findIndex(item => item === store.currentItemId)
|
||||
const currentItemIndex = store.currentItemIndex // store.items.findIndex(item => item === store.currentItemId)
|
||||
const newCurrentItemIndex = getNextItemIndexFn(infinite)(currentItemIndex, store.items)
|
||||
return {
|
||||
...store,
|
||||
currentItemId: store.items[newCurrentItemIndex]
|
||||
currentItemId: store.items[newCurrentItemIndex],
|
||||
currentItemIndex: newCurrentItemIndex,
|
||||
action: 'next'
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function prev({ infinite }) {
|
||||
update(store => {
|
||||
const currentItemIndex = store.items.findIndex(item => item === store.currentItemId)
|
||||
const currentItemIndex = store.currentItemIndex // store.items.findIndex(item => item === store.currentItemId)
|
||||
const newCurrentItemIndex = getPrevItemIndexFn(infinite)(currentItemIndex, store.items)
|
||||
return {
|
||||
...store,
|
||||
currentItemId: store.items[newCurrentItemIndex]
|
||||
currentItemId: store.items[newCurrentItemIndex],
|
||||
currentItemIndex: newCurrentItemIndex,
|
||||
action: 'prev'
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user