1+ import { UserNotification } from '@uzh-bf/design-system'
12import { useTranslations } from 'next-intl'
23import { Dispatch , SetStateAction , useEffect , useState } from 'react'
34import { trpc } from '../../../lib/trpc'
@@ -28,17 +29,20 @@ function ElementDeletionModal({
2829 } )
2930
3031 // fetch element information
31- const { data, isLoading : queryLoading } = trpc . element . summary . useQuery (
32- { id : elementId } ,
33- { enabled : ! ! elementId }
32+ const {
33+ data,
34+ error : summaryError ,
35+ isLoading : queryLoading ,
36+ } = trpc . element . summary . useQuery ( { id : elementId } , { enabled : ! ! elementId } )
37+ const summary = data ?. elementSummary
38+ const initialSummaryLoading = queryLoading && ! summary
39+ const summaryUnavailable = Boolean (
40+ ( summaryError || ! queryLoading ) && ! summary
3441 )
3542
36- const notApplicableShared =
37- ! ! data ?. elementSummary && ! data . elementSummary . sharedElementActivityUse
38- const notApplicableDerived =
39- ! ! data ?. elementSummary && ! data . elementSummary . retainsDerivedAccess
40- const notApplicableResources =
41- ! ! data ?. elementSummary && ! data . elementSummary . derivedAccessToResources
43+ const notApplicableShared = ! ! summary && ! summary . sharedElementActivityUse
44+ const notApplicableDerived = ! ! summary && ! summary . retainsDerivedAccess
45+ const notApplicableResources = ! ! summary && ! summary . derivedAccessToResources
4246
4347 // on modal opening, reset the confirmation state
4448 useEffect ( ( ) => {
@@ -62,7 +66,7 @@ function ElementDeletionModal({
6266 confirmationType = "delete"
6367 onClose = { ( ) => setModalOpen ( false ) }
6468 title = { t ( 'manage.questionPool.deleteElement' ) }
65- loading = { queryLoading }
69+ loading = { initialSummaryLoading }
6670 message = { t . rich ( 'manage.questionPool.confirmDeletion' , {
6771 name : title ,
6872 b : ( content ) => < b > { content } </ b > ,
@@ -77,79 +81,90 @@ function ElementDeletionModal({
7781 setModalOpen ( false )
7882 } }
7983 submitting = { deleteElement . isLoading }
80- confirmations = { confirmations }
81- confirmationsInitializing = { false }
84+ confirmations = { summary ? confirmations : { summaryLoaded : false } }
85+ confirmationsInitializing = { initialSummaryLoading }
8286 >
83- < div className = "flex flex-col gap-2" >
84- < ConfirmationItem
85- confirmationType = "delete"
86- label = { t ( 'manage.questionPool.elementDeletionFinal' ) }
87- onClick = { ( ) => {
88- setConfirmations ( ( prev ) => ( {
89- ...prev ,
90- actionFinal : true ,
91- } ) )
92- } }
93- confirmed = { confirmations . actionFinal }
94- notApplicable = { false }
95- data = { { cy : 'confirm-deletion-final' } }
87+ { summaryUnavailable ? (
88+ < UserNotification
89+ type = "error"
90+ message = { t ( 'shared.generic.systemError' ) }
9691 />
97- < ConfirmationItem
98- confirmationType = "delete"
99- label = {
100- notApplicableShared
101- ? t ( 'manage.questionPool.elementDeletionOtherUsersNotApplicable' )
102- : t ( 'manage.questionPool.elementDeletionOtherUsers' )
103- }
104- onClick = { ( ) => {
105- setConfirmations ( ( prev ) => ( {
106- ...prev ,
107- otherUsersAccess : true ,
108- } ) )
109- } }
110- confirmed = { confirmations . otherUsersAccess }
111- notApplicable = { notApplicableShared }
112- data = { { cy : 'confirm-other-users-access' } }
113- />
114- < ConfirmationItem
115- confirmationType = "delete"
116- label = {
117- notApplicableDerived
118- ? t (
119- 'manage.questionPool.elementDeletionDerivedAccessNotApplicable'
120- )
121- : t ( 'manage.questionPool.elementDeletionDerivedAccessHint' )
122- }
123- onClick = { ( ) => {
124- setConfirmations ( ( prev ) => ( {
125- ...prev ,
126- derivedAccessHint : true ,
127- } ) )
128- } }
129- confirmed = { confirmations . derivedAccessHint }
130- notApplicable = { notApplicableDerived }
131- data = { { cy : 'confirm-derived-access' } }
132- />
133- < ConfirmationItem
134- confirmationType = "delete"
135- label = {
136- notApplicableResources
137- ? t (
138- 'manage.questionPool.elementDeletionDependencyAccessNotApplicable'
139- )
140- : t ( 'manage.questionPool.elementDeletionDependencyAccess' )
141- }
142- onClick = { ( ) => {
143- setConfirmations ( ( prev ) => ( {
144- ...prev ,
145- dependencyAccess : true ,
146- } ) )
147- } }
148- confirmed = { confirmations . dependencyAccess }
149- notApplicable = { notApplicableResources }
150- data = { { cy : 'confirm-dependency-access' } }
151- />
152- </ div >
92+ ) : null }
93+
94+ { summary ? (
95+ < div className = "flex flex-col gap-2" >
96+ < ConfirmationItem
97+ confirmationType = "delete"
98+ label = { t ( 'manage.questionPool.elementDeletionFinal' ) }
99+ onClick = { ( ) => {
100+ setConfirmations ( ( prev ) => ( {
101+ ...prev ,
102+ actionFinal : true ,
103+ } ) )
104+ } }
105+ confirmed = { confirmations . actionFinal }
106+ notApplicable = { false }
107+ data = { { cy : 'confirm-deletion-final' } }
108+ />
109+ < ConfirmationItem
110+ confirmationType = "delete"
111+ label = {
112+ notApplicableShared
113+ ? t (
114+ 'manage.questionPool.elementDeletionOtherUsersNotApplicable'
115+ )
116+ : t ( 'manage.questionPool.elementDeletionOtherUsers' )
117+ }
118+ onClick = { ( ) => {
119+ setConfirmations ( ( prev ) => ( {
120+ ...prev ,
121+ otherUsersAccess : true ,
122+ } ) )
123+ } }
124+ confirmed = { confirmations . otherUsersAccess }
125+ notApplicable = { notApplicableShared }
126+ data = { { cy : 'confirm-other-users-access' } }
127+ />
128+ < ConfirmationItem
129+ confirmationType = "delete"
130+ label = {
131+ notApplicableDerived
132+ ? t (
133+ 'manage.questionPool.elementDeletionDerivedAccessNotApplicable'
134+ )
135+ : t ( 'manage.questionPool.elementDeletionDerivedAccessHint' )
136+ }
137+ onClick = { ( ) => {
138+ setConfirmations ( ( prev ) => ( {
139+ ...prev ,
140+ derivedAccessHint : true ,
141+ } ) )
142+ } }
143+ confirmed = { confirmations . derivedAccessHint }
144+ notApplicable = { notApplicableDerived }
145+ data = { { cy : 'confirm-derived-access' } }
146+ />
147+ < ConfirmationItem
148+ confirmationType = "delete"
149+ label = {
150+ notApplicableResources
151+ ? t (
152+ 'manage.questionPool.elementDeletionDependencyAccessNotApplicable'
153+ )
154+ : t ( 'manage.questionPool.elementDeletionDependencyAccess' )
155+ }
156+ onClick = { ( ) => {
157+ setConfirmations ( ( prev ) => ( {
158+ ...prev ,
159+ dependencyAccess : true ,
160+ } ) )
161+ } }
162+ confirmed = { confirmations . dependencyAccess }
163+ notApplicable = { notApplicableResources }
164+ data = { { cy : 'confirm-dependency-access' } }
165+ />
166+ </ div >
167+ ) : null }
153168 </ ActivityConfirmationModal >
154169 )
155170}
0 commit comments