Skip to content

Commit

Permalink
Minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
danielkjellid committed Jun 6, 2024
1 parent cf6461d commit 0679b53
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 39 deletions.
10 changes: 7 additions & 3 deletions frontend/apps/recipe/detail/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ import View from '../../../components/View'
import { useCommonContext } from '../../../contexts/CommonProvider'
import { useFetch } from '../../../hooks/fetcher'
import { type RecipeDetailRecordAPIResponse, RecipeStatus } from '../../../types'
import { routes as recipesRoutes } from '../../recipes/routes'
import { urls } from '../../urls'
import { Recipe } from '../components/Recipe'

interface RecipeDetailInnerProps {
recipeId: string
results: {
recipeResponse: RecipeDetailRecordAPIResponse
}
}

function RecipeDetailInner({ results }: RecipeDetailInnerProps) {
function RecipeDetailInner({ recipeId, results }: RecipeDetailInnerProps) {
const navigate = useNavigate()
const { currentUser } = useCommonContext()
const { data: recipe } = results.recipeResponse
Expand Down Expand Up @@ -65,7 +67,9 @@ function RecipeDetailInner({ results }: RecipeDetailInnerProps) {
View in admin
</MButton>
)}
<Button onClick={() => navigate('/')}>Edit recipe</Button>
<a href={recipesRoutes.edit.build({ recipeId: recipeId })}>
<Button>Edit recipe</Button>
</a>
</div>
</div>
)}
Expand All @@ -86,7 +90,7 @@ function RecipeDetail() {
<View<object, RecipeDetailInnerProps>
component={RecipeDetailInner}
results={{ recipeResponse: recipeResponse }}
componentProps={{}}
componentProps={{ recipeId: recipeId }}
loadingProps={{ description: 'Loading recipe' }}
errorProps={{
description: 'There was an error retrieving the recipe. Please try again later.',
Expand Down
5 changes: 4 additions & 1 deletion frontend/apps/recipes/edit/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { notifications } from '@mantine/notifications'
import { useParams } from 'react-router-dom'
import { useNavigate, useParams } from 'react-router-dom'
import invariant from 'tiny-invariant'

import View from '../../../components/View'
Expand All @@ -11,6 +11,7 @@ import {
} from '../../../types'
import { urls } from '../../urls'
import { type Recipe, RecipeForm, makePayload } from '../components/RecipeForm'
import { routes } from '../routes'

interface RecipeEditInnerProps {
recipeId: string
Expand All @@ -23,6 +24,7 @@ interface RecipeEditInnerProps {
function RecipeEditInner({ recipeId, results }: RecipeEditInnerProps) {
const { data: ingredients } = results.ingredients
const { data: recipe } = results.recipe
const navigate = useNavigate()

const editRecipe = async (recipeData: Recipe) => {
const payload = makePayload(recipeData)
Expand All @@ -34,6 +36,7 @@ function RecipeEditInner({ recipeId, results }: RecipeEditInnerProps) {
title: 'Recipe updated',
message: 'Recipe was successfully updated.',
})
navigate(routes.overview.build())
} catch (e) {
console.log(e)
}
Expand Down
55 changes: 24 additions & 31 deletions frontend/hooks/forms/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,37 +108,30 @@ export function useForm<T extends object>({

const validate = () => {
const errors = {}
// TODO: Emit empty, non required, values from validation
// Object.entries(formData || {}).map(([k, v]) => {
// if (v === null) {
// delete formData[k]
// }
// })
// console.log(formData)
// validator.validate(schema, formData || {})

// if (!formData) {
// Object.keys(schema.properties).map((key) => {
// if (schema.required.includes(key)) {
// // @ts-ignore
// errors[key] = 'Field must not be empty'
// }
// })
// }

// if (validator.errors) {
// console.error(validator.errors)
// validator.errors.map((error) => {
// const pathParts = error.instancePath.split('/')
// const inputKey = pathParts[pathParts.length - 1]
// const errorMsg = error.message

// if (!inputKey || !errorMsg) return undefined

// // @ts-ignore
// errors[inputKey] = errorMsg.charAt(0).toUpperCase() + errorMsg.slice(1).toLocaleLowerCase()
// })
// }
validator.validate(schema, formData || {})

if (!formData) {
Object.keys(schema.properties).map((key) => {
if (schema.required.includes(key)) {
// @ts-ignore
errors[key] = 'Field must not be empty'
}
})
}

if (validator.errors) {
console.error(validator.errors)
validator.errors.map((error) => {
const pathParts = error.instancePath.split('/')
const inputKey = pathParts[pathParts.length - 1]
const errorMsg = error.message

if (!inputKey || !errorMsg) return undefined

// @ts-ignore
errors[inputKey] = errorMsg.charAt(0).toUpperCase() + errorMsg.slice(1).toLocaleLowerCase()
})
}

setErrors(errors)
return !Object.keys(errors).length
Expand Down
3 changes: 0 additions & 3 deletions nest/recipes/ingredients/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,6 @@ class Meta:
)


# TODO: Should be called RecipeIngredientGroup?


class RecipeIngredientItemGroup(BaseModel):
recipe = models.ForeignKey(
"recipes.Recipe",
Expand Down
2 changes: 1 addition & 1 deletion nest/recipes/steps/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Step(BaseModel):
duration: int
instruction: str
step_type: RecipeStepType
ingredient_items: list["IngredientItem"] # TODO fix without circular import
ingredient_items: list[IngredientItem]


def _validate_steps(steps: list[Step]) -> None:
Expand Down

0 comments on commit 0679b53

Please sign in to comment.