@@ -2,9 +2,13 @@ import { PaginationState, SortingState } from '@tanstack/react-table';
22import { FC , useCallback , useEffect , useMemo , useState } from 'react' ;
33import { useTranslation } from 'react-i18next' ;
44import { useNavigate } from 'react-router-dom' ;
5+ import { ROUTES } from 'resources/routes-constants' ;
56import useServiceListStore from 'store/services.store' ;
7+ import useToastStore from 'store/toasts.store' ;
8+ import { ActivationBlocker } from 'types/activation-blocker' ;
9+ import { getServiceStateLabelType } from 'utils/service-state-label' ;
610
7- import { Button , Card , Modal , Track } from '..' ;
11+ import { Button , Card , Label , Modal , Track } from '..' ;
812import DataTable from '../DataTable' ;
913import { getColumns } from './columns' ;
1014
@@ -27,6 +31,8 @@ const getStoredPageSize = (): number => {
2731const ServicesTable : FC < ServicesTableProps > = ( { isCommon = false } ) => {
2832 const { t } = useTranslation ( ) ;
2933 const [ isDeletePopupVisible , setIsDeletePopupVisible ] = useState ( false ) ;
34+ const [ activationBlockers , setActivationBlockers ] = useState < ActivationBlocker [ ] | null > ( null ) ;
35+ const [ serviceIdBeingChecked , setServiceIdBeingChecked ] = useState < string | null > ( null ) ;
3036 const services = useServiceListStore ( ( state ) => ( isCommon ? state . commonServices : state . notCommonServices ) ) ;
3137 const navigate = useNavigate ( ) ;
3238 const [ pagination , setPagination ] = useState < PaginationState > ( {
@@ -55,7 +61,7 @@ const ServicesTable: FC<ServicesTableProps> = ({ isCommon = false }) => {
5561
5662 const changeServiceState = useCallback (
5763 ( activate : boolean = false , draft : boolean = false ) => {
58- useServiceListStore
64+ return useServiceListStore
5965 . getState ( )
6066 . changeServiceState (
6167 ( ) => { } ,
@@ -66,24 +72,44 @@ const ServicesTable: FC<ServicesTableProps> = ({ isCommon = false }) => {
6672 pagination ,
6773 sorting ,
6874 )
69- . then ( ( ) => { } )
7075 . catch ( ( e ) => {
7176 console . error ( e ) ;
7277 } ) ;
7378 } ,
7479 [ t , pagination , sorting ] ,
7580 ) ;
7681
82+ const attemptActivation = useCallback ( async ( ) => {
83+ const service = useServiceListStore . getState ( ) . selectedService ;
84+ if ( ! service || serviceIdBeingChecked ) return ;
85+
86+ setServiceIdBeingChecked ( service . serviceId ) ;
87+ try {
88+ const blockers = await useServiceListStore . getState ( ) . loadActivationBlockers ( service ) ;
89+ if ( blockers . length > 0 ) {
90+ setActivationBlockers ( blockers ) ;
91+ return ;
92+ }
93+ await changeServiceState ( true ) ;
94+ } catch ( e ) {
95+ console . error ( e ) ;
96+ useToastStore . getState ( ) . error ( { title : t ( 'overview.service.toast.failed.state' ) } ) ;
97+ } finally {
98+ setServiceIdBeingChecked ( null ) ;
99+ }
100+ } , [ changeServiceState , serviceIdBeingChecked , t ] ) ;
101+
77102 const columns = useMemo ( ( ) => {
78103 return getColumns ( {
79104 isCommon,
80105 navigate,
81106 hideDeletePopup : ( ) => setIsDeletePopupVisible ( true ) ,
82107 showReadyPopup : ( ) => {
83- changeServiceState ( true ) ;
108+ void attemptActivation ( ) ;
84109 } ,
110+ serviceIdBeingChecked,
85111 } ) ;
86- } , [ isCommon , changeServiceState , navigate ] ) ;
112+ } , [ isCommon , attemptActivation , navigate , serviceIdBeingChecked ] ) ;
87113
88114 const deleteSelectedService = ( ) => {
89115 setIsDeletingService ( true ) ;
@@ -122,6 +148,51 @@ const ServicesTable: FC<ServicesTableProps> = ({ isCommon = false }) => {
122148 </ Track >
123149 </ Modal >
124150 ) }
151+ { activationBlockers && activationBlockers . length > 0 && (
152+ < Modal
153+ title = { t ( 'overview.popup.activationBlocked.title' ) }
154+ onClose = { ( ) => setActivationBlockers ( null ) }
155+ footer = {
156+ < Button appearance = "primary" onClick = { ( ) => setActivationBlockers ( null ) } >
157+ { t ( 'global.continue' ) }
158+ </ Button >
159+ }
160+ >
161+ < ul className = "activation-blockers-list" >
162+ { activationBlockers . map ( ( blocker ) => {
163+ const content = (
164+ < Track justify = "between" >
165+ < strong > { blocker . name } </ strong >
166+ < Label type = { getServiceStateLabelType ( blocker . state ) } >
167+ { t ( `overview.service.states.${ blocker . state } ` ) }
168+ </ Label >
169+ </ Track >
170+ ) ;
171+
172+ return (
173+ < li key = { blocker . serviceId } >
174+ { blocker . state === 'missing' ? (
175+ < div className = "activation-blockers-list__item activation-blockers-list__item--static" >
176+ { content }
177+ </ div >
178+ ) : (
179+ < button
180+ type = "button"
181+ className = "activation-blockers-list__item"
182+ onClick = { ( ) => {
183+ setActivationBlockers ( null ) ;
184+ navigate ( ROUTES . replaceWithId ( ROUTES . EDITSERVICE_ROUTE , blocker . serviceId ) ) ;
185+ } }
186+ >
187+ { content }
188+ </ button >
189+ ) }
190+ </ li >
191+ ) ;
192+ } ) }
193+ </ ul >
194+ </ Modal >
195+ ) }
125196 < DataTable
126197 sortable
127198 data = { services }
0 commit comments