Skip to content

Commit

Permalink
Merge branch 'Dev' of https://github.com/ConnectingPets/PetsMatch int…
Browse files Browse the repository at this point in the history
…o PET-30-BE-Write-UTests
  • Loading branch information
AntonTodorov321 committed Jan 18, 2024
2 parents e99de62 + 7c908f9 commit 588ad11
Show file tree
Hide file tree
Showing 13 changed files with 281 additions and 110 deletions.
2 changes: 2 additions & 0 deletions Application/DTOs/User/UserDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@ public class UserDto
public string? Gender { get; set; }

public string? JobTitle { get; set; }

public string[] Roles { get; set; } = null!;
}
}
8 changes: 7 additions & 1 deletion Application/User/LoginUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,20 @@ public class LoginUserCommand : IRequest<Result<UserDto>>
public class LoginUserHandler : IRequestHandler<LoginUserCommand, Result<UserDto>>
{
private readonly SignInManager<User> signInManager;
private readonly UserManager<User> userManager;
private readonly IRepository repository;
private readonly ITokenService tokenService;

public LoginUserHandler(
SignInManager<User> signInManager,
UserManager<User> userManager,
IRepository repository,
ITokenService tokenService)
{
this.repository = repository;
this.signInManager = signInManager;
this.tokenService = tokenService;
this.userManager = userManager;
}

public async Task<Result<UserDto>> Handle(LoginUserCommand request, CancellationToken cancellationToken)
Expand All @@ -64,6 +67,8 @@ public async Task<Result<UserDto>> Handle(LoginUserCommand request, Cancellation
return Result<UserDto>.Failure(FailedLogin);
}

string[] roles = (await userManager.GetRolesAsync(user)).ToArray();

UserDto userDto = new UserDto
{
Name = user.Name,
Expand All @@ -74,7 +79,8 @@ public async Task<Result<UserDto>> Handle(LoginUserCommand request, Cancellation
JobTitle = user.JobTitle,
Gender = user.Gender.ToString(),
Education = user.Education,
Age = user.Age
Age = user.Age,
Roles = roles
};

return Result<UserDto>.Success(userDto);
Expand Down
3 changes: 2 additions & 1 deletion Application/User/RegisterUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ public async Task<Result<UserDto>> Handle(RegisterUserCommand request, Cancellat
UserDto userDto = new UserDto
{
Name = user.Name,
Token = this.tokenService.CreateToken(user)
Token = this.tokenService.CreateToken(user),
Roles = roles
};

foreach (var role in roles)
Expand Down
4 changes: 3 additions & 1 deletion client-app/src/api/axiosAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ const requests = {
};

const apiUser = {
getUserProfile: () => requests.get('/api/profile'),
register: (userData: IUser) => requests.post('/api/user/register', userData, headers.appJSON),
login: (userData: IUser) => requests.post('/api/user/login', userData, headers.appJSON),
logout: (body: object) => requests.post('/api/user/logout', body, headers.appJSON),
editUser: (body: IUser) => requests.patch('/api/profile/edit', body, headers.appJSON),
deleteUser: () => requests.del('/api/profile/delete')
deleteUser: () => requests.del('/api/profile/delete'),
deleteRole: (role: string) => requests.del(`/api/profile/deleterole?rolename=${role}`)
};

const apiAnimal = {
Expand Down
68 changes: 41 additions & 27 deletions client-app/src/components/Login/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,41 @@ export const Login: React.FC<LoginProps> = ({
const navigate = useNavigate();

const onSubmit = async (values: IUser) => {
const { RememberMe, ...userData } = values;

try {
const result = await agent.apiUser.login(values);
const result = await agent.apiUser.login(userData);

if (result.isSuccess) {
const {
name: Name,
photoUrl: PhotoUrl,
age: Age,
jobTitle: JobTitle,
city: City,
address: Address,
education: Education,
gender: Gender,
token
} = result.data;
const Email = values.Email;

userStore.setUser({
Name,
PhotoUrl,
Age,
Email,
JobTitle,
City,
Address,
Education,
Gender
}, token);
if (RememberMe) {
const {
name: Name,
photoUrl: PhotoUrl,
age: Age,
jobTitle: JobTitle,
city: City,
address: Address,
education: Education,
gender: Gender,
token
} = result.data;
const Email = values.Email;

userStore.setUser({
Name,
PhotoUrl,
Age,
Email,
JobTitle,
City,
Address,
Education,
Gender,
Roles: []
}, token);
}

navigate('/dashboard');
navigate('/dashboard');
} else {
toast.error(result.errorMessage);
}
Expand Down Expand Up @@ -91,8 +96,17 @@ export const Login: React.FC<LoginProps> = ({
)}
</Field>

<Field name='RememberMe'>
{({ input }) => (
<div>
<CLabel inputName='RememberMe' title='Remember me' />
<input type="checkbox" {...input} name='RememberMe' id='RememberMe' />
</div>
)}
</Field>

<CSubmitButton textContent='Login' />
<p className='account__message' onClick={showRegister}>If you don't have an account click here!</p>
<p className='account__message' onClick={showRegister}>Register Now!</p>
</form>
)
} />
Expand Down
84 changes: 52 additions & 32 deletions client-app/src/components/Register/Register.scss
Original file line number Diff line number Diff line change
@@ -1,35 +1,55 @@
@use '../../global-styles/' as gs;
@use "../../global-styles/" as gs;

.register__form {
@include gs.set-form(100%, center, 85%, column, center,gs.$dark-shadow);

&__section {
width: 19rem;
}

&__input{
width: 100%;
border-radius: 1.5rem;
padding: 0.3rem;
border: 1px solid lightgrey;
text-align: center;

&:focus{
background: gs.$light-gradient-second-transparent;
border: 1px solid gs.$light-button-color;
}

&::placeholder{
text-align: center;
}
}

&__error__message{
margin-top: 0.4rem;
font-size: 0.7rem;
font-weight: 500;
color: rgb(138, 0, 0);
text-transform: capitalize;
text-align: center;
}
@include gs.set-form(100%, center, 85%, column, center, gs.$dark-shadow);

&__section {
width: 19rem;
}

&__input {
width: 100%;
border-radius: 1.5rem;
padding: 0.3rem;
border: 1px solid lightgrey;
text-align: center;

&:focus {
background: gs.$light-gradient-second-transparent;
border: 1px solid gs.$light-button-color;
}

&::placeholder {
text-align: center;
}
}

&__error__message {
margin-top: 0.4rem;
font-size: 0.7rem;
font-weight: 500;
color: rgb(138, 0, 0);
text-transform: capitalize;
text-align: center;
}

&__roles {
@include gs.display-flex(column, none, center);
margin-bottom: 1rem;

&__matching {
margin-bottom: 0.25rem;

&>label {
font-weight: 500;
}
}
&__marketplace {
@include gs.display-flex(column, none, center);

&>div>label {
font-weight: 500;
}
}
}
}
57 changes: 36 additions & 21 deletions client-app/src/components/Register/Register.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import 'react-toastify/dist/ReactToastify.css';
import { IUser } from '../../interfaces/Interfaces';
import { registerFormValidator } from '../../validators/userProfileFormValidators';
import agent from '../../api/axiosAgent';
import userStore from '../../stores/userStore';

import { CLabel } from '../common/CLabel/CLabel';
import { CSubmitButton } from '../common/CSubmitButton/CSubmitButton';
Expand All @@ -18,23 +17,15 @@ interface RegisterProps {
showLogin: () => void;
}

export const Register: React.FC<RegisterProps> = observer(({
showLogin
}) => {
export const Register: React.FC<RegisterProps> = observer(() => {
const navigate = useNavigate();

const onSubmit = async (values: IUser) => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
// const { ConfirmPassword, ...userData } = values;

try {
const result = await agent.apiUser.register(values);

if (result.isSuccess) {
const { name: Name, token } = result.data;
const Email = values.Email;

userStore.setUser({ Name, Email }, token);

navigate('/dashboard');
} else {
Expand All @@ -48,19 +39,19 @@ export const Register: React.FC<RegisterProps> = observer(({
};

return (
<section className='register__form__section'>
<section className="register__form__section">
<Form
onSubmit={onSubmit}
validate={registerFormValidator}
render={({ handleSubmit }) => (
<form className='register__form' onSubmit={handleSubmit}>
<form className="register__form" onSubmit={handleSubmit}>

<Field name="Name">
{({ input, meta }) => (
<div>
<CLabel inputName={'Name'} title={'Name'} />
<input className='register__form__input' type="text" {...input} name='Name' id='Name' placeholder="John Sillver" />
{meta.touched && meta.error && <div className='register__form__error__message'>{meta.error}</div>}
<input className="register__form__input" type="text" {...input} name="Name" id="Name" placeholder="John Sillver" />
{meta.touched && meta.error && <div className="register__form__error__message">{meta.error}</div>}
</div>
)}
</Field>
Expand All @@ -69,8 +60,8 @@ export const Register: React.FC<RegisterProps> = observer(({
{({ input, meta }) => (
<div>
<CLabel inputName={'Email'} title={'Email'} />
<input className='register__form__input' type="text" {...input} name='Email' id='Email' placeholder="[email protected]" />
{meta.touched && meta.error && <div className='register__form__error__message'>{meta.error}</div>}
<input className="register__form__input" type="text" {...input} name="Email" id="Email" placeholder="[email protected]" />
{meta.touched && meta.error && <div className="register__form__error__message">{meta.error}</div>}
</div>
)}
</Field>
Expand All @@ -79,8 +70,8 @@ export const Register: React.FC<RegisterProps> = observer(({
{({ input, meta }) => (
<div>
<CLabel inputName={'Password'} title={'Password'} />
<input className='register__form__input' type="password" {...input} name='Password' id='Password' placeholder="* * * * * * *" />
{meta.touched && meta.error && <div className='register__form__error__message'>{meta.error}</div>}
<input className="register__form__input" type="password" {...input} name="Password" id="Password" placeholder="* * * * * * *" />
{meta.touched && meta.error && <div className="register__form__error__message">{meta.error}</div>}
</div>
)}
</Field>
Expand All @@ -89,14 +80,38 @@ export const Register: React.FC<RegisterProps> = observer(({
{({ input, meta }) => (
<div>
<CLabel inputName={'ConfirmPassword'} title={'Confirm Password'} />
<input className='register__form__input' type="password" {...input} name='ConfirmPassword' id='ConfirmPassword' placeholder="* * * * * * *" />
{meta.touched && meta.error && <div className='register__form__error__message'>{meta.error}</div>}
<input className="register__form__input" type="password" {...input} name="ConfirmPassword" id="ConfirmPassword" placeholder="* * * * * * *" />
{meta.touched && meta.error && <div className="register__form__error__message">{meta.error}</div>}
</div>
)}
</Field>

<section className="register__form__roles">
<CLabel inputName={''} title={'Select Role'} />

<Field type="checkbox" name="Roles" value="Matching">
{({ input }) => (
<div className="register__form__roles__matching">
<label htmlFor='Roles'>Matching</label>
<input type="checkbox" {...input} name="Roles" value="Matching" className="checkbox__input" />
</div>
)}
</Field>

<Field type="checkbox" name="Roles" value="Marketplace">
{({ input, meta }) => (
<div className="register__form__roles__marketplace">
<div>
<label htmlFor='Roles'>Marketplace</label>
<input type="checkbox" {...input} name="Roles" value="Marketplace" className="checkbox__input" />
</div>
{meta.touched && meta.error && <div className="register__form__error__message">{meta.error}</div>}
</div>
)}
</Field>
</section>

<CSubmitButton textContent='Register' />
<p className='account__message' onClick={showLogin}>If you have an account click here!</p>
</form>
)} />
</section>
Expand Down
Loading

0 comments on commit 588ad11

Please sign in to comment.