1- import { useIdentityAffectationStore } from "~/stores/identityAffectation"
21import { IdentityState , useIdentityStates } from "./useIdentityStates"
3- import { useIdentityStateStore } from "~/stores/identityState"
2+ import qs from 'qs'
43
5- const { getStateInfos } = useIdentityStates ( )
4+ const { getStateBadge } = useIdentityStates ( )
65const config = useAppConfig ( )
76
87type Badge = {
9- name : string
108 color : string
11- value : string
9+ name ?: string
10+ value ?: string
1211}
1312
1413type Menu = {
@@ -17,19 +16,19 @@ type Menu = {
1716 path : string
1817 color : string
1918 part : string
20- badgeValue ?: string
2119 badge ?: Badge
22- hideInMenuBar ?:boolean
20+ hideInMenuBar ?: boolean
2321}
2422
25- function useMenu ( identityStateStore , identityAffectationStore ) {
23+ function useMenu ( identityStateStore ) {
2624 const menuParts = ref ( [ 'Données' , 'Listes' , 'Affectations' , 'Etats' , 'Activation' ] )
27- const menus = ref ( [
25+ const menus = ref < Menu [ ] > ( [
2826 {
2927 icon : 'mdi-account' ,
3028 label : 'Liste des identités' ,
31- path : '/identities?sort[metadata.lastUpdatedAt]=desc&skip=0' ,
29+ path : '/identities?sort[metadata.lastUpdatedAt]=desc&skip=0&filters ' ,
3230 color : 'primary' ,
31+ badge : { color : 'primary' } ,
3332 part : 'Données' ,
3433 hideInMenuBar : false
3534 } ,
@@ -69,44 +68,44 @@ function useMenu(identityStateStore, identityAffectationStore) {
6968 label : 'A valider' ,
7069 path : `/identities?sort[metadata.lastUpdatedAt]=desc&skip=0&filters[@state][]=${ IdentityState . TO_VALIDATE } ` ,
7170 color : 'primary' ,
71+ badge : getStateBadge ( IdentityState . TO_VALIDATE ) ,
7272 part : 'Etats' ,
73- badgeValue : 'TO_VALIDATE' ,
7473 hideInMenuBar : false
7574 } ,
7675 {
7776 icon : 'mdi-account-alert' ,
7877 label : 'A compléter' ,
7978 path : `/identities?sort[metadata.lastUpdatedAt]=desc&skip=0&filters[@state][]=${ IdentityState . TO_COMPLETE } ` ,
8079 color : 'primary' ,
80+ badge : getStateBadge ( IdentityState . TO_COMPLETE ) ,
8181 part : 'Etats' ,
82- badgeValue : 'TO_COMPLETE' ,
8382 hideInMenuBar : false
8483 } ,
8584 {
8685 icon : 'mdi-sync' ,
8786 label : 'A synchroniser' ,
8887 path : `/identities/readonly?sort[metadata.lastUpdatedAt]=desc&skip=0&filters[@state][]=${ IdentityState . TO_SYNC } ` ,
8988 color : 'primary' ,
89+ badge : getStateBadge ( IdentityState . TO_SYNC ) ,
9090 part : 'Etats' ,
91- badgeValue : 'TO_SYNC' ,
9291 hideInMenuBar : false
9392 } ,
9493 {
9594 icon : 'mdi-loading' ,
9695 label : 'En cours de synchro.' ,
9796 path : `/identities/readonly?sort[metadata.lastUpdatedAt]=desc&skip=0&filters[@state][]=${ IdentityState . PROCESSING } ` ,
9897 color : 'primary' ,
98+ badge : getStateBadge ( IdentityState . PROCESSING ) ,
9999 part : 'Etats' ,
100- badgeValue : 'PROCESSING' ,
101100 hideInMenuBar : false
102101 } ,
103102 {
104103 icon : 'mdi-check' ,
105104 label : 'Synchronisées' ,
106105 path : `/identities?sort[metadata.lastUpdatedAt]=desc&skip=0&filters[@state][]=${ IdentityState . SYNCED } ` ,
106+ badge : getStateBadge ( IdentityState . SYNCED ) ,
107107 color : 'primary' ,
108108 part : 'Etats' ,
109- badgeValue : 'SYNCED' ,
110109 hideInMenuBar : false
111110 } ,
112111 {
@@ -123,8 +122,8 @@ function useMenu(identityStateStore, identityAffectationStore) {
123122 label : 'En erreur' ,
124123 path : `/identities?sort[metadata.lastUpdatedAt]=desc&skip=0&filters[@state][]=${ IdentityState . ON_ERROR } ` ,
125124 color : 'primary' ,
125+ badge : getStateBadge ( IdentityState . ON_ERROR ) ,
126126 part : 'Etats' ,
127- badgeValue : 'ON_ERROR' ,
128127 hideInMenuBar : false
129128 } ,
130129 {
@@ -133,6 +132,7 @@ function useMenu(identityStateStore, identityAffectationStore) {
133132 path : '/identities?limit=10&skip=0&filters[&filters[%23initState]=0&sort[metadata.lastUpdatedAt]=desc' ,
134133 color : 'negative' ,
135134 part : 'Activation' ,
135+ badge : { color : 'grey' } ,
136136 hideInMenuBar : false
137137 } ,
138138 {
@@ -141,6 +141,7 @@ function useMenu(identityStateStore, identityAffectationStore) {
141141 path : '/identities?limit=10&skip=0&filters[&filters[%23initState]=1&sort[metadata.lastUpdatedAt]=desc' ,
142142 color : 'warning' ,
143143 part : 'Activation' ,
144+ badge : { color : 'grey' } ,
144145 hideInMenuBar : false
145146 } ,
146147 {
@@ -149,6 +150,7 @@ function useMenu(identityStateStore, identityAffectationStore) {
149150 path : '/identities?limit=10&skip=0&filters[&filters[%23initState]=2&sort[metadata.lastUpdatedAt]=desc' ,
150151 color : 'positive' ,
151152 part : 'Activation' ,
153+ badge : { color : 'grey' } ,
152154 hideInMenuBar : false
153155 } ,
154156 {
@@ -161,41 +163,60 @@ function useMenu(identityStateStore, identityAffectationStore) {
161163 } ,
162164 ] )
163165
164- const badgesValues = ref ( {
165- TO_VALIDATE : computed ( ( ) => ( identityStateStore . getToValidateCount > 9999 ? '9999+' : identityStateStore . getToValidateCount ) ) ,
166- TO_COMPLETE : computed ( ( ) => ( identityStateStore . getToCompleteCount > 9999 ? '9999+' : identityStateStore . getToCompleteCount ) ) ,
167- TO_SYNC : computed ( ( ) => ( identityStateStore . getToSyncCount > 9999 ? '9999+' : identityStateStore . getToSyncCount ) ) ,
168- PROCESSING : computed ( ( ) => ( identityStateStore . getProcessingCount > 9999 ? '9999+' : identityStateStore . getProcessingCount ) ) ,
169- SYNCED : computed ( ( ) => ( identityStateStore . getSyncedCount > 9999 ? '9999+' : identityStateStore . getSyncedCount ) ) ,
170- ON_ERROR : computed ( ( ) => ( identityStateStore . getOnErrorCount > 9999 ? '9999+' : identityStateStore . getOnErrorCount ) ) ,
171- ETD : computed ( ( ) => ( identityAffectationStore . getEtdCount > 9999 ? '9999+' : identityAffectationStore . getEtdCount ) ) ,
172- ADM : computed ( ( ) => ( identityAffectationStore . getAdmCount > 9999 ? '9999+' : identityAffectationStore . getAdmCount ) ) ,
173- ESN : computed ( ( ) => ( identityAffectationStore . getEsnCount > 9999 ? '9999+' : identityAffectationStore . getEsnCount ) ) ,
174- } )
166+ function normalizeLabel ( label : string ) : string {
167+ return label . toLowerCase ( ) . normalize ( "NFD" ) . replace ( / [ \u0300 - \u036f ] / g, '' ) . replace ( / / g, '_' ) . replace ( / \. / g, '' )
168+ }
175169
176170 function getMenu ( ) : Menu [ ] {
177- const menuWithBadgeValue : Menu [ ] = menus . value . reduce ( ( acc : Menu [ ] , menu ) => {
178- const badgeType = menu . badgeValue ? menu . badgeValue : 'UNKNOWN'
179- const stateInfo = getStateInfos ( IdentityState [ badgeType ] )
180- const badgeInfos : Badge = {
181- name : stateInfo . name ,
182- color : stateInfo . color === 'grey' ? 'primary' : stateInfo . color ,
183- value : badgesValues . value [ badgeType ] || '0' ,
184- }
171+ const menuList : Menu [ ] = menus . value . reduce ( ( acc : Menu [ ] , menu ) => {
172+ const label = normalizeLabel ( menu . label )
173+ const stateValue = identityStateStore . getStateValue ( label )
174+ const value = stateValue > 9999 ? '9999+' : stateValue ?. toString ( ) || '0'
175+
176+ console . log ( 'stateValue' , label , stateValue , value )
177+
178+ // const badgeType = menu.badgeValue ? menu.badgeValue : 'UNKNOWN'
179+ // const stateInfo = getStateInfos(IdentityState[badgeType])
180+ // const badge: Badge = {
181+
182+ // // name: label,
183+ // // name: stateInfo.name,
184+ // // color: 'red',
185+ // // color: stateInfo.color === 'grey' ? 'primary' : stateInfo.color,
186+ // value,
187+ // }
185188 acc . push ( {
186189 ...menu ,
187- badge : menu . badgeValue ? badgeInfos : undefined
190+ badge : menu . badge ? < Badge > {
191+ ...menu ?. badge ,
192+ value,
193+ } : undefined ,
188194 } )
189195 return acc
190196 } , [ ] )
191- return menuWithBadgeValue
197+ return menuList
192198 }
193199
194200 function getMenuByPart ( part : string ) : Menu [ ] {
195201 return getMenu ( ) . filter ( ( menu ) => menu . part === part )
196202 }
197203
198- return { getMenu, menuParts, badgesValues, getMenuByPart }
204+ async function initialize ( ) {
205+ const filters = { }
206+ for ( const menu of menus . value ) {
207+ if ( menu . path && menu . badge ) {
208+ const label = normalizeLabel ( menu . label )
209+ const params = new URL ( window . location . host + menu . path ) . searchParams
210+ const queryString = qs . parse ( params . toString ( ) )
211+
212+ filters [ label ] = queryString [ 'filters' ]
213+ }
214+ }
215+
216+ await identityStateStore . initialize ( filters )
217+ }
218+
219+ return { getMenu, menuParts, getMenuByPart, initialize }
199220}
200221
201222export { useMenu }
0 commit comments