Skip to content

Commit

Permalink
Merge pull request #41 from jordus100/games-migration
Browse files Browse the repository at this point in the history
Fix stuff in Games, handle user role
  • Loading branch information
StanislawMalinski authored Apr 28, 2024
2 parents 88a0005 + 6433884 commit aea0010
Show file tree
Hide file tree
Showing 14 changed files with 44 additions and 32 deletions.
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"axios": "^1.6.5",
"jwt-decode": "^4.0.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-redux": "^8.1.3",
Expand Down
2 changes: 1 addition & 1 deletion src/Tournaments/TournamentDetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getListOfTournaments } from './getListOfTournaments';
import { useParams } from 'react-router-dom';
import { connect } from 'react-redux';
import { Link } from 'react-router-dom';
import { login, logout } from '../User/actions';
import { login, logout } from '../User/store';
function TournamentDetails({isAuthenticated, user, login, logout }) {

const { tournamentId } = useParams();
Expand Down
2 changes: 1 addition & 1 deletion src/Tournaments/TournamentNav.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import './TournamentNav.scss';
import React from "react";
import { NavLink, Link } from 'react-router-dom';
import { login, logout } from '../User/actions';
import { login, logout } from '../User/store';
import { connect } from 'react-redux';


Expand Down
2 changes: 1 addition & 1 deletion src/User/User.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import '../App.scss'
import { connect } from 'react-redux';
import { login, logout } from './actions';
import { login, logout } from './store';
import {Link} from "react-router-dom";

function User({ isAuthenticated, user, login, logout }) {
Expand Down
2 changes: 1 addition & 1 deletion src/User/UserButtons.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import logoutIcon from '../resources/logout.svg';
import userIcon from '../resources/user.svg';
import settingsIcon from '../resources/settings.svg';
import { connect } from 'react-redux';
import { login, logout } from './actions';
import { login, logout } from './store';
import {Link} from "react-router-dom";

function UserButtons({ isAuthenticated, user, login, logout }) {
Expand Down
10 changes: 0 additions & 10 deletions src/User/actions.js

This file was deleted.

13 changes: 10 additions & 3 deletions src/User/store.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// store.js
import { createStore } from 'redux';
import { persistStore, persistReducer } from 'redux-persist';
import storage from 'redux-persist/lib/storage/session'; // Use sessionStorage
import storage from 'redux-persist/lib/storage/session';

const initialState = {
isAuthenticated: false,
Expand Down Expand Up @@ -35,5 +34,13 @@ const persistConfig = {
const persistedReducer = persistReducer(persistConfig, authReducer);
const store = createStore(persistedReducer);
export const persistor = persistStore(store);

export default store;

export const login = (user) => ({
type: 'LOGIN',
payload: user,
});

export const logout = () => ({
type: 'LOGOUT',
});
2 changes: 1 addition & 1 deletion src/forms/AddTournamentForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import React, { useState } from 'react';
import './Form.scss';
import { connect } from 'react-redux';
import { login, logout } from '../User/actions';
import { login, logout } from '../User/store';
import {TournamentService} from "../services/TournamentService";


Expand Down
7 changes: 1 addition & 6 deletions src/forms/EditTournamentForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useState, useEffect } from 'react';
import { useParams } from 'react-router-dom';
import './Form.scss';
import { connect } from 'react-redux';
import { login, logout } from '../User/actions';
import { login, logout } from '../User/store';
import { getListOfTournaments } from '../Tournaments/getListOfTournaments';

function EditTournamentForm({ isAuthenticated, user, login, logout }) {
Expand All @@ -23,7 +23,6 @@ function EditTournamentForm({ isAuthenticated, user, login, logout }) {
const [date, setDate] = useState('');

useEffect(() => {
// Initialize state based on tournament data when available
if (tournament) {
setTitle(tournament.name);
setDescription(tournament.description);
Expand All @@ -37,8 +36,6 @@ function EditTournamentForm({ isAuthenticated, user, login, logout }) {
return <div>Resource not allowed :( </div>;
}

// Handle form submissions and updates here

return (
<div className="form">
<h1>Dodaj nowy turniej</h1>
Expand Down Expand Up @@ -105,8 +102,6 @@ function EditTournamentForm({ isAuthenticated, user, login, logout }) {
/>
</div>

{/* Add logic for restrictions if needed */}

<div className="form-group actions">
<button type="submit" className="submit">Dodaj turniej</button>
<button type="button" className="cancel">Anuluj</button>
Expand Down
5 changes: 2 additions & 3 deletions src/forms/LoginForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import './Form.scss'
import React, { useState } from "react";
import {UserService} from "../services/UserService";
import { connect } from 'react-redux';
import { login, logout } from '../User/actions';
import { login, logout } from '../User/store';

function LoginForm({isAuthenticated, user, login, logout}) {

Expand All @@ -15,8 +15,7 @@ function LoginForm({isAuthenticated, user, login, logout}) {
e.preventDefault();

try {
const response = await UserService.loginUser(email, password)
login({ email: email , token: response.data.data})
await UserService.loginUser(email, password)
setMessage('Login succesful.')
} catch (e) {
setMessage('There was a problem with login.')
Expand Down
8 changes: 5 additions & 3 deletions src/lists/GamesList.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import './List.scss'
import DeleteGameButton from './DeleteGameButton';
import { Link } from 'react-router-dom';
import { connect } from 'react-redux';
import { login, logout } from '../User/actions';
import { login, logout } from '../User/store';
import React, { useState, useEffect } from "react";
import {GameService} from "../services/GameService";
function GamesList({games, isAuthenticated, user, login, logout }) {
Expand All @@ -28,15 +28,17 @@ function GamesList({games, isAuthenticated, user, login, logout }) {
<h1>Games</h1>
<div className="menu-btns container-list">
<div className="item-list">
{isAuthenticated && (
{isAuthenticated && user.role === 'Admin' && (
<div className="menu-btns list-element btn">
<Link className="item-name add-btn color-primary-3" to="/games/add">Dodaj grę</Link>
</div>
)}
{gamesList.map((game, index) => (
<div key={index} className="menu-btns list-element btn">
<button className="item-name color-primary-3 btn">{game.gameFile}</button>
<button className="item-name color-primary-3 btn">{game.gameFileName}</button>
{isAuthenticated && user.role === 'Admin' && (
<DeleteGameButton gameId={index} />
)}
</div>
))}
</div>
Expand Down
1 change: 0 additions & 1 deletion src/services/Api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import axios from "axios";
import c from "./client.config.json";
const baseURL = c["protocol"] + "://" + c["host"] + ":" + c["port"] + "/" + c["path"] + c["version"];


export const Api = axios.create({
baseURL: '/api/v1/',
withCredentials: true
Expand Down
12 changes: 11 additions & 1 deletion src/services/UserService.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import {jwtDecode} from 'jwt-decode'
import { Api } from './Api'
import {login} from "../User/store";
import store from '../User/store'
export const UserService = {

registerUser: async function (username, email, password, roleId) {
Expand All @@ -17,10 +20,17 @@ export const UserService = {
},
loginUser: async function (email, password) {
try {
return await Api.post('Player/login', {
const response = await Api.post('Player/login', {
email: email,
password: password,
})
const token = response.data.data
const jwtData = jwtDecode(token)
store.dispatch(login({
email: email,
token: token,
role: jwtData['http://schemas.microsoft.com/ws/2008/06/identity/claims/role']
}))
} catch(e) {
return Api.processError(e)
}
Expand Down

0 comments on commit aea0010

Please sign in to comment.