import React from 'react';
import { View, Alert } from 'react-native';
import { useRouter, useLocalSearchParams } from 'expo-router';
import { AppContainer, MyText, tw } from 'common-ui';
import StoreForm, { StoreFormData } from '@/components/StoreForm';
import { trpc } from '@/src/trpc-client';
export default function EditStore() {
const router = useRouter();
const { id } = useLocalSearchParams();
const storeId = parseInt(id as string);
const { data: storeData, isLoading: isLoadingStore, refetch } = trpc.admin.store.getStoreById.useQuery(
{ id: storeId },
{ enabled: !!storeId }
);
const updateStoreMutation = trpc.admin.store.updateStore.useMutation();
const handleSubmit = (values: StoreFormData) => {
updateStoreMutation.mutate({ id: storeId, ...values }, {
onSuccess: (data) => {
refetch();
Alert.alert('Success', data.message);
router.push('/(drawer)/stores' as any);
},
onError: (error: any) => {
Alert.alert('Error', error.message || 'Failed to update store');
},
});
};
if (isLoadingStore) {
return (
Loading store...
);
}
if (!storeData?.store) {
return (
Store not found
);
}
const initialValues = {
name: storeData.store.name,
description: storeData.store.description || '',
imageUrl: storeData.store.imageUrl || '',
owner: storeData.store.owner.id,
products: [], // Will be set by StoreForm
};
return (
Edit Store
);
}