-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shop.ts
62 lines (52 loc) · 1.45 KB
/
Shop.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { createMemo } from 'solid-js';
import { createToast } from './Toast';
import { MODE, createModeStore } from './ModeStore';
import { createBookStore } from './BookStore';
import { createCartStore, itemIsValid, itemPrice } from './CartStore';
import type { Accessor } from 'solid-js';
import type { ToastState } from './Toast';
import type { Mode, ModeArgs, ModeStore } from './ModeStore';
import type { Book, BookId, BookRaw, BookStore, Fetcher } from './BookStore';
import type { CartStore, CartStorage } from './CartStore';
type Shop = {
loading: Accessor<boolean>;
toast: Accessor<ToastState>;
currentBook: Accessor<Book | undefined>;
mode: ModeStore;
books: BookStore;
cart: CartStore;
};
function createShop(fetcher: Fetcher, storage?: CartStorage): Shop {
const [toast, send] = createToast(2500, 500);
const mode = createModeStore();
const [storeMode] = mode;
const books = createBookStore(fetcher);
const [storeBooks] = books;
const currentBook = createMemo(() => {
const id = storeMode.id;
return id ? storeBooks[id] : undefined;
});
const cart = createCartStore(books[1].loading, storeBooks, storage, send);
const loading = cart[1].loading;
return {
loading,
toast,
currentBook,
mode,
books,
cart,
};
}
export { MODE, createShop, itemIsValid, itemPrice };
export type {
BookId,
BookRaw,
BookStore,
CartStore,
CartStorage,
Fetcher,
Mode,
ModeArgs,
Shop,
ToastState,
};