Skip to content

Commit

Permalink
refactor: Navigate to the recipe view after creation
Browse files Browse the repository at this point in the history
  • Loading branch information
TiagoRibeiro25 committed Aug 26, 2023
1 parent 1736d29 commit 529b257
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions src/views/AddRecipe/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import classNames from 'classnames';
import { useContext, useMemo, useState } from 'react';
import { useNavigate } from 'react-router-dom';
import requests from '../../api/requests';
import LoadingIcon from '../../assets/icons/loading.svg';
import AddImages from '../../components/AddImages';
Expand All @@ -13,6 +14,8 @@ import { UserContext } from '../../contextProviders/UserContext';
import { Base64Img } from '../../types/types';

const AddRecipe: React.FC = () => {
const navigate = useNavigate();

const recipesContext = useContext(RecipesContext);
const { loggedUser } = useContext(UserContext);

Expand Down Expand Up @@ -52,14 +55,6 @@ const AddRecipe: React.FC = () => {
if (response.data.success && response.data.data && loggedUser) {
setStatusMSg('Recipe successfully created!');

// Reset the form
setImages([]);
setTitle('');
setDescription('');
setIngredients([{ id: '1', value: '' }]);
setInstructions([{ id: '1', value: '' }]);
setNotes('');

recipesContext.handleNewRecipe({
id: response.data.data.recipe.id,
title: response.data.data.recipe.title,
Expand All @@ -81,6 +76,8 @@ const AddRecipe: React.FC = () => {
isLiked: false,
createdAt: response.data.data.recipe.createdAt,
});

navigate(`/recipe/${response.data.data.recipe.id}`);
} else {
setStatusMSg(response.data.message);
}
Expand Down

0 comments on commit 529b257

Please sign in to comment.