@@ -2,23 +2,93 @@ import SpotCard from 'design-library/molecules/cards/spot-card/spot-card'
22import LoginFormSignup from 'design-library/molecules/form-section/login-form/login-form-signup/login-form-signup'
33import React from 'react'
44import { FormattedMessage } from 'react-intl'
5- import { useHistory } from 'react-router-dom'
5+ import { useHistory , useParams } from 'react-router-dom'
6+
7+ const normalizeTypeValue = ( value = '' ) =>
8+ value
9+ . toString ( )
10+ . trim ( )
11+ . toLowerCase ( )
12+ . replace ( / [ _ \s ] + / g, '-' )
13+
14+ const singularizeTypeValue = ( value = '' ) => normalizeTypeValue ( value ) . replace ( / s $ / , '' )
15+
16+ const formatTypeLabel = ( value = '' ) =>
17+ value
18+ . toString ( )
19+ . replace ( / [ - _ ] + / g, ' ' )
20+ . replace ( / \b \w / g, ( char ) => char . toUpperCase ( ) )
21+
22+ const getThemeVariant = ( type = '' ) => {
23+ const normalizedType = normalizeTypeValue ( type )
24+
25+ if ( normalizedType . includes ( 'maintainer' ) ) return 'primary'
26+ if ( normalizedType . includes ( 'sponsor' ) ) return 'secondary'
27+ if ( normalizedType . includes ( 'contributor' ) ) return 'success'
28+ if ( normalizedType . includes ( 'manager' ) ) return 'warning'
29+ return 'info'
30+ }
31+
32+ const resolveRoleByType = ( rawType , rolesData = [ ] ) => {
33+ const normalizedType = normalizeTypeValue ( rawType )
34+ const singularType = singularizeTypeValue ( rawType )
35+
36+ if ( ! normalizedType || ! Array . isArray ( rolesData ) || rolesData . length === 0 ) {
37+ return null
38+ }
39+
40+ return (
41+ rolesData . find ( ( role ) => {
42+ const candidates = [ role ?. name , role ?. label , role ?. id ] . filter ( Boolean )
43+ return candidates . some ( ( candidate ) => {
44+ const normalizedCandidate = normalizeTypeValue ( candidate )
45+ const singularCandidate = singularizeTypeValue ( candidate )
46+
47+ return (
48+ normalizedCandidate === normalizedType ||
49+ singularCandidate === singularType ||
50+ normalizedCandidate . includes ( normalizedType ) ||
51+ normalizedType . includes ( normalizedCandidate ) ||
52+ singularCandidate . includes ( singularType ) ||
53+ singularType . includes ( singularCandidate )
54+ )
55+ } )
56+ } ) || null
57+ )
58+ }
659
760const SignupPage = ( { handleSignup, roles, fetchRoles } ) => {
861 const history = useHistory ( )
62+ const { type, status } = useParams < { type ?: string ; status ?: string } > ( )
63+ const routeType = type || status
64+ const resolvedRole = resolveRoleByType ( routeType , roles ?. data )
65+ const selectedTypeLabel = resolvedRole ?. label || resolvedRole ?. name || formatTypeLabel ( routeType )
66+ const isTypedSignup = ! ! resolvedRole
67+
968 return (
1069 < SpotCard
1170 title = { < FormattedMessage id = "signup.welcome" defaultMessage = "Welcome!" /> }
1271 description = {
13- < FormattedMessage id = "signup.description" defaultMessage = "Create your account" />
72+ isTypedSignup ? (
73+ < FormattedMessage
74+ id = "signup.asType"
75+ defaultMessage = "Signup as a {type}"
76+ values = { { type : selectedTypeLabel } }
77+ />
78+ ) : (
79+ < FormattedMessage id = "signup.description" defaultMessage = "Create your account" />
80+ )
1481 }
82+ themeVariant = { isTypedSignup ? getThemeVariant ( resolvedRole ?. name || routeType ) : 'default' }
1583 >
1684 < LoginFormSignup
1785 onSignin = { ( ) => history . push ( '/signin' ) }
1886 onSubmit = { handleSignup }
1987 roles = { roles }
2088 fetchRoles = { fetchRoles }
2189 noCancelButton
90+ hideRoleSelection = { isTypedSignup }
91+ presetRoleId = { resolvedRole ?. id }
2292 />
2393 </ SpotCard >
2494 )
0 commit comments