Skip to content

Commit

Permalink
Use enum for recipe and chef's feed type and using switch case for re…
Browse files Browse the repository at this point in the history
…ndering.
  • Loading branch information
Divs-B committed May 28, 2024
1 parent fae45f6 commit 91c3256
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions fronts-client/src/components/feed/RecipeSearchContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,34 @@ interface Props {
chefs: Record<string, Chef>;
}

enum FeedType {
recipes = 'recipeFeed',
chefs = 'chefFeed',
}

const FeastSearchContainerComponent = ({
rightHandContainer,
recipes,
chefs,
}: Props) => {
const [selectedOption, setSelectedOption] = useState<string>('recipeFeed');
const handleOptionChange = (optionName: string) => {
const [selectedOption, setSelectedOption] = useState(FeedType.recipes);
const handleOptionChange = (optionName: FeedType) => {
setSelectedOption(optionName);
};

const renderTheFeed = () => {
switch (selectedOption) {
case FeedType.recipes:
return Object.values(recipes).map((recipe) => (
<RecipeFeedItem key={recipe.id} recipe={recipe} />
));
case FeedType.chefs:
return Object.values(chefs).map((chef) => (
<ChefFeedItem key={chef.id} chef={chef} />
));
}
};

return (
<React.Fragment>
<InputContainer>
Expand All @@ -54,15 +73,15 @@ const FeastSearchContainerComponent = ({
</InputContainer>
<RadioGroup>
<RadioButton
checked={selectedOption === 'recipeFeed'}
onChange={() => handleOptionChange('recipeFeed')}
checked={selectedOption === FeedType.recipes}
onChange={() => handleOptionChange(FeedType.recipes)}
label="Recipes"
inline
name="recipeFeed"
/>
<RadioButton
checked={selectedOption === 'chefFeed'}
onChange={() => handleOptionChange('chefFeed')}
checked={selectedOption === FeedType.chefs}
onChange={() => handleOptionChange(FeedType.chefs)}
label="Chefs"
inline
name="chefFeed"
Expand All @@ -75,13 +94,7 @@ const FeastSearchContainerComponent = ({
<ShortVerticalPinline />
</SearchTitle>
</SearchResultsHeadingContainer>
{selectedOption === 'recipeFeed'
? Object.values(recipes).map((recipe) => (
<RecipeFeedItem key={recipe.id} recipe={recipe} />
))
: Object.values(chefs).map((chef) => (
<ChefFeedItem key={chef.id} chef={chef} />
))}
{renderTheFeed()}
</FixedContentContainer>
</React.Fragment>
);
Expand Down

0 comments on commit 91c3256

Please sign in to comment.