Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/admin login 1 #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions react-app/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useState, useContext } from 'react';
import { BrowserRouter as Router, Switch, Route, Redirect } from 'react-router-dom'
import LoginPage from './pages/LoginPage';
import DashboardPage from './pages/DashboardPage';
import PasswordAvatar from './components/PasswordAvatar';

const UserLoginContext = React.createContext(null)

Expand All @@ -18,7 +19,7 @@ function App() {

return (
<Router>
<UserLoginContext.Provider value={{userLoggedIn, signUserOut }}>
<UserLoginContext.Provider value={{userLoggedIn, setUserLoggedIn, signUserOut }}>
<Switch>
<Route exact path="/">
{userLoggedIn ? (
Expand All @@ -31,7 +32,7 @@ function App() {
<LoginPage logUserIn={setUserLoggedIn}/>
</Route>
<Route path='/secure-login'>
<div>Password</div>
<PasswordAvatar />
</Route>
</Switch>
</UserLoginContext.Provider>
Expand Down
3 changes: 3 additions & 0 deletions react-app/src/assets/back-arrow.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions react-app/src/assets/show-password.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions react-app/src/components/CardAvatar.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import React from 'react'
import SignOutIcon from '../assets/sign-out.svg'

function CardAvatar({ user, onSignOut }) {
const {name, image} = user
const {name, image, coins} = user
return (
<div className='card-avatar-wrapper'>
<div className='card-avatar'>
<img src={image} alt={`${name} avatar`} />
<div className='card-avatar-info-container'>
<div className='card-avatar-title'>Hello, {name}!</div>
<button className='help-point-btn'>{425} HC</button>
<button className='btn help-point-btn'>{coins} HC</button>
</div>
</div>
<button className='leave-btn' onClick={onSignOut}>
Expand Down
41 changes: 41 additions & 0 deletions react-app/src/components/PasswordAvatar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React, { useState, useEffect } from 'react'
import RoundedAvatar from './RoundedAvatar'
import BackArrowImg from '../assets/back-arrow.svg'
import ShowPasswordImg from '../assets/show-password.svg'
Comment on lines +3 to +4
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now these imports give you paths to the image, not components, so I would recommend using the lower case (otherwise it's a little bit confusing).

Alternatively, you can use SVGO's feature, supported by the CRA, to import SVGs as real react components:

import { ReactComponent as Logo } from './logo.svg';

function App() {
  return (
    <div>
      {/* Logo is an actual React component */}
      <Logo />
    </div>
  );
}


function PasswordAvatar({ user, onBack }) {
const [password, setPassword] = useState('')
const [showPassword, setShowPassword] = useState(false)

function authenticateUser(event) {
event.preventDefault()
}

function onPasswordChange(event) {
setPassword(event.target.value)
}

return (
<div className='password-avatar-container'>
<button type='button' className='default-btn back-arrow-btn' onClick={onBack}>
<img src={BackArrowImg} alt='back arrow'/>
</button>
<div>
<RoundedAvatar user={user} />
<form className='password-form' onSubmit={authenticateUser}>
<div className='sign-in-container'>
<div className='password-textbox-container'>
<input className='textbox password-textbox' type={(showPassword) ? 'password' : 'text'} name='password' onChange={onPasswordChange} required/>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JFYI: when you have quite a few props (or just attributes) on the component, you could split it into multiple lines to improve readability (especially for PR's viewers, but also for developers in the code):

 <input 
  className='textbox password-textbox' 
  type={(showPassword) ? 'password' : 'text'} 
  name='password' 
  onChange={onPasswordChange} 
  required
/>

<button type='button' className={`default-btn show-password-btn ${(showPassword) ? 'visible' : ''}`} onMouseDown={() => setShowPassword(!showPassword)}>
<img src={ShowPasswordImg} alt='show password'/>
</button>
</div>
<button type='button' className='btn'>Sign In</button>
</div>
</form>
</div>
</div>
)
}

export default PasswordAvatar
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, when you use default exports, do you get auto completes in your VSCode?

2 changes: 1 addition & 1 deletion react-app/src/components/task-card/FlipTaskCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function FlipTaskCard({ task, completeTask, flippable = true }) {
return (
<CSSTransition in={task.isComplete} timeout={REMOVE_TASK_DELAY} classNames="remove-fade">
<ReactCardFlip isFlipped={isFlip} flipDirection='horizontal' infinite={true}>
<TaskCard key='front' task={task} onClick={handleCardClick}/>
<TaskCard key='front' task={task} isFlippable={flippable} onClick={handleCardClick}/>
{(task.isComplete) ? (
<CompletionCard key='back' points={task.rewardPoints} onClick={handleCardClick}/>
) : (
Expand Down
5 changes: 3 additions & 2 deletions react-app/src/components/task-card/TaskCard.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React from 'react'
import Card from './Card'

function TaskCard({ task, ...rest }) {
function TaskCard({ task, isFlippable, ...rest }) {
const { title, rewardPoints, colour } = task

return (
<Card colour={colour} {...rest}>
<Card colour={colour} className={(isFlippable) ? 'pointer': ''} {...rest}>
<div className='reward'><span className='reward-points'>{rewardPoints}</span></div>
<div className='task-title'>{title}</div>
</Card>
Expand Down
16 changes: 15 additions & 1 deletion react-app/src/components/task-grid/TaskGrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import firebase from '../../firebase'
export const REMOVE_TASK_DELAY = 2000

function TaskGrid({ tasks = [], flippableTask = true, className = '' }) {
const { userLoggedIn } = useUserLogged()
const { userLoggedIn, setUserLoggedIn } = useUserLogged()
const [taskGridtasks, setTaskGridTasks] = useState([])
const [completedTask, setCompletedTask] = useState(null)

Expand All @@ -32,11 +32,25 @@ function TaskGrid({ tasks = [], flippableTask = true, className = '' }) {
completedBy: task.completedBy,
completedDate: firebase.firestore.FieldValue.serverTimestamp()
})
const totalHelpCoins = userLoggedIn.coins + task.rewardPoints
updateHelpCoins(totalHelpCoins)

} catch (err) {
console.log(err)
}
}

function updateHelpCoins(amount) {
firebase
.firestore()
.collection('users')
.doc(userLoggedIn.id)
.update({
coins: amount
})
setUserLoggedIn({ ...userLoggedIn, coins: amount})
}

function completeTask(task) {
setCompletedTask(task)
const newTasks = taskGridtasks.map(function completeSingleTask(t) {
Expand Down
5 changes: 3 additions & 2 deletions react-app/src/domain/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import JackImg from '../assets/users/[email protected]'
export const ADMIN_USER = 'admin'
export const REGULAR_USER = 'regular'

export function createUser({id, name, age, role}) {
export function createUser({id, name, age, role, coins}) {
return {
id,
name,
age,
role,
image: getUserImage(name)
image: getUserImage(name),
coins
}
}

Expand Down
90 changes: 85 additions & 5 deletions react-app/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ body {
padding: 0 0.5rem;
}

.pointer {
cursor: pointer;
}

.task-option-btn {
display: flex;
flex-direction: column;
Expand All @@ -70,6 +74,8 @@ body {
background: none;
border: none;
padding: 0;

cursor: pointer;
}

.task-option-btn.cross-option {
Expand Down Expand Up @@ -227,6 +233,77 @@ body {
color: #555555;
}

/* Password Avatar */
.password-avatar-container {
position: relative;
display: flex;
justify-content: center;
align-items: center;
}

.default-btn {
padding: 0;
border: 0;
background: none;
}

.back-arrow-btn {
position: absolute;
top: 0;
left: 0;
margin-top: -3.5rem;
margin-left: -1rem;
cursor: pointer;
}

.password-form {
margin-top: 1rem;
}

.textbox {
width: 16rem;
height: 2.25rem;
border: 2px solid #555555;
}

.password-textbox {
padding: 0 1rem;
padding-right: 2rem;
letter-spacing: 0.2em;
font-size: 0.75rem;
}

.sign-in-container {
display: flex;
align-items: center;
margin-top: 3rem;
}

.sign-in-container .btn{
margin-left: 1rem;
}

.password-textbox-container {
position: relative;
}

.show-password-btn {
position: absolute;
right: 0;
top: 0;
margin-top: 0.75rem;
margin-right: 0.5rem;
opacity: 0.6;
}

.show-password-btn:hover {
opacity: 1;
}

.show-password-btn .visible {
opacity: 1;
}

/* Sidebar */

.sidebar {
Expand Down Expand Up @@ -268,14 +345,17 @@ body {
font-weight: 300;
}

.help-point-btn {
margin-top: 0.25rem;
padding: 0.5rem 0.5rem;
color: white;
.btn {
height: 2.25rem;
font-size: inherit;
font-weight: 600;
background-color: #FF8933;
border: none;
color: white;
}

.help-point-btn {
margin-top: 0.25rem;
font-weight: 600;
}

.leave-btn {
Expand Down
22 changes: 16 additions & 6 deletions react-app/src/pages/LoginPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import { createUser, ADMIN_USER } from '../domain/User'
import RoundedAvatar from '../components/RoundedAvatar'
import LoadingAnimation from '../components/LoadingAnimation'
import firebase from 'firebase'
import PasswordAvatar from '../components/PasswordAvatar';

function LoginPage({ logUserIn }) {
const history = useHistory()
const [users, setUsers] = useState([])
const [isLoading, setIsLoading] = useState(true)
const [adminUser, setAdminUser] = useState(null)

useEffect(() => {
function fetchUsers() {
Expand All @@ -29,10 +31,14 @@ function LoginPage({ logUserIn }) {

function onAvatarImgClick(user) {
if (user.role === ADMIN_USER) {
history.push('/secure-login')
setAdminUser(user)
} else {
history.replace('/')
logUser(user)
}
}

function logUser(user) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest using signUserIn as a name, instead of logUser.

Log by itself is the same log as in console.log - on the first view of the PR I didn't spot this and thought it some debug thing.

history.replace('/')
logUserIn(user)
}

Expand All @@ -42,10 +48,14 @@ function LoginPage({ logUserIn }) {
{isLoading ? (
<LoadingAnimation />
): (
<div className='users-container'>
{users && users.map((user) =>
(<RoundedAvatar key={user.id} user={user} onImgClick={() => onAvatarImgClick(user)}/>))}
</div>
(adminUser) ? (
<PasswordAvatar user={adminUser} onBack={() => setAdminUser(null)}/>
) : (
<div className='users-container'>
{users && users.map((user) =>
(<RoundedAvatar key={user.id} user={user} onImgClick={() => onAvatarImgClick(user)}/>))}
</div>
)
)}
</div>
<div className='login-side-img'></div>
Expand Down