Skip to content

Commit

Permalink
chore: Fixed all missing parts
Browse files Browse the repository at this point in the history
Signed-off-by: Johannes Groß <mail@gross-johannes.de>
  • Loading branch information
jo-gross committed Sep 12, 2024
1 parent 947a2c2 commit 539ac38
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 140 deletions.
8 changes: 5 additions & 3 deletions components/cocktails/CocktailRecipeCardItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Loading } from '../Loading';
import { fetchCocktail } from '../../lib/network/cocktails';

interface CocktailRecipeOverviewItemProps {
cocktailRecipe: CocktailRecipeFull;
cocktailRecipe: CocktailRecipeFull | string;
showImage?: boolean;
specialPrice?: number;
showPrice?: boolean;
Expand All @@ -29,17 +29,19 @@ export default function CocktailRecipeCardItem(props: CocktailRecipeOverviewItem
const [submittingStatistic, setSubmittingStatistic] = useState(false);
const [submittingQueue, setSubmittingQueue] = useState(false);

const [cocktailRecipe, setCocktailRecipe] = useState<CocktailRecipeFull | undefined>(
const [loadedCocktailRecipe, setLoadedCocktailRecipe] = useState<CocktailRecipeFull | undefined>(
typeof props.cocktailRecipe === 'string' ? undefined : props.cocktailRecipe,
);
const [cocktailRecipeLoading, setCocktailRecipeLoading] = useState(false);

useEffect(() => {
if (typeof props.cocktailRecipe === 'string') {
fetchCocktail(workspaceId, props.cocktailRecipe, setCocktailRecipe, setCocktailRecipeLoading);
fetchCocktail(workspaceId, props.cocktailRecipe, setLoadedCocktailRecipe, setCocktailRecipeLoading);
}
}, [workspaceId, props.cocktailRecipe]);

const cocktailRecipe = typeof props.cocktailRecipe === 'string' ? loadedCocktailRecipe : props.cocktailRecipe;

return (
<div className={'col-span-1'}>
<div className={'card card-side h-full'}>
Expand Down
28 changes: 28 additions & 0 deletions lib/network/cocktails.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { alertService } from '../alertService';
import { CocktailRecipeFull } from '../../models/CocktailRecipeFull';
import { CocktailRecipeFullWithImage } from '../../models/CocktailRecipeFullWithImage';

export function fetchCocktail(
workspaceId: string | string[] | undefined,
Expand Down Expand Up @@ -27,3 +28,30 @@ export function fetchCocktail(
setCocktailLoading(false);
});
}

export function fetchCocktailWithImage(
workspaceId: string | string[] | undefined,
cocktailId: string,
setCocktail: (cocktail: CocktailRecipeFullWithImage) => void,
setCocktailLoading: (loading: boolean) => void,
) {
if (!workspaceId) return;
setCocktailLoading(true);
fetch(`/api/workspaces/${workspaceId}/cocktails/${cocktailId}?include=image`)
.then(async (response) => {
const body = await response.json();
if (response.ok) {
setCocktail(body.data);
} else {
console.error('fetchCocktail', response);
alertService.error(body.message ?? 'Fehler beim Laden des Cocktails', response.status, response.statusText);
}
})
.catch((error) => {
console.error('fetchCocktail', error);
alertService.error('Fehler beim Laden des Cocktails');
})
.finally(() => {
setCocktailLoading(false);
});
}
31 changes: 1 addition & 30 deletions models/CocktailCardFull.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,7 @@ export type CocktailCardFull = Prisma.CocktailCardGetPayload<{
include: {
groups: {
include: {
items: {
include: {
cocktail: {
include: {
ice: true;
glass: { include: { _count: { select: { GlassImage: true } } } };
garnishes: {
include: {
garnish: { include: { _count: { select: { GarnishImage: true } } } };
};
};
_count: { select: { CocktailRecipeImage: true } };
steps: {
include: {
action: true;
ingredients: {
include: {
ingredient: {
include: { _count: { select: { IngredientImage: true } } };
unit: true;
};
};
};
};
};
ratings: true;
};
};
};
};
items: true;
};
};
};
Expand Down
28 changes: 1 addition & 27 deletions pages/api/workspaces/[workspaceId]/cards/[cardId]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,7 @@ export default withHttpMethods({
include: {
groups: {
include: {
items: {
include: {
cocktail: {
include: {
_count: { select: { CocktailRecipeImage: true } },
ice: true,
glass: { include: { _count: { select: { GlassImage: true } } } },
garnishes: {
include: {
garnish: true,
},
},
steps: {
include: {
action: true,
ingredients: {
include: {
ingredient: true,
unit: true,
},
},
},
},
},
},
},
},
items: true,
},
},
},
Expand Down
28 changes: 1 addition & 27 deletions pages/api/workspaces/[workspaceId]/cards/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,33 +32,7 @@ export default withHttpMethods({
include: {
groups: {
include: {
items: {
include: {
cocktail: {
include: {
_count: { select: { CocktailRecipeImage: true } },
ice: true,
glass: true,
garnishes: {
include: {
garnish: true,
},
},
steps: {
include: {
action: true,
ingredients: {
include: {
ingredient: true,
unit: true,
},
},
},
},
},
},
},
},
items: true,
},
},
},
Expand Down
91 changes: 63 additions & 28 deletions pages/api/workspaces/[workspaceId]/cocktails/[cocktailId]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,83 @@ import { withHttpMethods } from '../../../../../../middleware/api/handleMethods'
import { CocktailRecipeStepFull } from '../../../../../../models/CocktailRecipeStepFull';
import { CocktailRecipeGarnishFull } from '../../../../../../models/CocktailRecipeGarnishFull';
import { CocktailRecipeFullWithImage } from '../../../../../../models/CocktailRecipeFullWithImage';
import { CocktailRecipeFull } from '../../../../../../models/CocktailRecipeFull';
import CocktailRecipeUpdateInput = Prisma.CocktailRecipeUpdateInput;

export default withHttpMethods({
[HTTPMethod.GET]: withWorkspacePermission([Role.USER], async (req: NextApiRequest, res: NextApiResponse, user, workspace) => {
const cocktailId = req.query.cocktailId as string | undefined;
if (!cocktailId) return res.status(400).json({ message: 'No cocktail id' });

const result: CocktailRecipeFullWithImage | null = await prisma.cocktailRecipe.findFirst({
where: {
id: cocktailId,
workspaceId: workspace.id,
},
include: {
_count: { select: { CocktailRecipeImage: true } },
ice: true,
glass: { include: { _count: { select: { GlassImage: true } } } },
CocktailRecipeImage: {
select: {
image: true,
},
const { include } = req.query;
if (include == 'image' || include?.includes('image')) {
const result: CocktailRecipeFullWithImage | null = await prisma.cocktailRecipe.findFirst({
where: {
id: cocktailId,
workspaceId: workspace.id,
},
garnishes: {
include: {
garnish: { include: { _count: { select: { GarnishImage: true } } } },
include: {
_count: { select: { CocktailRecipeImage: true } },
ice: true,
glass: { include: { _count: { select: { GlassImage: true } } } },
CocktailRecipeImage: {
select: {
image: true,
},
},
garnishes: {
include: {
garnish: { include: { _count: { select: { GarnishImage: true } } } },
},
},
steps: {
include: {
action: true,
ingredients: {
include: {
ingredient: { include: { _count: { select: { IngredientImage: true } } } },
unit: true,
},
},
},
},
ratings: true,
},
steps: {
include: {
action: true,
ingredients: {
include: {
ingredient: { include: { _count: { select: { IngredientImage: true } } } },
unit: true,
});
return res.json({ data: result });
} else {
const result: CocktailRecipeFull | null = await prisma.cocktailRecipe.findFirst({
where: {
id: cocktailId,
workspaceId: workspace.id,
},
include: {
_count: { select: { CocktailRecipeImage: true } },
ice: true,
glass: { include: { _count: { select: { GlassImage: true } } } },
garnishes: {
include: {
garnish: { include: { _count: { select: { GarnishImage: true } } } },
},
},
steps: {
include: {
action: true,
ingredients: {
include: {
ingredient: {
include: { _count: { select: { IngredientImage: true } } },
},
unit: true,
},
},
},
},
ratings: true,
},
ratings: true,
},
});

return res.json({ data: result });
});
return res.json({ data: result });
}
}),
[HTTPMethod.PUT]: withWorkspacePermission([Role.MANAGER], async (req: NextApiRequest, res: NextApiResponse, user, workspace) => {
const cocktailId = req.query.cocktailId as string | undefined;
Expand Down
4 changes: 2 additions & 2 deletions pages/workspaces/[workspaceId]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ export default function OverviewPage() {
group.items
?.sort((a, b) => a.itemNumber - b.itemNumber)
.map((groupItem, index) => {
if (groupItem.cocktail != undefined) {
if (groupItem.cocktailId != undefined) {
return (
<CocktailRecipeCardItem
key={`card-${selectedCard.id}-group-${group.id}-cocktail-${groupItem.cocktailId}-${index}`}
Expand All @@ -437,7 +437,7 @@ export default function OverviewPage() {
showInfo={true}
showPrice={groupItem.specialPrice == undefined && group.groupPrice == undefined}
specialPrice={groupItem.specialPrice ?? group.groupPrice ?? undefined}
cocktailRecipe={groupItem.cocktail.id}
cocktailRecipe={groupItem.cocktailId}
showStatisticActions={showStatisticActions}
showDescription={showDescription}
showNotes={showNotes}
Expand Down
4 changes: 2 additions & 2 deletions pages/workspaces/[workspaceId]/manage/cards/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ function EditCocktailCard() {
<div className={'pt-2'}>
<FieldArray name={`groups.${groupIndex}.items`}>
{({ push: pushItem, remove: removeItem }) => (
<div className={'grid grid-cols-1 gap-2 md:grid-cols-3'}>
<div className={'grid grid-cols-1 gap-2 md:grid-cols-5 lg:grid-cols-7'}>
{values.groups[groupIndex].items
.sort((a, b) => a.itemNumber - b.itemNumber)
.map((item, itemIndex) => (
Expand Down Expand Up @@ -450,7 +450,7 @@ function EditCocktailCard() {
</div>
</div>
))}
<div className={'col-span-1 flex flex-row justify-end md:col-span-3'}>
<div className={'col-span-full flex flex-row justify-end'}>
{!card?.archived ? (
<button
type="button"
Expand Down
24 changes: 3 additions & 21 deletions pages/workspaces/[workspaceId]/manage/cocktails/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { PageCenter } from '../../../../../components/layout/PageCenter';
import { UserContext } from '../../../../../lib/context/UserContextProvider';
import { ModalContext } from '../../../../../lib/context/ModalContextProvider';
import { NotSavedArchiveConfirmation } from '../../../../../components/modals/NotSavedArchiveConfirmation';
import { fetchCocktailWithImage } from '../../../../../lib/network/cocktails';

function EditCocktailRecipe() {
const router = useRouter();
Expand All @@ -27,27 +28,8 @@ function EditCocktailRecipe() {
const formRef: any = useRef<FormikProps<any>>(null);

useEffect(() => {
if (!id) return;
if (!workspaceId) return;
setLoading(true);
fetch(`/api/workspaces/${workspaceId}/cocktails/${id}`)
.then(async (response) => {
const body = await response.json();
if (response.ok) {
setCocktailRecipe(body.data);
} else {
console.error('CocktailId -> fetchCocktail', response);
alertService.error(body.message ?? 'Fehler beim Laden des Cocktails', response.status, response.statusText);
}
})
.catch((error) => {
console.error('CocktailId -> fetchCocktail', error);
alertService.error('Fehler beim Laden des Cocktails');
})
.finally(() => {
setLoading(false);
});
}, [id, workspaceId]);
fetchCocktailWithImage(workspaceId as string, id as string, setCocktailRecipe, setLoading);
}, []);

return loading ? (
<PageCenter>
Expand Down

0 comments on commit 539ac38

Please sign in to comment.