package com.example.myapplication.components.templates


import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import com.example.myapplication.database.AppDb
import com.example.myapplication.database.entities.Product
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

@Composable
fun ProductForPage(id: Int) {
    val context = LocalContext.current
    val (product, setProduct)  = remember { mutableStateOf<Product?>(null) } //спросить

    LaunchedEffect(Unit) {
        withContext(Dispatchers.IO){
            AppDb.getInstance(context).productDao().getById(id).collect {
                setProduct(it)
            }
        }
    }

    if (product != null){
        ProductPage(product!!.name, product!!.info, product!!.price, product!!.img, { }  )
    }
}