Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
IT2810 H21
Team 29
iWhatToWatch
Commits
51dbbaf2
Commit
51dbbaf2
authored
Nov 22, 2021
by
kavusi98
Browse files
(
#15
) Fixed bug when favorizing a movie
Co-authored-by:
dherikk
<
dherikk@users.noreply.github.com
>
parent
2fb3d1cd
Changes
2
Hide whitespace changes
Inline
Side-by-side
frontend/src/components/favButton/index.tsx
View file @
51dbbaf2
...
...
@@ -6,7 +6,8 @@ interface FavButtonProps {
isFavorited
:
boolean
;
userName
:
string
;
id
:
string
;
}
onPressed
:
(
newValue
:
boolean
)
=>
void
;
}
/**
* This is a component for favoriting a movie. We take in three props.
...
...
@@ -15,7 +16,7 @@ interface FavButtonProps {
* @param isFavorited, userName, id
* @returns a button with Heart.
*/
const
FavButton
:
React
.
FC
<
FavButtonProps
>
=
({
isFavorited
,
userName
,
id
})
=>
{
const
FavButton
:
React
.
FC
<
FavButtonProps
>
=
({
isFavorited
,
userName
,
id
,
onPressed
})
=>
{
const
[
favorited
,
setFavorited
]
=
useState
(
isFavorited
);
...
...
@@ -48,6 +49,7 @@ const FavButton: React.FC<FavButtonProps> =({isFavorited, userName, id}) => {
else
{
removeFavorite
();
}
onPressed
(
!
favorited
,
id
)
setFavorited
(
!
favorited
);
}
...
...
frontend/src/components/moviesview/index.tsx
View file @
51dbbaf2
...
...
@@ -23,6 +23,11 @@ interface MovieTableProps {
fetchMore
:
()
=>
void
;
}
type
favoritedMovie
=
{
id
:
string
;
isFavorited
:
boolean
;
}
const
MovieTable
:
React
.
FC
<
MovieTableProps
>
=
({
fetchMore
})
=>
{
const
movies
=
useSelector
(
selectMovies
);
...
...
@@ -30,6 +35,7 @@ const MovieTable: React.FC<MovieTableProps> = ({fetchMore}) => {
const
userName
=
useSelector
(
selectUserName
)
const
[
modalVisible
,
setModalVisible
]
=
useState
(
false
);
const
[
modalMovie
,
setModalMovie
]
=
useState
<
searchMovies_getMoviesBySearch
>
();
const
[
favoritedInSession
,
setFavoritedInSession
]
=
useState
<
Array
<
favoritedMovie
>>
([]);
const
[
fontsLoaded
]
=
useFonts
({
'
Quicksand-Regular
'
:
require
(
'
../../assets/fonts/Quicksand-Regular.ttf
'
)
...
...
@@ -39,9 +45,27 @@ const MovieTable: React.FC<MovieTableProps> = ({fetchMore}) => {
if
(
movie
===
null
||
!
userName
)
{
return
false
;
}
let
favoritedArray
=
favoritedInSession
.
filter
(
favoritedInSession
=>
favoritedInSession
.
id
===
movie
.
id
)
if
(
favoritedArray
.
length
>
0
)
{
return
favoritedArray
[
0
].
isFavorited
;
}
return
movie
.
favoritedByUser
.
includes
(
userName
)
}
function
handleFavorize
(
newValue
:
boolean
,
id
:
string
)
{
let
alreadyInFavorites
=
favoritedInSession
.
filter
(
favoritedInSession
=>
favoritedInSession
.
id
===
id
)
if
(
alreadyInFavorites
.
length
>
0
)
{
let
newFav
=
alreadyInFavorites
[
0
]
newFav
.
isFavorited
=
newValue
}
else
{
setFavoritedInSession
((
state
)
=>
[...
state
,
{
id
:
id
,
isFavorited
:
newValue
}])
}
}
const
Movie
=
({
item
}:
IMovieObject
)
=>
(
<
Card
style
=
{
styles
.
cardContainer
}
...
...
@@ -57,7 +81,7 @@ const MovieTable: React.FC<MovieTableProps> = ({fetchMore}) => {
</
View
>
<
Card
.
Content
style
=
{
styles
.
contentContainer
}
>
{
isLoggedIn
?
<
FavButton
isFavorited
=
{
isFavorited
(
item
)
}
userName
=
{
userName
!==
undefined
?
userName
:
""
}
id
=
{
item
.
id
}
/>
?
<
FavButton
isFavorited
=
{
isFavorited
(
item
)
}
userName
=
{
userName
!==
undefined
?
userName
:
""
}
id
=
{
item
.
id
}
onPressed
=
{
(
newValue
:
boolean
,
id
:
string
)
=>
handleFavorize
(
newValue
,
id
)
}
/>
:
null
}
<
Title
style
=
{
styles
.
title
}
>
{
item
?.
title
}
</
Title
>
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment