Get Unit Converter

This commit is contained in:
efrilm 2025-09-12 18:57:23 +07:00
parent 54c7598e7a
commit 76ee71e7fe
3 changed files with 52 additions and 0 deletions

View File

@ -0,0 +1,13 @@
import { UnitConversion } from '@/types/services/productRecipe'
import { useQuery } from '@tanstack/react-query'
import { api } from '../api'
export function useUnitConverterByIngredient(IngredientId: string) {
return useQuery<UnitConversion[]>({
queryKey: ['unit-converters/ingredient', IngredientId],
queryFn: async () => {
const res = await api.get(`/unit-converters/ingredient/${IngredientId}`)
return res.data.data
}
})
}

View File

@ -73,3 +73,29 @@ export interface IngredientUnitConverterRequest {
to_unit_id: string to_unit_id: string
conversion_factor: number conversion_factor: number
} }
export interface UnitConversion {
id: string
organization_id: string
ingredient_id: string
from_unit_id: string
to_unit_id: string
conversion_factor: number
is_active: boolean
created_at: string
updated_at: string
created_by: string
updated_by: string
from_unit: UnitConversionFrom
to_unit: UnitConversionTo
}
export interface UnitConversionFrom {
id: string
name: string
}
export interface UnitConversionTo {
id: string
name: string
}

View File

@ -3,6 +3,7 @@ import React, { useState } from 'react'
import { Card, CardContent, CardHeader, Typography, Button, Box, Stack } from '@mui/material' import { Card, CardContent, CardHeader, Typography, Button, Box, Stack } from '@mui/material'
import IngedientUnitConversionDrawer from './IngedientUnitConversionDrawer' // Sesuaikan dengan path file Anda import IngedientUnitConversionDrawer from './IngedientUnitConversionDrawer' // Sesuaikan dengan path file Anda
import { Ingredient } from '@/types/services/productRecipe' import { Ingredient } from '@/types/services/productRecipe'
import { useUnitConverterByIngredient } from '@/services/queries/unitConverter'
interface Props { interface Props {
data: Ingredient | undefined data: Ingredient | undefined
@ -12,6 +13,8 @@ const IngredientDetailUnit = ({ data }: Props) => {
// State untuk mengontrol drawer // State untuk mengontrol drawer
const [openConversionDrawer, setOpenConversionDrawer] = useState(false) const [openConversionDrawer, setOpenConversionDrawer] = useState(false)
const { data: unitConverters, isLoading } = useUnitConverterByIngredient(data?.id as string)
// Function untuk membuka drawer // Function untuk membuka drawer
const handleOpenConversionDrawer = () => { const handleOpenConversionDrawer = () => {
setOpenConversionDrawer(true) setOpenConversionDrawer(true)
@ -42,6 +45,16 @@ const IngredientDetailUnit = ({ data }: Props) => {
: {data?.unit.name ?? '-'} : {data?.unit.name ?? '-'}
</Typography> </Typography>
</Box> </Box>
{unitConverters?.map(unitConverter => (
<Box sx={{ display: 'flex', alignItems: 'center' }}>
<Typography variant='body1' color='text.secondary'>
1 {unitConverter.from_unit.name}
</Typography>
<Typography variant='body1' sx={{ fontWeight: 'medium' }}>
: {unitConverter.conversion_factor} {unitConverter.to_unit.name}
</Typography>
</Box>
)) ?? []}
</Stack> </Stack>
<Button <Button