Skip to content

Commit

Permalink
Don't show actor in suggestions if they're already selected
Browse files Browse the repository at this point in the history
  • Loading branch information
dmahely committed Apr 21, 2023
1 parent a7f0b6e commit 6dfc650
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/apis/fetchActors.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Actor } from '../types/types'

const fetchActors = async (input: string) => {
const fetchActors = async (input: string): Promise<Actor[]> => {
const data = await (
await fetch(
`${process.env.REACT_APP_BASE_URL}/search/person?api_key=${process.env.REACT_APP_API_TOKEN}&query=${input}`
Expand Down
1 change: 1 addition & 0 deletions src/components/App/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const App = () => {
inputValue={inputValue}
setInputValue={setInputValue}
setActors={setActors}
selectedActors={selectedActors}
/>
<Suggestions
actors={actors}
Expand Down
20 changes: 18 additions & 2 deletions src/components/InputForm/InputForm.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Input, VStack } from '@chakra-ui/react'
import React, { useEffect } from 'react'
import { Actor } from '../../types/types'
import { Actor, ActorWithMovies } from '../../types/types'
import { fetchActors } from '../../apis/fetchActors'
import { useState } from 'react'
import { Loading } from '../Loading/Loading'
Expand All @@ -9,12 +9,14 @@ type InputFormProps = {
setActors: React.Dispatch<React.SetStateAction<Actor[]>>
inputValue: string
setInputValue: React.Dispatch<React.SetStateAction<string>>
selectedActors: ActorWithMovies[]
}

const InputForm: React.FC<InputFormProps> = ({
setActors,
inputValue,
setInputValue,
selectedActors,
}) => {
const [isLoading, setIsLoading] = useState<boolean>(false)

Expand All @@ -25,10 +27,24 @@ const InputForm: React.FC<InputFormProps> = ({
setInputValue(inputText)
}

const filterSelectedActors = (
actorsResult: Actor[],
selectedActors: ActorWithMovies[]
) => {
const selectedActorsIds = selectedActors.map((actor) => actor.actor.id)
return actorsResult.filter(
(actor) => !selectedActorsIds.includes(actor.id)
)
}

const getActorResult = async (inputText: string) => {
const actorsResult = await fetchActors(inputText)
setIsLoading(false)
setActors(actorsResult)
const filteredActors = filterSelectedActors(
actorsResult,
selectedActors
)
setActors(filteredActors)
}

useEffect(() => {
Expand Down

0 comments on commit 6dfc650

Please sign in to comment.