Plan stuff. Log stuff.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.2 KiB

  1. import { writable } from "svelte/store";
  2. import slApi from "../api/stufflog";
  3. import stufflogStore from "./stufflog";
  4. function createItemStore() {
  5. const {set, update, subscribe} = writable([])
  6. return {
  7. subscribe,
  8. listItems() {
  9. return slApi.listItems().then(items => {
  10. set(items)
  11. console.log(items)
  12. return items
  13. })
  14. },
  15. findItem(id) {
  16. return slApi.getItem(id).then(item => {
  17. update(s => replaceItem(s, item))
  18. return item
  19. })
  20. },
  21. createItem(item) {
  22. return slApi.postItem(item).then(item => {
  23. update(s => replaceItem(s, item))
  24. return item
  25. })
  26. },
  27. updateItem(id, ...updates) {
  28. return slApi.updateItem(id, ...updates).then(item => {
  29. update(s => replaceItem(s, item))
  30. return item
  31. })
  32. },
  33. deleteItem(id) {
  34. return slApi.deleteItem(id).then(item => {
  35. update(s => s.items.filter(i => i.id !== item.id))
  36. return item
  37. })
  38. },
  39. }
  40. }
  41. function replaceItem(s, item) {
  42. return ([
  43. ...s.items.filter(i => i.id !== item.id),
  44. item
  45. ].sort((a, b) => a.name.localeCompare(b.name)))
  46. }
  47. export default createItemStore();