1
+ import { defineStore , skipHydrate , acceptHMRUpdate } from "pinia" ;
2
+ import { nanoid } from "nanoid" ;
3
+
4
+ import type { Spot , Lineup } from '~~/types' ;
5
+
6
+ // NOTE: This requires `ssr: false` in `nuxt.config.*` because the server will try to
7
+ // serialize our computed state properties to JSON, and they will fail because
8
+ // functions can't be serialized. (With SSR turned on but with the experimental
9
+ // `renderJsonPayloads: false` flag, it may work.)
10
+ //
11
+ // See: [The Pinia guide](https://pinia.vuejs.org/core-concepts/)
12
+ // https://github.com/vuejs/pinia/issues/447#issuecomment-1455285437
13
+ // https://github.com/nuxt/nuxt/issues/20889
14
+
15
+ export const useAppSettingsStoreClientOnly = defineStore ( 'AppSettingsStoreClientOnly' , {
16
+ state : ( ) => {
17
+ const _colorMode = useColorMode ( ) ;
18
+
19
+ const _lineup = useLocalStorage < Lineup > ( 'AppSettingsStoreClientOnly:lineup' , {
20
+ id : nanoid ( ) ,
21
+
22
+ teamName : '' ,
23
+ jerseyColor : 'f47373' ,
24
+ jerseyTextColor : '000000' ,
25
+
26
+ isLocked : false ,
27
+ spots : [ ] ,
28
+ } , {
29
+ mergeDefaults : true
30
+ } ) ;
31
+
32
+ // NOTE: We provide setters so we can persist to storage.
33
+ // Source: https://github.com/vuejs/pinia/issues/447#issuecomment-1455285437
34
+
35
+ const colorMode = computed < string > ( {
36
+ get : ( ) => {
37
+ return _colorMode . preference ;
38
+ } ,
39
+ set : ( v ) => {
40
+ _colorMode . preference = v ;
41
+ } ,
42
+ } ) ;
43
+
44
+ const teamName = computed < string > ( {
45
+ get : ( ) => {
46
+ return _lineup . value . teamName ;
47
+ } ,
48
+ set : ( v ) => {
49
+ _lineup . value . teamName = v ;
50
+ } ,
51
+ } ) ;
52
+
53
+ const jerseyColor = computed < string > ( {
54
+ get : ( ) => {
55
+ return _lineup . value . jerseyColor ;
56
+ } ,
57
+ set : ( v ) => {
58
+ _lineup . value . jerseyColor = v ;
59
+ } ,
60
+ } ) ;
61
+
62
+ const jerseyTextColor = computed < string > ( {
63
+ get : ( ) => {
64
+ return _lineup . value . jerseyTextColor ;
65
+ } ,
66
+ set : ( v ) => {
67
+ _lineup . value . jerseyTextColor = v ;
68
+ } ,
69
+ } ) ;
70
+
71
+ const isLocked = computed < boolean > ( {
72
+ get : ( ) => {
73
+ return _lineup . value . isLocked ;
74
+ } ,
75
+ set : ( v ) => {
76
+ _lineup . value . isLocked = v ;
77
+ } ,
78
+ } ) ;
79
+
80
+ const spots = computed < Spot [ ] > ( {
81
+ get : ( ) => {
82
+ return _lineup . value . spots ;
83
+ } ,
84
+ set : ( v ) => {
85
+ _lineup . value . spots = v ;
86
+ } ,
87
+ } ) ;
88
+
89
+ function $reset ( ) {
90
+ //TODO
91
+ }
92
+
93
+ return {
94
+ colorMode : skipHydrate ( colorMode ) ,
95
+
96
+ teamName : skipHydrate ( teamName ) ,
97
+ jerseyColor : skipHydrate ( jerseyColor ) ,
98
+ jerseyTextColor : skipHydrate ( jerseyTextColor ) ,
99
+
100
+ isLocked : skipHydrate ( isLocked ) ,
101
+ spots : skipHydrate ( spots ) ,
102
+
103
+ $reset,
104
+ } ;
105
+ } ,
106
+ actions : {
107
+ addSpot ( spot : Spot ) {
108
+ if ( ! this . isLocked ) {
109
+ this . spots . push ( { ...spot } ) ;
110
+ }
111
+ } ,
112
+ removeSpot ( playerId : string ) {
113
+ this . spots = this . spots . filter ( s => s . player . id !== playerId )
114
+ } ,
115
+ } ,
116
+ // getters: {
117
+
118
+ // }
119
+ } ) ;
120
+
121
+ if ( import . meta. hot ) {
122
+ import . meta. hot . accept ( acceptHMRUpdate ( useAppSettingsStoreClientOnly , import . meta. hot ) ) ;
123
+ }
0 commit comments