Skip to content

Commit

Permalink
Split FrontendRoutes on two enums (#2866)
Browse files Browse the repository at this point in the history
* Split FrontendRoutes on two enums
  • Loading branch information
Cheshiriks authored Nov 30, 2023
1 parent 325aef9 commit 7a7e833
Show file tree
Hide file tree
Showing 38 changed files with 197 additions and 164 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* Names that are used as endpoints in the frontend.
* If you create a new view with new URL - add it here.
*/

package com.saveourtool.save.validation

import com.saveourtool.save.utils.URL_PATH_DELIMITER
import kotlin.js.JsExport

/**
* @property path substring of url that defines given route
*/
@JsExport
enum class FrontendCosvRoutes(val path: String) {
ABOUT_US("about"),
BAN("ban"),
COOKIE("cookie"),
ERROR_404("404"),
MANAGE_ORGANIZATIONS("organizations"),
NOT_FOUND("not-found"),
PROFILE("profile"),
REGISTRATION("registration"),
SETTINGS_DELETE("$SETTINGS/delete"),
SETTINGS_EMAIL("$SETTINGS/email"),
SETTINGS_ORGANIZATIONS("$SETTINGS/organizations"),
SETTINGS_PROFILE("$SETTINGS/profile"),
SETTINGS_TOKEN("$SETTINGS/token"),
TERMS_OF_USE("terms-of-use"),
THANKS_FOR_REGISTRATION("thanks-for-registration"),
VULN("vuln"),
VULNERABILITIES("$VULN/list"),
VULNERABILITY_SINGLE("$VULN/collection"),
VULN_COSV_SCHEMA("$VULN/schema"),
VULN_CREATE("$VULN/create-vulnerability"),
VULN_TOP_RATING("$VULN/top-rating"),
VULN_UPLOAD("$VULN/upload-vulnerability"),
;

override fun toString(): String = path

companion object {
/**
* List of views on which topbar should not be rendered
*/
val noTopBarViewList = arrayOf(
REGISTRATION,
ERROR_404,
TERMS_OF_USE,
THANKS_FOR_REGISTRATION,
)

/**
* Get forbidden words from [FrontendCosvRoutes].
*
* @return list of forbidden words
*/
fun getForbiddenWords() = FrontendCosvRoutes.values()
.map { it.path.split(URL_PATH_DELIMITER) }
.flatten()
.toTypedArray()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ enum class FrontendRoutes(val path: String) {
INDEX(""),
MANAGE_ORGANIZATIONS("organizations"),
NOT_FOUND("not-found"),
PROFILE("profile"),
PROJECTS("projects"),
REGISTRATION("registration"),
SANDBOX("sandbox"),
Expand All @@ -41,14 +42,6 @@ enum class FrontendRoutes(val path: String) {
SETTINGS_TOKEN("$SETTINGS/token"),
TERMS_OF_USE("terms-of-use"),
THANKS_FOR_REGISTRATION("thanks-for-registration"),
VULN("vuln"),
VULNERABILITIES("$VULN/list"),
VULNERABILITY_SINGLE("$VULN/collection"),
VULN_COSV_SCHEMA("$VULN/schema"),
VULN_CREATE("$VULN/create-vulnerability"),
VULN_PROFILE("$VULN/profile"),
VULN_TOP_RATING("$VULN/top-rating"),
VULN_UPLOAD("$VULN/upload-vulnerability"),
;

override fun toString(): String = path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private fun String.hasOnlyAlphaNumOrAllowedSpecialSymbols(
allowedSpecialSymbols: Set<Char> = namingAllowedSpecialSymbols
) = all { it.isLetterOrDigit() || allowedSpecialSymbols.contains(it) }

private fun String.containsForbiddenWords() = (FrontendRoutes.getForbiddenWords() + BackendRoutes.getForbiddenWords())
private fun String.containsForbiddenWords() = (FrontendRoutes.getForbiddenWords() + FrontendCosvRoutes.getForbiddenWords() + BackendRoutes.getForbiddenWords())
.any { this == it }

private fun String.isLengthOk(allowedLength: Int) = length <= allowedLength
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
package com.saveourtool.save.cosv.frontend.components.topbar

import com.saveourtool.save.frontend.common.externals.i18next.useTranslation
import com.saveourtool.save.validation.FrontendRoutes
import com.saveourtool.save.validation.FrontendCosvRoutes

import react.*
import react.dom.html.ReactHTML.a
Expand All @@ -27,10 +27,9 @@ val topBarLinks: FC<TopBarLinksProps> = FC { props ->
val (t) = useTranslation("topbar")

val vulnTopbarLinks = sequenceOf(
TopBarLink(hrefAnchor = FrontendRoutes.INDEX.path, text = "Main page".t()),
TopBarLink(hrefAnchor = FrontendRoutes.VULN_CREATE.path, text = "Propose vulnerability".t()),
TopBarLink(hrefAnchor = FrontendRoutes.VULNERABILITIES.path, text = "Vulnerabilities list".t()),
TopBarLink(hrefAnchor = FrontendRoutes.VULN_TOP_RATING.path, text = "Top Rating".t()),
TopBarLink(hrefAnchor = FrontendCosvRoutes.VULN_CREATE.path, text = "Propose vulnerability".t()),
TopBarLink(hrefAnchor = FrontendCosvRoutes.VULNERABILITIES.path, text = "Vulnerabilities list".t()),
TopBarLink(hrefAnchor = FrontendCosvRoutes.VULN_TOP_RATING.path, text = "Top Rating".t()),
)

ul {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package com.saveourtool.save.cosv.frontend.components.topbar

import com.saveourtool.save.frontend.common.utils.TopBarUrl
import com.saveourtool.save.utils.URL_PATH_DELIMITER
import com.saveourtool.save.validation.FrontendRoutes
import com.saveourtool.save.validation.FrontendCosvRoutes

import react.FC
import react.Props
Expand Down Expand Up @@ -41,7 +41,7 @@ val topBarUrlSplits: FC<TopBarUrlSplitsProps> = FC { props ->
}
props.location.pathname
// workaround for avoiding invalid routing to /vuln/list/:param from /vuln/collection/vulnName
.replace("${FrontendRoutes.VULNERABILITY_SINGLE}", "${FrontendRoutes.VULNERABILITIES}")
.replace("${FrontendCosvRoutes.VULNERABILITY_SINGLE}", "${FrontendCosvRoutes.VULNERABILITIES}")
.substringBeforeLast("?")
.split(URL_PATH_DELIMITER)
.filterNot { it.isBlank() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import com.saveourtool.save.frontend.common.utils.*
import com.saveourtool.save.frontend.common.utils.AVATAR_PROFILE_PLACEHOLDER
import com.saveourtool.save.frontend.common.utils.UserInfoAwareProps
import com.saveourtool.save.frontend.common.utils.isSuperAdmin
import com.saveourtool.save.validation.FrontendRoutes
import com.saveourtool.save.validation.FrontendCosvRoutes

import js.core.jso
import react.*
Expand Down Expand Up @@ -105,20 +105,20 @@ val topBarUserField: FC<UserInfoAwareProps> = FC { props ->
props.userInfo?.name?.let { name ->
dropdownEntry(faUser, "Profile".t()) { attrs ->
attrs.onClick = {
navigate(to = "/${FrontendRoutes.VULN_PROFILE}/$name")
navigate(to = "/${FrontendCosvRoutes.PROFILE}/$name")
}
}
dropdownEntry(faCog, "Settings".t()) { attrs ->
attrs.onClick = {
navigate(to = "/${FrontendRoutes.SETTINGS_PROFILE}")
navigate(to = "/${FrontendCosvRoutes.SETTINGS_PROFILE}")
}
}
dropdownEntry(
faCity,
"Manage organizations".t()
) { attrs ->
attrs.onClick = {
navigate(to = "/${FrontendRoutes.SETTINGS_ORGANIZATIONS}")
navigate(to = "/${FrontendCosvRoutes.SETTINGS_ORGANIZATIONS}")
}
}
dropdownEntry(faSignOutAlt, "Log out".t()) { attrs ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import com.saveourtool.save.frontend.common.externals.i18next.useTranslation
import com.saveourtool.save.frontend.common.themes.Colors
import com.saveourtool.save.frontend.common.utils.*
import com.saveourtool.save.utils.*
import com.saveourtool.save.validation.FrontendRoutes
import com.saveourtool.save.validation.FrontendCosvRoutes
import com.saveourtool.save.validation.isValidUrl

import com.saveourtool.osv4k.Reference
Expand Down Expand Up @@ -105,7 +105,7 @@ val createVulnerabilityView: FC<Props> = FC {
responseHandler = ::responseHandlerWithValidation,
)
if (response.ok) {
navigate(to = "/${FrontendRoutes.VULNERABILITIES}")
navigate(to = "/${FrontendCosvRoutes.VULNERABILITIES}")
} else if (response.isConflict()) {
setConflictErrorMessage(response.unpackMessageOrHttpStatus())
}
Expand Down Expand Up @@ -440,7 +440,7 @@ val createVulnerabilityView: FC<Props> = FC {
div {
className = ClassName("invalid-feedback d-block text-center")
markdown(
"Vulnerability associated with this identifier: [${cosv.id}](/${FrontendRoutes.VULNERABILITY_SINGLE}/${cosv.id}) already exists"
"Vulnerability associated with this identifier: [${cosv.id}](/${FrontendCosvRoutes.VULNERABILITY_SINGLE}/${cosv.id}) already exists"
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import com.saveourtool.save.frontend.common.externals.fontawesome.faTrash
import com.saveourtool.save.frontend.common.externals.i18next.useTranslation
import com.saveourtool.save.frontend.common.utils.*
import com.saveourtool.save.info.UserInfo
import com.saveourtool.save.validation.FrontendRoutes
import com.saveourtool.save.validation.FrontendCosvRoutes

import js.core.jso
import react.*
Expand Down Expand Up @@ -56,7 +56,7 @@ internal val headerMenu: FC<HeaderMenuProps> = FC { props ->
loadingHandler = ::loadingHandler,
)
if (response.ok) {
navigate(to = "/${FrontendRoutes.VULNERABILITIES}")
navigate(to = "/${FrontendCosvRoutes.VULNERABILITIES}")
}
}

Expand All @@ -82,7 +82,7 @@ internal val headerMenu: FC<HeaderMenuProps> = FC { props ->
loadingHandler = ::loadingHandler,
)
if (response.ok) {
navigate(to = "/${FrontendRoutes.VULNERABILITIES}")
navigate(to = "/${FrontendCosvRoutes.VULNERABILITIES}")
}
}

Expand All @@ -108,7 +108,7 @@ internal val headerMenu: FC<HeaderMenuProps> = FC { props ->
loadingHandler = ::loadingHandler,
)
if (response.ok) {
navigate(to = "/${FrontendRoutes.VULNERABILITIES}")
navigate(to = "/${FrontendCosvRoutes.VULNERABILITIES}")
}
}

Expand All @@ -122,7 +122,7 @@ internal val headerMenu: FC<HeaderMenuProps> = FC { props ->
loadingHandler = ::loadingHandler,
)
if (response.ok) {
navigate(to = "/${FrontendRoutes.VULNERABILITIES}")
navigate(to = "/${FrontendCosvRoutes.VULNERABILITIES}")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import com.saveourtool.save.frontend.common.utils.*
import com.saveourtool.save.frontend.common.utils.buttonBuilder
import com.saveourtool.save.frontend.common.utils.useTooltip
import com.saveourtool.save.info.UserInfo
import com.saveourtool.save.validation.FrontendRoutes
import com.saveourtool.save.validation.FrontendCosvRoutes
import com.saveourtool.save.validation.isValidTag

import react.*
Expand Down Expand Up @@ -89,7 +89,7 @@ val vulnerabilityTagsComponent: FC<VulnerabilityTagsComponentProps> = FC { props
)
}
} else {
navigate("/${FrontendRoutes.VULNERABILITIES}?tag=$tag")
navigate("/${FrontendCosvRoutes.VULNERABILITIES}?tag=$tag")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import com.saveourtool.save.entities.cosv.VulnerabilityExt
import com.saveourtool.save.frontend.TabMenuBar
import com.saveourtool.save.frontend.common.utils.*
import com.saveourtool.save.info.UserInfo
import com.saveourtool.save.validation.FrontendRoutes
import com.saveourtool.save.validation.FrontendCosvRoutes

import js.core.jso
import react.*
Expand Down Expand Up @@ -193,7 +193,7 @@ enum class VulnerabilityTab {
companion object : TabMenuBar<VulnerabilityTab> {
override val nameOfTheHeadUrlSection = ""
override val defaultTab: VulnerabilityTab = INFO
override val regexForUrlClassification = "/${FrontendRoutes.VULN_PROFILE}"
override val regexForUrlClassification = "/${FrontendCosvRoutes.PROFILE}"
override fun valueOf(elem: String): VulnerabilityTab = VulnerabilityTab.valueOf(elem)
override fun values(): Array<VulnerabilityTab> = VulnerabilityTab.values()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import com.saveourtool.save.frontend.common.utils.Style
import com.saveourtool.save.frontend.common.utils.tab
import com.saveourtool.save.frontend.common.utils.title
import com.saveourtool.save.frontend.common.utils.useBackground
import com.saveourtool.save.validation.FrontendRoutes
import com.saveourtool.save.validation.FrontendCosvRoutes
import react.FC
import react.Props
import react.dom.html.ReactHTML.div
Expand Down Expand Up @@ -55,7 +55,7 @@ enum class TopRatingTab {
companion object : TabMenuBar<TopRatingTab> {
override val nameOfTheHeadUrlSection = ""
override val defaultTab: TopRatingTab = USERS
override val regexForUrlClassification = "/${FrontendRoutes.VULN_TOP_RATING}"
override val regexForUrlClassification = "/${FrontendCosvRoutes.VULN_TOP_RATING}"
override fun valueOf(elem: String): TopRatingTab = TopRatingTab.valueOf(elem)
override fun values(): Array<TopRatingTab> = TopRatingTab.values()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import com.saveourtool.save.frontend.common.utils.*
import com.saveourtool.save.frontend.common.utils.UserInfoAwarePropsWithChildren
import com.saveourtool.save.frontend.common.utils.useBackground
import com.saveourtool.save.info.OauthProviderInfo
import com.saveourtool.save.validation.FrontendRoutes
import com.saveourtool.save.validation.FrontendCosvRoutes

import js.core.jso
import org.w3c.fetch.Headers
Expand Down Expand Up @@ -104,16 +104,16 @@ val vulnWelcomeView: FC<UserInfoAwarePropsWithChildren> = FC { props ->
// if user is not logged in - he needs to input credentials
props.userInfo?.let {
welcomeUserMenu(props.userInfo, Colors.VULN_PRIMARY, t) {
menuTextAndLink("Vulnerability database".t(), FrontendRoutes.VULNERABILITIES, faCode)
menuTextAndLink("Vulnerability database".t(), FrontendCosvRoutes.VULNERABILITIES, faCode)
hrNoMargin()
menuTextAndLink("Propose vulnerability".t(), FrontendRoutes.VULN_CREATE, faPlus)
menuTextAndLink("Propose vulnerability".t(), FrontendCosvRoutes.VULN_CREATE, faPlus)
hrNoMargin()
menuTextAndLink("Top rating".t(), FrontendRoutes.VULN_TOP_RATING, faTrophy)
menuTextAndLink("Top rating".t(), FrontendCosvRoutes.VULN_TOP_RATING, faTrophy)
}
} ?: inputCredentialsView(
oauthProviders,
Colors.VULN_PRIMARY,
"/${FrontendRoutes.VULNERABILITIES}",
"/${FrontendCosvRoutes.VULNERABILITIES}",
t
)
}
Expand All @@ -138,7 +138,7 @@ val vulnWelcomeView: FC<UserInfoAwarePropsWithChildren> = FC { props ->
@Suppress("IDENTIFIER_LENGTH")
fun ChildrenBuilder.stats(vulnerabilitiesNumber: Long, t: TranslationFunction) {
Link {
to = "/${FrontendRoutes.VULNERABILITIES}"
to = "/${FrontendCosvRoutes.VULNERABILITIES}"
div {
className =
ClassName("card rounded rounded-pill col mt-4 justify-content-center button_animated_card")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import com.saveourtool.save.cosv.frontend.components.views.welcome.BIG_FONT_SIZE
import com.saveourtool.save.cosv.frontend.components.views.welcome.FIRST_RAW_HEIGHT
import com.saveourtool.save.cosv.frontend.components.views.welcome.SECOND_RAW_HEIGHT
import com.saveourtool.save.frontend.common.externals.i18next.TranslationFunction
import com.saveourtool.save.validation.FrontendRoutes
import com.saveourtool.save.validation.FrontendCosvRoutes
import js.core.jso
import react.ChildrenBuilder
import react.dom.html.ReactHTML.div
Expand Down Expand Up @@ -138,7 +138,7 @@ private fun ChildrenBuilder.cosvSchemaCard(
t: TranslationFunction,
) {
Link {
to = "/${FrontendRoutes.VULN_COSV_SCHEMA}"
to = "/${FrontendCosvRoutes.VULN_COSV_SCHEMA}"
div {
className =
ClassName("card button_animated_card rounded rounded-pill col justify-content-center")
Expand Down
Loading

0 comments on commit 7a7e833

Please sign in to comment.