Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
Bataille Navale Front
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Taha BELKHIRI
Bataille Navale Front
Commits
214def84
Commit
214def84
authored
Mar 7, 2023
by
Taha
Browse files
Options
Downloads
Patches
Plain Diff
finished linking frontend to api in menu section
parent
b72a2b36
Branches
link_Menu_API
Branches containing commit
No related tags found
1 merge request
!2
Link menu api
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/components/CreateGame.js
+68
-126
68 additions, 126 deletions
src/components/CreateGame.js
src/components/JoinGame.js
+159
-75
159 additions, 75 deletions
src/components/JoinGame.js
src/index.js
+39
-28
39 additions, 28 deletions
src/index.js
with
266 additions
and
229 deletions
src/components/CreateGame.js
+
68
−
126
View file @
214def84
import
'
./CreateGame.css
'
;
import
React
from
'
react
'
;
import
ReturnButton
from
'
./ReturnButton
'
;
class
CreateGame
extends
React
.
Component
{
constructor
(
props
)
{
super
(
props
);
this
.
state
=
{
value
:
''
};
this
.
handleChange
=
this
.
handleChange
.
bind
(
this
);
this
.
handleSubmit
=
this
.
handleSubmit
.
bind
(
this
);
}
handleChange
(
event
)
{
this
.
setState
({
username
:
event
.
target
.
form
[
0
].
value
});
}
/*createGame(event) {
this.setState({isGameStarted: true});
console.log("Creating game");
function
CreateGame
(
props
)
{
function
handleCreateGame
(
event
)
{
event
.
preventDefault
();
const
usernameInput
=
document
.
getElementById
(
'
username
'
);
const
username
=
usernameInput
.
value
;
const
url
=
'
https://localhost:7080/api/game/
'
;
fetch
(
url
,
{
method
:
'
POST
'
,
headers
:
{
'
Content-Type
'
:
'
application/json
'
},
body
:
JSON
.
stringify
({
username
})
})
.then(response =>
if(response.status == 200){
await;
.
then
(
response
=>
{
if
(
response
.
ok
)
{
return
response
;
}
throw
new
Error
(
'
Network response was not ok.
'
);
})
.
then
(
response
=>
response
.
text
())
.
then
(
data
=>
{
// Manipuler les données de la réponse
console.log(data);
})
.then()
.catch(error => console.error('Error:', error))
if(responseCOde = 200){
const id_game = response.id;
const id_player = response.playerId;
//afficher l'id de la game
//mettre le joueur dans la game
url = 'https://localhost:7080/api/game/'+ id_game + '?playerId=' + id_player;
fetch(url, {
props
.
gameIdCallBack
(
data
);
fetch
(
'
https://localhost:7080/api/game/
'
+
data
+
'
?playerId=1
'
,
{
method
:
'
PUT
'
,
headers
:
{
'
Content-Type
'
:
'
application/json
'
},
})
}
}*/
createGame
(
event
)
{
this
.
setState
({
isGameStarted
:
true
});
console
.
log
(
"
Creating game
"
);
const
url
=
'
https://localhost:7080/api/game/
'
;
fetch
(
url
,
{
method
:
'
POST
'
,
headers
:
{
'
Content-Type
'
:
'
application/json
'
},
body
:
JSON
.
stringify
({
username
})
})
.
then
(
response
=>
{
if
(
response
.
status
==
200
)
{
return
response
.
json
();
// renvoie la réponse sous forme de JSON
}
else
{
throw
new
Error
(
'
Erreur lors de la création de la partie
'
);
if
(
response
.
ok
)
{
return
response
;
}
throw
new
Error
(
response
.
text
());
})
.
catch
(
error
=>
{
alert
(
error
);
// dont execute the rest of the function
return
Promise
.
reject
(
error
);})
.
then
(
response
=>
response
.
text
())
.
then
(
data
=>
{
const
id_game
=
data
.
id
;
const
id_player
=
data
.
playerId
;
console
.
log
(
'
ID de la partie :
'
,
id_game
);
// Ajouter le joueur à la partie
const
url_join
=
`https://localhost:7080/api/game/
${
id_game
}
?playerId=
${
id_player
}
`
;
return
fetch
(
url_join
,
{
method
:
'
PUT
'
,
headers
:
{
'
Content-Type
'
:
'
application/json
'
},
});
props
.
playerIdCallBack
(
1
);
})
.
then
(
response
=>
{
if
(
response
.
status
==
200
)
{
console
.
log
(
'
Le joueur a été ajouté à la partie
'
);
}
else
{
throw
new
Error
(
'
Erreur lors de l
\'
ajout du joueur à la partie
'
);
}
.
catch
(
error
=>
alert
(
error
));
})
.
catch
(
error
=>
console
.
error
(
'
Error:
'
,
error
));
.
catch
(
error
=>
alert
(
error
));
}
render
()
{
return
(
<
React
.
StrictMode
>
<
>
<
div
>
<
button
className
=
"
button
"
type
=
"
button
"
onClick
=
{
R
eturnButton
}
>
<
button
className
=
"
button
"
type
=
"
button
"
onClick
=
{
props
.
r
eturnButton
CallBack
}
>
BACK
<
/button
>
<
/div
>
...
...
@@ -118,20 +63,17 @@ class CreateGame extends React.Component {
<
div
className
=
"
CreateGameForm
"
>
<
form
id
=
'
create
'
>
<
label
>
<
input
type
=
"
text
"
placeholder
=
'
username
'
value
=
{
this
.
state
.
value
}
onChange
=
{
this
.
handleChange
}
/
>
<
input
type
=
"
text
"
id
=
"
username
"
placeholder
=
'
username
'
/>
<
/label
>
<
br
/>
<
input
type
=
"
submit
"
value
=
"
Submit
"
/>
<
/form
>
<
button
className
=
"
button
"
type
=
"
button
"
onClick
=
{
this
.
c
reateGame
}
>
<
button
className
=
"
button
"
type
=
"
button
"
onClick
=
{
handleC
reateGame
}
>
CREATE
<
/button
>
<
/div
>
<
/
React.StrictMode
>
<
/
>
);
}
}
export
default
CreateGame
;
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/components/JoinGame.js
+
159
−
75
View file @
214def84
...
...
@@ -4,50 +4,54 @@ import React from 'react';
import
Container
from
'
../Container
'
;
import
ReturnButton
from
'
./ReturnButton
'
;
import
{
useState
,
useEffect
}
from
'
react
'
;
function
JoinGame
(
props
)
{
const
[
username
,
setUsername
]
=
useState
(
''
);
const
[
gameid
,
setGameId
]
=
useState
(
''
);
const
[
isGameStarted
,
setIsGameStarted
]
=
useState
(
false
);
class
JoinGame
extends
React
.
Component
{
constructor
(
props
)
{
super
(
props
);
this
.
state
=
{
username
:
''
,
gameid
:
''
,
isGameStarted
:
false
};
this
.
handleChange
=
this
.
handleChange
.
bind
(
this
);
this
.
joinGame
=
this
.
joinGame
.
bind
(
this
);
function
handleChange
(
event
)
{
setUsername
(
event
.
target
.
form
[
0
].
value
);
setGameId
(
event
.
target
.
form
[
1
].
value
);
}
handleChange
(
event
)
{
this
.
setState
({
username
:
event
.
target
.
form
[
0
].
value
,
gameid
:
event
.
target
.
form
[
1
].
value
});
}
joinGame
(
event
)
{
this
.
setState
({
isGameStarted
:
true
});
function
joinGame
(
event
)
{
setIsGameStarted
(
true
);
console
.
log
(
"
Joining game
"
);
const
url
=
'
https://localhost:7080/api/game/
'
+
this
.
state
.
gameid
+
'
/
'
+
this
.
state
.
username
;
const
url
=
'
https://localhost:7080/api/game/
'
+
gameid
+
'
?playerId=2
'
;
console
.
log
(
url
)
fetch
(
url
,
{
method
:
'
P
OS
T
'
,
method
:
'
P
U
T
'
,
headers
:
{
'
Content-Type
'
:
'
application/json
'
}
,
}
})
.
then
(
response
=>
response
.
json
())
.
catch
(
error
=>
console
.
error
(
'
Error:
'
,
error
))
.
then
(
async
response
=>
{
if
(
response
.
ok
)
{
return
response
;
}
const
message
=
await
response
.
text
();
throw
new
Error
(
message
);
})
.
catch
(
error
=>
{
alert
(
error
);
// dont execute the rest of the function
return
Promise
.
reject
(
error
);})
.
then
(
async
response
=>
{
const
data
=
await
response
.
text
();
props
.
gameIdCallBack
(
data
);
props
.
playerIdCallBack
(
2
);
})
.
catch
(
error
=>
{
alert
(
error
);
});
}
//'https://localhost:7080/api/game/'+ this.state.username, pour créer une game
render
()
{
if
(
this
.
state
.
isGameStarted
)
{
if
(
isGameStarted
)
{
return
(
<
Container
/>
);
...
...
@@ -67,27 +71,107 @@ class JoinGame extends React.Component {
<
label
>
<
input
type
=
"
text
"
placeholder
=
'
Username
'
value
=
{
this
.
state
.
username
}
onChange
=
{
this
.
handleChange
}
/
>
value
=
{
username
}
onChange
=
{
handleChange
}
/
>
<
/label
>
<
br
/>
<
label
>
<
input
type
=
"
text
"
placeholder
=
'
GameId
'
value
=
{
this
.
state
.
gameid
}
onChange
=
{
this
.
handleChange
}
/
>
value
=
{
gameid
}
onChange
=
{
handleChange
}
/
>
<
/label
>
<
br
/>
<
input
className
=
"
button
"
type
=
"
submit
"
value
=
"
Join
"
onClick
=
{
joinGame
}
/
>
<
/form
>
<
button
className
=
"
button
"
type
=
"
button
"
onClick
=
{
this
.
joinGame
}
>
JOIN
<
/button
>
<
/div
>
<
/React.StrictMode
>
);
}
}
// class JoinGame extends React.Component {
// constructor(props) {
// super(props);
// this.state = {username: '',
// gameid: '',
// isGameStarted: false};
// this.handleChange = this.handleChange.bind(this);
// this.joinGame = this.joinGame.bind(this);
// }
// handleChange(event) {
// this.setState({username: event.target.form[0].value,
// gameid: event.target.form[1].value});
// }
// joinGame(event) {
// this.setState({isGameStarted: true});
// console.log("Joining game");
// const url = 'https://localhost:7080/api/game/' + this.state.gameid + '/' + this.state.username;
// console.log(url)
// fetch(url, {
// method: 'POST',
// headers: {
// 'Content-Type': 'application/json'
// },
// })
// .then(response =>
// response.json())
// .catch(error => console.error('Error:', error))
// }
// //'https://localhost:7080/api/game/'+ this.state.username, pour créer une game
// render() {
// if (this.state.isGameStarted) {
// return (
// <Container />
// );
// }
// return (
// <React.StrictMode>
// <div>
// <button className="button" type="button" onClick={ReturnButton}>
// BACK
// </button>
// </div>
// <div className = "Title">
// <h1>SPATIAL WAR</h1>
// </div>
// <div className = "JoinGameForm">
// <form /*onSubmit={this.handleSubmit}*/>
// <label>
// <input type="text"
// placeholder='Username'
// value={this.state.username}
// onChange={this.handleChange} />
// </label>
// <br/>
// <label>
// <input type="text"
// placeholder='GameId'
// value={this.state.gameid}
// onChange={this.handleChange} />
// </label>
// <br/>
// </form>
// <button className="button" type="button" onClick={ this.joinGame }>
// JOIN
// </button>
// </div>
// </React.StrictMode>
// );
// }
// }
...
...
This diff is collapsed.
Click to expand it.
src/index.js
+
39
−
28
View file @
214def84
...
...
@@ -7,48 +7,59 @@ import App from "./App";
import
JoinGame
from
'
./components/JoinGame
'
;
import
CreateGame
from
'
./components/CreateGame
'
;
import
reportWebVitals
from
'
./reportWebVitals
'
;
import
{
useState
,
useEffect
}
from
'
react
'
;
import
'
./index.css
'
;
function
Menu
()
{
const
[
page
,
setPage
]
=
useState
(
'
menu
'
);
const
[
playerId
,
setPlayerId
]
=
useState
(
null
);
const
[
gameId
,
setGameId
]
=
useState
(
null
);
function
JoinGameMenu
(){
root
.
render
(
<
React
.
StrictMode
>
<
JoinGame
/>
<
/React.StrictMode
>
);
function
returnButtonCallBack
()
{
setPage
(
'
menu
'
);
}
function
CreateGameMenu
(){
root
.
render
(
<
React
.
StrictMode
>
<
CreateGame
/>
<
/React.StrictMode
>
);
function
playerIdCallBack
(
playerId
)
{
setPlayerId
(
playerId
);
console
.
log
(
"
playerId:
"
+
playerId
);
setPage
(
'
game
'
);
}
function
gameIdCallBack
(
gameId
)
{
console
.
log
(
"
gameId:
"
+
gameId
);
setGameId
(
gameId
);
}
const
root
=
ReactDOM
.
createRoot
(
document
.
getElementById
(
'
root
'
));
function
Menu
(){
root
.
render
(
<
React
.
StrictMode
>
if
(
page
===
'
menu
'
)
{
return
(
<>
<
div
className
=
"
Title
"
>
<
h1
>
SPATIAL
WAR
<
/h1
>
<
/div
>
<
div
className
=
"
Buttons
"
>
<
button
className
=
"
button
"
type
=
"
button
"
onClick
=
{
JoinGameMenu
}
>
<
button
className
=
"
button
"
type
=
"
button
"
onClick
=
{
()
=>
setPage
(
'
join
'
)
}
>
JOIN
GAME
<
/button
>
<
button
className
=
"
button
"
type
=
"
button
"
onClick
=
{
CreateGameMenu
}
>
<
button
className
=
"
button
"
type
=
"
button
"
onClick
=
{
()
=>
setPage
(
'
create
'
)
}
>
CREATE
GAME
<
/button
>
<
/div
>
<
/React.StrictMode
>
);
<
/>
)
;
}
else
if
(
page
===
'
join
'
)
{
return
<
JoinGame
returnButtonCallBack
=
{
returnButtonCallBack
}
playerIdCallBack
=
{
playerIdCallBack
}
gameIdCallBack
=
{
gameIdCallBack
}
/>
;
}
else
if
(
page
===
'
create
'
)
{
return
<
CreateGame
returnButtonCallBack
=
{
returnButtonCallBack
}
playerIdCallBack
=
{
playerIdCallBack
}
gameIdCallBack
=
{
gameIdCallBack
}
/>
;
}
else
if
(
page
===
'
game
'
)
{
return
<
App
playerId
=
{
playerId
}
gameId
=
{
gameId
}
/>
;
}
}
const
root
=
ReactDOM
.
createRoot
(
document
.
getElementById
(
'
root
'
));
root
.
render
(
<
React
.
StrictMode
>
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment