Skip to content

Commit

Permalink
Merge pull request #120 from Ryan-slither/main
Browse files Browse the repository at this point in the history
Build out Admin Register Page
  • Loading branch information
IkeHunter authored Nov 7, 2024
2 parents 6d122b2 + 67d408b commit cf7ee8e
Show file tree
Hide file tree
Showing 4 changed files with 231 additions and 2 deletions.
131 changes: 131 additions & 0 deletions src/apps/auth/components/AuthRegisterForm.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import { useRef, useState } from 'react'
import { useSelector } from 'react-redux'
import { Form, FormInputGroup, FormSection, FormSubmit } from 'src/components'
import { selectUserStatus } from 'src/store'

export const AuthRegisterForm = (props: {
onSubmit: (
email: string,
firstName: string,
lastName: string,
password: string,
confirmPassword: string,
) => Promise<
| ({
success: true
} & { [key: string]: any })
| {
success: false
emailError?: string
firstNameError?: string
lastNameError?: string
passwordError?: string
confirmPasswordError?: string
error?: string
}
>
}) => {
const { onSubmit } = props
const [errors, setErrors] = useState<{
error?: string
email?: string
firstName?: string
lastName?: string
password?: string
confirmPassword?: string
}>({})

const userStatus = useSelector(selectUserStatus)

// Register form refs
const emailRef = useRef<HTMLInputElement>(null)
const firstNameRef = useRef<HTMLInputElement>(null)
const lastNameRef = useRef<HTMLInputElement>(null)
const passwordRef = useRef<HTMLInputElement>(null)
const confirmPasswordRef = useRef<HTMLInputElement>(null)

const handleRegisterSubmit = () => {
const email = emailRef.current?.value || ''
const firstName = firstNameRef.current?.value || ''
const lastName = lastNameRef.current?.value || ''
const password = passwordRef.current?.value || ''
const confirmPassword = confirmPasswordRef.current?.value || ''

onSubmit(email, firstName, lastName, password, confirmPassword).then(
(res) => {
if (!res.success) {
setErrors({
error: res.error,
email: res.emailError,
firstName: res.firstNameError,
lastName: res.lastNameError,
password: res.passwordError,
confirmPassword: res.confirmPasswordError,
})
}
},
)
}

return (
<Form onSubmit={handleRegisterSubmit}>
<FormSection>
<FormInputGroup
label="Email"
id="email"
type="text"
ref={emailRef}
disabled={userStatus === 'loading'}
required
error={errors.email}
/>
<div className="form-row">
<FormInputGroup
label="First Name"
id="firstName"
type="text"
ref={firstNameRef}
disabled={userStatus === 'loading'}
required
error={errors.firstName}
/>

<FormInputGroup
label="Last Name"
id="lastName"
type="text"
ref={lastNameRef}
disabled={userStatus === 'loading'}
required
error={errors.lastName}
/>
</div>
<FormInputGroup
label="Password"
id="password"
type="password"
ref={passwordRef}
disabled={userStatus === 'loading'}
required
error={errors.password}
/>
<FormInputGroup
label="Confirm Password"
id="confirmPassword"
type="password"
ref={confirmPasswordRef}
disabled={userStatus === 'loading'}
required
error={errors.confirmPassword}
/>
<div>
{errors.error && (
<p className="form-feedback error">{errors.error}</p>
)}
{userStatus === 'loading' && <p>Loading...</p>}
</div>
</FormSection>
<FormSubmit disabled={userStatus === 'loading'} />
</Form>
)
}
36 changes: 36 additions & 0 deletions src/apps/auth/pages/AdminRegister.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
.register-page {
display: grid;
place-content: center;
height: 100vh;
background-color: var(--color-surface-container);
}

.register-page__form-container {
grid-column-start: 5;
grid-column-end: 9;
text-align: center;
}

.register-page__form-container > h2 {
color: var(--color-surface-on);
padding-bottom: 2vw;
}

.register-page__form-authcontainer {
margin: 0 auto;
padding: 2vw 3vw 4vw 3vw;
box-sizing: border-box;
background-color: var(--color-surface);
}

.form-row {
display: flex;
gap: 1rem;
width: 100%;
}

.form-row > div {
flex: 1;
min-width: 0;
box-sizing: border-box;
}
35 changes: 34 additions & 1 deletion src/apps/auth/pages/AdminRegister.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,36 @@
import { useSelector } from 'react-redux'
import { useNavigate } from 'react-router-dom'
import { AuthRegisterForm } from 'src/apps/auth/components/AuthRegisterForm'
import { registerUser, selectUserStatus } from 'src/store'
import './AdminRegister.scss'

export const AdminRegister = () => {
return <div>AdminRegister</div>
const userStatus = useSelector(selectUserStatus)
const navigate = useNavigate()

const handleRegisterUser = (
email: string,
firstName: string,
lastName: string,
password: string,
confirmPassword: string,
) => registerUser(email, firstName, lastName, password, confirmPassword)

// Issue: userStatus needs an exception to not succeed if on register page
// useEffect(() => {
// if (userStatus === 'succeeded') {
// navigate('/auth/admin/login')
// }
// }, [userStatus])

return (
<div className="register-page grid">
<div className="register-page__form-container">
<h2>Register Account</h2>
<div className="register-page__form-authcontainer">
<AuthRegisterForm onSubmit={handleRegisterUser} />
</div>
</div>
</div>
)
}
31 changes: 30 additions & 1 deletion src/store/user/userActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { Network } from 'src/network'
import { generateLocalData, isUser } from 'src/utils'
import { store } from '../store'
import { userSlice } from './userSlice'
import { thunkFetchUserInfo, thunkLoginUser } from './userThunks'
import {
thunkFetchUserInfo,
thunkGetUserSpotifyToken,

Check failure on line 8 in src/store/user/userActions.ts

View workflow job for this annotation

GitHub Actions / ts-check

Module '"./userThunks"' has no exported member 'thunkGetUserSpotifyToken'.
thunkLoginUser,
} from './userThunks'

const { logout, set, update } = userSlice.actions

Expand Down Expand Up @@ -42,6 +46,30 @@ export const loginUser = async (email: string, password: string) => {
return res
}

/**
* Register user, needs work!!!
*/
export const registerUser = async (
email: string,
firstName: string,
lastName: string,
password: string,
confirmPassword: string,
): Promise<{ success: boolean } & { [key: string]: any }> => {
if (password === confirmPassword) {
console.log(
`Registration successful - ${email}, ${firstName}, ${lastName}, ${password}, ${confirmPassword}`,
)
return {
success: true,
}
}
console.log('Registeratiion failed')
return {
success: false,
}
}

/**
* Fetch user's info, return user
*/
Expand Down Expand Up @@ -79,6 +107,7 @@ export const initializeUser = async () => {

if (isUser(user)) {
setUser(user, token)
await store.dispatch(thunkGetUserSpotifyToken())
} else {
logoutUser()
}
Expand Down

0 comments on commit cf7ee8e

Please sign in to comment.