Skip to content

Commit 47882bf

Browse files
authored
Enhance credits edit (#2818)
Enhance credits edit ### What's done: * Add buttons to add/remove contacts
1 parent 6a7f116 commit 47882bf

1 file changed

Lines changed: 69 additions & 18 deletions

File tree

save-frontend/src/main/kotlin/com/saveourtool/save/frontend/components/views/vuln/VulnerabilityGeneralInfoProps.kt

Lines changed: 69 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ val vulnerabilityGeneralInfoProps: FC<VulnerabilityGeneralInfoProps> = FC { prop
161161
className = ClassName("font-weight-bold text-primary-blue mb-4")
162162
+"CWE IDs".t()
163163
}
164+
164165
textarea {
165166
className = ClassName("auto_height form-control-plaintext px-2 pt-0 pb-0 text-gray-900")
166167
value = cosv.cweIds!!.joinToString(PRETTY_DELIMITER)
@@ -187,6 +188,7 @@ val vulnerabilityGeneralInfoProps: FC<VulnerabilityGeneralInfoProps> = FC { prop
187188
className = ClassName("font-weight-bold text-primary-blue mb-4")
188189
+"CWE Names".t()
189190
}
191+
190192
textarea {
191193
className = ClassName("auto_height form-control-plaintext px-2 pt-0 pb-0 text-gray-900")
192194
value = cosv.cweNames!!.joinToString("\n\n")
@@ -272,7 +274,6 @@ val vulnerabilityGeneralInfoProps: FC<VulnerabilityGeneralInfoProps> = FC { prop
272274
vulnerabilityTagsComponent {
273275
this.currentUserInfo = props.currentUserInfo
274276
this.vulnerability = props.vulnerability
275-
276277
this.isEditDisabled = props.isEditDisabled
277278
this.setVulnerabilityExt = props.setVulnerability
278279
}
@@ -347,8 +348,7 @@ val vulnerabilityGeneralInfoProps: FC<VulnerabilityGeneralInfoProps> = FC { prop
347348
className = ClassName("font-weight-bold text-primary-blue mb-4")
348349
+"Credits"
349350
}
350-
351-
cosv.credits!!.forEachIndexed { ind, credit ->
351+
cosv.credits!!.forEachIndexed { creditInd, credit ->
352352
val userPrefix = if (credit.name in saveContributors.map { it.name }) "[SAVE] user" else "External user"
353353
if (props.isEditDisabled) {
354354
div {
@@ -369,32 +369,71 @@ val vulnerabilityGeneralInfoProps: FC<VulnerabilityGeneralInfoProps> = FC { prop
369369
onChangeFun = { event ->
370370
val updatedCredit = credit.copy(name = event.target.value)
371371
val updatedCredits = cosv.credits as MutableList<Credit>
372-
updatedCredits[ind] = updatedCredit
372+
updatedCredits[creditInd] = updatedCredit
373373
props.setVulnerability { vulnerability ->
374374
vulnerability?.copy(
375375
cosv = cosv.copy(credits = updatedCredits)
376376
)
377377
}
378378
}
379379
}
380-
inputTextFormRequired {
381-
form = InputTypes.VULN_CREDIT_CONTACTS
382-
textValue = credit.contact!!.joinToString(PRETTY_DELIMITER)
383-
classes = "col-6 pl-2 pr-2 mt-3 text-left"
384-
name = ""
385-
validInput = credit.contact!!.isNotEmpty()
386-
onChangeFun = { event ->
387-
val updatedCredit = credit.copy(contact = event.target.value.split(PRETTY_DELIMITER))
388-
val updatedCredits = cosv.credits as MutableList<Credit>
389-
updatedCredits[ind] = updatedCredit
390-
props.setVulnerability { vulnerability ->
391-
vulnerability?.copy(
392-
cosv = cosv.copy(credits = updatedCredits)
393-
)
380+
credit.contact!!.forEachIndexed { contactInd, contact ->
381+
inputTextFormRequired {
382+
form = InputTypes.VULN_CREDIT_CONTACTS
383+
textValue = contact
384+
classes = "col-6 pl-2 pr-2 mt-3 text-left"
385+
name = ""
386+
validInput = contact.isNotBlank()
387+
onChangeFun = { event ->
388+
val updatedContacts = credit.contact as MutableList<String>
389+
updatedContacts[contactInd] = event.target.value
390+
391+
props.setVulnerability { vulnerability ->
392+
vulnerability?.copy(
393+
cosv = cosv.copy(credits = updateCredits(cosv.credits!!, updatedContacts, credit, creditInd))
394+
)
395+
}
394396
}
395397
}
396398
}
397399
}
400+
div {
401+
className = ClassName("row")
402+
buttonBuilder(
403+
faPlus,
404+
style = "primary ml-2 mr-1 mt-1",
405+
title = "Add new contact",
406+
classes = "btn-sm",
407+
isOutline = true
408+
) {
409+
val updatedContacts = (credit.contact as MutableList<String>)
410+
updatedContacts.add("")
411+
412+
props.setVulnerability { vulnerability ->
413+
vulnerability?.copy(
414+
cosv = cosv.copy(credits = updateCredits(cosv.credits!!, updatedContacts, credit, creditInd))
415+
)
416+
}
417+
}
418+
buttonBuilder(
419+
faTrash,
420+
"danger mr-1 mt-1",
421+
isOutline = true,
422+
title = "Delete contact",
423+
classes = "btn-sm"
424+
) {
425+
val updatedContacts = (credit.contact as MutableList<String>)
426+
if (updatedContacts.size >= 1) {
427+
updatedContacts.removeLast()
428+
}
429+
430+
props.setVulnerability { vulnerability ->
431+
vulnerability?.copy(
432+
cosv = cosv.copy(credits = updateCredits(cosv.credits!!, updatedContacts, credit, creditInd))
433+
)
434+
}
435+
}
436+
}
398437
}
399438
}
400439
}
@@ -483,3 +522,15 @@ external interface VulnerabilityGeneralInfoProps : Props {
483522
private fun hasRightsToEdit(currentUserInfo: UserInfo?, vulnerability: VulnerabilityExt): Boolean =
484523
currentUserInfo?.isSuperAdmin() == true || ((currentUserInfo?.name == vulnerability.metadataDto.user.name ||
485524
currentUserInfo?.name in vulnerability.getAllParticipants().map { it.name }) && vulnerability.metadataDto.status != VulnerabilityStatus.APPROVED)
525+
526+
private fun updateCredits(
527+
cosvCredits: List<Credit>,
528+
newContacts: List<String>,
529+
currentCredit: Credit,
530+
currentCreditInd: Int
531+
): List<Credit> {
532+
val updatedCredit = currentCredit.copy(contact = newContacts)
533+
val updatedCredits = cosvCredits as MutableList<Credit>
534+
updatedCredits[currentCreditInd] = updatedCredit
535+
return updatedCredits
536+
}

0 commit comments

Comments
 (0)