Compare commits
No commits in common. "2d37726c62813d117f742a05b19125ac7314890b" and "e5f80c923734b5d68510abffa09abc0fdaefec07" have entirely different histories.
2d37726c62
...
e5f80c9237
58 changed files with 1222 additions and 1657 deletions
|
|
@ -1,4 +0,0 @@
|
|||
- trpc.user.tags.getTagsByStore — apps/backend/src/trpc/apis/user-apis/apis/tags.ts
|
||||
- trpc.common.product.getAllProductsSummary — apps/backend/src/trpc/apis/common-apis/common.ts
|
||||
- remove slots from products cache
|
||||
- remove redundant product details like name, description etc from the slots api
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
ENV_MODE=PROD
|
||||
DATABASE_URL=postgresql://postgres:meatfarmer_master_password@57.128.212.174:7447/meatfarmer #technocracy
|
||||
# DATABASE_URL=postgres://postgres:meatfarmer_master_password@5.223.55.14:7447/meatfarmer #hetzner
|
||||
# DATABASE_URL=postgresql://postgres:meatfarmer_master_password@57.128.212.174:7447/meatfarmer #technocracy
|
||||
DATABASE_URL=postgres://postgres:meatfarmer_master_password@5.223.55.14:7447/meatfarmer #hetzner
|
||||
PHONE_PE_BASE_URL=https://api-preprod.phonepe.com/
|
||||
PHONE_PE_CLIENT_ID=TEST-M23F2IGP34ZAR_25090
|
||||
PHONE_PE_CLIENT_VERSION=1
|
||||
|
|
@ -21,10 +21,6 @@ S3_BUCKET_NAME=meatfarmer
|
|||
EXPO_ACCESS_TOKEN=Asvpy8cByRh6T4ksnWScO6PLcio2n35-BwES5zK-
|
||||
JWT_SECRET=my_meatfarmer_jwt_secret_key
|
||||
ASSETS_DOMAIN=https://assets.freshyo.in/
|
||||
API_CACHE_KEY=api-cache-dev
|
||||
# CLOUDFLARE_API_TOKEN=I8Vp4E9TX58E8qEDeH0nTFDS2d2zXNYiXvbs4Ckj
|
||||
CLOUDFLARE_API_TOKEN=N7jAg5X-RUj_fVfMW6zbfJ8qIYc81TSIKKlbZ6oh
|
||||
CLOUDFLARE_ZONE_ID=edefbf750bfc3ff26ccd11e8e28dc8d7
|
||||
# REDIS_URL=redis://default:redis_shafi_password@5.223.55.14:6379
|
||||
REDIS_URL=redis://default:redis_shafi_password@57.128.212.174:6379
|
||||
APP_URL=http://localhost:4000
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
|
|
@ -7,7 +7,6 @@ import { imageUploadS3, generateSignedUrlFromS3Url } from "@/src/lib/s3-client";
|
|||
import { deleteS3Image } from "@/src/lib/delete-image";
|
||||
import { initializeAllStores } from '@/src/stores/store-initializer';
|
||||
|
||||
|
||||
/**
|
||||
* Create a new product tag
|
||||
*/
|
||||
|
|
@ -61,8 +60,7 @@ export const createTag = async (req: Request, res: Response) => {
|
|||
// Reinitialize stores to reflect changes in cache
|
||||
await initializeAllStores();
|
||||
|
||||
// Send response first
|
||||
res.status(201).json({
|
||||
return res.status(201).json({
|
||||
tag: newTag,
|
||||
message: "Tag created successfully",
|
||||
});
|
||||
|
|
@ -181,8 +179,7 @@ export const updateTag = async (req: Request, res: Response) => {
|
|||
// Reinitialize stores to reflect changes in cache
|
||||
await initializeAllStores();
|
||||
|
||||
// Send response first
|
||||
res.status(200).json({
|
||||
return res.status(200).json({
|
||||
tag: updatedTag,
|
||||
message: "Tag updated successfully",
|
||||
});
|
||||
|
|
@ -219,8 +216,7 @@ export const deleteTag = async (req: Request, res: Response) => {
|
|||
// Reinitialize stores to reflect changes in cache
|
||||
await initializeAllStores();
|
||||
|
||||
// Send response first
|
||||
res.status(200).json({
|
||||
return res.status(200).json({
|
||||
message: "Tag deleted successfully",
|
||||
});
|
||||
};
|
||||
|
|
@ -8,7 +8,6 @@ import { deleteS3Image } from "@/src/lib/delete-image";
|
|||
import type { SpecialDeal } from "@/src/db/types";
|
||||
import { initializeAllStores } from '@/src/stores/store-initializer';
|
||||
|
||||
|
||||
type CreateDeal = {
|
||||
quantity: number;
|
||||
price: number;
|
||||
|
|
@ -111,8 +110,7 @@ export const createProduct = async (req: Request, res: Response) => {
|
|||
// Reinitialize stores to reflect changes
|
||||
await initializeAllStores();
|
||||
|
||||
// Send response first
|
||||
res.status(201).json({
|
||||
return res.status(201).json({
|
||||
product: newProduct,
|
||||
deals: createdDeals,
|
||||
message: "Product created successfully",
|
||||
|
|
@ -298,8 +296,7 @@ export const updateProduct = async (req: Request, res: Response) => {
|
|||
// Reinitialize stores to reflect changes
|
||||
await initializeAllStores();
|
||||
|
||||
// Send response first
|
||||
res.status(200).json({
|
||||
return res.status(200).json({
|
||||
product: updatedProduct,
|
||||
message: "Product updated successfully",
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,376 +0,0 @@
|
|||
import axios from 'axios'
|
||||
import { scaffoldProducts } from '@/src/trpc/apis/common-apis/common'
|
||||
import { scaffoldEssentialConsts } from '@/src/trpc/apis/common-apis/common-trpc-index'
|
||||
import { scaffoldStores } from '@/src/trpc/apis/user-apis/apis/stores'
|
||||
import { scaffoldSlotsWithProducts } from '@/src/trpc/apis/user-apis/apis/slots'
|
||||
import { scaffoldBanners } from '@/src/trpc/apis/user-apis/apis/banners'
|
||||
import { scaffoldStoreWithProducts } from '@/src/trpc/apis/user-apis/apis/stores'
|
||||
import { storeInfo } from '@/src/db/schema'
|
||||
import { db } from '@/src/db/db_index'
|
||||
import { imageUploadS3 } from '@/src/lib/s3-client'
|
||||
import { apiCacheKey, cloudflareApiToken, cloudflareZoneId, assetsDomain } from '@/src/lib/env-exporter'
|
||||
import { CACHE_FILENAMES } from '@packages/shared'
|
||||
import { retryWithExponentialBackoff } from '@/src/lib/retry'
|
||||
|
||||
function constructCacheUrl(path: string): string {
|
||||
return `${assetsDomain}${apiCacheKey}/${path}`
|
||||
}
|
||||
|
||||
export async function createProductsFile(): Promise<string> {
|
||||
// Get products data from the API method
|
||||
const productsData = await scaffoldProducts()
|
||||
|
||||
// Convert to JSON string with pretty formatting
|
||||
const jsonContent = JSON.stringify(productsData, null, 2)
|
||||
|
||||
// Convert to Buffer for S3 upload
|
||||
const buffer = Buffer.from(jsonContent, 'utf-8')
|
||||
|
||||
// Upload to S3 at the specified path using apiCacheKey
|
||||
const s3Key = await imageUploadS3(buffer, 'application/json', `${apiCacheKey}/${CACHE_FILENAMES.products}`)
|
||||
|
||||
// Purge cache with retry
|
||||
const url = constructCacheUrl(CACHE_FILENAMES.products)
|
||||
try {
|
||||
await retryWithExponentialBackoff(() => clearUrlCache([url]))
|
||||
console.log(`Cache purged for ${url}`)
|
||||
} catch (error) {
|
||||
console.error(`Failed to purge cache for ${url} after 3 retries:`, error)
|
||||
}
|
||||
|
||||
return s3Key
|
||||
}
|
||||
|
||||
export async function createEssentialConstsFile(): Promise<string> {
|
||||
// Get essential consts data from the API method
|
||||
const essentialConstsData = await scaffoldEssentialConsts()
|
||||
|
||||
// Convert to JSON string with pretty formatting
|
||||
const jsonContent = JSON.stringify(essentialConstsData, null, 2)
|
||||
|
||||
// Convert to Buffer for S3 upload
|
||||
const buffer = Buffer.from(jsonContent, 'utf-8')
|
||||
|
||||
// Upload to S3 at the specified path using apiCacheKey
|
||||
const s3Key = await imageUploadS3(buffer, 'application/json', `${apiCacheKey}/${CACHE_FILENAMES.essentialConsts}`)
|
||||
|
||||
// Purge cache with retry
|
||||
const url = constructCacheUrl(CACHE_FILENAMES.essentialConsts)
|
||||
try {
|
||||
await retryWithExponentialBackoff(() => clearUrlCache([url]))
|
||||
console.log(`Cache purged for ${url}`)
|
||||
} catch (error) {
|
||||
console.error(`Failed to purge cache for ${url} after 3 retries:`, error)
|
||||
}
|
||||
|
||||
return s3Key
|
||||
}
|
||||
|
||||
export async function createStoresFile(): Promise<string> {
|
||||
// Get stores data from the API method
|
||||
const storesData = await scaffoldStores()
|
||||
|
||||
// Convert to JSON string with pretty formatting
|
||||
const jsonContent = JSON.stringify(storesData, null, 2)
|
||||
|
||||
// Convert to Buffer for S3 upload
|
||||
const buffer = Buffer.from(jsonContent, 'utf-8')
|
||||
|
||||
// Upload to S3 at the specified path using apiCacheKey
|
||||
const s3Key = await imageUploadS3(buffer, 'application/json', `${apiCacheKey}/${CACHE_FILENAMES.stores}`)
|
||||
|
||||
// Purge cache with retry
|
||||
const url = constructCacheUrl(CACHE_FILENAMES.stores)
|
||||
try {
|
||||
await retryWithExponentialBackoff(() => clearUrlCache([url]))
|
||||
console.log(`Cache purged for ${url}`)
|
||||
} catch (error) {
|
||||
console.error(`Failed to purge cache for ${url} after 3 retries:`, error)
|
||||
}
|
||||
|
||||
return s3Key
|
||||
}
|
||||
|
||||
export async function createSlotsFile(): Promise<string> {
|
||||
// Get slots data from the API method
|
||||
const slotsData = await scaffoldSlotsWithProducts()
|
||||
|
||||
// Convert to JSON string with pretty formatting
|
||||
const jsonContent = JSON.stringify(slotsData, null, 2)
|
||||
|
||||
// Convert to Buffer for S3 upload
|
||||
const buffer = Buffer.from(jsonContent, 'utf-8')
|
||||
|
||||
// Upload to S3 at the specified path using apiCacheKey
|
||||
const s3Key = await imageUploadS3(buffer, 'application/json', `${apiCacheKey}/${CACHE_FILENAMES.slots}`)
|
||||
|
||||
// Purge cache with retry
|
||||
const url = constructCacheUrl(CACHE_FILENAMES.slots)
|
||||
try {
|
||||
await retryWithExponentialBackoff(() => clearUrlCache([url]))
|
||||
console.log(`Cache purged for ${url}`)
|
||||
} catch (error) {
|
||||
console.error(`Failed to purge cache for ${url} after 3 retries:`, error)
|
||||
}
|
||||
|
||||
return s3Key
|
||||
}
|
||||
|
||||
export async function createBannersFile(): Promise<string> {
|
||||
// Get banners data from the API method
|
||||
const bannersData = await scaffoldBanners()
|
||||
|
||||
// Convert to JSON string with pretty formatting
|
||||
const jsonContent = JSON.stringify(bannersData, null, 2)
|
||||
|
||||
// Convert to Buffer for S3 upload
|
||||
const buffer = Buffer.from(jsonContent, 'utf-8')
|
||||
|
||||
// Upload to S3 at the specified path using apiCacheKey
|
||||
const s3Key = await imageUploadS3(buffer, 'application/json', `${apiCacheKey}/${CACHE_FILENAMES.banners}`)
|
||||
|
||||
// Purge cache with retry
|
||||
const url = constructCacheUrl(CACHE_FILENAMES.banners)
|
||||
try {
|
||||
await retryWithExponentialBackoff(() => clearUrlCache([url]))
|
||||
console.log(`Cache purged for ${url}`)
|
||||
} catch (error) {
|
||||
console.error(`Failed to purge cache for ${url} after 3 retries:`, error)
|
||||
}
|
||||
|
||||
return s3Key
|
||||
}
|
||||
|
||||
export async function createStoreFile(storeId: number): Promise<string> {
|
||||
// Get store data from the API method
|
||||
const storeData = await scaffoldStoreWithProducts(storeId)
|
||||
|
||||
// Convert to JSON string with pretty formatting
|
||||
const jsonContent = JSON.stringify(storeData, null, 2)
|
||||
|
||||
// Convert to Buffer for S3 upload
|
||||
const buffer = Buffer.from(jsonContent, 'utf-8')
|
||||
|
||||
// Upload to S3 at the specified path using apiCacheKey
|
||||
const s3Key = await imageUploadS3(buffer, 'application/json', `${apiCacheKey}/stores/${storeId}.json`)
|
||||
|
||||
// Purge cache with retry
|
||||
const url = constructCacheUrl(`stores/${storeId}.json`)
|
||||
try {
|
||||
await retryWithExponentialBackoff(() => clearUrlCache([url]))
|
||||
console.log(`Cache purged for ${url}`)
|
||||
} catch (error) {
|
||||
console.error(`Failed to purge cache for ${url} after 3 retries:`, error)
|
||||
}
|
||||
|
||||
return s3Key
|
||||
}
|
||||
|
||||
export async function createAllStoresFiles(): Promise<string[]> {
|
||||
// Fetch all store IDs from database
|
||||
const stores = await db.select({ id: storeInfo.id }).from(storeInfo)
|
||||
|
||||
// Create cache files for all stores and collect URLs
|
||||
const results: string[] = []
|
||||
const urls: string[] = []
|
||||
|
||||
for (const store of stores) {
|
||||
const s3Key = await createStoreFile(store.id)
|
||||
results.push(s3Key)
|
||||
urls.push(constructCacheUrl(`stores/${store.id}.json`))
|
||||
}
|
||||
|
||||
console.log(`Created ${results.length} store cache files`)
|
||||
|
||||
// Purge all store caches in one batch with retry
|
||||
try {
|
||||
await retryWithExponentialBackoff(() => clearUrlCache(urls))
|
||||
console.log(`Cache purged for ${urls.length} store files`)
|
||||
} catch (error) {
|
||||
console.error(`Failed to purge cache for store files after 3 retries. URLs: ${urls.join(', ')}`, error)
|
||||
}
|
||||
|
||||
return results
|
||||
}
|
||||
|
||||
export interface CreateAllCacheFilesResult {
|
||||
products: string
|
||||
essentialConsts: string
|
||||
stores: string
|
||||
slots: string
|
||||
banners: string
|
||||
individualStores: string[]
|
||||
}
|
||||
|
||||
export async function createAllCacheFiles(): Promise<CreateAllCacheFilesResult> {
|
||||
console.log('Starting creation of all cache files...')
|
||||
|
||||
// Create all global cache files in parallel
|
||||
const [
|
||||
productsKey,
|
||||
essentialConstsKey,
|
||||
storesKey,
|
||||
slotsKey,
|
||||
bannersKey,
|
||||
individualStoreKeys,
|
||||
] = await Promise.all([
|
||||
createProductsFileInternal(),
|
||||
createEssentialConstsFileInternal(),
|
||||
createStoresFileInternal(),
|
||||
createSlotsFileInternal(),
|
||||
createBannersFileInternal(),
|
||||
createAllStoresFilesInternal(),
|
||||
])
|
||||
|
||||
// Collect all URLs for batch cache purge
|
||||
const urls = [
|
||||
constructCacheUrl(CACHE_FILENAMES.products),
|
||||
constructCacheUrl(CACHE_FILENAMES.essentialConsts),
|
||||
constructCacheUrl(CACHE_FILENAMES.stores),
|
||||
constructCacheUrl(CACHE_FILENAMES.slots),
|
||||
constructCacheUrl(CACHE_FILENAMES.banners),
|
||||
...individualStoreKeys.map((_, index) => constructCacheUrl(`stores/${index + 1}.json`)),
|
||||
]
|
||||
|
||||
// Purge all caches in one batch with retry
|
||||
try {
|
||||
await retryWithExponentialBackoff(() => clearUrlCache(urls))
|
||||
console.log(`Cache purged for all ${urls.length} files`)
|
||||
} catch (error) {
|
||||
console.error(`Failed to purge cache for all files after 3 retries`, error)
|
||||
}
|
||||
|
||||
console.log('All cache files created successfully')
|
||||
|
||||
return {
|
||||
products: productsKey,
|
||||
essentialConsts: essentialConstsKey,
|
||||
stores: storesKey,
|
||||
slots: slotsKey,
|
||||
banners: bannersKey,
|
||||
individualStores: individualStoreKeys,
|
||||
}
|
||||
}
|
||||
|
||||
// Internal versions that skip cache purging (for batch operations)
|
||||
async function createProductsFileInternal(): Promise<string> {
|
||||
const productsData = await scaffoldProducts()
|
||||
const jsonContent = JSON.stringify(productsData, null, 2)
|
||||
const buffer = Buffer.from(jsonContent, 'utf-8')
|
||||
return await imageUploadS3(buffer, 'application/json', `${apiCacheKey}/${CACHE_FILENAMES.products}`)
|
||||
}
|
||||
|
||||
async function createEssentialConstsFileInternal(): Promise<string> {
|
||||
const essentialConstsData = await scaffoldEssentialConsts()
|
||||
const jsonContent = JSON.stringify(essentialConstsData, null, 2)
|
||||
const buffer = Buffer.from(jsonContent, 'utf-8')
|
||||
return await imageUploadS3(buffer, 'application/json', `${apiCacheKey}/${CACHE_FILENAMES.essentialConsts}`)
|
||||
}
|
||||
|
||||
async function createStoresFileInternal(): Promise<string> {
|
||||
const storesData = await scaffoldStores()
|
||||
const jsonContent = JSON.stringify(storesData, null, 2)
|
||||
const buffer = Buffer.from(jsonContent, 'utf-8')
|
||||
return await imageUploadS3(buffer, 'application/json', `${apiCacheKey}/${CACHE_FILENAMES.stores}`)
|
||||
}
|
||||
|
||||
async function createSlotsFileInternal(): Promise<string> {
|
||||
const slotsData = await scaffoldSlotsWithProducts()
|
||||
const jsonContent = JSON.stringify(slotsData, null, 2)
|
||||
const buffer = Buffer.from(jsonContent, 'utf-8')
|
||||
return await imageUploadS3(buffer, 'application/json', `${apiCacheKey}/${CACHE_FILENAMES.slots}`)
|
||||
}
|
||||
|
||||
async function createBannersFileInternal(): Promise<string> {
|
||||
const bannersData = await scaffoldBanners()
|
||||
const jsonContent = JSON.stringify(bannersData, null, 2)
|
||||
const buffer = Buffer.from(jsonContent, 'utf-8')
|
||||
return await imageUploadS3(buffer, 'application/json', `${apiCacheKey}/${CACHE_FILENAMES.banners}`)
|
||||
}
|
||||
|
||||
async function createAllStoresFilesInternal(): Promise<string[]> {
|
||||
const stores = await db.select({ id: storeInfo.id }).from(storeInfo)
|
||||
const results: string[] = []
|
||||
|
||||
for (const store of stores) {
|
||||
const storeData = await scaffoldStoreWithProducts(store.id)
|
||||
const jsonContent = JSON.stringify(storeData, null, 2)
|
||||
const buffer = Buffer.from(jsonContent, 'utf-8')
|
||||
const s3Key = await imageUploadS3(buffer, 'application/json', `${apiCacheKey}/stores/${store.id}.json`)
|
||||
results.push(s3Key)
|
||||
}
|
||||
|
||||
console.log(`Created ${results.length} store cache files`)
|
||||
return results
|
||||
}
|
||||
|
||||
export async function clearUrlCache(urls: string[]): Promise<{ success: boolean; errors?: string[] }> {
|
||||
if (!cloudflareApiToken || !cloudflareZoneId) {
|
||||
console.warn('Cloudflare credentials not configured, skipping cache clear')
|
||||
return { success: false, errors: ['Cloudflare credentials not configured'] }
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await axios.post(
|
||||
`https://api.cloudflare.com/client/v4/zones/${cloudflareZoneId}/purge_cache`,
|
||||
{ files: urls },
|
||||
{
|
||||
headers: {
|
||||
'Authorization': `Bearer ${cloudflareApiToken}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
const result = response.data as { success: boolean; errors?: { message: string }[] }
|
||||
|
||||
if (!result.success) {
|
||||
const errorMessages = result.errors?.map(e => e.message) || ['Unknown error']
|
||||
console.error(`Cloudflare cache purge failed for URLs: ${urls.join(', ')}`, errorMessages)
|
||||
return { success: false, errors: errorMessages }
|
||||
}
|
||||
|
||||
console.log(`Successfully purged ${urls.length} URLs from Cloudflare cache: ${urls.join(', ')}`)
|
||||
return { success: true }
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
const errorMessage = error instanceof Error ? error.message : 'Unknown error'
|
||||
console.error(`Error clearing Cloudflare cache for URLs: ${urls.join(', ')}`, errorMessage)
|
||||
return { success: false, errors: [errorMessage] }
|
||||
}
|
||||
}
|
||||
|
||||
export async function clearAllCache(): Promise<{ success: boolean; errors?: string[] }> {
|
||||
if (!cloudflareApiToken || !cloudflareZoneId) {
|
||||
console.warn('Cloudflare credentials not configured, skipping cache clear')
|
||||
return { success: false, errors: ['Cloudflare credentials not configured'] }
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await axios.post(
|
||||
`https://api.cloudflare.com/client/v4/zones/${cloudflareZoneId}/purge_cache`,
|
||||
{ purge_everything: true },
|
||||
{
|
||||
headers: {
|
||||
'Authorization': `Bearer ${cloudflareApiToken}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
const result = response.data as { success: boolean; errors?: { message: string }[] }
|
||||
|
||||
if (!result.success) {
|
||||
const errorMessages = result.errors?.map(e => e.message) || ['Unknown error']
|
||||
console.error('Cloudflare cache purge failed:', errorMessages)
|
||||
return { success: false, errors: errorMessages }
|
||||
}
|
||||
|
||||
console.log('Successfully purged all cache from Cloudflare')
|
||||
return { success: true }
|
||||
} catch (error) {
|
||||
const errorMessage = error instanceof Error ? error.message : 'Unknown error'
|
||||
console.error('Error clearing Cloudflare cache:', errorMessage)
|
||||
return { success: false, errors: [errorMessage] }
|
||||
}
|
||||
}
|
||||
|
|
@ -17,12 +17,6 @@ export const s3Region = process.env.S3_REGION as string
|
|||
|
||||
export const assetsDomain = process.env.ASSETS_DOMAIN as string;
|
||||
|
||||
export const apiCacheKey = process.env.API_CACHE_KEY as string;
|
||||
|
||||
export const cloudflareApiToken = process.env.CLOUDFLARE_API_TOKEN as string;
|
||||
|
||||
export const cloudflareZoneId = process.env.CLOUDFLARE_ZONE_ID as string;
|
||||
|
||||
export const s3Url = process.env.S3_URL as string
|
||||
|
||||
export const redisUrl = process.env.REDIS_URL as string
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@ import { initializeAllStores } from '@/src/stores/store-initializer'
|
|||
import { initializeUserNegativityStore } from '@/src/stores/user-negativity-store'
|
||||
import { startOrderHandler, startCancellationHandler, publishOrder } from '@/src/lib/post-order-handler'
|
||||
import { deleteOrders } from '@/src/lib/delete-orders'
|
||||
import { createAllCacheFiles } from '@/src/lib/cloud_cache'
|
||||
|
||||
/**
|
||||
* Initialize all application services
|
||||
|
|
@ -26,10 +25,6 @@ export const initFunc = async (): Promise<void> => {
|
|||
startCancellationHandler(),
|
||||
]);
|
||||
|
||||
// Create all cache files after stores are initialized
|
||||
await createAllCacheFiles();
|
||||
console.log('Cache files created successfully');
|
||||
|
||||
console.log('Application initialization completed successfully');
|
||||
} catch (error) {
|
||||
console.error('Application initialization failed:', error);
|
||||
|
|
|
|||
|
|
@ -1,23 +0,0 @@
|
|||
export async function retryWithExponentialBackoff<T>(
|
||||
fn: () => Promise<T>,
|
||||
maxRetries: number = 3,
|
||||
delayMs: number = 1000
|
||||
): Promise<T> {
|
||||
let lastError: Error | undefined
|
||||
|
||||
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
||||
try {
|
||||
return await fn()
|
||||
} catch (error) {
|
||||
lastError = error instanceof Error ? error : new Error(String(error))
|
||||
|
||||
if (attempt < maxRetries) {
|
||||
console.log(`Attempt ${attempt} failed, retrying in ${delayMs}ms...`)
|
||||
await new Promise(resolve => setTimeout(resolve, delayMs))
|
||||
delayMs *= 2
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw lastError
|
||||
}
|
||||
|
|
@ -4,7 +4,6 @@ import { initializeProducts } from '@/src/stores/product-store'
|
|||
import { initializeProductTagStore } from '@/src/stores/product-tag-store'
|
||||
import { initializeSlotStore } from '@/src/stores/slot-store'
|
||||
import { initializeBannerStore } from '@/src/stores/banner-store'
|
||||
import { createAllCacheFiles } from '@/src/lib/cloud_cache'
|
||||
|
||||
/**
|
||||
* Initialize all application stores
|
||||
|
|
@ -30,11 +29,6 @@ export const initializeAllStores = async (): Promise<void> => {
|
|||
]);
|
||||
|
||||
console.log('All application stores initialized successfully');
|
||||
|
||||
// Regenerate all cache files (fire-and-forget)
|
||||
createAllCacheFiles().catch(error => {
|
||||
console.error('Failed to regenerate cache files during store initialization:', error)
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('Application stores initialization failed:', error);
|
||||
throw error;
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@ import { extractKeyFromPresignedUrl, generateSignedUrlFromS3Url } from '@/src/li
|
|||
import { ApiError } from '@/src/lib/api-error';
|
||||
import { initializeAllStores } from '@/src/stores/store-initializer'
|
||||
|
||||
|
||||
export const bannerRouter = router({
|
||||
// Get all banners
|
||||
getBanners: protectedProcedure
|
||||
|
|
|
|||
|
|
@ -9,7 +9,6 @@ import { deleteS3Image } from '@/src/lib/delete-image'
|
|||
import type { SpecialDeal } from '@/src/db/types'
|
||||
import { initializeAllStores } from '@/src/stores/store-initializer'
|
||||
|
||||
|
||||
type CreateDeal = {
|
||||
quantity: number;
|
||||
price: number;
|
||||
|
|
@ -468,15 +467,15 @@ export const productRouter = router({
|
|||
throw new ApiError('Group not found', 404);
|
||||
}
|
||||
|
||||
// Reinitialize stores to reflect changes
|
||||
await initializeAllStores();
|
||||
// Reinitialize stores to reflect changes
|
||||
await initializeAllStores();
|
||||
|
||||
return {
|
||||
message: 'Group deleted successfully',
|
||||
};
|
||||
}),
|
||||
return {
|
||||
message: 'Group deleted successfully',
|
||||
};
|
||||
}),
|
||||
|
||||
updateProductPrices: protectedProcedure
|
||||
updateProductPrices: protectedProcedure
|
||||
.input(z.object({
|
||||
updates: z.array(z.object({
|
||||
productId: z.number(),
|
||||
|
|
@ -524,12 +523,12 @@ export const productRouter = router({
|
|||
|
||||
await Promise.all(updatePromises);
|
||||
|
||||
// Reinitialize stores to reflect changes
|
||||
await initializeAllStores();
|
||||
// Reinitialize stores to reflect changes
|
||||
await initializeAllStores();
|
||||
|
||||
return {
|
||||
message: `Updated prices for ${updates.length} product(s)`,
|
||||
updatedCount: updates.length,
|
||||
};
|
||||
}),
|
||||
});
|
||||
return {
|
||||
message: `Updated prices for ${updates.length} product(s)`,
|
||||
updatedCount: updates.length,
|
||||
};
|
||||
}),
|
||||
});
|
||||
|
|
|
|||
|
|
@ -10,7 +10,6 @@ import redisClient from "@/src/lib/redis-client"
|
|||
import { getSlotSequenceKey } from "@/src/lib/redisKeyGetters"
|
||||
import { initializeAllStores } from '@/src/stores/store-initializer'
|
||||
|
||||
|
||||
interface CachedDeliverySequence {
|
||||
[userId: string]: number[];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,6 @@ import { ApiError } from '@/src/lib/api-error'
|
|||
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
|
||||
import { initializeAllStores } from '@/src/stores/store-initializer'
|
||||
|
||||
|
||||
export const storeRouter = router({
|
||||
getStores: protectedProcedure
|
||||
.query(async ({ ctx }) => {
|
||||
|
|
@ -164,16 +163,16 @@ export const storeRouter = router({
|
|||
}
|
||||
}
|
||||
|
||||
// Reinitialize stores to reflect changes
|
||||
await initializeAllStores();
|
||||
// Reinitialize stores to reflect changes
|
||||
await initializeAllStores();
|
||||
|
||||
return {
|
||||
store: updatedStore,
|
||||
message: "Store updated successfully",
|
||||
};
|
||||
}),
|
||||
return {
|
||||
store: updatedStore,
|
||||
message: "Store updated successfully",
|
||||
};
|
||||
}),
|
||||
|
||||
deleteStore: protectedProcedure
|
||||
deleteStore: protectedProcedure
|
||||
.input(z.object({
|
||||
storeId: z.number(),
|
||||
}))
|
||||
|
|
@ -202,9 +201,9 @@ export const storeRouter = router({
|
|||
};
|
||||
});
|
||||
|
||||
// Reinitialize stores to reflect changes (outside transaction)
|
||||
await initializeAllStores();
|
||||
// Reinitialize stores to reflect changes (outside transaction)
|
||||
await initializeAllStores();
|
||||
|
||||
return result;
|
||||
}),
|
||||
});
|
||||
return result;
|
||||
}),
|
||||
});
|
||||
|
|
@ -9,32 +9,9 @@ import { generateUploadUrl } from '@/src/lib/s3-client'
|
|||
import { ApiError } from '@/src/lib/api-error'
|
||||
import { getAllConstValues } from '@/src/lib/const-store'
|
||||
import { CONST_KEYS } from '@/src/lib/const-keys'
|
||||
import { assetsDomain, apiCacheKey } from '@/src/lib/env-exporter'
|
||||
|
||||
const polygon = turf.polygon(mbnrGeoJson.features[0].geometry.coordinates);
|
||||
|
||||
export async function scaffoldEssentialConsts() {
|
||||
const consts = await getAllConstValues();
|
||||
|
||||
return {
|
||||
freeDeliveryThreshold: consts[CONST_KEYS.freeDeliveryThreshold] ?? 200,
|
||||
deliveryCharge: consts[CONST_KEYS.deliveryCharge] ?? 0,
|
||||
flashFreeDeliveryThreshold: consts[CONST_KEYS.flashFreeDeliveryThreshold] ?? 500,
|
||||
flashDeliveryCharge: consts[CONST_KEYS.flashDeliveryCharge] ?? 69,
|
||||
popularItems: consts[CONST_KEYS.popularItems] ?? '5,3,2,4,1',
|
||||
versionNum: consts[CONST_KEYS.versionNum] ?? '1.1.0',
|
||||
playStoreUrl: consts[CONST_KEYS.playStoreUrl] ?? 'https://play.google.com/store/apps/details?id=in.freshyo.app',
|
||||
appStoreUrl: consts[CONST_KEYS.appStoreUrl] ?? 'https://apps.apple.com/in/app/freshyo/id6756889077',
|
||||
webViewHtml: null,
|
||||
isWebviewClosable: true,
|
||||
isFlashDeliveryEnabled: consts[CONST_KEYS.isFlashDeliveryEnabled] ?? true,
|
||||
supportMobile: consts[CONST_KEYS.supportMobile] ?? '',
|
||||
supportEmail: consts[CONST_KEYS.supportEmail] ?? '',
|
||||
assetsDomain,
|
||||
apiCacheKey,
|
||||
};
|
||||
}
|
||||
|
||||
export const commonApiRouter = router({
|
||||
product: commonRouter,
|
||||
getStoresSummary: publicProcedure
|
||||
|
|
@ -122,8 +99,23 @@ export const commonApiRouter = router({
|
|||
}),
|
||||
essentialConsts: publicProcedure
|
||||
.query(async () => {
|
||||
const response = await scaffoldEssentialConsts();
|
||||
return response;
|
||||
const consts = await getAllConstValues();
|
||||
|
||||
return {
|
||||
freeDeliveryThreshold: consts[CONST_KEYS.freeDeliveryThreshold] ?? 200,
|
||||
deliveryCharge: consts[CONST_KEYS.deliveryCharge] ?? 0,
|
||||
flashFreeDeliveryThreshold: consts[CONST_KEYS.flashFreeDeliveryThreshold] ?? 500,
|
||||
flashDeliveryCharge: consts[CONST_KEYS.flashDeliveryCharge] ?? 69,
|
||||
popularItems: consts[CONST_KEYS.popularItems] ?? '5,3,2,4,1',
|
||||
versionNum: consts[CONST_KEYS.versionNum] ?? '1.1.0',
|
||||
playStoreUrl: consts[CONST_KEYS.playStoreUrl] ?? 'https://play.google.com/store/apps/details?id=in.freshyo.app',
|
||||
appStoreUrl: consts[CONST_KEYS.appStoreUrl] ?? 'https://apps.apple.com/in/app/freshyo/id6756889077',
|
||||
webViewHtml: null,
|
||||
isWebviewClosable: true,
|
||||
isFlashDeliveryEnabled: consts[CONST_KEYS.isFlashDeliveryEnabled] ?? true,
|
||||
supportMobile: consts[CONST_KEYS.supportMobile] ?? '',
|
||||
supportEmail: consts[CONST_KEYS.supportEmail] ?? '',
|
||||
};
|
||||
}),
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -1,10 +1,12 @@
|
|||
import { router, publicProcedure } from '@/src/trpc/trpc-index'
|
||||
import { db } from '@/src/db/db_index'
|
||||
import { productInfo, units, productSlots, deliverySlotInfo, storeInfo } from '@/src/db/schema'
|
||||
import { productInfo, units, productSlots, deliverySlotInfo, storeInfo, productTags, productTagInfo } from '@/src/db/schema'
|
||||
import { eq, gt, and, sql, inArray } from 'drizzle-orm';
|
||||
import { generateSignedUrlsFromS3Urls, generateSignedUrlFromS3Url } from '@/src/lib/s3-client'
|
||||
import { z } from 'zod';
|
||||
import { getAllProducts as getAllProductsFromCache } from '@/src/stores/product-store'
|
||||
import { getDashboardTags as getDashboardTagsFromCache } from '@/src/stores/product-tag-store'
|
||||
import Fuse from 'fuse.js';
|
||||
|
||||
export const getNextDeliveryDate = async (productId: number): Promise<Date | null> => {
|
||||
const result = await db
|
||||
|
|
@ -26,51 +28,9 @@ export const getNextDeliveryDate = async (productId: number): Promise<Date | nul
|
|||
return result[0]?.deliveryTime || null;
|
||||
};
|
||||
|
||||
export async function scaffoldProducts() {
|
||||
// Get all products from cache
|
||||
let products = await getAllProductsFromCache();
|
||||
products = products.filter(item => Boolean(item.id))
|
||||
|
||||
// Get suspended product IDs to filter them out
|
||||
const suspendedProducts = await db
|
||||
.select({ id: productInfo.id })
|
||||
.from(productInfo)
|
||||
.where(eq(productInfo.isSuspended, true));
|
||||
|
||||
const suspendedProductIds = new Set(suspendedProducts.map(sp => sp.id));
|
||||
|
||||
// Filter out suspended products
|
||||
products = products.filter(product => !suspendedProductIds.has(product.id));
|
||||
|
||||
// Format products to match the expected response structure
|
||||
const formattedProducts = await Promise.all(
|
||||
products.map(async (product) => {
|
||||
const nextDeliveryDate = await getNextDeliveryDate(product.id);
|
||||
return {
|
||||
id: product.id,
|
||||
name: product.name,
|
||||
shortDescription: product.shortDescription,
|
||||
price: parseFloat(product.price),
|
||||
marketPrice: product.marketPrice ? parseFloat(product.marketPrice) : null,
|
||||
unit: product.unitNotation,
|
||||
unitNotation: product.unitNotation,
|
||||
incrementStep: product.incrementStep,
|
||||
productQuantity: product.productQuantity,
|
||||
storeId: product.store?.id || null,
|
||||
isOutOfStock: product.isOutOfStock,
|
||||
isFlashAvailable: product.isFlashAvailable,
|
||||
nextDeliveryDate: nextDeliveryDate ? nextDeliveryDate.toISOString() : null,
|
||||
images: product.images,
|
||||
flashPrice: product.flashPrice
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
return {
|
||||
products: formattedProducts,
|
||||
count: formattedProducts.length,
|
||||
};
|
||||
}
|
||||
|
||||
export const commonRouter = router({
|
||||
getDashboardTags: publicProcedure
|
||||
|
|
@ -84,9 +44,89 @@ export const commonRouter = router({
|
|||
}),
|
||||
|
||||
getAllProductsSummary: publicProcedure
|
||||
.query(async () => {
|
||||
const response = await scaffoldProducts();
|
||||
return response;
|
||||
.input(z.object({
|
||||
searchQuery: z.string().optional(),
|
||||
tagId: z.number().optional()
|
||||
}))
|
||||
.query(async ({ input }) => {
|
||||
const { searchQuery, tagId } = input;
|
||||
|
||||
// Get all products from cache
|
||||
let products = await getAllProductsFromCache();
|
||||
products = products.filter(item => Boolean(item.id))
|
||||
|
||||
// Apply tag filtering if tagId is provided
|
||||
if (tagId) {
|
||||
// Get products that have this tag from the database
|
||||
const taggedProducts = await db
|
||||
.select({ productId: productTags.productId })
|
||||
.from(productTags)
|
||||
.where(eq(productTags.tagId, tagId));
|
||||
|
||||
const taggedProductIds = new Set(taggedProducts.map(tp => tp.productId));
|
||||
|
||||
// Filter products based on tag
|
||||
products = products.filter(product => taggedProductIds.has(product.id));
|
||||
}
|
||||
|
||||
// Apply search filtering if searchQuery is provided using Fuse.js
|
||||
if (searchQuery) {
|
||||
const fuse = new Fuse(products, {
|
||||
keys: [
|
||||
'name',
|
||||
'shortDescription',
|
||||
'longDescription',
|
||||
'store.name', // Search in store name too
|
||||
'productTags', // Search in product tags too
|
||||
],
|
||||
threshold: 0.3, // Adjust fuzziness (0.0 = exact match, 1.0 = match anything)
|
||||
includeScore: true,
|
||||
shouldSort: true,
|
||||
});
|
||||
|
||||
const fuseResults = fuse.search(searchQuery);
|
||||
products = fuseResults.map(result => result.item);
|
||||
}
|
||||
|
||||
// Get suspended product IDs to filter them out
|
||||
const suspendedProducts = await db
|
||||
.select({ id: productInfo.id })
|
||||
.from(productInfo)
|
||||
.where(eq(productInfo.isSuspended, true));
|
||||
|
||||
const suspendedProductIds = new Set(suspendedProducts.map(sp => sp.id));
|
||||
|
||||
// Filter out suspended products
|
||||
products = products.filter(product => !suspendedProductIds.has(product.id));
|
||||
|
||||
// Format products to match the expected response structure
|
||||
const formattedProducts = await Promise.all(
|
||||
products.map(async (product) => {
|
||||
const nextDeliveryDate = await getNextDeliveryDate(product.id);
|
||||
return {
|
||||
id: product.id,
|
||||
name: product.name,
|
||||
shortDescription: product.shortDescription,
|
||||
price: parseFloat(product.price),
|
||||
marketPrice: product.marketPrice ? parseFloat(product.marketPrice) : null,
|
||||
unit: product.unitNotation,
|
||||
unitNotation: product.unitNotation,
|
||||
incrementStep: product.incrementStep,
|
||||
productQuantity: product.productQuantity,
|
||||
storeId: product.store?.id || null,
|
||||
isOutOfStock: product.isOutOfStock,
|
||||
isFlashAvailable: product.isFlashAvailable,
|
||||
nextDeliveryDate: nextDeliveryDate ? nextDeliveryDate.toISOString() : null,
|
||||
images: product.images, // Already signed URLs from cache
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
|
||||
return {
|
||||
products: formattedProducts,
|
||||
count: formattedProducts.length,
|
||||
};
|
||||
}),
|
||||
|
||||
getStoresSummary: publicProcedure
|
||||
|
|
@ -113,4 +153,4 @@ export const commonRouter = router({
|
|||
status: "ok",
|
||||
};
|
||||
}),
|
||||
});
|
||||
});
|
||||
|
|
@ -1,30 +1,38 @@
|
|||
import { db } from '@/src/db/db_index';
|
||||
import { homeBanners } from '@/src/db/schema';
|
||||
import { publicProcedure, router } from '@/src/trpc/trpc-index';
|
||||
import { scaffoldAssetUrl } from '@/src/lib/s3-client';
|
||||
import { generateSignedUrlFromS3Url } from '@/src/lib/s3-client';
|
||||
import { isNotNull, asc } from 'drizzle-orm';
|
||||
|
||||
export async function scaffoldBanners() {
|
||||
const banners = await db.query.homeBanners.findMany({
|
||||
where: isNotNull(homeBanners.serialNum), // Only show assigned banners
|
||||
orderBy: asc(homeBanners.serialNum), // Order by slot number 1-4
|
||||
});
|
||||
|
||||
// Convert S3 keys to signed URLs for client
|
||||
const bannersWithSignedUrls = banners.map((banner) => ({
|
||||
...banner,
|
||||
imageUrl: banner.imageUrl ? scaffoldAssetUrl(banner.imageUrl) : banner.imageUrl,
|
||||
}));
|
||||
|
||||
return {
|
||||
banners: bannersWithSignedUrls,
|
||||
};
|
||||
}
|
||||
|
||||
export const bannerRouter = router({
|
||||
getBanners: publicProcedure
|
||||
.query(async () => {
|
||||
const response = await scaffoldBanners();
|
||||
return response;
|
||||
const banners = await db.query.homeBanners.findMany({
|
||||
where: isNotNull(homeBanners.serialNum), // Only show assigned banners
|
||||
orderBy: asc(homeBanners.serialNum), // Order by slot number 1-4
|
||||
});
|
||||
|
||||
// Convert S3 keys to signed URLs for client
|
||||
const bannersWithSignedUrls = await Promise.all(
|
||||
banners.map(async (banner) => {
|
||||
try {
|
||||
return {
|
||||
...banner,
|
||||
imageUrl: banner.imageUrl ? await generateSignedUrlFromS3Url(banner.imageUrl) : banner.imageUrl,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error(`Failed to generate signed URL for banner ${banner.id}:`, error);
|
||||
return {
|
||||
...banner,
|
||||
imageUrl: banner.imageUrl, // Keep original on error
|
||||
};
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
|
||||
return {
|
||||
banners: bannersWithSignedUrls,
|
||||
};
|
||||
}),
|
||||
});
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ import {
|
|||
productInfo,
|
||||
units,
|
||||
} from "@/src/db/schema";
|
||||
import { eq, and } from "drizzle-orm";
|
||||
import { eq, and, gt, asc } from "drizzle-orm";
|
||||
import { getAllSlots as getAllSlotsFromCache, getSlotById as getSlotByIdFromCache } from "@/src/stores/slot-store";
|
||||
import dayjs from 'dayjs';
|
||||
|
||||
|
|
@ -32,42 +32,6 @@ async function getSlotData(slotId: number) {
|
|||
};
|
||||
}
|
||||
|
||||
export async function scaffoldSlotsWithProducts() {
|
||||
const allSlots = await getAllSlotsFromCache();
|
||||
const currentTime = new Date();
|
||||
const validSlots = allSlots
|
||||
.filter((slot) => {
|
||||
return dayjs(slot.freezeTime).isAfter(currentTime) &&
|
||||
dayjs(slot.deliveryTime).isAfter(currentTime) &&
|
||||
!slot.isCapacityFull;
|
||||
})
|
||||
.sort((a, b) => dayjs(a.deliveryTime).valueOf() - dayjs(b.deliveryTime).valueOf());
|
||||
|
||||
// Fetch all products for availability info
|
||||
const allProducts = await db
|
||||
.select({
|
||||
id: productInfo.id,
|
||||
name: productInfo.name,
|
||||
isOutOfStock: productInfo.isOutOfStock,
|
||||
isFlashAvailable: productInfo.isFlashAvailable,
|
||||
})
|
||||
.from(productInfo)
|
||||
.where(eq(productInfo.isSuspended, false));
|
||||
|
||||
const productAvailability = allProducts.map(product => ({
|
||||
id: product.id,
|
||||
name: product.name,
|
||||
isOutOfStock: product.isOutOfStock,
|
||||
isFlashAvailable: product.isFlashAvailable,
|
||||
}));
|
||||
|
||||
return {
|
||||
slots: validSlots,
|
||||
productAvailability,
|
||||
count: validSlots.length,
|
||||
};
|
||||
}
|
||||
|
||||
export const slotsRouter = router({
|
||||
getSlots: publicProcedure.query(async () => {
|
||||
const slots = await db.query.deliverySlotInfo.findMany({
|
||||
|
|
@ -80,8 +44,40 @@ export const slotsRouter = router({
|
|||
}),
|
||||
|
||||
getSlotsWithProducts: publicProcedure.query(async () => {
|
||||
const response = await scaffoldSlotsWithProducts();
|
||||
return response;
|
||||
const allSlots = await getAllSlotsFromCache();
|
||||
const currentTime = new Date();
|
||||
const validSlots = allSlots
|
||||
.filter((slot) => {
|
||||
return dayjs(slot.freezeTime).isAfter(currentTime) &&
|
||||
dayjs(slot.deliveryTime).isAfter(currentTime) &&
|
||||
!slot.isCapacityFull;
|
||||
})
|
||||
.sort((a, b) => dayjs(a.deliveryTime).valueOf() - dayjs(b.deliveryTime).valueOf());
|
||||
|
||||
return {
|
||||
slots: validSlots,
|
||||
count: validSlots.length,
|
||||
};
|
||||
}),
|
||||
|
||||
nextMajorDelivery: publicProcedure.query(async () => {
|
||||
const now = new Date();
|
||||
|
||||
// Find the next upcoming active delivery slot ID
|
||||
const nextSlot = await db.query.deliverySlotInfo.findFirst({
|
||||
where: and(
|
||||
eq(deliverySlotInfo.isActive, true),
|
||||
gt(deliverySlotInfo.deliveryTime, now),
|
||||
),
|
||||
orderBy: asc(deliverySlotInfo.deliveryTime),
|
||||
});
|
||||
|
||||
if (!nextSlot) {
|
||||
return null; // No upcoming delivery slots
|
||||
}
|
||||
|
||||
// Get formatted data using helper method
|
||||
return await getSlotData(nextSlot.id);
|
||||
}),
|
||||
|
||||
getSlotById: publicProcedure
|
||||
|
|
|
|||
|
|
@ -5,149 +5,67 @@ import { storeInfo, productInfo, units } from '@/src/db/schema';
|
|||
import { eq, and, sql } from 'drizzle-orm';
|
||||
import { scaffoldAssetUrl } from '@/src/lib/s3-client';
|
||||
import { ApiError } from '@/src/lib/api-error';
|
||||
import { getTagsByStoreId } from '@/src/stores/product-tag-store';
|
||||
|
||||
export async function scaffoldStores() {
|
||||
const storesData = await db
|
||||
.select({
|
||||
id: storeInfo.id,
|
||||
name: storeInfo.name,
|
||||
description: storeInfo.description,
|
||||
imageUrl: storeInfo.imageUrl,
|
||||
productCount: sql<number>`count(${productInfo.id})`.as('productCount'),
|
||||
})
|
||||
.from(storeInfo)
|
||||
.leftJoin(
|
||||
productInfo,
|
||||
and(eq(productInfo.storeId, storeInfo.id), eq(productInfo.isSuspended, false))
|
||||
)
|
||||
.groupBy(storeInfo.id);
|
||||
|
||||
// Generate signed URLs for store images and fetch sample products
|
||||
const storesWithDetails = await Promise.all(
|
||||
storesData.map(async (store) => {
|
||||
const signedImageUrl = store.imageUrl ? scaffoldAssetUrl(store.imageUrl) : null;
|
||||
|
||||
// Fetch up to 3 products for this store
|
||||
const sampleProducts = await db
|
||||
export const storesRouter = router({
|
||||
getStores: publicProcedure
|
||||
.query(async () => {
|
||||
const storesData = await db
|
||||
.select({
|
||||
id: productInfo.id,
|
||||
name: productInfo.name,
|
||||
images: productInfo.images,
|
||||
id: storeInfo.id,
|
||||
name: storeInfo.name,
|
||||
description: storeInfo.description,
|
||||
imageUrl: storeInfo.imageUrl,
|
||||
productCount: sql<number>`count(${productInfo.id})`.as('productCount'),
|
||||
})
|
||||
.from(productInfo)
|
||||
.where(and(eq(productInfo.storeId, store.id), eq(productInfo.isSuspended, false)))
|
||||
.limit(3);
|
||||
.from(storeInfo)
|
||||
.leftJoin(
|
||||
productInfo,
|
||||
and(eq(productInfo.storeId, storeInfo.id), eq(productInfo.isSuspended, false))
|
||||
)
|
||||
.groupBy(storeInfo.id);
|
||||
|
||||
// Generate signed URLs for store images and fetch sample products
|
||||
const storesWithDetails = await Promise.all(
|
||||
storesData.map(async (store) => {
|
||||
const signedImageUrl = store.imageUrl ? scaffoldAssetUrl(store.imageUrl) : null;
|
||||
|
||||
// Fetch up to 3 products for this store
|
||||
const sampleProducts = await db
|
||||
.select({
|
||||
id: productInfo.id,
|
||||
name: productInfo.name,
|
||||
images: productInfo.images,
|
||||
})
|
||||
.from(productInfo)
|
||||
.where(and(eq(productInfo.storeId, store.id), eq(productInfo.isSuspended, false)))
|
||||
.limit(3);
|
||||
|
||||
// Generate signed URLs for product images
|
||||
const productsWithSignedUrls = await Promise.all(
|
||||
sampleProducts.map(async (product) => {
|
||||
const images = product.images as string[];
|
||||
return {
|
||||
id: product.id,
|
||||
name: product.name,
|
||||
signedImageUrl: (images && images.length > 0) ? scaffoldAssetUrl(images[0]) : null,
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
// Generate signed URLs for product images
|
||||
const productsWithSignedUrls = await Promise.all(
|
||||
sampleProducts.map(async (product) => {
|
||||
const images = product.images as string[];
|
||||
return {
|
||||
id: product.id,
|
||||
name: product.name,
|
||||
signedImageUrl: (images && images.length > 0) ? scaffoldAssetUrl(images[0]) : null,
|
||||
id: store.id,
|
||||
name: store.name,
|
||||
description: store.description,
|
||||
signedImageUrl,
|
||||
productCount: store.productCount,
|
||||
sampleProducts: productsWithSignedUrls,
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
return {
|
||||
id: store.id,
|
||||
name: store.name,
|
||||
description: store.description,
|
||||
signedImageUrl,
|
||||
productCount: store.productCount,
|
||||
sampleProducts: productsWithSignedUrls,
|
||||
stores: storesWithDetails,
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
return {
|
||||
stores: storesWithDetails,
|
||||
};
|
||||
}
|
||||
|
||||
export async function scaffoldStoreWithProducts(storeId: number) {
|
||||
// Fetch store info
|
||||
const storeData = await db.query.storeInfo.findFirst({
|
||||
where: eq(storeInfo.id, storeId),
|
||||
columns: {
|
||||
id: true,
|
||||
name: true,
|
||||
description: true,
|
||||
imageUrl: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!storeData) {
|
||||
throw new ApiError('Store not found', 404);
|
||||
}
|
||||
|
||||
// Generate signed URL for store image
|
||||
const signedImageUrl = storeData.imageUrl ? scaffoldAssetUrl(storeData.imageUrl) : null;
|
||||
|
||||
// Fetch products for this store
|
||||
const productsData = await db
|
||||
.select({
|
||||
id: productInfo.id,
|
||||
name: productInfo.name,
|
||||
shortDescription: productInfo.shortDescription,
|
||||
price: productInfo.price,
|
||||
marketPrice: productInfo.marketPrice,
|
||||
images: productInfo.images,
|
||||
isOutOfStock: productInfo.isOutOfStock,
|
||||
incrementStep: productInfo.incrementStep,
|
||||
unitShortNotation: units.shortNotation,
|
||||
unitNotation: units.shortNotation,
|
||||
productQuantity: productInfo.productQuantity,
|
||||
})
|
||||
.from(productInfo)
|
||||
.innerJoin(units, eq(productInfo.unitId, units.id))
|
||||
.where(and(eq(productInfo.storeId, storeId), eq(productInfo.isSuspended, false)));
|
||||
|
||||
|
||||
// Generate signed URLs for product images
|
||||
const productsWithSignedUrls = await Promise.all(
|
||||
productsData.map(async (product) => ({
|
||||
id: product.id,
|
||||
name: product.name,
|
||||
shortDescription: product.shortDescription,
|
||||
price: product.price,
|
||||
marketPrice: product.marketPrice,
|
||||
incrementStep: product.incrementStep,
|
||||
unit: product.unitShortNotation,
|
||||
unitNotation: product.unitNotation,
|
||||
images: scaffoldAssetUrl((product.images as string[]) || []),
|
||||
isOutOfStock: product.isOutOfStock,
|
||||
productQuantity: product.productQuantity
|
||||
}))
|
||||
);
|
||||
|
||||
const tags = await getTagsByStoreId(storeId);
|
||||
|
||||
return {
|
||||
store: {
|
||||
id: storeData.id,
|
||||
name: storeData.name,
|
||||
description: storeData.description,
|
||||
signedImageUrl,
|
||||
},
|
||||
products: productsWithSignedUrls,
|
||||
tags: tags.map(tag => ({
|
||||
id: tag.id,
|
||||
tagName: tag.tagName,
|
||||
tagDescription: tag.tagDescription,
|
||||
imageUrl: tag.imageUrl,
|
||||
productIds: tag.productIds,
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
export const storesRouter = router({
|
||||
getStores: publicProcedure
|
||||
.query(async () => {
|
||||
const response = await scaffoldStores();
|
||||
return response;
|
||||
}),
|
||||
|
||||
getStoreWithProducts: publicProcedure
|
||||
|
|
@ -156,7 +74,70 @@ export const storesRouter = router({
|
|||
}))
|
||||
.query(async ({ input }) => {
|
||||
const { storeId } = input;
|
||||
const response = await scaffoldStoreWithProducts(storeId);
|
||||
return response;
|
||||
|
||||
// Fetch store info
|
||||
const storeData = await db.query.storeInfo.findFirst({
|
||||
where: eq(storeInfo.id, storeId),
|
||||
columns: {
|
||||
id: true,
|
||||
name: true,
|
||||
description: true,
|
||||
imageUrl: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!storeData) {
|
||||
throw new ApiError('Store not found', 404);
|
||||
}
|
||||
|
||||
// Generate signed URL for store image
|
||||
const signedImageUrl = storeData.imageUrl ? scaffoldAssetUrl(storeData.imageUrl) : null;
|
||||
|
||||
// Fetch products for this store
|
||||
const productsData = await db
|
||||
.select({
|
||||
id: productInfo.id,
|
||||
name: productInfo.name,
|
||||
shortDescription: productInfo.shortDescription,
|
||||
price: productInfo.price,
|
||||
marketPrice: productInfo.marketPrice,
|
||||
images: productInfo.images,
|
||||
isOutOfStock: productInfo.isOutOfStock,
|
||||
incrementStep: productInfo.incrementStep,
|
||||
unitShortNotation: units.shortNotation,
|
||||
unitNotation: units.shortNotation,
|
||||
productQuantity: productInfo.productQuantity,
|
||||
})
|
||||
.from(productInfo)
|
||||
.innerJoin(units, eq(productInfo.unitId, units.id))
|
||||
.where(and(eq(productInfo.storeId, storeId), eq(productInfo.isSuspended, false)));
|
||||
|
||||
|
||||
// Generate signed URLs for product images
|
||||
const productsWithSignedUrls = await Promise.all(
|
||||
productsData.map(async (product) => ({
|
||||
id: product.id,
|
||||
name: product.name,
|
||||
shortDescription: product.shortDescription,
|
||||
price: product.price,
|
||||
marketPrice: product.marketPrice,
|
||||
incrementStep: product.incrementStep,
|
||||
unit: product.unitShortNotation,
|
||||
unitNotation: product.unitNotation,
|
||||
images: scaffoldAssetUrl((product.images as string[]) || []),
|
||||
isOutOfStock: product.isOutOfStock,
|
||||
productQuantity: product.productQuantity
|
||||
}))
|
||||
);
|
||||
|
||||
return {
|
||||
store: {
|
||||
id: storeData.id,
|
||||
name: storeData.name,
|
||||
description: storeData.description,
|
||||
signedImageUrl,
|
||||
},
|
||||
products: productsWithSignedUrls,
|
||||
};
|
||||
}),
|
||||
});
|
||||
|
|
|
|||
|
|
@ -3,11 +3,6 @@ import { z } from 'zod';
|
|||
import { adminRouter } from '@/src/trpc/apis/admin-apis/apis/admin-trpc-index'
|
||||
import { userRouter } from '@/src/trpc/apis/user-apis/apis/user-trpc-index'
|
||||
import { commonApiRouter } from '@/src/trpc/apis/common-apis/common-trpc-index'
|
||||
import { scaffoldProducts } from './apis/common-apis/common';
|
||||
import { scaffoldStores, scaffoldStoreWithProducts } from './apis/user-apis/apis/stores';
|
||||
import { scaffoldSlotsWithProducts } from './apis/user-apis/apis/slots';
|
||||
import { scaffoldEssentialConsts } from './apis/common-apis/common-trpc-index';
|
||||
import { scaffoldBanners } from './apis/user-apis/apis/banners';
|
||||
|
||||
// Create the main app router
|
||||
export const appRouter = router({
|
||||
|
|
@ -21,13 +16,5 @@ export const appRouter = router({
|
|||
common: commonApiRouter,
|
||||
});
|
||||
|
||||
|
||||
// Export type definition of API
|
||||
export type AppRouter = typeof appRouter;
|
||||
|
||||
export type AllProductsApiType = Awaited<ReturnType<typeof scaffoldProducts>>;
|
||||
export type StoresApiType = Awaited<ReturnType<typeof scaffoldStores>>;
|
||||
export type SlotsApiType = Awaited<ReturnType<typeof scaffoldSlotsWithProducts>>;
|
||||
export type EssentialConstsApiType = Awaited<ReturnType<typeof scaffoldEssentialConsts>>;
|
||||
export type BannersApiType = Awaited<ReturnType<typeof scaffoldBanners>>;
|
||||
export type StoreWithProductsApiType = Awaited<ReturnType<typeof scaffoldStoreWithProducts>>;
|
||||
export type AppRouter = typeof appRouter;
|
||||
|
|
@ -33,8 +33,6 @@
|
|||
"shared-types": ["../shared-types"],
|
||||
"@commonTypes": ["../../packages/ui/shared-types"],
|
||||
"@commonTypes/*": ["../../packages/ui/shared-types/*"],
|
||||
"@packages/shared": ["../../packages/shared"],
|
||||
"@packages/shared/*": ["../../packages/shared/*"]
|
||||
},
|
||||
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
|
||||
// "typeRoots": [""], /* Specify multiple folders that act like './node_modules/@types'. */
|
||||
|
|
@ -118,6 +116,6 @@
|
|||
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
|
||||
"skipLibCheck": true /* Skip type checking all .d.ts files. */
|
||||
},
|
||||
"include": ["src", "types", "index.ts", "../shared-types", "../../packages/shared"]
|
||||
"include": ["src", "types", "index.ts", "../shared-types"]
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import { trpc } from '@/src/trpc-client';
|
|||
import { MyText, MyTouchableOpacity, tw, AppContainer } from 'common-ui';
|
||||
import { useRouter } from 'expo-router';
|
||||
import MaterialIcons from '@expo/vector-icons/MaterialIcons';
|
||||
import { useGetEssentialConsts } from '@/src/hooks/prominent-api-hooks';
|
||||
import { useGetEssentialConsts } from '@/src/api-hooks/essential-consts.api';
|
||||
|
||||
export default function FlashDeliveryBaseLayout() {
|
||||
const router = useRouter();
|
||||
|
|
|
|||
|
|
@ -21,15 +21,13 @@ import AddToCartDialog from "@/src/components/AddToCartDialog";
|
|||
import MyFlatList from "common-ui/src/components/flat-list";
|
||||
|
||||
import { trpc } from "@/src/trpc-client";
|
||||
import { useAllProducts, useStores, useSlots, useGetEssentialConsts } from "@/src/hooks/prominent-api-hooks";
|
||||
import { useProductSlotIdentifier } from "@/hooks/useProductSlotIdentifier";
|
||||
import { useCentralSlotStore } from "@/src/store/centralSlotStore";
|
||||
import { useCentralProductStore } from "@/src/store/centralProductStore";
|
||||
import FloatingCartBar from "@/components/floating-cart-bar";
|
||||
import BannerCarousel from "@/components/BannerCarousel";
|
||||
import { useUserDetails } from "@/src/contexts/AuthContext";
|
||||
import TabLayoutWrapper from "@/components/TabLayoutWrapper";
|
||||
import { useNavigationStore } from "@/src/store/navigationStore";
|
||||
import { useGetEssentialConsts } from "@/src/api-hooks/essential-consts.api";
|
||||
import NextOrderGlimpse from "@/components/NextOrderGlimpse";
|
||||
dayjs.extend(relativeTime);
|
||||
|
||||
|
|
@ -362,6 +360,8 @@ export default function Dashboard() {
|
|||
const router = useRouter();
|
||||
const userDetails = useUserDetails();
|
||||
const [inputQuery, setInputQuery] = useState("");
|
||||
const [searchQuery, setSearchQuery] = useState("");
|
||||
const [selectedTagId, setSelectedTagId] = useState<number | null>(null);
|
||||
const [isLoadingDialogOpen, setIsLoadingDialogOpen] = useState(false);
|
||||
const [gradientHeight, setGradientHeight] = useState(0);
|
||||
const [displayedProducts, setDisplayedProducts] = useState<any[]>([]);
|
||||
|
|
@ -369,21 +369,22 @@ export default function Dashboard() {
|
|||
const [isLoadingMore, setIsLoadingMore] = useState(false);
|
||||
const { backgroundColor } = useStatusBarStore();
|
||||
const { getQuickestSlot } = useProductSlotIdentifier();
|
||||
const productSlotsMap = useCentralSlotStore((state) => state.productSlotsMap);
|
||||
const refetchProducts = useCentralProductStore((state) => state.refetchProducts);
|
||||
const refetchSlotsFromStore = useCentralSlotStore((state) => state.refetchSlots);
|
||||
const [isRefreshing, setIsRefreshing] = useState(false);
|
||||
|
||||
const {
|
||||
data: productsData,
|
||||
isLoading,
|
||||
error,
|
||||
} = useAllProducts();
|
||||
refetch,
|
||||
} = trpc.common.product.getAllProductsSummary.useQuery({
|
||||
searchQuery: searchQuery || undefined,
|
||||
tagId: selectedTagId || undefined,
|
||||
});
|
||||
|
||||
const { data: essentialConsts, isLoading: isLoadingConsts, error: constsError, refetch: refetchConsts } = useGetEssentialConsts();
|
||||
|
||||
const { data: storesData, refetch: refetchStores } = useStores();
|
||||
const { data: slotsData } = useSlots();
|
||||
const { data: storesData, refetch: refetchStores } = trpc.user.stores.getStores.useQuery();
|
||||
const { data: slotsData, refetch: refetchSlots } = trpc.user.slots.getSlotsWithProducts.useQuery();
|
||||
|
||||
const products = productsData?.products || [];
|
||||
|
||||
|
|
@ -396,18 +397,15 @@ export default function Dashboard() {
|
|||
const slotB = getQuickestSlot(b.id);
|
||||
if (slotA && !slotB) return -1;
|
||||
if (!slotA && slotB) return 1;
|
||||
const aOutOfStock = productSlotsMap[a.id]?.isOutOfStock;
|
||||
const bOutOfStock = productSlotsMap[b.id]?.isOutOfStock;
|
||||
if (aOutOfStock && !bOutOfStock) return 1;
|
||||
if (!aOutOfStock && bOutOfStock) return -1;
|
||||
if (a.isOutOfStock && !b.isOutOfStock) return 1;
|
||||
if (!a.isOutOfStock && b.isOutOfStock) return -1;
|
||||
return 0;
|
||||
});
|
||||
|
||||
console.log('setting the displayed products')
|
||||
|
||||
setDisplayedProducts(initialBatch);
|
||||
setHasMore(products.length > 10);
|
||||
}
|
||||
}, [productsData, productSlotsMap]);
|
||||
}, [productsData]);
|
||||
|
||||
const popularItemIds = useMemo(() => {
|
||||
const popularItems = essentialConsts?.popularItems;
|
||||
|
|
@ -442,22 +440,11 @@ export default function Dashboard() {
|
|||
const handleRefresh = useCallback(async () => {
|
||||
setIsRefreshing(true);
|
||||
try {
|
||||
const promises = [];
|
||||
|
||||
if (refetchProducts) {
|
||||
promises.push(refetchProducts());
|
||||
}
|
||||
if (refetchSlotsFromStore) {
|
||||
promises.push(refetchSlotsFromStore());
|
||||
}
|
||||
promises.push(refetchStores());
|
||||
promises.push(refetchConsts());
|
||||
|
||||
await Promise.all(promises);
|
||||
await Promise.all([refetch(), refetchStores(), refetchSlots(), refetchConsts()]);
|
||||
} finally {
|
||||
setIsRefreshing(false);
|
||||
}
|
||||
}, [refetchProducts, refetchSlotsFromStore, refetchStores, refetchConsts]);
|
||||
}, [refetch, refetchStores, refetchSlots, refetchConsts]);
|
||||
|
||||
useManualRefresh(() => {
|
||||
handleRefresh();
|
||||
|
|
@ -525,9 +512,7 @@ export default function Dashboard() {
|
|||
</View>
|
||||
);
|
||||
}
|
||||
let str = ''
|
||||
displayedProducts.forEach(product => str += `${product.id}-`)
|
||||
// console.log(str)
|
||||
|
||||
return (
|
||||
<TabLayoutWrapper>
|
||||
<View style={searchBarContainerStyle}>
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import React, { useState, useRef, useEffect, useCallback, useMemo } from "react";
|
||||
import React, { useState, useRef, useEffect, useCallback } from "react";
|
||||
import { View, Dimensions } from "react-native";
|
||||
import { useRouter, useLocalSearchParams } from "expo-router";
|
||||
import {
|
||||
|
|
@ -10,8 +10,7 @@ import {
|
|||
SearchBar,
|
||||
} from "common-ui";
|
||||
import MaterialIcons from "@expo/vector-icons/MaterialIcons";
|
||||
import Fuse from "fuse.js";
|
||||
import { useAllProducts } from "@/src/hooks/prominent-api-hooks";
|
||||
import { trpc } from "@/src/trpc-client";
|
||||
import ProductCard from "@/components/ProductCard";
|
||||
import FloatingCartBar from "@/components/floating-cart-bar";
|
||||
|
||||
|
|
@ -52,27 +51,12 @@ export default function SearchResults() {
|
|||
});
|
||||
}, []);
|
||||
|
||||
const { data: productsData, isLoading, error, refetch } = useAllProducts();
|
||||
|
||||
const allProducts = productsData?.products || [];
|
||||
|
||||
// Client-side search filtering using Fuse.js
|
||||
const products = useMemo(() => {
|
||||
if (!debouncedQuery.trim()) return allProducts;
|
||||
|
||||
const fuse = new Fuse(allProducts, {
|
||||
keys: [
|
||||
'name',
|
||||
'shortDescription',
|
||||
],
|
||||
threshold: 0.3,
|
||||
includeScore: true,
|
||||
shouldSort: true,
|
||||
const { data: productsData, isLoading, error, refetch } =
|
||||
trpc.common.product.getAllProductsSummary.useQuery({
|
||||
searchQuery: debouncedQuery || undefined,
|
||||
});
|
||||
|
||||
const fuseResults = fuse.search(debouncedQuery);
|
||||
return fuseResults.map(result => result.item);
|
||||
}, [allProducts, debouncedQuery]);
|
||||
const products = productsData?.products || [];
|
||||
|
||||
useManualRefresh(() => {
|
||||
refetch();
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import { Image } from 'expo-image';
|
|||
import { MyText, tw, useManualRefresh, MyFlatList, useMarkDataFetchers, theme, MyTouchableOpacity } from 'common-ui';
|
||||
import { MaterialIcons, Ionicons } from '@expo/vector-icons';
|
||||
import { trpc } from '@/src/trpc-client';
|
||||
import { useGetEssentialConsts } from '@/src/hooks/prominent-api-hooks';
|
||||
import { useGetEssentialConsts } from '@/src/api-hooks/essential-consts.api';
|
||||
import dayjs from 'dayjs';
|
||||
import { useRouter } from 'expo-router';
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,9 @@
|
|||
import { Stack } from 'expo-router'
|
||||
|
||||
function DeliverySlotsLayout() {
|
||||
return (
|
||||
<Stack screenOptions={{ headerShown: true, title: 'Delivery Slots' }} />
|
||||
)
|
||||
}
|
||||
|
||||
export default DeliverySlotsLayout
|
||||
230
apps/user-ui/app/(drawer)/(tabs)/me/delivery-slots/index.tsx
Normal file
230
apps/user-ui/app/(drawer)/(tabs)/me/delivery-slots/index.tsx
Normal file
|
|
@ -0,0 +1,230 @@
|
|||
import React, { useState } from 'react';
|
||||
import { View, ScrollView } from 'react-native';
|
||||
import { Image } from 'expo-image';
|
||||
import { useRouter } from 'expo-router';
|
||||
import { MyFlatList, MyText, tw, useMarkDataFetchers, BottomDialog, theme, MyTouchableOpacity } from 'common-ui';
|
||||
import { trpc } from '@/src/trpc-client';
|
||||
import MaterialIcons from '@expo/vector-icons/MaterialIcons';
|
||||
import dayjs from 'dayjs';
|
||||
|
||||
export default function DeliverySlots() {
|
||||
const router = useRouter();
|
||||
const { data, isLoading, error, refetch } = trpc.user.slots.getSlotsWithProducts.useQuery();
|
||||
const [selectedSlotForDialog, setSelectedSlotForDialog] = useState<any>(null);
|
||||
|
||||
useMarkDataFetchers(() => {
|
||||
refetch();
|
||||
});
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
<MyFlatList
|
||||
data={[]}
|
||||
renderItem={() => null}
|
||||
ListHeaderComponent={() => (
|
||||
<View style={tw`flex-1 justify-center items-center p-8`}>
|
||||
<MyText style={tw`text-gray-600`}>Loading delivery slots...</MyText>
|
||||
</View>
|
||||
)}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
if (error) {
|
||||
return (
|
||||
<MyFlatList
|
||||
data={[]}
|
||||
renderItem={() => null}
|
||||
ListHeaderComponent={() => (
|
||||
<View style={tw`flex-1 justify-center items-center p-8`}>
|
||||
<MyText style={tw`text-red-600`}>Error loading delivery slots</MyText>
|
||||
<MyTouchableOpacity
|
||||
onPress={() => refetch()}
|
||||
style={tw`mt-4 bg-blue-500 px-4 py-2 rounded-lg`}
|
||||
>
|
||||
<MyText style={tw`text-white font-semibold`}>Retry</MyText>
|
||||
</MyTouchableOpacity>
|
||||
</View>
|
||||
)}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
const slots = data?.slots || [];
|
||||
|
||||
if (slots.length === 0) {
|
||||
return (
|
||||
<MyFlatList
|
||||
data={[]}
|
||||
renderItem={() => null}
|
||||
ListHeaderComponent={() => (
|
||||
<View style={tw`flex-1 justify-center items-center p-8`}>
|
||||
<MaterialIcons name="schedule" size={64} color="#D1D5DB" />
|
||||
<MyText style={tw`text-gray-500 text-center mt-4 text-lg`}>
|
||||
No upcoming delivery slots available
|
||||
</MyText>
|
||||
<MyText style={tw`text-gray-400 text-center mt-2`}>
|
||||
Check back later for new delivery schedules
|
||||
</MyText>
|
||||
</View>
|
||||
)}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<MyFlatList
|
||||
data={slots}
|
||||
keyExtractor={(item) => item.id.toString()}
|
||||
// ListHeaderComponent={() => (
|
||||
// <View style={tw`p-4 pb-2`}>
|
||||
// <MyText style={tw`text-2xl font-bold text-gray-800`}>Delivery Slots</MyText>
|
||||
// <MyText style={tw`text-gray-600 mt-1`}>
|
||||
// Choose your preferred delivery time
|
||||
// </MyText>
|
||||
// </View>
|
||||
// )}
|
||||
renderItem={({ item: slot }) => (
|
||||
<View style={tw`mx-4 mb-4 bg-white rounded-xl shadow-md overflow-hidden`}>
|
||||
{/* Slot Header */}
|
||||
<View style={tw`bg-pink-50 p-4 border-b border-pink-100`}>
|
||||
<View style={tw`flex-row items-center justify-between`}>
|
||||
<View>
|
||||
<MyText style={tw`text-lg font-bold text-gray-800`}>
|
||||
{dayjs(slot.deliveryTime).format('ddd DD MMM, h:mm a')}
|
||||
</MyText>
|
||||
<MyText style={tw`text-sm text-gray-600 mt-1`}>
|
||||
Orders close by: {dayjs(slot.freezeTime).format('h:mm a')}
|
||||
</MyText>
|
||||
</View>
|
||||
<View style={tw`flex-row items-center`}>
|
||||
<View style={tw`bg-pink-500 px-3 py-1 rounded-full mr-3`}>
|
||||
<MyText style={tw`text-white text-sm font-semibold`}>
|
||||
{slot.products.length} items
|
||||
</MyText>
|
||||
</View>
|
||||
<MyTouchableOpacity
|
||||
onPress={() => router.push(`/(drawer)/(tabs)/home/cart?slot=${slot.id}`)}
|
||||
style={tw`bg-pink-500 p-2 rounded-full`}
|
||||
>
|
||||
<MaterialIcons name="flash-on" size={16} color="white" />
|
||||
</MyTouchableOpacity>
|
||||
</View>
|
||||
</View>
|
||||
</View>
|
||||
|
||||
{/* Products List */}
|
||||
<View style={tw`p-4`}>
|
||||
<MyText style={tw`text-base font-semibold text-gray-700 mb-3`}>
|
||||
Available Products
|
||||
</MyText>
|
||||
<View style={tw`space-y-2`}>
|
||||
{slot.products.slice(0, 2).map((product) => (
|
||||
<MyTouchableOpacity
|
||||
key={product.id}
|
||||
onPress={() => router.push(`/(drawer)/(tabs)/home/product-detail/${product.id}`)}
|
||||
style={tw`bg-gray-50 rounded-lg p-3 flex-row items-center`}
|
||||
>
|
||||
{product.images && product.images.length > 0 ? (
|
||||
<Image
|
||||
source={{ uri: product.images[0] }}
|
||||
style={tw`w-8 h-8 rounded mr-3`}
|
||||
resizeMode="cover"
|
||||
/>
|
||||
) : (
|
||||
<View style={tw`w-8 h-8 bg-gray-200 rounded mr-3 justify-center items-center`}>
|
||||
<MaterialIcons name="image" size={16} color="#9CA3AF" />
|
||||
</View>
|
||||
)}
|
||||
<View style={tw`flex-1`}>
|
||||
<MyText style={tw`text-sm font-medium text-gray-800`} numberOfLines={1}>
|
||||
{product.name}
|
||||
</MyText>
|
||||
<MyText style={tw`text-xs text-gray-600`}>
|
||||
₹{product.price} {product.unit && `per ${product.unit}`}
|
||||
</MyText>
|
||||
</View>
|
||||
{product.isOutOfStock && (
|
||||
<MyText style={tw`text-xs text-red-500 font-medium`}>Out of stock</MyText>
|
||||
)}
|
||||
</MyTouchableOpacity>
|
||||
))}
|
||||
|
||||
{slot.products.length > 2 && (
|
||||
<MyTouchableOpacity
|
||||
onPress={() => setSelectedSlotForDialog(slot)}
|
||||
style={tw`bg-pink-50 rounded-lg p-3 flex-row items-center justify-center border border-pink-200`}
|
||||
>
|
||||
<MyText style={tw`text-sm font-medium text-pink-700`}>
|
||||
+{slot.products.length - 2} more products
|
||||
</MyText>
|
||||
<MaterialIcons name="chevron-right" size={16} color={theme.colors.brand500} style={tw`ml-1`} />
|
||||
</MyTouchableOpacity>
|
||||
)}
|
||||
</View>
|
||||
</View>
|
||||
</View>
|
||||
)}
|
||||
ListFooterComponent={() => <View style={tw`h-4`} />}
|
||||
showsVerticalScrollIndicator={false}
|
||||
contentContainerStyle={tw`pt-2`}
|
||||
/>
|
||||
|
||||
{/* Products Dialog */}
|
||||
<BottomDialog
|
||||
open={!!selectedSlotForDialog}
|
||||
onClose={() => setSelectedSlotForDialog(null)}
|
||||
>
|
||||
<View style={tw`p-6`}>
|
||||
<MyText style={tw`text-xl font-bold text-gray-800 mb-4`}>
|
||||
All Products - {dayjs(selectedSlotForDialog?.deliveryTime).format('ddd DD MMM, h:mm a')}
|
||||
</MyText>
|
||||
|
||||
<ScrollView style={tw`max-h-96`} showsVerticalScrollIndicator={false}>
|
||||
<View style={tw`space-y-3`}>
|
||||
{selectedSlotForDialog?.products.map((product: any) => (
|
||||
<MyTouchableOpacity
|
||||
key={product.id}
|
||||
onPress={() => {
|
||||
setSelectedSlotForDialog(null);
|
||||
router.push(`/(drawer)/(tabs)/home/product-detail/${product.id}`);
|
||||
}}
|
||||
style={tw`bg-gray-50 rounded-lg p-4 flex-row items-center`}
|
||||
>
|
||||
{product.images && product.images.length > 0 ? (
|
||||
<Image
|
||||
source={{ uri: product.images[0] }}
|
||||
style={tw`w-12 h-12 rounded mr-4`}
|
||||
resizeMode="cover"
|
||||
/>
|
||||
) : (
|
||||
<View style={tw`w-12 h-12 bg-gray-200 rounded mr-4 justify-center items-center`}>
|
||||
<MaterialIcons name="image" size={20} color="#9CA3AF" />
|
||||
</View>
|
||||
)}
|
||||
<View style={tw`flex-1`}>
|
||||
<MyText style={tw`text-base font-medium text-gray-800`} numberOfLines={1}>
|
||||
{product.name}
|
||||
</MyText>
|
||||
<MyText style={tw`text-sm text-gray-600 mt-1`}>
|
||||
₹{product.price} {product.unit && `per ${product.unit}`}
|
||||
</MyText>
|
||||
{product.marketPrice && (
|
||||
<MyText style={tw`text-sm text-gray-500 line-through`}>
|
||||
₹{product.marketPrice}
|
||||
</MyText>
|
||||
)}
|
||||
</View>
|
||||
{product.isOutOfStock && (
|
||||
<MyText style={tw`text-xs text-red-500 font-medium`}>Out of stock</MyText>
|
||||
)}
|
||||
</MyTouchableOpacity>
|
||||
))}
|
||||
</View>
|
||||
</ScrollView>
|
||||
</View>
|
||||
</BottomDialog>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
|
@ -16,7 +16,7 @@ import {
|
|||
} from "common-ui";
|
||||
import MaterialIcons from "@expo/vector-icons/MaterialIcons";
|
||||
import { Ionicons } from "@expo/vector-icons";
|
||||
import { useStores } from "@/src/hooks/prominent-api-hooks";
|
||||
import { trpc } from "@/src/trpc-client";
|
||||
import { LinearGradient } from "expo-linear-gradient";
|
||||
import TabLayoutWrapper from "@/components/TabLayoutWrapper";
|
||||
import FloatingCartBar from "@/components/floating-cart-bar";
|
||||
|
|
@ -157,7 +157,7 @@ export default function Stores() {
|
|||
isLoading,
|
||||
error,
|
||||
refetch,
|
||||
} = useStores();
|
||||
} = trpc.user.stores.getStores.useQuery();
|
||||
|
||||
const stores = storesData?.stores || [];
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import React, { useMemo, useState } from "react";
|
||||
import React, { useState } from "react";
|
||||
import { View, Dimensions, ScrollView, TouchableOpacity } from "react-native";
|
||||
import { useRouter, useLocalSearchParams } from "expo-router";
|
||||
import { Image } from 'expo-image';
|
||||
|
|
@ -13,10 +13,10 @@ import {
|
|||
} from "common-ui";
|
||||
import MaterialIcons from "@expo/vector-icons/MaterialIcons";
|
||||
import FontAwesome5 from "@expo/vector-icons/FontAwesome5";
|
||||
import { trpc } from "@/src/trpc-client";
|
||||
import ProductCard from "@/components/ProductCard";
|
||||
import FloatingCartBar from "@/components/floating-cart-bar";
|
||||
import { useStoreHeaderStore } from "@/src/store/storeHeaderStore";
|
||||
import { useAllProducts, useStoreWithProducts } from "@/src/hooks/prominent-api-hooks";
|
||||
|
||||
const { width: screenWidth } = Dimensions.get("window");
|
||||
const itemWidth = (screenWidth - 48) / 2;
|
||||
|
|
@ -63,32 +63,24 @@ export default function StoreDetail() {
|
|||
const [selectedTagId, setSelectedTagId] = useState<number | null>(null);
|
||||
|
||||
const { data: storeData, isLoading, refetch, error } =
|
||||
useStoreWithProducts(storeIdNum);
|
||||
trpc.user.stores.getStoreWithProducts.useQuery(
|
||||
{ storeId: storeIdNum },
|
||||
{ enabled: !!storeIdNum }
|
||||
);
|
||||
|
||||
const { data: productsData, isLoading: isProductsLoading } = useAllProducts();
|
||||
|
||||
const productById = useMemo(() => {
|
||||
const map = new Map<number, any>();
|
||||
productsData?.products?.forEach((product) => {
|
||||
map.set(product.id, product);
|
||||
});
|
||||
return map;
|
||||
}, [productsData]);
|
||||
|
||||
const storeProducts = useMemo(() => {
|
||||
if (!storeData?.products) return [];
|
||||
return storeData.products
|
||||
.map((product) => productById.get(product.id))
|
||||
.filter(Boolean);
|
||||
}, [storeData, productById]);
|
||||
const { data: tagsData, isLoading: isLoadingTags } =
|
||||
trpc.user.tags.getTagsByStore.useQuery(
|
||||
{ storeId: storeIdNum },
|
||||
{ enabled: !!storeIdNum }
|
||||
);
|
||||
|
||||
// Filter products based on selected tag
|
||||
const filteredProducts = selectedTagId
|
||||
? storeProducts.filter(product => {
|
||||
const selectedTag = storeData?.tags.find(t => t.id === selectedTagId);
|
||||
? storeData?.products.filter(product => {
|
||||
const selectedTag = tagsData?.tags.find(t => t.id === selectedTagId);
|
||||
return selectedTag?.productIds?.includes(product.id) ?? false;
|
||||
})
|
||||
: storeProducts;
|
||||
}) || []
|
||||
: storeData?.products || [];
|
||||
|
||||
// Set the store header title
|
||||
const setStoreHeaderTitle = useStoreHeaderStore((state) => state.setTitle);
|
||||
|
|
@ -106,12 +98,10 @@ export default function StoreDetail() {
|
|||
|
||||
useDrawerTitle(storeData?.store?.name || "Store", [storeData?.store?.name]);
|
||||
|
||||
if (isLoading || isProductsLoading) {
|
||||
if (isLoading) {
|
||||
return (
|
||||
<View style={tw`flex-1 justify-center items-center bg-gray-50`}>
|
||||
<MyText style={tw`text-gray-500 font-medium`}>
|
||||
{isLoading ? 'Loading store...' : 'Loading products...'}
|
||||
</MyText>
|
||||
<MyText style={tw`text-gray-500 font-medium`}>Loading store...</MyText>
|
||||
</View>
|
||||
);
|
||||
}
|
||||
|
|
@ -194,13 +184,13 @@ export default function StoreDetail() {
|
|||
)}
|
||||
</View>
|
||||
{/* Tags Section */}
|
||||
{storeData?.tags && storeData.tags.length > 0 && (
|
||||
{tagsData && tagsData.tags.length > 0 && (
|
||||
<ScrollView
|
||||
horizontal
|
||||
showsHorizontalScrollIndicator={false}
|
||||
contentContainerStyle={tw`gap-2 mt-6`}
|
||||
>
|
||||
{storeData.tags.map((tag) => (
|
||||
{tagsData.tags.map((tag) => (
|
||||
<Chip
|
||||
key={tag.id}
|
||||
tag={tag}
|
||||
|
|
@ -216,7 +206,7 @@ export default function StoreDetail() {
|
|||
<MaterialIcons name="grid-view" size={20} color="#374151" />
|
||||
<MyText style={tw`text-lg font-bold text-gray-900 ml-2`}>
|
||||
{selectedTagId
|
||||
? `${storeData?.tags.find(t => t.id === selectedTagId)?.tagName} items`
|
||||
? `${tagsData?.tags.find(t => t.id === selectedTagId)?.tagName} items`
|
||||
: `${filteredProducts.length} products`}
|
||||
</MyText>
|
||||
</View>
|
||||
|
|
|
|||
|
|
@ -22,7 +22,6 @@ import LocationTestWrapper from "@/components/LocationTestWrapper";
|
|||
import HealthTestWrapper from "@/components/HealthTestWrapper";
|
||||
import FirstUserWrapper from "@/components/FirstUserWrapper";
|
||||
import UpdateChecker from "@/components/UpdateChecker";
|
||||
import CentralStoreInitializer from "@/src/components/CentralStoreInitializer";
|
||||
import { RefreshProvider } from "../../../packages/ui/src/lib/refresh-context";
|
||||
import WebViewWrapper from "@/components/WebViewWrapper";
|
||||
import BackHandlerWrapper from "@/components/BackHandler";
|
||||
|
|
@ -69,12 +68,10 @@ export default function RootLayout() {
|
|||
<PaperProvider>
|
||||
<LocationTestWrapper>
|
||||
<RefreshProvider queryClient={queryClient}>
|
||||
<CentralStoreInitializer>
|
||||
<BackHandlerWrapper />
|
||||
<Stack screenOptions={{ headerShown: false }} />
|
||||
<AddToCartDialog />
|
||||
</CentralStoreInitializer>
|
||||
</RefreshProvider>
|
||||
</RefreshProvider>
|
||||
</LocationTestWrapper>
|
||||
</PaperProvider>
|
||||
</NotificationProvider>
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ import React, { useState, useRef, useEffect } from 'react';
|
|||
import { View, Dimensions, Image, ScrollView, NativeSyntheticEvent, NativeScrollEvent } from 'react-native';
|
||||
import { MyTouchableOpacity, MyText, tw } from 'common-ui';
|
||||
import { useRouter } from 'expo-router';
|
||||
import { useBanners } from '@/src/hooks/prominent-api-hooks';
|
||||
import { trpc } from '@/src/trpc-client';
|
||||
import MaterialIcons from '@expo/vector-icons/MaterialIcons';
|
||||
|
||||
const { width: screenWidth } = Dimensions.get('window');
|
||||
|
|
@ -25,7 +25,7 @@ export default function BannerCarousel() {
|
|||
const [isAutoPlaying, setIsAutoPlaying] = useState(true);
|
||||
|
||||
// Fetch banners data
|
||||
const { data: bannersData, isLoading, error } = useBanners();
|
||||
const { data: bannersData, isLoading, error } = trpc.user.banner.getBanners.useQuery();
|
||||
|
||||
const banners = bannersData?.banners || [];
|
||||
|
||||
|
|
@ -123,7 +123,7 @@ export default function BannerCarousel() {
|
|||
{/* Pagination Dots */}
|
||||
{banners.length > 1 && (
|
||||
<View style={tw`flex-row justify-center mt-3`}>
|
||||
{banners.map((_: Banner, index: number) => (
|
||||
{banners.map((_, index: number) => (
|
||||
<MyTouchableOpacity
|
||||
key={index}
|
||||
onPress={() => goToSlide(index)}
|
||||
|
|
@ -140,4 +140,4 @@ export default function BannerCarousel() {
|
|||
)}
|
||||
</View>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -2,7 +2,7 @@ import React, { useState, useEffect } from 'react';
|
|||
import { View, ActivityIndicator, Platform } from 'react-native';
|
||||
import { tw, theme, MyText, MyTouchableOpacity , BottomDialog } from 'common-ui';
|
||||
import { trpc, trpcClient } from '@/src/trpc-client';
|
||||
import { useGetEssentialConsts } from '@/src/hooks/prominent-api-hooks';
|
||||
import { useGetEssentialConsts } from '@/src/api-hooks/essential-consts.api';
|
||||
import Constants from 'expo-constants';
|
||||
import * as Linking from 'expo-linking';
|
||||
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ import dayjs from 'dayjs';
|
|||
import { trpc } from '@/src/trpc-client';
|
||||
import { Image } from 'expo-image';
|
||||
import { orderStatusManipulator } from '@/src/lib/string-manipulators';
|
||||
import { useGetEssentialConsts } from '@/src/hooks/prominent-api-hooks';
|
||||
import { useGetEssentialConsts } from '@/src/api-hooks/essential-consts.api';
|
||||
|
||||
interface OrderItem {
|
||||
productName: string;
|
||||
|
|
|
|||
|
|
@ -6,8 +6,6 @@ import MaterialIcons from '@expo/vector-icons/MaterialIcons';
|
|||
// import RazorpayCheckout from 'react-native-razorpay';
|
||||
|
||||
import { trpc } from '@/src/trpc-client';
|
||||
import { useCentralProductStore } from '@/src/store/centralProductStore';
|
||||
import { useCentralSlotStore } from '@/src/store/centralSlotStore';
|
||||
import { clearLocalCart } from '@/hooks/cart-query-hooks';
|
||||
import { useQueryClient } from '@tanstack/react-query';
|
||||
import { FontAwesome5, FontAwesome6 } from '@expo/vector-icons';
|
||||
|
|
@ -56,19 +54,17 @@ const PaymentAndOrderComponent: React.FC<PaymentAndOrderProps> = ({
|
|||
queryClient.invalidateQueries({ queryKey: [`local-cart-${cartType}`] });
|
||||
};
|
||||
|
||||
const products = useCentralProductStore((state) => state.products);
|
||||
const productsById = useCentralProductStore((state) => state.productsById);
|
||||
const productSlotsMap = useCentralSlotStore((state) => state.productSlotsMap);
|
||||
const { data: productsData } = trpc.common.product.getAllProductsSummary.useQuery({});
|
||||
|
||||
// Memoized flash-eligible product IDs
|
||||
const flashEligibleProductIds = useMemo(() => {
|
||||
if (!products.length) return new Set<number>();
|
||||
if (!productsData?.products) return new Set<number>();
|
||||
return new Set(
|
||||
products
|
||||
.filter((product) => productSlotsMap[product.id]?.isFlashAvailable)
|
||||
.map((product) => product.id)
|
||||
productsData.products
|
||||
.filter((product: any) => product.isFlashAvailable)
|
||||
.map((product: any) => product.id)
|
||||
);
|
||||
}, [products, productSlotsMap]);
|
||||
}, [productsData]);
|
||||
|
||||
const placeOrderMutation = trpc.user.order.placeOrder.useMutation({
|
||||
onSuccess: (data) => {
|
||||
|
|
@ -130,7 +126,7 @@ const PaymentAndOrderComponent: React.FC<PaymentAndOrderProps> = ({
|
|||
|
||||
const availableItems = cartItems
|
||||
.filter(item => {
|
||||
if (productSlotsMap[item.productId]?.isOutOfStock) return false;
|
||||
if (item.product?.isOutOfStock) return false;
|
||||
// For flash delivery, check if product supports flash delivery
|
||||
if (isFlashDelivery) {
|
||||
return flashEligibleProductIds.has(item.productId);
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import React from 'react';
|
||||
import React, { useMemo } from 'react';
|
||||
import { View, Alert, TouchableOpacity, Text } from 'react-native';
|
||||
import { Image } from 'expo-image';
|
||||
import { tw, theme, MyText, MyTouchableOpacity, Quantifier, MiniQuantifier } from 'common-ui';
|
||||
|
|
@ -14,7 +14,7 @@ import {
|
|||
} from '@/hooks/cart-query-hooks';
|
||||
import { useProductSlotIdentifier } from '@/hooks/useProductSlotIdentifier';
|
||||
import { useCartStore } from '@/src/store/cartStore';
|
||||
import { useCentralSlotStore } from '@/src/store/centralSlotStore';
|
||||
import { trpc } from '@/src/trpc-client';
|
||||
|
||||
|
||||
interface ProductCardProps {
|
||||
|
|
@ -68,18 +68,17 @@ const ProductCard: React.FC<ProductCardProps> = ({
|
|||
const cartItem = cartData?.items?.find((cartItem: any) => cartItem.productId === item.id);
|
||||
const quantity = cartItem?.quantity || 0;
|
||||
|
||||
// Get slots data from central store
|
||||
const slots = useCentralSlotStore((state) => state.slots);
|
||||
const productSlotsMap = useCentralSlotStore((state) => state.productSlotsMap);
|
||||
// Query all slots with products
|
||||
const { data: slotsData } = trpc.user.slots.getSlotsWithProducts.useQuery();
|
||||
|
||||
// Create slot lookup map
|
||||
const slotMap = React.useMemo(() => {
|
||||
const slotMap = useMemo(() => {
|
||||
const map: Record<number, any> = {};
|
||||
slots?.forEach((slot: any) => {
|
||||
slotsData?.slots?.forEach((slot: any) => {
|
||||
map[slot.id] = slot;
|
||||
});
|
||||
return map;
|
||||
}, [slots]);
|
||||
}, [slotsData]);
|
||||
|
||||
// Get cart item's slot delivery time if item is in cart
|
||||
const cartSlot = cartItem?.slotId ? slotMap[cartItem.slotId] : null;
|
||||
|
|
@ -87,11 +86,7 @@ const ProductCard: React.FC<ProductCardProps> = ({
|
|||
|
||||
// Precompute the next slot and determine display out of stock status
|
||||
const slotId = getQuickestSlot(item.id);
|
||||
|
||||
// Use isOutOfStock from productSlotsMap (all products now included)
|
||||
const productSlotInfo = productSlotsMap[item.id];
|
||||
const isOutOfStockFromSlots = productSlotInfo?.isOutOfStock;
|
||||
const displayIsOutOfStock = isOutOfStockFromSlots || !slotId;
|
||||
const displayIsOutOfStock = item.isOutOfStock || !slotId;
|
||||
|
||||
// if(item.name.startsWith('Mutton Curry Cut')) {
|
||||
// console.log({slotId, displayIsOutOfStock})
|
||||
|
|
@ -123,7 +118,6 @@ const ProductCard: React.FC<ProductCardProps> = ({
|
|||
}
|
||||
};
|
||||
|
||||
// console.log('rendering the product cart for id', item.id)
|
||||
return (
|
||||
<ContainerComp>
|
||||
<MyTouchableOpacity
|
||||
|
|
@ -138,7 +132,7 @@ const ProductCard: React.FC<ProductCardProps> = ({
|
|||
<Image
|
||||
source={{ uri: item.images?.[0] }}
|
||||
style={{ width: "100%", height: itemWidth, resizeMode: "cover" }}
|
||||
/>
|
||||
/>
|
||||
{displayIsOutOfStock && (
|
||||
<View style={tw`absolute inset-0 bg-black/40 items-center justify-center`}>
|
||||
<View style={tw`bg-red-500 px-3 py-1 rounded-full`}>
|
||||
|
|
@ -219,4 +213,4 @@ const ProductCard: React.FC<ProductCardProps> = ({
|
|||
);
|
||||
};
|
||||
|
||||
export default ProductCard;
|
||||
export default ProductCard;
|
||||
|
|
@ -12,11 +12,9 @@ import { trpc, trpcClient } from '@/src/trpc-client';
|
|||
import { useAddToCart, useGetCart, useUpdateCartItem, useRemoveFromCart } from '@/hooks/cart-query-hooks';
|
||||
import { useProductSlotIdentifier } from '@/hooks/useProductSlotIdentifier';
|
||||
import { useFlashNavigationStore } from '@/components/stores/flashNavigationStore';
|
||||
import { useSlots } from '@/src/hooks/prominent-api-hooks';
|
||||
import FloatingCartBar from './floating-cart-bar';
|
||||
import { useStoreHeaderStore } from '@/src/store/storeHeaderStore';
|
||||
import { useCartStore } from '@/src/store/cartStore';
|
||||
import { useCentralSlotStore } from '@/src/store/centralSlotStore';
|
||||
|
||||
const { width: screenWidth } = Dimensions.get("window");
|
||||
const carouselWidth = screenWidth;
|
||||
|
|
@ -59,28 +57,15 @@ const ProductDetail: React.FC<ProductDetailProps> = ({ productId, isFlashDeliver
|
|||
const { getQuickestSlot } = useProductSlotIdentifier();
|
||||
const { setShouldNavigateToCart } = useFlashNavigationStore();
|
||||
const { setAddedToCartProduct } = useCartStore();
|
||||
const { data: slotsData } = useSlots();
|
||||
const productSlotsMap = useCentralSlotStore((state) => state.productSlotsMap);
|
||||
|
||||
const productAvailability = useMemo(() => {
|
||||
if (!productDetail) return null;
|
||||
return productSlotsMap[productDetail.id];
|
||||
}, [productDetail, productSlotsMap]);
|
||||
|
||||
const sortedDeliverySlots = useMemo(() => {
|
||||
if (!slotsData?.slots || !productDetail) return []
|
||||
|
||||
// Filter slots that contain this product
|
||||
const productSlots = slotsData.slots.filter((slot: any) =>
|
||||
slot.products?.some((p: any) => p.id === productDetail.id)
|
||||
)
|
||||
|
||||
return productSlots.sort((a: any, b: any) => {
|
||||
if (!productDetail?.deliverySlots) return []
|
||||
return [...productDetail.deliverySlots].sort((a, b) => {
|
||||
const deliveryDiff = new Date(a.deliveryTime).getTime() - new Date(b.deliveryTime).getTime()
|
||||
if (deliveryDiff !== 0) return deliveryDiff
|
||||
return new Date(a.freezeTime).getTime() - new Date(b.freezeTime).getTime()
|
||||
})
|
||||
}, [slotsData, productDetail])
|
||||
}, [productDetail?.deliverySlots])
|
||||
|
||||
// Find current quantity from cart data
|
||||
const cartItem = productDetail ? cartData?.data?.items?.find((item: any) => item.productId === productDetail.id) : null;
|
||||
|
|
@ -109,7 +94,7 @@ const ProductDetail: React.FC<ProductDetailProps> = ({ productId, isFlashDeliver
|
|||
|
||||
const handleAddToCart = (productId: number) => {
|
||||
if (isFlashDelivery) {
|
||||
if (!productAvailability?.isFlashAvailable) {
|
||||
if (!productDetail?.isFlashAvailable) {
|
||||
Alert.alert("Error", "This product is not available for flash delivery");
|
||||
return;
|
||||
}
|
||||
|
|
@ -128,7 +113,7 @@ const ProductDetail: React.FC<ProductDetailProps> = ({ productId, isFlashDeliver
|
|||
|
||||
const handleBuyNow = (productId: number) => {
|
||||
if (isFlashDelivery) {
|
||||
if (!productAvailability?.isFlashAvailable) {
|
||||
if (!productDetail?.isFlashAvailable) {
|
||||
Alert.alert("Error", "This product is not available for flash delivery");
|
||||
return;
|
||||
}
|
||||
|
|
@ -256,13 +241,13 @@ const ProductDetail: React.FC<ProductDetailProps> = ({ productId, isFlashDeliver
|
|||
<View style={tw`flex-row justify-between items-start mb-2`}>
|
||||
<MyText style={tw`text-2xl font-bold text-gray-900 flex-1 mr-2`}>{productDetail.name}</MyText>
|
||||
<View style={tw`flex-row gap-2`}>
|
||||
{productAvailability?.isFlashAvailable && (
|
||||
{productDetail.isFlashAvailable && (
|
||||
<View style={tw`bg-pink-100 px-3 py-1 rounded-full flex-row items-center`}>
|
||||
<MaterialIcons name="bolt" size={12} color="#EC4899" style={tw`mr-1`} />
|
||||
<MyText style={tw`text-pink-700 text-xs font-bold`}>1 Hr Delivery</MyText>
|
||||
</View>
|
||||
)}
|
||||
{productAvailability?.isOutOfStock && (
|
||||
{productDetail.isOutOfStock && (
|
||||
<View style={tw`bg-red-100 px-3 py-1 rounded-full`}>
|
||||
<MyText style={tw`text-red-700 text-xs font-bold`}>Out of Stock</MyText>
|
||||
</View>
|
||||
|
|
@ -292,7 +277,7 @@ const ProductDetail: React.FC<ProductDetailProps> = ({ productId, isFlashDeliver
|
|||
</View>
|
||||
|
||||
{/* Flash price on separate line - smaller and less prominent */}
|
||||
{productAvailability?.isFlashAvailable && productDetail.flashPrice && productDetail.flashPrice !== productDetail.price && (
|
||||
{productDetail.isFlashAvailable && productDetail.flashPrice && productDetail.flashPrice !== productDetail.price && (
|
||||
<View style={tw`mt-1`}>
|
||||
<MyText style={tw`text-pink-600 text-lg font-bold`}>
|
||||
1 Hr Delivery: ₹{productDetail.flashPrice} / {formatQuantity(productDetail.productQuantity || 1, productDetail.unitNotation).display}
|
||||
|
|
@ -319,11 +304,11 @@ const ProductDetail: React.FC<ProductDetailProps> = ({ productId, isFlashDeliver
|
|||
// Show "Add to Cart" button when not in cart
|
||||
<MyTouchableOpacity
|
||||
style={[tw`flex-1 py-3.5 rounded-xl items-center border`, {
|
||||
borderColor: (productAvailability?.isOutOfStock || (isFlashDelivery && !productAvailability?.isFlashAvailable)) ? '#9ca3af' : theme.colors.brand500,
|
||||
borderColor: (productDetail.isOutOfStock || (isFlashDelivery && !productDetail.isFlashAvailable)) ? '#9ca3af' : theme.colors.brand500,
|
||||
backgroundColor: 'white'
|
||||
}]}
|
||||
onPress={() => {
|
||||
if (productAvailability?.isOutOfStock || (isFlashDelivery && !productAvailability?.isFlashAvailable)) {
|
||||
if (productDetail.isOutOfStock || (isFlashDelivery && !productDetail.isFlashAvailable)) {
|
||||
return;
|
||||
}
|
||||
if (isFlashDelivery) {
|
||||
|
|
@ -334,10 +319,10 @@ const ProductDetail: React.FC<ProductDetailProps> = ({ productId, isFlashDeliver
|
|||
setAddedToCartProduct({ productId: productDetail.id, product: productDetail });
|
||||
}
|
||||
}}
|
||||
disabled={productAvailability?.isOutOfStock || (isFlashDelivery && !productAvailability?.isFlashAvailable)}
|
||||
disabled={productDetail.isOutOfStock || (isFlashDelivery && !productDetail.isFlashAvailable)}
|
||||
>
|
||||
<MyText style={[tw`font-bold text-base`, { color: (productAvailability?.isOutOfStock || (isFlashDelivery && !productAvailability?.isFlashAvailable)) ? '#9ca3af' : theme.colors.brand500 }]}>
|
||||
{(productAvailability?.isOutOfStock || (isFlashDelivery && !productAvailability?.isFlashAvailable)) ? 'Unavailable' : 'Add to Cart'}
|
||||
<MyText style={[tw`font-bold text-base`, { color: (productDetail.isOutOfStock || (isFlashDelivery && !productDetail.isFlashAvailable)) ? '#9ca3af' : theme.colors.brand500 }]}>
|
||||
{(productDetail.isOutOfStock || (isFlashDelivery && !productDetail.isFlashAvailable)) ? 'Unavailable' : 'Add to Cart'}
|
||||
</MyText>
|
||||
</MyTouchableOpacity>
|
||||
)}
|
||||
|
|
@ -345,26 +330,26 @@ const ProductDetail: React.FC<ProductDetailProps> = ({ productId, isFlashDeliver
|
|||
{isFlashDelivery ? (
|
||||
<MyTouchableOpacity
|
||||
style={[tw`flex-1 py-3.5 rounded-xl items-center shadow-md`, {
|
||||
backgroundColor: (productAvailability?.isOutOfStock || !productAvailability?.isFlashAvailable) ? '#9ca3af' : '#FDF2F8'
|
||||
backgroundColor: (productDetail.isOutOfStock || !productDetail.isFlashAvailable) ? '#9ca3af' : '#FDF2F8'
|
||||
}]}
|
||||
onPress={() => !(productAvailability?.isOutOfStock || !productAvailability?.isFlashAvailable) && handleBuyNow(productDetail.id)}
|
||||
disabled={productAvailability?.isOutOfStock || !productAvailability?.isFlashAvailable}
|
||||
onPress={() => !(productDetail.isOutOfStock || !productDetail.isFlashAvailable) && handleBuyNow(productDetail.id)}
|
||||
disabled={productDetail.isOutOfStock || !productDetail.isFlashAvailable}
|
||||
>
|
||||
<MyText style={tw`text-base font-bold ${productAvailability?.isOutOfStock || !productAvailability?.isFlashAvailable ? 'text-gray-400' : 'text-pink-600'}`}>
|
||||
{productAvailability?.isOutOfStock ? 'Out of Stock' :
|
||||
(!productAvailability?.isFlashAvailable ? 'Not Flash Eligible' : 'Get in 1 Hour')}
|
||||
<MyText style={tw`text-base font-bold ${productDetail.isOutOfStock || !productDetail.isFlashAvailable ? 'text-gray-400' : 'text-pink-600'}`}>
|
||||
{productDetail.isOutOfStock ? 'Out of Stock' :
|
||||
(!productDetail.isFlashAvailable ? 'Not Flash Eligible' : 'Get in 1 Hour')}
|
||||
</MyText>
|
||||
</MyTouchableOpacity>
|
||||
) : productAvailability?.isFlashAvailable ? (
|
||||
) : productDetail.isFlashAvailable ? (
|
||||
<MyTouchableOpacity
|
||||
style={[tw`flex-1 py-3.5 rounded-xl items-center shadow-md`, {
|
||||
backgroundColor: sortedDeliverySlots.length === 0 ? '#9ca3af' : '#FDF2F8'
|
||||
backgroundColor: productDetail.deliverySlots.length === 0 ? '#9ca3af' : '#FDF2F8'
|
||||
}]}
|
||||
onPress={() => sortedDeliverySlots.length > 0 && handleBuyNow(productDetail.id)}
|
||||
disabled={sortedDeliverySlots.length === 0}
|
||||
onPress={() => productDetail.deliverySlots.length > 0 && handleBuyNow(productDetail.id)}
|
||||
disabled={productDetail.deliverySlots.length === 0}
|
||||
>
|
||||
<MyText style={tw`text-base font-bold ${sortedDeliverySlots.length === 0 ? 'text-gray-400' : 'text-pink-600'}`}>
|
||||
{sortedDeliverySlots.length === 0 ? 'No Slots' : 'Get in 1 Hour'}
|
||||
<MyText style={tw`text-base font-bold ${productDetail.deliverySlots.length === 0 ? 'text-gray-400' : 'text-pink-600'}`}>
|
||||
{productDetail.deliverySlots.length === 0 ? 'No Slots' : 'Get in 1 Hour'}
|
||||
</MyText>
|
||||
</MyTouchableOpacity>
|
||||
) : (
|
||||
|
|
@ -393,7 +378,7 @@ const ProductDetail: React.FC<ProductDetailProps> = ({ productId, isFlashDeliver
|
|||
key={index}
|
||||
style={tw`flex-row items-start mb-4 bg-gray-50 p-3 rounded-xl border border-gray-100`}
|
||||
onPress={() => handleSlotAddToCart(productDetail.id, slot.id)}
|
||||
disabled={productAvailability?.isOutOfStock}
|
||||
disabled={productDetail.isOutOfStock}
|
||||
activeOpacity={0.7}
|
||||
>
|
||||
<MaterialIcons name="local-shipping" size={20} color="#3B82F6" style={tw`mt-0.5`} />
|
||||
|
|
@ -605,7 +590,7 @@ const ProductDetail: React.FC<ProductDetailProps> = ({ productId, isFlashDeliver
|
|||
key={index}
|
||||
style={tw`flex-row items-start mb-4 bg-gray-50 p-4 rounded-xl border border-gray-100`}
|
||||
onPress={() => handleSlotAddToCart(productDetail.id, slot.id)}
|
||||
disabled={productAvailability?.isOutOfStock}
|
||||
disabled={productDetail.isOutOfStock}
|
||||
activeOpacity={0.7}
|
||||
>
|
||||
<MaterialIcons name="local-shipping" size={20} color="#3B82F6" style={tw`mt-0.5`} />
|
||||
|
|
@ -812,4 +797,4 @@ const ReviewForm = ({ productId, onReviewSubmitted }: ReviewFormProps) => {
|
|||
);
|
||||
};
|
||||
|
||||
export default ProductDetail;
|
||||
export default ProductDetail;
|
||||
|
|
@ -6,7 +6,6 @@ import { BottomDialog, MyTouchableOpacity, MyText, tw, theme } from 'common-ui';
|
|||
import { useAuth } from '@/src/contexts/AuthContext';
|
||||
import { trpc } from '@/src/trpc-client';
|
||||
import { useAddressStore } from '@/src/store/addressStore';
|
||||
import { useSlots } from '@/src/hooks/prominent-api-hooks';
|
||||
import dayjs from 'dayjs';
|
||||
|
||||
interface QuickDeliveryAddressSelectorProps {
|
||||
|
|
@ -32,13 +31,13 @@ const QuickDeliveryAddressSelector: React.FC<QuickDeliveryAddressSelectorProps>
|
|||
const { data: addressesData } = trpc.user.address.getUserAddresses.useQuery(undefined, {
|
||||
enabled: isAuthenticated,
|
||||
});
|
||||
const { data: slotsData } = useSlots();
|
||||
const { data: slotsData } = trpc.user.slots.getSlotsWithProducts.useQuery();
|
||||
|
||||
const defaultAddress = defaultAddressData?.data;
|
||||
const addresses = addressesData?.data || [];
|
||||
|
||||
// Format time range helper
|
||||
const formatTimeRange = (deliveryTime: string | Date) => {
|
||||
const formatTimeRange = (deliveryTime: string) => {
|
||||
const time = dayjs(deliveryTime);
|
||||
const endTime = time.add(1, 'hour');
|
||||
const startPeriod = time.format('A');
|
||||
|
|
@ -277,4 +276,4 @@ const QuickDeliveryAddressSelector: React.FC<QuickDeliveryAddressSelectorProps>
|
|||
);
|
||||
};
|
||||
|
||||
export default QuickDeliveryAddressSelector;
|
||||
export default QuickDeliveryAddressSelector;
|
||||
|
|
@ -7,11 +7,7 @@ import { useRouter, usePathname } from 'expo-router';
|
|||
import MaterialIcons from '@expo/vector-icons/MaterialIcons';
|
||||
import { tw, theme, MyText, MyTouchableOpacity, MyFlatList, AppContainer, MiniQuantifier } from 'common-ui';
|
||||
import { trpc } from '@/src/trpc-client';
|
||||
import { useAllProducts, useStores, useSlots } from '@/src/hooks/prominent-api-hooks';
|
||||
import { AllProductsApiType } from '@backend/trpc/router';
|
||||
import { useQuickDeliveryStore } from '@/src/store/quickDeliveryStore';
|
||||
import { useCentralSlotStore } from '@/src/store/centralSlotStore';
|
||||
import { useCentralProductStore } from '@/src/store/centralProductStore';
|
||||
import { useAddToCart, useGetCart, useUpdateCartItem, useRemoveFromCart } from '@/hooks/cart-query-hooks';
|
||||
import { useHideTabNav } from '@/src/hooks/useHideTabNav';
|
||||
import CartIcon from '@/components/icons/CartIcon';
|
||||
|
|
@ -36,7 +32,7 @@ interface SlotLayoutProps {
|
|||
function CustomDrawerContent(baseUrl: string, drawerProps: DrawerContentComponentProps, slotIdParent?: number, storeIdParent?: number) {
|
||||
const router = useRouter();
|
||||
const pathname = usePathname();
|
||||
const { data: storesData } = useStores();
|
||||
const { data: storesData } = trpc.user.stores.getStores.useQuery();
|
||||
const setStoreId = useSlotStore(state => state.setStoreId);
|
||||
|
||||
const { slotId, storeId } = useSlotStore();
|
||||
|
|
@ -183,10 +179,17 @@ export function SlotLayout({ slotId, storeId, baseUrl, isForFlashDelivery }: Slo
|
|||
router.replace(`${baseUrl}?slotId=${newSlotId}` as any);
|
||||
};
|
||||
|
||||
const slotQuery = slotId
|
||||
? trpc.user.slots.getSlotById.useQuery({ slotId: Number(slotId) })
|
||||
: trpc.user.slots.nextMajorDelivery.useQuery();
|
||||
const deliveryTime = dayjs(slotQuery.data?.deliveryTime).format('DD MMM hh:mm A');
|
||||
|
||||
|
||||
return (
|
||||
<>
|
||||
<View style={tw` w-full flex-row bg-white px-4 py-2 mb-1`}>
|
||||
<QuickDeliveryAddressSelector
|
||||
deliveryTime={deliveryTime}
|
||||
slotId={Number(slotId)}
|
||||
onSlotChange={handleSlotChange}
|
||||
isForFlashDelivery={isForFlashDelivery}
|
||||
|
|
@ -240,7 +243,6 @@ const CompactProductCard = ({
|
|||
|
||||
// Cart management for miniView
|
||||
const { data: cartData } = useGetCart({}, cartType);
|
||||
const productSlotsMap = useCentralSlotStore((state) => state.productSlotsMap);
|
||||
const updateCartItem = useUpdateCartItem({
|
||||
showSuccessAlert: false,
|
||||
showErrorAlert: false,
|
||||
|
|
@ -254,7 +256,6 @@ const CompactProductCard = ({
|
|||
|
||||
const cartItem = cartData?.items?.find((cartItem: any) => cartItem.productId === item.id);
|
||||
const quantity = cartItem?.quantity || 0;
|
||||
const isOutOfStock = productSlotsMap[item.id]?.isOutOfStock;
|
||||
|
||||
const handleQuantityChange = (newQuantity: number) => {
|
||||
if (newQuantity === 0 && cartItem) {
|
||||
|
|
@ -280,7 +281,7 @@ const CompactProductCard = ({
|
|||
source={{ uri: item.images?.[0] }}
|
||||
style={{ width: "100%", height: itemWidth, resizeMode: "cover" }}
|
||||
/>
|
||||
{isOutOfStock && (
|
||||
{item.isOutOfStock && (
|
||||
<View style={tw`absolute inset-0 bg-black/30 items-center justify-center`}>
|
||||
<MyText style={tw`text-white text-xs font-bold`}>Out of Stock</MyText>
|
||||
</View>
|
||||
|
|
@ -339,20 +340,22 @@ export function SlotProducts({ slotId:slotIdParent, storeId:storeIdParent, baseU
|
|||
const slotId = slotIdParent;
|
||||
const storeId = storeIdParent;
|
||||
const storeIdNum = storeId;
|
||||
// const { storeId, slotId: slotIdRaw } = useLocalSearchParams();
|
||||
// const slotId = Number(slotIdRaw);
|
||||
|
||||
const { data: slotsData, isLoading: slotsLoading, error: slotsError } = useSlots();
|
||||
const { productsById } = useCentralProductStore();
|
||||
const productSlotsMap = useCentralSlotStore((state) => state.productSlotsMap);
|
||||
|
||||
// Find the specific slot from cached data
|
||||
const slot = slotsData?.slots?.find(s => s.id === slotId);
|
||||
// const storeIdNum = storeId ? Number(storeId) : undefined;
|
||||
|
||||
const slotQuery = trpc.user.slots.getSlotById.useQuery({ slotId: slotId! }, { enabled: !!slotId });
|
||||
|
||||
const productsQuery = trpc.common.product.getAllProductsSummary.useQuery({});
|
||||
|
||||
const { addToCart = () => { } } = useAddToCart({ showSuccessAlert: false, showErrorAlert: false, refetchCart: true }, "regular") || {};
|
||||
|
||||
const handleAddToCart = (productId: number) => {
|
||||
setIsLoadingDialogOpen(true);
|
||||
const item = filteredProducts.find((p) => p.id === productId);
|
||||
const deliveryTime = slot?.deliveryTime ? dayjs(slot.deliveryTime).format('ddd, DD MMM • h:mm A') : '';
|
||||
const item = filteredProducts.find((p: any) => p.id === productId);
|
||||
const deliveryTime = slotQuery.data?.deliveryTime ? dayjs(slotQuery.data.deliveryTime).format('ddd, DD MMM • h:mm A') : '';
|
||||
addToCart(productId, 1, slotId || 0, () => {
|
||||
setIsLoadingDialogOpen(false);
|
||||
if (item) {
|
||||
|
|
@ -361,7 +364,7 @@ export function SlotProducts({ slotId:slotIdParent, storeId:storeIdParent, baseU
|
|||
});
|
||||
};
|
||||
|
||||
if (slotsLoading) {
|
||||
if (slotQuery.isLoading || (storeIdNum && productsQuery?.isLoading)) {
|
||||
return (
|
||||
<AppContainer>
|
||||
<View style={tw`flex-1 justify-center items-center bg-gray-50`}>
|
||||
|
|
@ -371,7 +374,7 @@ export function SlotProducts({ slotId:slotIdParent, storeId:storeIdParent, baseU
|
|||
);
|
||||
}
|
||||
|
||||
if (slotsError) {
|
||||
if (slotQuery.error || (storeIdNum && productsQuery?.error)) {
|
||||
return (
|
||||
<AppContainer>
|
||||
<View style={tw`flex-1 justify-center items-center bg-gray-50`}>
|
||||
|
|
@ -383,7 +386,7 @@ export function SlotProducts({ slotId:slotIdParent, storeId:storeIdParent, baseU
|
|||
);
|
||||
}
|
||||
|
||||
if (!slot) {
|
||||
if (!slotQuery.data) {
|
||||
return (
|
||||
<AppContainer>
|
||||
<View style={tw`flex-1 justify-center items-center`}>
|
||||
|
|
@ -394,16 +397,14 @@ export function SlotProducts({ slotId:slotIdParent, storeId:storeIdParent, baseU
|
|||
);
|
||||
}
|
||||
|
||||
// Get product details from central store using slot product IDs
|
||||
// Filter: 1) Must exist in productsById, 2) Must not be out of stock (from slots data)
|
||||
const slotProducts = slot.products
|
||||
?.map(p => productsById[p.id])
|
||||
?.filter((product): product is NonNullable<typeof product> => product !== null && product !== undefined)
|
||||
?.filter(product => !productSlotsMap[product.id]?.isOutOfStock) || [];
|
||||
// Create a Set of product IDs from slot data for O(1) lookup
|
||||
const slotProductIds = new Set(slotQuery.data.products?.map((p: any) => p.id) || []);
|
||||
|
||||
const filteredProducts = storeIdNum
|
||||
? slotProducts.filter(p => p.storeId === storeIdNum)
|
||||
: slotProducts;
|
||||
const filteredProducts: any[] = storeIdNum
|
||||
? productsQuery?.data?.products?.filter(p =>
|
||||
p.storeId === storeIdNum && slotProductIds.has(p.id)
|
||||
) || []
|
||||
: slotQuery.data.products;
|
||||
|
||||
return (
|
||||
<View testID="slot-detail-page" style={tw`flex-1`}>
|
||||
|
|
@ -421,7 +422,7 @@ export function SlotProducts({ slotId:slotIdParent, storeId:storeIdParent, baseU
|
|||
keyExtractor={(item, index) => index.toString()}
|
||||
columnWrapperStyle={{ gap: 16, justifyContent: 'flex-start' }}
|
||||
contentContainerStyle={[tw`pb-24 px-4`, { gap: 16 }]}
|
||||
onRefresh={() => {}}
|
||||
onRefresh={() => slotQuery.refetch()}
|
||||
ListEmptyComponent={
|
||||
storeIdNum ? (
|
||||
<View style={tw`items-center justify-center py-10`}>
|
||||
|
|
@ -447,8 +448,7 @@ export function FlashDeliveryProducts({ storeId:storeIdParent, baseUrl, onProduc
|
|||
const storeId = storeIdParent;
|
||||
const storeIdNum = storeId;
|
||||
|
||||
const productsQuery = useAllProducts();
|
||||
const productSlotsMap = useCentralSlotStore((state) => state.productSlotsMap);
|
||||
const productsQuery = trpc.common.product.getAllProductsSummary.useQuery({});
|
||||
|
||||
const { addToCart = () => { } } = useAddToCart({ showSuccessAlert: false, showErrorAlert: false, refetchCart: true }, "flash") || {};
|
||||
|
||||
|
|
@ -486,22 +486,20 @@ export function FlashDeliveryProducts({ storeId:storeIdParent, baseUrl, onProduc
|
|||
}
|
||||
|
||||
// Filter products to only include those eligible for flash delivery
|
||||
let flashProducts: AllProductsApiType['products'][number][] = [];
|
||||
let flashProducts: any[] = [];
|
||||
if (storeIdNum) {
|
||||
// Filter by store, flash availability, and stock status
|
||||
flashProducts = productsQuery?.data?.products?.filter(p => {
|
||||
const productInfo = productSlotsMap[p.id];
|
||||
return p.storeId === storeIdNum &&
|
||||
productInfo?.isFlashAvailable &&
|
||||
!productInfo?.isOutOfStock;
|
||||
}) || [];
|
||||
flashProducts = productsQuery?.data?.products?.filter(p =>
|
||||
p.storeId === storeIdNum &&
|
||||
p.isFlashAvailable &&
|
||||
!p.isOutOfStock
|
||||
) || [];
|
||||
} else {
|
||||
// Show all flash-available products that are in stock
|
||||
flashProducts = productsQuery?.data?.products?.filter(p => {
|
||||
const productInfo = productSlotsMap[p.id];
|
||||
return productInfo?.isFlashAvailable &&
|
||||
!productInfo?.isOutOfStock;
|
||||
}) || [];
|
||||
flashProducts = productsQuery?.data?.products?.filter(p =>
|
||||
p.isFlashAvailable &&
|
||||
!p.isOutOfStock
|
||||
) || [];
|
||||
}
|
||||
|
||||
return (
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ import React, { useState } from 'react';
|
|||
import { View, ActivityIndicator } from 'react-native';
|
||||
import { WebView } from 'react-native-webview';
|
||||
import { trpc } from '@/src/trpc-client';
|
||||
import { useGetEssentialConsts } from '@/src/hooks/prominent-api-hooks';
|
||||
import { useGetEssentialConsts } from '@/src/api-hooks/essential-consts.api';
|
||||
import { theme, MyText, MyTouchableOpacity } from 'common-ui';
|
||||
import MaterialIcons from '@expo/vector-icons/MaterialIcons';
|
||||
|
||||
|
|
|
|||
|
|
@ -24,10 +24,8 @@ import TestingPhaseNote from "@/components/TestingPhaseNote";
|
|||
|
||||
import dayjs from "dayjs";
|
||||
import { trpc } from "@/src/trpc-client";
|
||||
import { useCentralProductStore } from '@/src/store/centralProductStore';
|
||||
import { useCentralSlotStore } from '@/src/store/centralSlotStore';
|
||||
import { useGetCart, useUpdateCartItem, useRemoveFromCart } from '@/hooks/cart-query-hooks';
|
||||
import { useGetEssentialConsts } from '@/src/hooks/prominent-api-hooks';
|
||||
import { useGetEssentialConsts } from '@/src/api-hooks/essential-consts.api';
|
||||
|
||||
interface CartPageProps {
|
||||
isFlashDelivery?: boolean;
|
||||
|
|
@ -82,34 +80,33 @@ export default function CartPage({ isFlashDelivery = false }: CartPageProps) {
|
|||
|
||||
const { data: couponsRaw, error: couponsError } = trpc.user.coupon.getEligible.useQuery();
|
||||
const { data: constsData } = useGetEssentialConsts();
|
||||
const products = useCentralProductStore((state) => state.products);
|
||||
const productsById = useCentralProductStore((state) => state.productsById);
|
||||
const productSlotsMap = useCentralSlotStore((state) => state.productSlotsMap);
|
||||
const { data: productsData } = trpc.common.product.getAllProductsSummary.useQuery({});
|
||||
|
||||
const cartItems = cartData?.items || [];
|
||||
|
||||
|
||||
// Memoized flash-eligible product IDs
|
||||
const flashEligibleProductIds = useMemo(() => {
|
||||
if (!products.length) return new Set<number>();
|
||||
if (!productsData?.products) return new Set<number>();
|
||||
return new Set(
|
||||
products
|
||||
.filter((product) => productSlotsMap[product.id]?.isFlashAvailable)
|
||||
.map((product) => product.id)
|
||||
productsData.products
|
||||
.filter((product: any) => product.isFlashAvailable)
|
||||
.map((product: any) => product.id)
|
||||
);
|
||||
}, [products, productSlotsMap]);
|
||||
}, [productsData]);
|
||||
|
||||
// Base total price without discounts for coupon eligibility check
|
||||
const baseTotalPrice = useMemo(
|
||||
() =>
|
||||
cartItems
|
||||
.filter((item) => !productSlotsMap[item.productId]?.isOutOfStock)
|
||||
.reduce((sum, item) => {
|
||||
const product = productsById[item.productId];
|
||||
const price = product?.price || 0;
|
||||
return sum + price * (quantities[item.id] || item.quantity);
|
||||
}, 0),
|
||||
[cartItems, quantities, productsById]
|
||||
const baseTotalPrice = useMemo(
|
||||
() =>
|
||||
cartItems
|
||||
.filter((item) => !item.product?.isOutOfStock)
|
||||
.reduce(
|
||||
(sum, item) =>
|
||||
sum +
|
||||
(item.product?.price || 0) * (quantities[item.id] || item.quantity),
|
||||
0
|
||||
),
|
||||
[cartItems, quantities]
|
||||
);
|
||||
|
||||
const eligibleCoupons = useMemo(() => {
|
||||
|
|
@ -202,14 +199,13 @@ export default function CartPage({ isFlashDelivery = false }: CartPageProps) {
|
|||
[eligibleCoupons, selectedCouponId]
|
||||
);
|
||||
|
||||
const totalPrice = cartItems
|
||||
.filter((item) => !productSlotsMap[item.productId]?.isOutOfStock)
|
||||
.reduce((sum, item) => {
|
||||
const product = productsById[item.productId];
|
||||
const quantity = quantities[item.id] || item.quantity;
|
||||
const price = isFlashDelivery ? (product?.flashPrice ?? product?.price ?? 0) : (product?.price || 0);
|
||||
return sum + price * quantity;
|
||||
}, 0);
|
||||
const totalPrice = cartItems
|
||||
.filter((item) => !item.product?.isOutOfStock)
|
||||
.reduce((sum, item) => {
|
||||
const quantity = quantities[item.id] || item.quantity;
|
||||
const price = isFlashDelivery ? (item.product?.flashPrice ?? item.product?.price ?? 0) : (item.product?.price || 0);
|
||||
return sum + price * quantity;
|
||||
}, 0);
|
||||
const dropdownData = useMemo(
|
||||
() =>
|
||||
eligibleCoupons?.map((coupon) => {
|
||||
|
|
@ -277,7 +273,7 @@ export default function CartPage({ isFlashDelivery = false }: CartPageProps) {
|
|||
|
||||
const finalTotalWithDelivery = finalTotal + deliveryCharge;
|
||||
|
||||
const hasAvailableItems = cartItems.some(item => !productSlotsMap[item.productId]?.isOutOfStock);
|
||||
const hasAvailableItems = cartItems.some(item => !item.product?.isOutOfStock);
|
||||
|
||||
useEffect(() => {
|
||||
const initial: Record<number, number> = {};
|
||||
|
|
@ -414,12 +410,10 @@ export default function CartPage({ isFlashDelivery = false }: CartPageProps) {
|
|||
const productSlots = getAvailableSlotsForProduct(item.productId);
|
||||
const selectedSlotForItem = selectedSlots[item.id];
|
||||
const isFlashEligible = isFlashDelivery ? flashEligibleProductIds.has(item.productId) : true;
|
||||
const product = productsById[item.productId];
|
||||
const productSlotInfo = productSlotsMap[item.productId];
|
||||
// const isAvailable = (productSlots.length > 0 || isFlashDelivery) && !item.product?.isOutOfStock && isFlashEligible;
|
||||
let isAvailable = true;
|
||||
|
||||
if (productSlotInfo?.isOutOfStock) {
|
||||
if(item.product?.isOutOfStock) {
|
||||
isAvailable = false;
|
||||
} else if(isFlashDelivery) {
|
||||
if(!isFlashEligible) {
|
||||
|
|
@ -436,7 +430,7 @@ export default function CartPage({ isFlashDelivery = false }: CartPageProps) {
|
|||
// isAvailable = isFlashEligible;
|
||||
// }
|
||||
const quantity = quantities[item.id] || item.quantity;
|
||||
const price = isFlashDelivery ? (product?.flashPrice ?? product?.price ?? 0) : (product?.price || 0);
|
||||
const price = isFlashDelivery ? (item.product?.flashPrice ?? item.product?.price ?? 0) : (item.product?.price || 0);
|
||||
const itemPrice = price * quantity;
|
||||
|
||||
return (
|
||||
|
|
@ -444,7 +438,7 @@ export default function CartPage({ isFlashDelivery = false }: CartPageProps) {
|
|||
<View style={tw`p-4`}>
|
||||
<View style={tw`flex-row items-center mb-2`}>
|
||||
<Image
|
||||
source={{ uri: product?.images?.[0] }}
|
||||
source={{ uri: item.product.images?.[0] }}
|
||||
style={tw`w-8 h-8 rounded-lg bg-gray-100 mr-3`}
|
||||
/>
|
||||
|
||||
|
|
@ -452,12 +446,12 @@ export default function CartPage({ isFlashDelivery = false }: CartPageProps) {
|
|||
style={tw`text-sm text-gray-900 flex-1 mr-3`}
|
||||
numberOfLines={2}
|
||||
>
|
||||
{product?.name}
|
||||
{item.product.name}
|
||||
</MyText>
|
||||
<MyText style={tw`text-xs text-gray-500 mr-2`}>
|
||||
{(() => {
|
||||
const qty = product?.productQuantity || 1;
|
||||
const unit = product?.unitNotation || '';
|
||||
const qty = item.product?.productQuantity || 1;
|
||||
const unit = item.product?.unitNotation || '';
|
||||
if (unit?.toLowerCase() === 'kg' && qty < 1) {
|
||||
return `${Math.round(qty * 1000)}g`;
|
||||
}
|
||||
|
|
@ -518,8 +512,8 @@ export default function CartPage({ isFlashDelivery = false }: CartPageProps) {
|
|||
});
|
||||
}
|
||||
}}
|
||||
step={product?.incrementStep}
|
||||
unit={product?.unitNotation}
|
||||
step={item.product.incrementStep}
|
||||
unit={item.product?.unitNotation}
|
||||
/>
|
||||
</View>
|
||||
</View>
|
||||
|
|
@ -585,7 +579,7 @@ export default function CartPage({ isFlashDelivery = false }: CartPageProps) {
|
|||
onPress={() => {
|
||||
Alert.alert(
|
||||
"Remove Item",
|
||||
`Remove ${product?.name} from cart?`,
|
||||
`Remove ${item.product.name} from cart?`,
|
||||
[
|
||||
{ text: "Cancel", style: "cancel" },
|
||||
{
|
||||
|
|
@ -636,7 +630,7 @@ export default function CartPage({ isFlashDelivery = false }: CartPageProps) {
|
|||
onPress={() => {
|
||||
Alert.alert(
|
||||
"Remove Item",
|
||||
`Remove ${product?.name} from cart?`,
|
||||
`Remove ${item.product.name} from cart?`,
|
||||
[
|
||||
{ text: "Cancel", style: "cancel" },
|
||||
{
|
||||
|
|
@ -676,12 +670,12 @@ export default function CartPage({ isFlashDelivery = false }: CartPageProps) {
|
|||
)}
|
||||
|
||||
{!isAvailable && (
|
||||
<View
|
||||
style={tw`bg-red-50 self-start px-2 py-1 rounded-md mt-2`}
|
||||
>
|
||||
<MyText style={tw`text-xs font-bold text-red-600`}>
|
||||
{productSlotInfo?.isOutOfStock
|
||||
? "Out of Stock"
|
||||
<View
|
||||
style={tw`bg-red-50 self-start px-2 py-1 rounded-md mt-2`}
|
||||
>
|
||||
<MyText style={tw`text-xs font-bold text-red-600`}>
|
||||
{item.product?.isOutOfStock
|
||||
? "Out of Stock"
|
||||
: isFlashDelivery && !flashEligibleProductIds.has(item.productId)
|
||||
? "Not available for flash delivery. Please remove"
|
||||
: "No delivery slots available"}
|
||||
|
|
@ -914,7 +908,7 @@ export default function CartPage({ isFlashDelivery = false }: CartPageProps) {
|
|||
onPress={() => {
|
||||
const availableItems = cartItems
|
||||
.filter(item => {
|
||||
if (productSlotsMap[item.productId]?.isOutOfStock) return false;
|
||||
if (item.product?.isOutOfStock) return false;
|
||||
if (isFlashDelivery) {
|
||||
// Check if product supports flash delivery
|
||||
return flashEligibleProductIds.has(item.productId);
|
||||
|
|
@ -923,10 +917,12 @@ export default function CartPage({ isFlashDelivery = false }: CartPageProps) {
|
|||
})
|
||||
.map(item => item.id);
|
||||
|
||||
if (availableItems.length === 0) {
|
||||
// Determine why no items are available
|
||||
const outOfStockItems = cartItems.filter(item => productSlotsMap[item.productId]?.isOutOfStock);
|
||||
const inStockItems = cartItems.filter(item => !productSlotsMap[item.productId]?.isOutOfStock);
|
||||
|
||||
|
||||
if (availableItems.length === 0) {
|
||||
// Determine why no items are available
|
||||
const outOfStockItems = cartItems.filter(item => item.product?.isOutOfStock);
|
||||
const inStockItems = cartItems.filter(item => !item.product?.isOutOfStock);
|
||||
|
||||
let errorTitle = "Cannot Proceed";
|
||||
let errorMessage = "";
|
||||
|
|
@ -963,9 +959,9 @@ export default function CartPage({ isFlashDelivery = false }: CartPageProps) {
|
|||
return;
|
||||
}
|
||||
|
||||
// Check if there are items without slots (for regular delivery)
|
||||
if (!isFlashDelivery && availableItems.length < cartItems.length) {
|
||||
const itemsWithoutSlots = cartItems.filter(item => !selectedSlots[item.id] && !productSlotsMap[item.productId]?.isOutOfStock);
|
||||
// Check if there are items without slots (for regular delivery)
|
||||
if (!isFlashDelivery && availableItems.length < cartItems.length) {
|
||||
const itemsWithoutSlots = cartItems.filter(item => !selectedSlots[item.id] && !item.product?.isOutOfStock);
|
||||
if (itemsWithoutSlots.length > 0) {
|
||||
Alert.alert(
|
||||
"Delivery Slot Required",
|
||||
|
|
|
|||
|
|
@ -8,10 +8,8 @@ import AddressForm from '@/src/components/AddressForm';
|
|||
import { useAuthenticatedRoute } from '@/hooks/useAuthenticatedRoute';
|
||||
|
||||
import { trpc } from '@/src/trpc-client';
|
||||
import { useCentralProductStore } from '@/src/store/centralProductStore';
|
||||
import { useCentralSlotStore } from '@/src/store/centralSlotStore';
|
||||
import { useGetCart } from '@/hooks/cart-query-hooks';
|
||||
import { useGetEssentialConsts } from '@/src/hooks/prominent-api-hooks';
|
||||
import { useGetEssentialConsts } from '@/src/api-hooks/essential-consts.api';
|
||||
import PaymentAndOrderComponent from '@/components/PaymentAndOrderComponent';
|
||||
import CheckoutAddressSelector from '@/components/CheckoutAddressSelector';
|
||||
import { useAddressStore } from '@/src/store/addressStore';
|
||||
|
|
@ -37,9 +35,7 @@ const CheckoutPage: React.FC<CheckoutPageProps> = ({ isFlashDelivery = false })
|
|||
const { data: addresses, refetch: refetchAddresses } = trpc.user.address.getUserAddresses.useQuery();
|
||||
const { data: slotsData, refetch: refetchSlots } = trpc.user.slots.getSlots.useQuery();
|
||||
const { data: constsData } = useGetEssentialConsts();
|
||||
const products = useCentralProductStore((state) => state.products);
|
||||
const productsById = useCentralProductStore((state) => state.productsById);
|
||||
const productSlotsMap = useCentralSlotStore((state) => state.productSlotsMap);
|
||||
const { data: productsData } = trpc.common.product.getAllProductsSummary.useQuery({});
|
||||
|
||||
useMarkDataFetchers(() => {
|
||||
refetchCart();
|
||||
|
|
@ -55,15 +51,15 @@ const CheckoutPage: React.FC<CheckoutPageProps> = ({ isFlashDelivery = false })
|
|||
|
||||
const cartItems = cartData?.items || [];
|
||||
|
||||
// Memoized flash-eligible product IDs
|
||||
const flashEligibleProductIds = useMemo(() => {
|
||||
if (!products.length) return new Set<number>();
|
||||
return new Set(
|
||||
products
|
||||
.filter((product) => productSlotsMap[product.id]?.isFlashAvailable)
|
||||
.map((product) => product.id)
|
||||
);
|
||||
}, [products, productSlotsMap]);
|
||||
// Memoized flash-eligible product IDs
|
||||
const flashEligibleProductIds = useMemo(() => {
|
||||
if (!productsData?.products) return new Set<number>();
|
||||
return new Set(
|
||||
productsData.products
|
||||
.filter((product: any) => product.isFlashAvailable)
|
||||
.map((product: any) => product.id)
|
||||
);
|
||||
}, [productsData]);
|
||||
|
||||
// Parse slots parameter from URL (format: "1:1,2,3;2:4,5")
|
||||
const selectedSlots = useMemo(() => {
|
||||
|
|
@ -127,11 +123,10 @@ const CheckoutPage: React.FC<CheckoutPageProps> = ({ isFlashDelivery = false })
|
|||
|
||||
|
||||
const totalPrice = selectedItems
|
||||
.filter((item) => !productSlotsMap[item.productId]?.isOutOfStock)
|
||||
.filter((item) => !item.product?.isOutOfStock)
|
||||
.reduce(
|
||||
(sum, item) => {
|
||||
const product = productsById[item.productId];
|
||||
const price = isFlashDelivery ? (product?.flashPrice ?? product?.price ?? 0) : (product?.price || 0);
|
||||
const price = isFlashDelivery ? (item.product?.flashPrice ?? item.product?.price ?? 0) : (item.product?.price || 0);
|
||||
return sum + price * item.quantity;
|
||||
},
|
||||
0
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ import {
|
|||
theme,
|
||||
updateStatusBarColor,
|
||||
} from "common-ui";
|
||||
import { trpc } from "@/src/trpc-client";
|
||||
import {
|
||||
useGetCart,
|
||||
useUpdateCartItem,
|
||||
|
|
@ -21,9 +22,8 @@ import {
|
|||
useAddToCart,
|
||||
type CartType,
|
||||
} from "@/hooks/cart-query-hooks";
|
||||
import { useGetEssentialConsts, useSlots } from "@/src/hooks/prominent-api-hooks"
|
||||
import { useGetEssentialConsts } from "@/src/api-hooks/essential-consts.api";
|
||||
import { useProductSlotIdentifier } from "@/hooks/useProductSlotIdentifier";
|
||||
import { useCentralProductStore } from "@/src/store/centralProductStore";
|
||||
import dayjs from "dayjs";
|
||||
import { LinearGradient } from "expo-linear-gradient";
|
||||
|
||||
|
|
@ -36,7 +36,7 @@ interface FloatingCartBarProps {
|
|||
}
|
||||
|
||||
// Smart time window formatting function
|
||||
const formatTimeRange = (deliveryTime: string | Date) => {
|
||||
const formatTimeRange = (deliveryTime: string) => {
|
||||
const time = dayjs(deliveryTime);
|
||||
const endTime = time.add(1, 'hour');
|
||||
const startPeriod = time.format('A');
|
||||
|
|
@ -79,8 +79,7 @@ const FloatingCartBar: React.FC<FloatingCartBarProps> = ({
|
|||
const setIsExpanded = controlledSetIsExpanded ?? setLocalIsExpanded;
|
||||
const { data: cartData, refetch: refetchCart } = useGetCart({}, cartType);
|
||||
const { data: constsData } = useGetEssentialConsts();
|
||||
const { data: slotsData } = useSlots();
|
||||
const productsById = useCentralProductStore((state) => state.productsById);
|
||||
const { data: slotsData } = trpc.user.slots.getSlotsWithProducts.useQuery();
|
||||
const { productSlotsMap } = useProductSlotIdentifier();
|
||||
const cartItems = cartData?.items || [];
|
||||
const itemCount = cartItems.length;
|
||||
|
|
@ -109,21 +108,21 @@ const FloatingCartBar: React.FC<FloatingCartBarProps> = ({
|
|||
setQuantities(initial);
|
||||
}, [cartData]);
|
||||
|
||||
useEffect(() => {
|
||||
useEffect(() => {
|
||||
if (!cartItems.length || !slotsData?.slots || !productSlotsMap) return;
|
||||
|
||||
const itemsToUpdate = cartItems.filter(item => {
|
||||
if (isFlashDelivery || !item.slotId) return false;
|
||||
|
||||
const availableSlots = productSlotsMap[item.productId]?.slots || [];
|
||||
const isSlotAvailable = availableSlots.some((slot) => slot.id === item.slotId);
|
||||
const availableSlots = productSlotsMap.get(item.productId) || [];
|
||||
const isSlotAvailable = availableSlots.includes(item.slotId);
|
||||
return !isSlotAvailable;
|
||||
});
|
||||
|
||||
itemsToUpdate.forEach((item) => {
|
||||
const availableSlots = productSlotsMap[item.productId]?.slots || [];
|
||||
const availableSlots = productSlotsMap.get(item.productId) || [];
|
||||
if (availableSlots.length > 0 && !isFlashDelivery) {
|
||||
const nearestSlotId = availableSlots[0].id;
|
||||
const nearestSlotId = availableSlots[0];
|
||||
removeFromCart.mutate({ itemId: item.id });
|
||||
addToCartHook.addToCart(item.productId, item.quantity, nearestSlotId);
|
||||
}
|
||||
|
|
@ -136,9 +135,7 @@ const FloatingCartBar: React.FC<FloatingCartBarProps> = ({
|
|||
// Calculate total cart value and free delivery info
|
||||
const totalCartValue = cartItems.reduce(
|
||||
(sum, item) => {
|
||||
const product = productsById[item.productId];
|
||||
const basePrice = product?.price ?? 0;
|
||||
const price = isFlashDelivery ? (product?.flashPrice ?? basePrice) : basePrice;
|
||||
const price = isFlashDelivery ? (item.product.flashPrice ?? item.product.price) : item.product.price;
|
||||
return sum + price * item.quantity;
|
||||
},
|
||||
0
|
||||
|
|
@ -259,21 +256,21 @@ const FloatingCartBar: React.FC<FloatingCartBarProps> = ({
|
|||
<React.Fragment key={item.id}>
|
||||
<View style={tw`py-4`}>
|
||||
<View style={tw`flex-row items-center`}>
|
||||
<Image
|
||||
source={{ uri: productsById[item.productId]?.images?.[0] }}
|
||||
style={tw`w-8 h-8 rounded-lg bg-slate-50 border border-slate-100`}
|
||||
/>
|
||||
<Image
|
||||
source={{ uri: item.product.images?.[0] }}
|
||||
style={tw`w-8 h-8 rounded-lg bg-slate-50 border border-slate-100`}
|
||||
/>
|
||||
|
||||
<View style={tw`flex-1 ml-4`}>
|
||||
<View style={tw`flex-row items-center justify-between mb-1`}>
|
||||
<ProductNameWithQuantity
|
||||
name={productsById[item.productId]?.name || ''}
|
||||
productQuantity={productsById[item.productId]?.productQuantity || 0}
|
||||
unitNotation={productsById[item.productId]?.unitNotation || ''}
|
||||
name={item.product.name}
|
||||
productQuantity={item.product.productQuantity}
|
||||
unitNotation={item.product.unitNotation}
|
||||
/>
|
||||
<MiniQuantifier
|
||||
value={quantities[item.id] || item.quantity}
|
||||
onChange={(value) => {
|
||||
<MiniQuantifier
|
||||
value={quantities[item.id] || item.quantity}
|
||||
onChange={(value) => {
|
||||
if (value === 0) {
|
||||
removeFromCart.mutate({ itemId: item.id });
|
||||
} else {
|
||||
|
|
@ -281,20 +278,21 @@ const FloatingCartBar: React.FC<FloatingCartBarProps> = ({
|
|||
updateCartItem.mutate({ itemId: item.id, quantity: value });
|
||||
}
|
||||
}}
|
||||
step={productsById[item.productId]?.incrementStep || 1}
|
||||
showUnits={true}
|
||||
unit={productsById[item.productId]?.unitNotation}
|
||||
/>
|
||||
step={item.product.incrementStep}
|
||||
showUnits={true}
|
||||
unit={item.product?.unitNotation}
|
||||
/>
|
||||
</View>
|
||||
<View style={tw`flex-row items-center justify-between`}>
|
||||
{item.slotId && slotsData && productSlotsMap[item.productId] && (
|
||||
{item.slotId && slotsData && productSlotsMap.has(item.productId) && (
|
||||
<BottomDropdown
|
||||
label="Select Delivery Slot"
|
||||
value={item.slotId}
|
||||
options={(productSlotsMap[item.productId]?.slots || []).map((slot) => {
|
||||
options={(productSlotsMap.get(item.productId) || []).map(slotId => {
|
||||
const slot = slotsData.slots.find(s => s.id === slotId);
|
||||
return {
|
||||
label: slot ? formatTimeRange(slot.deliveryTime) : "N/A",
|
||||
value: slot.id,
|
||||
value: slotId,
|
||||
};
|
||||
})}
|
||||
onValueChange={async (val) => {
|
||||
|
|
@ -327,12 +325,7 @@ const FloatingCartBar: React.FC<FloatingCartBarProps> = ({
|
|||
/>
|
||||
)}
|
||||
<MyText style={tw`text-slate-900 text-sm font-bold`}>
|
||||
₹{(() => {
|
||||
const product = productsById[item.productId];
|
||||
const basePrice = product?.price ?? 0;
|
||||
const price = isFlashDelivery ? (product?.flashPrice ?? basePrice) : basePrice;
|
||||
return price * item.quantity;
|
||||
})()}
|
||||
₹{(isFlashDelivery ? (item.product.flashPrice ?? item.product.price) : item.product.price) * item.quantity}
|
||||
</MyText>
|
||||
</View>
|
||||
</View>
|
||||
|
|
|
|||
|
|
@ -1,12 +1,15 @@
|
|||
import { useAllProducts } from '@/src/hooks/prominent-api-hooks';
|
||||
import { useCentralSlotStore } from '@/src/store/centralSlotStore';
|
||||
import { trpc } from '@/src/trpc-client';
|
||||
import { Alert } from 'react-native';
|
||||
import { useQuery, useMutation, useQueryClient, UseQueryResult, UseMutationResult } from '@tanstack/react-query';
|
||||
import { useState, useEffect } from 'react';
|
||||
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
import { StorageServiceCasual } from 'common-ui/src/services/StorageServiceCasual';
|
||||
|
||||
// Cart type definition
|
||||
export type CartType = "regular" | "flash";
|
||||
|
||||
// const CART_MODE: 'remote' | 'local' = 'remote';
|
||||
const CART_MODE: 'remote' | 'local' = 'local';
|
||||
|
||||
const getCartStorageKey = (cartType: CartType = "regular"): string => {
|
||||
return cartType === "flash" ? "flash_cart_items" : "cart_items";
|
||||
};
|
||||
|
|
@ -23,99 +26,15 @@ interface ProductSummary {
|
|||
id: number;
|
||||
price: string;
|
||||
incrementStep: number;
|
||||
isOutOfStock: boolean;
|
||||
isFlashAvailable: boolean;
|
||||
name?: string;
|
||||
flashPrice?: string | null;
|
||||
images?: string[];
|
||||
productQuantity?: number;
|
||||
unitNotation?: string;
|
||||
marketPrice?: string | null;
|
||||
}
|
||||
|
||||
export interface CartItem {
|
||||
interface CartItem {
|
||||
id: number;
|
||||
productId: number;
|
||||
quantity: number;
|
||||
addedAt: string;
|
||||
product: ProductSummary;
|
||||
subtotal: number;
|
||||
slotId: number;
|
||||
}
|
||||
|
||||
interface CartData {
|
||||
items: CartItem[];
|
||||
totalItems: number;
|
||||
totalAmount: number;
|
||||
}
|
||||
|
||||
interface UseGetCartOptions {
|
||||
refetchOnWindowFocus?: boolean;
|
||||
enabled?: boolean;
|
||||
}
|
||||
|
||||
interface UseGetCartReturn {
|
||||
data: CartData | undefined;
|
||||
isLoading: boolean;
|
||||
error: Error | null;
|
||||
refetch: () => Promise<UseQueryResult<CartData, Error>>;
|
||||
cartItems: CartItem[];
|
||||
totalItems: number;
|
||||
totalPrice: number;
|
||||
isEmpty: boolean;
|
||||
hasItems: boolean;
|
||||
}
|
||||
|
||||
interface AddToCartVariables {
|
||||
productId: number;
|
||||
quantity: number;
|
||||
slotId: number;
|
||||
}
|
||||
|
||||
interface UpdateCartVariables {
|
||||
itemId: number;
|
||||
quantity: number;
|
||||
}
|
||||
|
||||
interface RemoveCartVariables {
|
||||
itemId: number;
|
||||
}
|
||||
|
||||
interface MutationOptions<TData, TVariables> {
|
||||
onSuccess?: (data: TData, variables: TVariables) => void;
|
||||
onError?: (error: Error) => void;
|
||||
showSuccessAlert?: boolean;
|
||||
showErrorAlert?: boolean;
|
||||
refetchCart?: boolean;
|
||||
}
|
||||
|
||||
interface UseAddToCartReturn {
|
||||
mutate: UseMutationResult<LocalCartItem[], Error, AddToCartVariables>['mutate'];
|
||||
mutateAsync: UseMutationResult<LocalCartItem[], Error, AddToCartVariables>['mutateAsync'];
|
||||
isLoading: boolean;
|
||||
error: Error | null;
|
||||
data: LocalCartItem[] | undefined;
|
||||
addToCart: (productId: number, quantity?: number, slotId?: number, onSettled?: (data: LocalCartItem[] | undefined, error: Error | null) => void) => void;
|
||||
addToCartAsync: (productId: number, quantity?: number, slotId?: number) => Promise<LocalCartItem[]>;
|
||||
}
|
||||
|
||||
interface UseUpdateCartItemReturn {
|
||||
mutate: UseMutationResult<LocalCartItem[], Error, UpdateCartVariables>['mutate'];
|
||||
mutateAsync: UseMutationResult<LocalCartItem[], Error, UpdateCartVariables>['mutateAsync'];
|
||||
isLoading: boolean;
|
||||
error: Error | null;
|
||||
data: LocalCartItem[] | undefined;
|
||||
updateCartItem: (itemId: number, quantity: number) => void;
|
||||
updateCartItemAsync: (itemId: number, quantity: number) => Promise<LocalCartItem[]>;
|
||||
}
|
||||
|
||||
interface UseRemoveFromCartReturn {
|
||||
mutate: UseMutationResult<LocalCartItem[], Error, RemoveCartVariables>['mutate'];
|
||||
mutateAsync: UseMutationResult<LocalCartItem[], Error, RemoveCartVariables>['mutateAsync'];
|
||||
isLoading: boolean;
|
||||
error: Error | null;
|
||||
data: LocalCartItem[] | undefined;
|
||||
removeFromCart: (itemId: number) => void;
|
||||
removeFromCartAsync: (itemId: number) => Promise<LocalCartItem[]>;
|
||||
}
|
||||
|
||||
const getLocalCart = async (cartType: CartType = "regular"): Promise<LocalCartItem[]> => {
|
||||
|
|
@ -127,7 +46,8 @@ const getLocalCart = async (cartType: CartType = "regular"): Promise<LocalCartIt
|
|||
const saveLocalCart = async (items: LocalCartItem[], cartType: CartType = "regular"): Promise<void> => {
|
||||
const key = getCartStorageKey(cartType);
|
||||
await StorageServiceCasual.setItem(key, JSON.stringify(items));
|
||||
await getLocalCart(cartType);
|
||||
const fetchedItems = await getLocalCart(cartType);
|
||||
|
||||
};
|
||||
|
||||
const getNextCartItemId = (items: LocalCartItem[]): number => {
|
||||
|
|
@ -135,7 +55,8 @@ const getNextCartItemId = (items: LocalCartItem[]): number => {
|
|||
return maxId + 1;
|
||||
};
|
||||
|
||||
const addToLocalCart = async (productId: number, quantity: number, slotId: number | undefined, cartType: CartType = "regular"): Promise<LocalCartItem[]> => {
|
||||
const addToLocalCart = async (productId: number, quantity: number, slotId?: number, cartType: CartType = "regular"): Promise<LocalCartItem[]> => {
|
||||
|
||||
const items = await getLocalCart(cartType);
|
||||
const existingIndex = items.findIndex(item => item.productId === productId);
|
||||
|
||||
|
|
@ -146,13 +67,13 @@ const addToLocalCart = async (productId: number, quantity: number, slotId: numbe
|
|||
}
|
||||
} else {
|
||||
const newId = getNextCartItemId(items);
|
||||
const cartItem: LocalCartItem = {
|
||||
const cartItem = {
|
||||
id: newId,
|
||||
productId,
|
||||
quantity,
|
||||
slotId: slotId ?? 0,
|
||||
slotId: slotId ?? 0, // Default to 0 if not provided
|
||||
addedAt: new Date().toISOString(),
|
||||
};
|
||||
}
|
||||
|
||||
items.push(cartItem);
|
||||
}
|
||||
|
|
@ -183,192 +104,401 @@ const clearLocalCart = async (cartType: CartType = "regular"): Promise<void> =>
|
|||
await StorageServiceCasual.setItem(key, JSON.stringify([]));
|
||||
};
|
||||
|
||||
export function useGetCart(options: UseGetCartOptions = {}, cartType: CartType = "regular"): UseGetCartReturn {
|
||||
const { data: products } = useAllProducts();
|
||||
const productSlotsMap = useCentralSlotStore((state) => state.productSlotsMap);
|
||||
|
||||
const query: UseQueryResult<CartData, Error> = useQuery({
|
||||
queryKey: [`local-cart-${cartType}`],
|
||||
queryFn: async (): Promise<CartData> => {
|
||||
const cartItems = await getLocalCart(cartType);
|
||||
export function useGetCart(options?: {
|
||||
refetchOnWindowFocus?: boolean;
|
||||
enabled?: boolean;
|
||||
}, cartType: CartType = "regular") {
|
||||
if (CART_MODE === 'remote') {
|
||||
const query = trpc.user.cart.getCart.useQuery(undefined, {
|
||||
refetchOnWindowFocus: options?.refetchOnWindowFocus ?? true,
|
||||
enabled: options?.enabled ?? true,
|
||||
...options
|
||||
});
|
||||
|
||||
const productMap: Record<number, Omit<ProductSummary, 'isOutOfStock' | 'isFlashAvailable'>> = Object.fromEntries(
|
||||
products?.products?.map((p) => [
|
||||
p.id,
|
||||
{
|
||||
id: p.id,
|
||||
price: String(p.price),
|
||||
incrementStep: p.incrementStep,
|
||||
marketPrice: p.marketPrice === null || p.marketPrice === undefined ? null : String(p.marketPrice),
|
||||
name: p.name,
|
||||
flashPrice: p.flashPrice,
|
||||
images: p.images,
|
||||
productQuantity: p.productQuantity,
|
||||
unitNotation: p.unitNotation,
|
||||
},
|
||||
]) ?? []
|
||||
);
|
||||
return {
|
||||
// Original tRPC returns
|
||||
data: query.data,
|
||||
isLoading: query.isLoading,
|
||||
error: query.error,
|
||||
refetch: query.refetch,
|
||||
|
||||
const items: CartItem[] = cartItems
|
||||
.map((cartItem): CartItem | null => {
|
||||
const productBasic = productMap[cartItem.productId];
|
||||
const productAvailability = productSlotsMap[cartItem.productId];
|
||||
// Computed properties
|
||||
cartItems: query.data?.items || [],
|
||||
totalItems: query.data?.totalItems || 0,
|
||||
totalPrice: query.data?.totalAmount || 0,
|
||||
|
||||
if (!productBasic || !productAvailability) return null;
|
||||
// Helper methods
|
||||
isEmpty: !query.data?.items?.length,
|
||||
hasItems: Boolean(query.data?.items?.length),
|
||||
};
|
||||
} else {
|
||||
|
||||
const { data: products } = trpc.common.product.getAllProductsSummary.useQuery({});
|
||||
const query = useQuery({
|
||||
queryKey: [`local-cart-${cartType}`],
|
||||
queryFn: async () => {
|
||||
|
||||
const cartItems = await getLocalCart(cartType);
|
||||
|
||||
const productMap = Object.fromEntries(
|
||||
products?.products?.map((p) => [
|
||||
p.id,
|
||||
{
|
||||
...p,
|
||||
price: String(p.price),
|
||||
marketPrice: p.marketPrice === null || p.marketPrice === undefined ? null : String(p.marketPrice),
|
||||
} as ProductSummary,
|
||||
]) || []
|
||||
);
|
||||
|
||||
const items: CartItem[] = cartItems.map(cartItem => {
|
||||
const product = productMap[cartItem.productId];
|
||||
|
||||
if (!product) return null as any;
|
||||
return {
|
||||
id: cartItem.id,
|
||||
productId: cartItem.productId,
|
||||
quantity: cartItem.quantity,
|
||||
addedAt: cartItem.addedAt,
|
||||
subtotal: Number(productBasic.price) * cartItem.quantity,
|
||||
addedAt: cartItem.addedAt,
|
||||
product,
|
||||
incrementStep: product.incrementStep,
|
||||
subtotal: Number(product.price) * cartItem.quantity,
|
||||
slotId: cartItem.slotId,
|
||||
};
|
||||
})
|
||||
.filter((item): item is CartItem => item !== null);
|
||||
}).filter(Boolean) as CartItem[];
|
||||
const totalAmount = items.reduce((sum, item) => sum + item.subtotal, 0);
|
||||
|
||||
const totalAmount = items.reduce((sum, item) => sum + item.subtotal, 0);
|
||||
return {
|
||||
items,
|
||||
totalItems: items.length,
|
||||
totalAmount,
|
||||
};
|
||||
},
|
||||
refetchOnWindowFocus: options?.refetchOnWindowFocus ?? true,
|
||||
enabled: (options?.enabled ?? true) && !!products,
|
||||
});
|
||||
|
||||
return {
|
||||
data: query.data,
|
||||
isLoading: query.isLoading,
|
||||
error: query.error,
|
||||
refetch: query.refetch,
|
||||
|
||||
return {
|
||||
items,
|
||||
totalItems: items.length,
|
||||
totalAmount,
|
||||
};
|
||||
},
|
||||
refetchOnWindowFocus: options?.refetchOnWindowFocus ?? true,
|
||||
enabled: (options?.enabled ?? true) && !!products,
|
||||
});
|
||||
|
||||
return {
|
||||
data: query.data,
|
||||
isLoading: query.isLoading,
|
||||
error: query.error,
|
||||
refetch: query.refetch,
|
||||
cartItems: query.data?.items ?? [],
|
||||
totalItems: query.data?.totalItems ?? 0,
|
||||
totalPrice: query.data?.totalAmount ?? 0,
|
||||
isEmpty: !(query.data?.items?.length ?? 0),
|
||||
hasItems: Boolean(query.data?.items?.length),
|
||||
};
|
||||
// Computed properties
|
||||
cartItems: query.data?.items || [],
|
||||
totalItems: query.data?.totalItems || 0,
|
||||
totalPrice: query.data?.totalAmount || 0,
|
||||
|
||||
// Helper methods
|
||||
isEmpty: !query.data?.items?.length,
|
||||
hasItems: Boolean(query.data?.items?.length),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export function useAddToCart(options: MutationOptions<LocalCartItem[], AddToCartVariables> = {}, cartType: CartType = "regular"): UseAddToCartReturn {
|
||||
const queryClient = useQueryClient();
|
||||
interface UseAddToCartReturn {
|
||||
mutate: any;
|
||||
mutateAsync: any;
|
||||
isLoading: boolean;
|
||||
error: any;
|
||||
data: any;
|
||||
addToCart: (productId: number, quantity?: number, slotId?: number, onSettled?: (data: any, error: any) => void) => void;
|
||||
addToCartAsync: (productId: number, quantity?: number, slotId?: number) => Promise<any>;
|
||||
}
|
||||
|
||||
const mutation: UseMutationResult<LocalCartItem[], Error, AddToCartVariables> = useMutation({
|
||||
mutationFn: async ({ productId, quantity, slotId }: AddToCartVariables): Promise<LocalCartItem[]> => {
|
||||
return await addToLocalCart(productId, quantity, slotId, cartType);
|
||||
},
|
||||
onSuccess: (data: LocalCartItem[], variables: AddToCartVariables) => {
|
||||
queryClient.invalidateQueries({ queryKey: [`local-cart-${cartType}`] });
|
||||
if (options?.showSuccessAlert !== false) {
|
||||
Alert.alert("Success", "Item added to cart!");
|
||||
}
|
||||
options?.onSuccess?.(data, variables);
|
||||
},
|
||||
onError: (error: Error) => {
|
||||
if (options?.showErrorAlert !== false) {
|
||||
Alert.alert("Error", error.message || "Failed to add item to cart");
|
||||
}
|
||||
options?.onError?.(error);
|
||||
},
|
||||
});
|
||||
export function useAddToCart(options?: {
|
||||
onSuccess?: (data: any, variables: any) => void;
|
||||
onError?: (error: any) => void;
|
||||
showSuccessAlert?: boolean;
|
||||
showErrorAlert?: boolean;
|
||||
refetchCart?: boolean;
|
||||
}, cartType: CartType = "regular"): UseAddToCartReturn {
|
||||
if (CART_MODE === 'remote') {
|
||||
const utils = trpc.useUtils();
|
||||
|
||||
const addToCart = (productId: number, quantity = 1, slotId?: number, onSettled?: (data: LocalCartItem[] | undefined, error: Error | null) => void): void => {
|
||||
if (slotId == null) {
|
||||
throw new Error('slotId is required for adding to cart');
|
||||
}
|
||||
mutation.mutate({ productId, quantity, slotId }, {
|
||||
onSettled: (data: LocalCartItem[] | undefined, error: Error | null) => {
|
||||
onSettled?.(data, error);
|
||||
}
|
||||
});
|
||||
};
|
||||
const mutation = trpc.user.cart.addToCart.useMutation({
|
||||
onSuccess: (data, variables) => {
|
||||
// Default success handling
|
||||
if (options?.showSuccessAlert !== false) {
|
||||
Alert.alert("Success", "Item added to cart!");
|
||||
}
|
||||
|
||||
return {
|
||||
mutate: mutation.mutate,
|
||||
mutateAsync: mutation.mutateAsync,
|
||||
isLoading: mutation.isPending,
|
||||
error: mutation.error,
|
||||
data: mutation.data,
|
||||
addToCart,
|
||||
addToCartAsync: (productId: number, quantity = 1, slotId?: number): Promise<LocalCartItem[]> => {
|
||||
// Auto-refetch cart if requested
|
||||
if (options?.refetchCart) {
|
||||
utils.user.cart.getCart.invalidate();
|
||||
}
|
||||
|
||||
// Custom success callback
|
||||
options?.onSuccess?.(data, variables);
|
||||
},
|
||||
onError: (error) => {
|
||||
// Default error handling
|
||||
if (options?.showErrorAlert !== false) {
|
||||
Alert.alert("Error", error.message || "Failed to add item to cart");
|
||||
}
|
||||
|
||||
// Custom error callback
|
||||
options?.onError?.(error);
|
||||
},
|
||||
}) as any;
|
||||
|
||||
const addToCart = (productId: number, quantity = 1, slotId?: number, onSettled?: (data: any, error: any) => void) => {
|
||||
|
||||
if (slotId == null) {
|
||||
throw new Error('slotId is required for adding to cart');
|
||||
}
|
||||
return mutation.mutateAsync({ productId, quantity, slotId });
|
||||
},
|
||||
};
|
||||
return mutation.mutate({ productId, quantity, slotId }, {
|
||||
onSettled: (data: any, error: any) => {
|
||||
onSettled?.(data, error);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
return {
|
||||
// Original mutation returns
|
||||
mutate: mutation.mutate,
|
||||
mutateAsync: mutation.mutateAsync,
|
||||
isLoading: mutation.isPending,
|
||||
error: mutation.error,
|
||||
data: mutation.data,
|
||||
|
||||
addToCart,
|
||||
|
||||
addToCartAsync: (productId: number, quantity = 1, slotId?: number) => {
|
||||
if (slotId == null) {
|
||||
throw new Error('slotId is required for adding to cart');
|
||||
}
|
||||
return mutation.mutateAsync({ productId, quantity, slotId });
|
||||
},
|
||||
};
|
||||
} else {
|
||||
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const mutation = useMutation({
|
||||
mutationFn: async ({ productId, quantity, slotId }: { productId: number, quantity: number, slotId: number }) => {
|
||||
return await addToLocalCart(productId, quantity, slotId, cartType);
|
||||
},
|
||||
onSuccess: (data, variables) => {
|
||||
queryClient.invalidateQueries({ queryKey: [`local-cart-${cartType}`] });
|
||||
if (options?.showSuccessAlert !== false) {
|
||||
Alert.alert("Success", "Item added to cart!");
|
||||
}
|
||||
options?.onSuccess?.(data, variables);
|
||||
},
|
||||
onError: (error) => {
|
||||
if (options?.showErrorAlert !== false) {
|
||||
Alert.alert("Error", error.message || "Failed to add item to cart");
|
||||
}
|
||||
options?.onError?.(error);
|
||||
},
|
||||
});
|
||||
|
||||
const addToCart = (productId: number, quantity = 1, slotId?: number, onSettled?: (data: any, error: any) => void) => {
|
||||
|
||||
if (slotId == null) {
|
||||
throw new Error('slotId is required for adding to cart');
|
||||
}
|
||||
return mutation.mutate({ productId, quantity, slotId }, {
|
||||
onSettled: (data: any, error: any) => {
|
||||
onSettled?.(data, error);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
return {
|
||||
mutate: mutation.mutate,
|
||||
mutateAsync: mutation.mutateAsync,
|
||||
isLoading: mutation.isPending,
|
||||
error: mutation.error,
|
||||
data: mutation.data,
|
||||
addToCart,
|
||||
addToCartAsync: (productId: number, quantity = 1, slotId?: number) => {
|
||||
if (slotId == null) {
|
||||
throw new Error('slotId is required for adding to cart');
|
||||
}
|
||||
return mutation.mutateAsync({ productId, quantity, slotId });
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export function useUpdateCartItem(options: MutationOptions<LocalCartItem[], UpdateCartVariables> = {}, cartType: CartType = "regular"): UseUpdateCartItemReturn {
|
||||
const queryClient = useQueryClient();
|
||||
export function useUpdateCartItem(options?: {
|
||||
onSuccess?: (data: any, variables: any) => void;
|
||||
onError?: (error: any) => void;
|
||||
showSuccessAlert?: boolean;
|
||||
showErrorAlert?: boolean;
|
||||
refetchCart?: boolean;
|
||||
}, cartType: CartType = "regular") {
|
||||
if (CART_MODE === 'remote') {
|
||||
const utils = trpc.useUtils();
|
||||
|
||||
const mutation: UseMutationResult<LocalCartItem[], Error, UpdateCartVariables> = useMutation({
|
||||
mutationFn: async ({ itemId, quantity }: UpdateCartVariables): Promise<LocalCartItem[]> => {
|
||||
return await updateLocalCartItem(itemId, quantity, cartType);
|
||||
},
|
||||
onSuccess: (data: LocalCartItem[], variables: UpdateCartVariables) => {
|
||||
queryClient.invalidateQueries({ queryKey: [`local-cart-${cartType}`] });
|
||||
if (options?.showSuccessAlert !== false) {
|
||||
Alert.alert("Success", "Cart item updated!");
|
||||
}
|
||||
options?.onSuccess?.(data, variables);
|
||||
},
|
||||
onError: (error: Error) => {
|
||||
if (options?.showErrorAlert !== false) {
|
||||
Alert.alert("Error", error.message || "Failed to update cart item");
|
||||
}
|
||||
options?.onError?.(error);
|
||||
},
|
||||
});
|
||||
const mutation = trpc.user.cart.updateCartItem.useMutation({
|
||||
onSuccess: (data, variables) => {
|
||||
// Default success handling
|
||||
if (options?.showSuccessAlert !== false) {
|
||||
Alert.alert("Success", "Cart item updated!");
|
||||
}
|
||||
|
||||
return {
|
||||
mutate: mutation.mutate,
|
||||
mutateAsync: mutation.mutateAsync,
|
||||
isLoading: mutation.isPending,
|
||||
error: mutation.error,
|
||||
data: mutation.data,
|
||||
updateCartItem: (itemId: number, quantity: number): void =>
|
||||
mutation.mutate({ itemId, quantity }),
|
||||
updateCartItemAsync: (itemId: number, quantity: number): Promise<LocalCartItem[]> =>
|
||||
mutation.mutateAsync({ itemId, quantity }),
|
||||
};
|
||||
// Auto-refetch cart if requested
|
||||
if (options?.refetchCart) {
|
||||
utils.user.cart.getCart.invalidate();
|
||||
}
|
||||
|
||||
// Custom success callback
|
||||
options?.onSuccess?.(data, variables);
|
||||
},
|
||||
onError: (error) => {
|
||||
// Default error handling
|
||||
if (options?.showErrorAlert !== false) {
|
||||
Alert.alert("Error", error.message || "Failed to update cart item");
|
||||
}
|
||||
|
||||
// Custom error callback
|
||||
options?.onError?.(error);
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
// Original mutation returns
|
||||
mutate: mutation.mutate,
|
||||
mutateAsync: mutation.mutateAsync,
|
||||
isLoading: mutation.isPending,
|
||||
error: mutation.error,
|
||||
data: mutation.data,
|
||||
|
||||
// Helper methods
|
||||
updateCartItem: (itemId: number, quantity: number) =>
|
||||
mutation.mutate({ itemId, quantity }),
|
||||
|
||||
updateCartItemAsync: (itemId: number, quantity: number) =>
|
||||
mutation.mutateAsync({ itemId, quantity }),
|
||||
};
|
||||
} else {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const mutation = useMutation({
|
||||
mutationFn: async ({ itemId, quantity }: { itemId: number, quantity: number }) => {
|
||||
return await updateLocalCartItem(itemId, quantity, cartType);
|
||||
},
|
||||
onSuccess: (data, variables) => {
|
||||
queryClient.invalidateQueries({ queryKey: [`local-cart-${cartType}`] });
|
||||
if (options?.showSuccessAlert !== false) {
|
||||
Alert.alert("Success", "Cart item updated!");
|
||||
}
|
||||
options?.onSuccess?.(data, variables);
|
||||
},
|
||||
onError: (error) => {
|
||||
if (options?.showErrorAlert !== false) {
|
||||
Alert.alert("Error", error.message || "Failed to update cart item");
|
||||
}
|
||||
options?.onError?.(error);
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
mutate: mutation.mutate,
|
||||
mutateAsync: mutation.mutateAsync,
|
||||
isLoading: mutation.isPending,
|
||||
error: mutation.error,
|
||||
data: mutation.data,
|
||||
|
||||
updateCartItem: (itemId: number, quantity: number) =>
|
||||
mutation.mutate({ itemId, quantity }),
|
||||
|
||||
updateCartItemAsync: (itemId: number, quantity: number) =>
|
||||
mutation.mutateAsync({ itemId, quantity }),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export function useRemoveFromCart(options: MutationOptions<LocalCartItem[], RemoveCartVariables> = {}, cartType: CartType = "regular"): UseRemoveFromCartReturn {
|
||||
const queryClient = useQueryClient();
|
||||
export function useRemoveFromCart(options?: {
|
||||
onSuccess?: (data: any, variables: any) => void;
|
||||
onError?: (error: any) => void;
|
||||
showSuccessAlert?: boolean;
|
||||
showErrorAlert?: boolean;
|
||||
refetchCart?: boolean;
|
||||
}, cartType: CartType = "regular") {
|
||||
if (CART_MODE === 'remote') {
|
||||
const utils = trpc.useUtils();
|
||||
|
||||
const mutation: UseMutationResult<LocalCartItem[], Error, RemoveCartVariables> = useMutation({
|
||||
mutationFn: async ({ itemId }: RemoveCartVariables): Promise<LocalCartItem[]> => {
|
||||
return await removeFromLocalCart(itemId, cartType);
|
||||
},
|
||||
onSuccess: (data: LocalCartItem[], variables: RemoveCartVariables) => {
|
||||
queryClient.invalidateQueries({ queryKey: [`local-cart-${cartType}`] });
|
||||
if (options?.showSuccessAlert !== false) {
|
||||
Alert.alert("Success", "Item removed from cart!");
|
||||
}
|
||||
options?.onSuccess?.(data, variables);
|
||||
},
|
||||
onError: (error: Error) => {
|
||||
if (options?.showErrorAlert !== false) {
|
||||
Alert.alert("Error", error.message || "Failed to remove item from cart");
|
||||
}
|
||||
options?.onError?.(error);
|
||||
},
|
||||
});
|
||||
const mutation = trpc.user.cart.removeFromCart.useMutation({
|
||||
onSuccess: (data, variables) => {
|
||||
// Default success handling
|
||||
if (options?.showSuccessAlert !== false) {
|
||||
Alert.alert("Success", "Item removed from cart!");
|
||||
}
|
||||
|
||||
return {
|
||||
mutate: mutation.mutate,
|
||||
mutateAsync: mutation.mutateAsync,
|
||||
isLoading: mutation.isPending,
|
||||
error: mutation.error,
|
||||
data: mutation.data,
|
||||
removeFromCart: (itemId: number): void =>
|
||||
mutation.mutate({ itemId }),
|
||||
removeFromCartAsync: (itemId: number): Promise<LocalCartItem[]> =>
|
||||
mutation.mutateAsync({ itemId }),
|
||||
};
|
||||
// Auto-refetch cart if requested
|
||||
if (options?.refetchCart) {
|
||||
utils.user.cart.getCart.invalidate();
|
||||
}
|
||||
|
||||
// Custom success callback
|
||||
options?.onSuccess?.(data, variables);
|
||||
},
|
||||
onError: (error) => {
|
||||
// Default error handling
|
||||
if (options?.showErrorAlert !== false) {
|
||||
Alert.alert("Error", error.message || "Failed to remove item from cart");
|
||||
}
|
||||
|
||||
// Custom error callback
|
||||
options?.onError?.(error);
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
// Original mutation returns
|
||||
mutate: mutation.mutate,
|
||||
mutateAsync: mutation.mutateAsync,
|
||||
isLoading: mutation.isPending,
|
||||
error: mutation.error,
|
||||
data: mutation.data,
|
||||
|
||||
// Helper methods
|
||||
removeFromCart: (itemId: number) =>
|
||||
mutation.mutate({ itemId }),
|
||||
|
||||
removeFromCartAsync: (itemId: number) =>
|
||||
mutation.mutateAsync({ itemId }),
|
||||
};
|
||||
} else {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const mutation = useMutation({
|
||||
mutationFn: async ({ itemId }: { itemId: number }) => {
|
||||
return await removeFromLocalCart(itemId, cartType);
|
||||
},
|
||||
onSuccess: (data, variables) => {
|
||||
queryClient.invalidateQueries({ queryKey: [`local-cart-${cartType}`] });
|
||||
if (options?.showSuccessAlert !== false) {
|
||||
Alert.alert("Success", "Item removed from cart!");
|
||||
}
|
||||
options?.onSuccess?.(data, variables);
|
||||
},
|
||||
onError: (error) => {
|
||||
if (options?.showErrorAlert !== false) {
|
||||
Alert.alert("Error", error.message || "Failed to remove item from cart");
|
||||
}
|
||||
options?.onError?.(error);
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
mutate: mutation.mutate,
|
||||
mutateAsync: mutation.mutateAsync,
|
||||
isLoading: mutation.isPending,
|
||||
error: mutation.error,
|
||||
data: mutation.data,
|
||||
|
||||
removeFromCart: (itemId: number) =>
|
||||
mutation.mutate({ itemId }),
|
||||
|
||||
removeFromCartAsync: (itemId: number) =>
|
||||
mutation.mutateAsync({ itemId }),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Export clear cart function for direct use
|
||||
|
|
|
|||
|
|
@ -1,28 +1,46 @@
|
|||
import { trpc } from '@/src/trpc-client';
|
||||
import dayjs from 'dayjs';
|
||||
import { useCentralSlotStore } from '@/src/store/centralSlotStore';
|
||||
|
||||
export function useProductSlotIdentifier() {
|
||||
// Get slots data from central store
|
||||
const slots = useCentralSlotStore((state) => state.slots);
|
||||
const productSlotsMap = useCentralSlotStore((state) => state.productSlotsMap);
|
||||
// Fetch all slots with products
|
||||
const { data: slotsData, isLoading: isProductsLoading } = trpc.user.slots.getSlotsWithProducts.useQuery();
|
||||
|
||||
|
||||
const productSlotsMap = new Map<number, number[]>();
|
||||
|
||||
if (slotsData?.slots) {
|
||||
const now = dayjs();
|
||||
|
||||
// Build map of productId to available slot IDs
|
||||
slotsData.slots.forEach(slot => {
|
||||
if (dayjs(slot.deliveryTime).isAfter(now)) {
|
||||
slot.products.forEach(product => {
|
||||
if (!productSlotsMap.has(product.id)) {
|
||||
productSlotsMap.set(product.id, []);
|
||||
}
|
||||
productSlotsMap.get(product.id)!.push(slot.id);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const getQuickestSlot = (productId: number): number | null => {
|
||||
if (!slots?.length) return null;
|
||||
|
||||
if (!slotsData?.slots) return null;
|
||||
|
||||
const now = dayjs();
|
||||
const productInfo = productSlotsMap[productId];
|
||||
|
||||
if (!productInfo?.slots?.length) return null;
|
||||
|
||||
// Find slots that contain this product and have future delivery time
|
||||
const availableSlots = productInfo.slots.filter((slot: any) =>
|
||||
const availableSlots = slotsData.slots.filter(slot =>
|
||||
slot.products.some(product => product.id === productId) &&
|
||||
dayjs(slot.deliveryTime).isAfter(now)
|
||||
);
|
||||
|
||||
// if(productId === 98)
|
||||
// console.log(JSON.stringify(slotsData))
|
||||
if (availableSlots.length === 0) return null;
|
||||
|
||||
// Return earliest slot ID (sorted by delivery time)
|
||||
const earliestSlot = availableSlots.sort((a: any, b: any) =>
|
||||
const earliestSlot = availableSlots.sort((a, b) =>
|
||||
dayjs(a.deliveryTime).diff(dayjs(b.deliveryTime))
|
||||
)[0];
|
||||
|
||||
|
|
@ -30,4 +48,4 @@ export function useProductSlotIdentifier() {
|
|||
};
|
||||
|
||||
return { getQuickestSlot, productSlotsMap };
|
||||
}
|
||||
}
|
||||
|
|
@ -1,19 +1,6 @@
|
|||
// Learn more on how to setup config for the app: https://docs.expo.dev/guides/config-plugins/#metro-config
|
||||
const { getDefaultConfig } = require('expo/metro-config');
|
||||
const path = require('path');
|
||||
|
||||
const config = getDefaultConfig(__dirname);
|
||||
|
||||
// Add the packages directory to watch folders
|
||||
config.watchFolders = [
|
||||
...config.watchFolders || [],
|
||||
path.resolve(__dirname, '../../packages/shared'),
|
||||
];
|
||||
|
||||
// Configure module resolution for @packages/*
|
||||
config.resolver.extraNodeModules = {
|
||||
...config.resolver.extraNodeModules,
|
||||
'@packages/shared': path.resolve(__dirname, '../../packages/shared'),
|
||||
};
|
||||
|
||||
module.exports = config;
|
||||
|
|
|
|||
|
|
@ -48,7 +48,6 @@
|
|||
"expo-updates": "~0.28.17",
|
||||
"expo-web-browser": "~14.2.0",
|
||||
"formik": "^2.4.6",
|
||||
"fuse.js": "^7.1.0",
|
||||
"jwt-decode": "^4.0.0",
|
||||
"react": "19.0.0",
|
||||
"react-dom": "19.0.0",
|
||||
|
|
|
|||
8
apps/user-ui/src/api-hooks/essential-consts.api.ts
Normal file
8
apps/user-ui/src/api-hooks/essential-consts.api.ts
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
import { trpc } from '@/src/trpc-client';
|
||||
|
||||
export const useGetEssentialConsts = () => {
|
||||
const query = trpc.common.essentialConsts.useQuery(undefined, {
|
||||
refetchInterval: 60000,
|
||||
});
|
||||
return { ...query, refetch: query.refetch };
|
||||
};
|
||||
|
|
@ -5,9 +5,9 @@ import { tw, BottomDialog, MyText, MyTouchableOpacity, Quantifier } from 'common
|
|||
import MaterialIcons from '@expo/vector-icons/MaterialIcons';
|
||||
import { useCartStore } from '@/src/store/cartStore';
|
||||
import { useFlashCartStore } from '@/src/store/flashCartStore';
|
||||
import { useCentralSlotStore } from '@/src/store/centralSlotStore';
|
||||
import { trpc } from '@/src/trpc-client';
|
||||
import { useAddToCart, useGetCart, useUpdateCartItem, useRemoveFromCart } from '@/hooks/cart-query-hooks';
|
||||
import { useGetEssentialConsts, useSlots } from '@/src/hooks/prominent-api-hooks';
|
||||
import { useGetEssentialConsts } from '@/src/api-hooks/essential-consts.api';
|
||||
import dayjs from 'dayjs';
|
||||
import { SafeAreaView } from 'react-native-safe-area-context';
|
||||
|
||||
|
|
@ -31,10 +31,9 @@ export default function AddToCartDialog() {
|
|||
const [selectedSlotId, setSelectedSlotId] = useState<number | null>(null);
|
||||
const [selectedFlashDelivery, setSelectedFlashDelivery] = useState(false);
|
||||
|
||||
const { data: slotsData } = useSlots();
|
||||
const { data: slotsData } = trpc.user.slots.getSlotsWithProducts.useQuery();
|
||||
const { data: cartData } = useGetCart();
|
||||
const { data: constsData } = useGetEssentialConsts();
|
||||
const productSlotsMap = useCentralSlotStore((state) => state.productSlotsMap);
|
||||
// const isFlashDeliveryEnabled = constsData?.isFlashDeliveryEnabled === true;
|
||||
const isFlashDeliveryEnabled = true;
|
||||
|
||||
|
|
@ -114,7 +113,7 @@ export default function AddToCartDialog() {
|
|||
const isUpdate = (cartItem?.quantity || 0) >= 1;
|
||||
|
||||
// Check if flash delivery option should be shown
|
||||
const showFlashOption = productSlotsMap[product?.id]?.isFlashAvailable === true && isFlashDeliveryEnabled;
|
||||
const showFlashOption = product?.isFlashAvailable === true && isFlashDeliveryEnabled;
|
||||
|
||||
const handleAddToCart = () => {
|
||||
if (selectedFlashDelivery) {
|
||||
|
|
|
|||
|
|
@ -1,14 +0,0 @@
|
|||
import React from 'react';
|
||||
import { useInitializeCentralSlotStore } from '@/src/store/centralSlotStore';
|
||||
import { useInitializeCentralProductStore } from '@/src/store/centralProductStore';
|
||||
|
||||
interface CentralStoreInitializerProps {
|
||||
children: React.ReactNode;
|
||||
}
|
||||
|
||||
export default function CentralStoreInitializer({ children }: CentralStoreInitializerProps) {
|
||||
useInitializeCentralSlotStore();
|
||||
useInitializeCentralProductStore();
|
||||
|
||||
return <>{children}</>;
|
||||
}
|
||||
|
|
@ -1,125 +0,0 @@
|
|||
import { useQuery } from '@tanstack/react-query'
|
||||
import axios from 'axios'
|
||||
import { trpc } from '@/src/trpc-client'
|
||||
import { AllProductsApiType, StoresApiType, SlotsApiType, EssentialConstsApiType, BannersApiType, StoreWithProductsApiType } from "@backend/trpc/router";
|
||||
import { CACHE_FILENAMES } from "@packages/shared";
|
||||
|
||||
// Local useGetEssentialConsts hook
|
||||
export const useGetEssentialConsts = () => {
|
||||
const query = trpc.common.essentialConsts.useQuery(undefined, {
|
||||
refetchInterval: 60000,
|
||||
})
|
||||
return { ...query, refetch: query.refetch }
|
||||
}
|
||||
|
||||
type ProductsResponse = AllProductsApiType;
|
||||
type StoresResponse = StoresApiType;
|
||||
type SlotsResponse = SlotsApiType;
|
||||
type EssentialConstsResponse = EssentialConstsApiType;
|
||||
type BannersResponse = BannersApiType;
|
||||
type StoreWithProductsResponse = StoreWithProductsApiType;
|
||||
|
||||
function useCacheUrl(filename: string): string | null {
|
||||
const { data: essentialConsts } = useGetEssentialConsts()
|
||||
|
||||
const assetsDomain = essentialConsts?.assetsDomain
|
||||
const apiCacheKey = essentialConsts?.apiCacheKey
|
||||
|
||||
return assetsDomain && apiCacheKey
|
||||
? `${assetsDomain}${apiCacheKey}/${filename}`
|
||||
: null
|
||||
}
|
||||
|
||||
export function useAllProducts() {
|
||||
const cacheUrl = useCacheUrl(CACHE_FILENAMES.products)
|
||||
|
||||
return useQuery<ProductsResponse>({
|
||||
queryKey: ['all-products', cacheUrl],
|
||||
queryFn: async () => {
|
||||
if (!cacheUrl) {
|
||||
throw new Error('Cache URL not available')
|
||||
}
|
||||
const response = await axios.get<ProductsResponse>(cacheUrl)
|
||||
return response.data
|
||||
},
|
||||
staleTime: 60000, // 1 minute
|
||||
enabled: !!cacheUrl,
|
||||
})
|
||||
}
|
||||
|
||||
export function useStores() {
|
||||
const cacheUrl = useCacheUrl(CACHE_FILENAMES.stores)
|
||||
|
||||
return useQuery<StoresResponse>({
|
||||
queryKey: ['stores', cacheUrl],
|
||||
queryFn: async () => {
|
||||
if (!cacheUrl) {
|
||||
throw new Error('Cache URL not available')
|
||||
}
|
||||
const response = await axios.get<StoresResponse>(cacheUrl)
|
||||
return response.data
|
||||
},
|
||||
staleTime: 60000, // 1 minute
|
||||
enabled: !!cacheUrl,
|
||||
})
|
||||
}
|
||||
|
||||
export function useSlots() {
|
||||
const cacheUrl = useCacheUrl(CACHE_FILENAMES.slots)
|
||||
|
||||
return useQuery<SlotsResponse>({
|
||||
queryKey: ['slots', cacheUrl],
|
||||
queryFn: async () => {
|
||||
if (!cacheUrl) {
|
||||
throw new Error('Cache URL not available')
|
||||
}
|
||||
const response = await axios.get<SlotsResponse>(cacheUrl)
|
||||
return response.data
|
||||
},
|
||||
staleTime: 60000, // 1 minute
|
||||
enabled: !!cacheUrl,
|
||||
})
|
||||
}
|
||||
|
||||
export function useBanners() {
|
||||
const cacheUrl = useCacheUrl(CACHE_FILENAMES.banners)
|
||||
|
||||
return useQuery<BannersResponse>({
|
||||
queryKey: ['banners', cacheUrl],
|
||||
queryFn: async () => {
|
||||
|
||||
if (!cacheUrl) {
|
||||
throw new Error('Cache URL not available')
|
||||
}
|
||||
const response = await axios.get<BannersResponse>(cacheUrl)
|
||||
return response.data
|
||||
|
||||
},
|
||||
staleTime: 60000, // 1 minute
|
||||
enabled: !!cacheUrl,
|
||||
})
|
||||
}
|
||||
|
||||
export function useStoreWithProducts(storeId: number) {
|
||||
const { data: essentialConsts } = useGetEssentialConsts()
|
||||
|
||||
const assetsDomain = essentialConsts?.assetsDomain
|
||||
const apiCacheKey = essentialConsts?.apiCacheKey
|
||||
|
||||
const cacheUrl = assetsDomain && apiCacheKey
|
||||
? `${assetsDomain}${apiCacheKey}/stores/${storeId}.json`
|
||||
: null
|
||||
|
||||
return useQuery<StoreWithProductsResponse>({
|
||||
queryKey: ['store-with-products', storeId, cacheUrl],
|
||||
queryFn: async () => {
|
||||
if (!cacheUrl) {
|
||||
throw new Error('Cache URL not available')
|
||||
}
|
||||
const response = await axios.get<StoreWithProductsResponse>(cacheUrl)
|
||||
return response.data
|
||||
},
|
||||
staleTime: 60000, // 1 minute
|
||||
enabled: !!cacheUrl,
|
||||
})
|
||||
}
|
||||
|
|
@ -1,50 +0,0 @@
|
|||
import { create } from 'zustand'
|
||||
import { useEffect } from 'react'
|
||||
import { useAllProducts } from '@/src/hooks/prominent-api-hooks'
|
||||
import { AllProductsApiType } from '@backend/trpc/router'
|
||||
|
||||
type Product = AllProductsApiType['products'][number]
|
||||
|
||||
interface CentralProductState {
|
||||
products: Product[]
|
||||
productsById: Record<number, Product>
|
||||
refetchProducts: (() => Promise<void>) | null
|
||||
setProducts: (products: Product[]) => void
|
||||
clearProducts: () => void
|
||||
setRefetchProducts: (refetch: () => Promise<void>) => void
|
||||
}
|
||||
|
||||
export const useCentralProductStore = create<CentralProductState>((set) => ({
|
||||
products: [],
|
||||
productsById: {},
|
||||
refetchProducts: null,
|
||||
setProducts: (products) => {
|
||||
const productsById: Record<number, Product> = {}
|
||||
|
||||
products.forEach((product) => {
|
||||
productsById[product.id] = product
|
||||
})
|
||||
|
||||
set({ products, productsById })
|
||||
},
|
||||
clearProducts: () => set({ products: [], productsById: {} }),
|
||||
setRefetchProducts: (refetchProducts) => set({ refetchProducts }),
|
||||
}))
|
||||
|
||||
export function useInitializeCentralProductStore() {
|
||||
const { data: productsData, refetch } = useAllProducts()
|
||||
const setProducts = useCentralProductStore((state) => state.setProducts)
|
||||
const setRefetchProducts = useCentralProductStore((state) => state.setRefetchProducts)
|
||||
|
||||
useEffect(() => {
|
||||
if (productsData?.products) {
|
||||
setProducts(productsData.products)
|
||||
}
|
||||
}, [productsData, setProducts])
|
||||
|
||||
useEffect(() => {
|
||||
setRefetchProducts(async () => {
|
||||
await refetch()
|
||||
})
|
||||
}, [refetch, setRefetchProducts])
|
||||
}
|
||||
|
|
@ -1,71 +0,0 @@
|
|||
import { create } from 'zustand';
|
||||
import { useSlots } from '@/src/hooks/prominent-api-hooks';
|
||||
import { useEffect } from 'react';
|
||||
import { SlotsApiType } from "@backend/trpc/router";
|
||||
|
||||
type Slot = SlotsApiType['slots'][number];
|
||||
type ProductAvailability = SlotsApiType['productAvailability'][number];
|
||||
|
||||
interface ProductSlotInfo {
|
||||
slots: Slot[];
|
||||
isOutOfStock: boolean;
|
||||
isFlashAvailable: boolean;
|
||||
}
|
||||
|
||||
interface CentralSlotState {
|
||||
slots: Slot[];
|
||||
productSlotsMap: Record<number, ProductSlotInfo>;
|
||||
refetchSlots: (() => Promise<void>) | null;
|
||||
setSlotsData: (slots: Slot[], productAvailability: ProductAvailability[]) => void;
|
||||
clearSlotsData: () => void;
|
||||
setRefetchSlots: (refetch: () => Promise<void>) => void;
|
||||
}
|
||||
|
||||
export const useCentralSlotStore = create<CentralSlotState>((set) => ({
|
||||
slots: [],
|
||||
productSlotsMap: {},
|
||||
refetchSlots: null,
|
||||
setSlotsData: (slots, productAvailability) => {
|
||||
const productSlotsMap: Record<number, ProductSlotInfo> = {};
|
||||
|
||||
// First, create entries for ALL products from productAvailability
|
||||
productAvailability.forEach((product) => {
|
||||
productSlotsMap[product.id] = {
|
||||
slots: [],
|
||||
isOutOfStock: product.isOutOfStock,
|
||||
isFlashAvailable: product.isFlashAvailable,
|
||||
};
|
||||
});
|
||||
|
||||
// Then, populate slots for products that appear in delivery slots
|
||||
slots.forEach((slot) => {
|
||||
slot.products?.forEach((product) => {
|
||||
if (productSlotsMap[product.id]) {
|
||||
productSlotsMap[product.id].slots.push(slot);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
set({ slots, productSlotsMap });
|
||||
},
|
||||
clearSlotsData: () => set({ slots: [], productSlotsMap: {} }),
|
||||
setRefetchSlots: (refetchSlots) => set({ refetchSlots }),
|
||||
}));
|
||||
|
||||
export function useInitializeCentralSlotStore() {
|
||||
const { data: slotsData, refetch } = useSlots();
|
||||
const setSlotsData = useCentralSlotStore((state) => state.setSlotsData);
|
||||
const setRefetchSlots = useCentralSlotStore((state) => state.setRefetchSlots);
|
||||
|
||||
useEffect(() => {
|
||||
if (slotsData?.slots) {
|
||||
setSlotsData(slotsData.slots, slotsData.productAvailability || []);
|
||||
}
|
||||
}, [slotsData, setSlotsData]);
|
||||
|
||||
useEffect(() => {
|
||||
setRefetchSlots(async () => {
|
||||
await refetch();
|
||||
});
|
||||
}, [refetch, setRefetchSlots]);
|
||||
}
|
||||
|
|
@ -18,12 +18,6 @@
|
|||
],
|
||||
"common-ui/*": [
|
||||
"../../packages/ui/*"
|
||||
],
|
||||
"@packages/shared": [
|
||||
"../../packages/shared"
|
||||
],
|
||||
"@packages/shared/*": [
|
||||
"../../packages/shared/*"
|
||||
]
|
||||
},
|
||||
"moduleSuffixes": [
|
||||
|
|
@ -40,6 +34,5 @@
|
|||
"**/*.tsx",
|
||||
".expo/types/**/*.ts",
|
||||
"expo-env.d.ts",
|
||||
"../../packages/shared"
|
||||
]
|
||||
}
|
||||
10
package-lock.json
generated
10
package-lock.json
generated
|
|
@ -25,7 +25,6 @@
|
|||
"expo-crypto": "~14.1.5",
|
||||
"expo-server-sdk": "^5.0.0",
|
||||
"expo-web-browser": "~14.2.0",
|
||||
"fuse.js": "^7.1.0",
|
||||
"node-cron": "^4.2.1",
|
||||
"pg": "^8.20.0",
|
||||
"react": "19.0.0",
|
||||
|
|
@ -508,7 +507,6 @@
|
|||
"expo-updates": "~0.28.17",
|
||||
"expo-web-browser": "~14.2.0",
|
||||
"formik": "^2.4.6",
|
||||
"fuse.js": "^7.1.0",
|
||||
"jwt-decode": "^4.0.0",
|
||||
"react": "19.0.0",
|
||||
"react-dom": "19.0.0",
|
||||
|
|
@ -5673,10 +5671,6 @@
|
|||
"node": ">=8.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@packages/shared": {
|
||||
"resolved": "packages/shared",
|
||||
"link": true
|
||||
},
|
||||
"node_modules/@pkgjs/parseargs": {
|
||||
"version": "0.11.0",
|
||||
"resolved": "https://registry.npmjs.org/@pkgjs/parseargs/-/parseargs-0.11.0.tgz",
|
||||
|
|
@ -25545,10 +25539,6 @@
|
|||
}
|
||||
}
|
||||
},
|
||||
"packages/shared": {
|
||||
"name": "@packages/shared",
|
||||
"version": "1.0.0"
|
||||
},
|
||||
"packages/ui": {
|
||||
"name": "common-ui",
|
||||
"version": "1.0.0",
|
||||
|
|
|
|||
|
|
@ -42,7 +42,6 @@
|
|||
"expo-crypto": "~14.1.5",
|
||||
"expo-server-sdk": "^5.0.0",
|
||||
"expo-web-browser": "~14.2.0",
|
||||
"fuse.js": "^7.1.0",
|
||||
"node-cron": "^4.2.1",
|
||||
"pg": "^8.20.0",
|
||||
"react": "19.0.0",
|
||||
|
|
|
|||
|
|
@ -1,9 +0,0 @@
|
|||
export const CACHE_FILENAMES = {
|
||||
products: 'products.json',
|
||||
stores: 'stores.json',
|
||||
slots: 'slots.json',
|
||||
essentialConsts: 'essential-consts.json',
|
||||
banners: 'banners.json',
|
||||
} as const
|
||||
|
||||
export type CacheFilename = typeof CACHE_FILENAMES[keyof typeof CACHE_FILENAMES]
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
{
|
||||
"name": "@packages/shared",
|
||||
"version": "1.0.0",
|
||||
"main": "index.ts",
|
||||
"types": "index.ts",
|
||||
"private": true
|
||||
}
|
||||
|
|
@ -64,9 +64,9 @@ const isDevMode = Constants.executionEnvironment !== "standalone";
|
|||
// const BASE_API_URL = 'http://10.0.2.2:4000';
|
||||
// const BASE_API_URL = 'http://192.168.100.101:4000';
|
||||
// const BASE_API_URL = 'http://192.168.1.5:4000';
|
||||
// let BASE_API_URL = "https://mf.freshyo.in";
|
||||
let BASE_API_URL = "https://mf.freshyo.in";
|
||||
// let BASE_API_URL = "https://freshyo.technocracy.ovh";
|
||||
let BASE_API_URL = 'http://192.168.100.107:4000';
|
||||
// let BASE_API_URL = 'http://192.168.100.107:4000';
|
||||
// let BASE_API_URL = 'http://192.168.29.176:4000';
|
||||
|
||||
// if(isDevMode) {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue