Skip to content

Commit 7b3351f

Browse files
authored
MMT-4080: As a MMT user, if I remove all selected collections and save, it should not default to all collections. (#1408)
* MMT-4080: As a MMT user, if I remove all selected collections and save, it should not default to all collections.
1 parent 93d0f1e commit 7b3351f

File tree

4 files changed

+1595
-725
lines changed

4 files changed

+1595
-725
lines changed

static/src/js/components/PermissionForm/PermissionForm.jsx

Lines changed: 152 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import GridLayout from '@/js/components/GridLayout/GridLayout'
4949
import GroupPermissionSelect from '@/js/components/GroupPermissionSelect/GroupPermissionSelect'
5050
import KeywordPicker from '@/js/components/KeywordPicker/KeywordPicker'
5151
import validGroupItems from '@/js/utils/validGroupItems'
52+
import CustomModal from '@/js/components/CustomModal/CustomModal'
5253

5354
/**
5455
* Validates the form data for the access constraints and temporal constraints.
@@ -167,14 +168,25 @@ const validate = (formData, errors) => {
167168
*/
168169
const PermissionForm = ({ selectedCollectionsPageSize }) => {
169170
const {
170-
draft,
171-
originalDraft,
172-
setDraft,
173-
setOriginalDraft,
174171
providerId,
175172
setProviderId
176173
} = useAppContext()
177174

175+
const initFormState = {
176+
collectionSelection: {
177+
allCollection: true,
178+
selectedCollections: {}
179+
},
180+
accessPermission: {
181+
collection: true,
182+
granule: true
183+
}
184+
}
185+
186+
const [formData, setFormData] = useState({ ...initFormState })
187+
const [showConfirmModal, setShowConfirmModal] = useState(false)
188+
const [pendingFormData, setPendingFormData] = useState(null)
189+
178190
const navigate = useNavigate()
179191

180192
const { addNotification } = useNotificationsContext()
@@ -202,12 +214,11 @@ const PermissionForm = ({ selectedCollectionsPageSize }) => {
202214
}, [providerIds])
203215

204216
useEffect(() => {
205-
const { formData = {} } = draft || {}
206217
formData.providers = providerId
207-
setDraft({
208-
...draft,
209-
formData
210-
})
218+
setFormData((prevFormData) => ({
219+
...prevFormData,
220+
providers: providerId
221+
}))
211222
}, [providerId])
212223

213224
const [createAclMutation] = useMutation(CREATE_ACL)
@@ -297,8 +308,7 @@ const PermissionForm = ({ selectedCollectionsPageSize }) => {
297308

298309
useEffect(() => {
299310
if (conceptId === 'new') {
300-
setDraft({})
301-
setOriginalDraft({})
311+
setFormData({ ...initFormState })
302312
}
303313
}, [conceptId])
304314

@@ -315,22 +325,35 @@ const PermissionForm = ({ selectedCollectionsPageSize }) => {
315325
* @param {Object} formData.accessPermission - The access permissions within the form data.
316326
* @param {boolean} formData.accessPermission.granule - The flag indicating if granule access is allowed.
317327
*/
318-
const showGranuleFields = (formData) => {
319-
if (formData.accessPermission) {
328+
const updateUiSchema = (formDataObj) => {
329+
const showFields = !formDataObj.collectionSelection?.allCollection
330+
331+
if (formDataObj.accessPermission) {
320332
const newUiSchema = {
321333
...uiSchema,
322334
accessConstraintFilter: {
323335
...uiSchema.accessConstraintFilter,
336+
'ui:disabled': !showFields,
337+
collectionAccessConstraint: {
338+
...uiSchema.accessConstraintFilter.collectionAccessConstraint,
339+
'ui:disabled': !formDataObj.accessPermission?.collection
340+
},
324341
granuleAccessConstraint: {
325342
...uiSchema.accessConstraintFilter.granuleAccessConstraint,
326-
'ui:disabled': !formData.accessPermission?.granule
343+
'ui:disabled': !formDataObj.accessPermission?.granule
327344
}
328345
},
329346
temporalConstraintFilter: {
330347
...uiSchema.temporalConstraintFilter,
348+
'ui:disabled': !showFields,
349+
350+
collectionTemporalConstraint: {
351+
...uiSchema.temporalConstraintFilter.collectionTemporalConstraint,
352+
'ui:disabled': !formDataObj.accessPermission?.collection
353+
},
331354
granuleTemporalConstraint: {
332355
...uiSchema.temporalConstraintFilter.granuleTemporalConstraint,
333-
'ui:disabled': !formData.accessPermission?.granule
356+
'ui:disabled': !formDataObj.accessPermission?.granule
334357
}
335358
}
336359
}
@@ -340,6 +363,7 @@ const PermissionForm = ({ selectedCollectionsPageSize }) => {
340363

341364
// When 'data' is available, this block generates formData using information from the ACL from CMR.
342365
useEffect(() => {
366+
console.log('data is ', data)
343367
if (data) {
344368
const { acl } = data
345369
const {
@@ -355,7 +379,7 @@ const PermissionForm = ({ selectedCollectionsPageSize }) => {
355379
const selectedCollections = items?.reduce((obj, item) => ({
356380
...obj,
357381
[item.conceptId]: item
358-
}), {})
382+
}), {}) || {}
359383

360384
const searchAndOrderGroupPermission = []
361385
const searchPermission = []
@@ -438,8 +462,12 @@ const PermissionForm = ({ selectedCollectionsPageSize }) => {
438462
mask: granuleMask
439463
} = granuleTemporal || {}
440464

465+
// Check if collectionIdentifier exists and has conceptIds
466+
const hasCollectionIdentifier = catalogItemIdentity
467+
&& catalogItemIdentity.collectionIdentifier
468+
441469
// Construct formData object
442-
const formData = {
470+
const formDataObj = {
443471
accessConstraintFilter: {
444472
collectionAccessConstraint: {
445473
includeUndefined: collectionIncludeUndefined,
@@ -457,7 +485,7 @@ const PermissionForm = ({ selectedCollectionsPageSize }) => {
457485
granule: granuleApplicable
458486
},
459487
collectionSelection: {
460-
allCollection: true,
488+
allCollection: !hasCollectionIdentifier,
461489
selectedCollections
462490
},
463491
groupPermissions: {
@@ -480,29 +508,86 @@ const PermissionForm = ({ selectedCollectionsPageSize }) => {
480508
}
481509

482510
// Call the function to show/hide granule fields based on formData
483-
showGranuleFields(formData)
511+
updateUiSchema(formDataObj)
484512

485-
formData.providers = savedProviderId
513+
formDataObj.providers = savedProviderId
486514

487515
// Update the draft with formData by removing empty fields
488-
setDraft({ formData: removeEmpty(formData) })
516+
setFormData((prevFormData) => {
517+
const updatedFormData = removeEmpty(formDataObj)
518+
519+
return {
520+
...prevFormData,
521+
...updatedFormData
522+
}
523+
})
489524
}
490525
}, [data])
491526

527+
const totalSelectedCollections = () => {
528+
const {
529+
collectionSelection
530+
} = formData
531+
532+
// Extract conceptIds from selectedCollections
533+
const { selectedCollections } = collectionSelection || {}
534+
535+
let conceptIds = []
536+
if (selectedCollections) {
537+
conceptIds = Object.keys(selectedCollections).map(
538+
(key) => selectedCollections[key].conceptId
539+
)
540+
}
541+
542+
return conceptIds.length
543+
}
544+
492545
const handleChange = (event) => {
493-
const { formData } = event
546+
const { formData: formDataObj } = event
494547

495-
showGranuleFields(formData)
548+
updateUiSchema(formDataObj)
496549

497-
setProviderId(formData.providers)
550+
setProviderId(formDataObj.providers)
498551

499-
setDraft({
500-
...draft,
501-
formData: removeEmpty(formData)
502-
})
552+
// Check if allCollection has changed to true and we nned to clear selections
553+
if (formDataObj.collectionSelection?.allCollection
554+
&& !formData.collectionSelection?.allCollection
555+
&& (formDataObj.accessConstraintFilter
556+
|| formDataObj.temporalConstraintFilter || totalSelectedCollections() > 0)) {
557+
// Instead of deleting immediately, show a confirmation modal
558+
setShowConfirmModal(true)
559+
setPendingFormData(formDataObj)
560+
} else {
561+
// If not changing to allCollection: true, update formData as usual
562+
setFormData({ ...formDataObj })
563+
}
564+
}
565+
566+
const handleConfirmAllCollection = () => {
567+
// User confirmed, so now we can delete the fields
568+
const updatedFormData = { ...pendingFormData }
569+
delete updatedFormData.collectionSelection?.selectedCollections
570+
delete updatedFormData.catalogItemIdentity?.collectionIdentifier
571+
delete updatedFormData.accessConstraintFilter
572+
delete updatedFormData.temporalConstraintFilter
573+
574+
setFormData(updatedFormData)
575+
setShowConfirmModal(false)
576+
}
577+
578+
const handleCancelAllCollection = () => {
579+
const updatedFormData = { ...formData }
580+
updatedFormData.collectionSelection.allCollection = false
581+
setFormData(updatedFormData)
582+
setShowConfirmModal(false)
503583
}
504584

505-
const { formData } = draft || {}
585+
const getWarningMessage = () => {
586+
const count = totalSelectedCollections()
587+
588+
return `Checking "All Collections" will remove ${count > 0
589+
? `${count} collection selections and ` : ''} related filters. Are you sure you want to proceed?`
590+
}
506591

507592
/**
508593
* Handles the submission of the permission form.
@@ -556,7 +641,7 @@ const PermissionForm = ({ selectedCollectionsPageSize }) => {
556641
} = granuleTemporalConstraint || {}
557642

558643
// Extract conceptIds from selectedCollections
559-
const { selectedCollections } = collectionSelection
644+
const { selectedCollections, allCollection } = collectionSelection
560645

561646
let conceptIds = []
562647
if (selectedCollections) {
@@ -626,7 +711,7 @@ const PermissionForm = ({ selectedCollectionsPageSize }) => {
626711
const permissions = searchGroupPermissions.concat(searchAndOrderGroupPermissions)
627712

628713
// Construct catalogItemIdentity object
629-
const catalogItemIdentity = {
714+
let catalogItemIdentity = {
630715
collectionApplicable,
631716
collectionIdentifier: {
632717
conceptIds,
@@ -687,9 +772,22 @@ const PermissionForm = ({ selectedCollectionsPageSize }) => {
687772
}
688773
})
689774
} else {
775+
// Remove empty fields from catalogItemIdentity
776+
catalogItemIdentity = removeEmpty(catalogItemIdentity)
777+
778+
if (!allCollection) {
779+
// Add conceptIds back if they exist
780+
if (conceptIds) {
781+
catalogItemIdentity.collectionIdentifier = {
782+
...catalogItemIdentity.collectionIdentifier,
783+
conceptIds
784+
}
785+
}
786+
}
787+
690788
updateAclMutation({
691789
variables: {
692-
catalogItemIdentity: removeEmpty(catalogItemIdentity),
790+
catalogItemIdentity,
693791
conceptId,
694792
groupPermissions: removeEmpty(permissions)
695793
},
@@ -718,7 +816,6 @@ const PermissionForm = ({ selectedCollectionsPageSize }) => {
718816
}
719817

720818
const handleClear = () => {
721-
setDraft(originalDraft)
722819
addNotification({
723820
message: 'Collection Permission cleared successfully',
724821
variant: 'success'
@@ -766,6 +863,28 @@ const PermissionForm = ({ selectedCollectionsPageSize }) => {
766863
</Form>
767864
</Col>
768865
</Row>
866+
867+
<CustomModal
868+
message={getWarningMessage()}
869+
show={showConfirmModal}
870+
size="lg"
871+
toggleModal={handleCancelAllCollection}
872+
actions={
873+
[
874+
{
875+
label: 'No',
876+
variant: 'secondary',
877+
onClick: handleCancelAllCollection
878+
},
879+
{
880+
label: 'Yes',
881+
variant: 'primary',
882+
onClick: handleConfirmAllCollection
883+
}
884+
]
885+
}
886+
/>
887+
769888
</Container>
770889
)
771890
}

0 commit comments

Comments
 (0)