mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-24 10:51:13 -05:00
Add robust error handling for image uploads, improve navigation logic in AutocompleteSearch with toast notifications for missing identifiers, refine useIsMobile hook return type, and update Supabase function error reporting to handle non-Error types. Replit-Commit-Author: Agent Replit-Commit-Session-Id: a759d451-40bf-440d-96f5-a19ad6af18a8 Replit-Commit-Checkpoint-Type: intermediate_checkpoint
152 lines
4.5 KiB
TypeScript
152 lines
4.5 KiB
TypeScript
import { supabase } from '@/integrations/supabase/client';
|
|
import type { Json } from '@/integrations/supabase/types';
|
|
import { ImageAssignments } from '@/components/upload/EntityMultiImageUploader';
|
|
import { uploadPendingImages } from './imageUploadHelper';
|
|
|
|
export interface CompanyFormData {
|
|
name: string;
|
|
slug: string;
|
|
description?: string;
|
|
person_type: 'company' | 'individual' | 'firm' | 'organization';
|
|
website_url?: string;
|
|
founded_year?: number;
|
|
headquarters_location?: string;
|
|
images?: ImageAssignments;
|
|
}
|
|
|
|
export async function submitCompanyCreation(
|
|
data: CompanyFormData,
|
|
companyType: 'manufacturer' | 'designer' | 'operator' | 'property_owner',
|
|
userId: string
|
|
) {
|
|
// Upload any pending local images first
|
|
let processedImages = data.images;
|
|
if (data.images?.uploaded && data.images.uploaded.length > 0) {
|
|
try {
|
|
const uploadedImages = await uploadPendingImages(data.images.uploaded);
|
|
processedImages = {
|
|
...data.images,
|
|
uploaded: uploadedImages
|
|
};
|
|
} catch (error) {
|
|
console.error(`Failed to upload images for ${companyType} creation:`, error);
|
|
throw new Error('Failed to upload images. Please check your connection and try again.');
|
|
}
|
|
}
|
|
|
|
// Create the main submission record
|
|
const { data: submissionData, error: submissionError } = await supabase
|
|
.from('content_submissions')
|
|
.insert({
|
|
user_id: userId,
|
|
submission_type: companyType,
|
|
content: {
|
|
action: 'create'
|
|
},
|
|
status: 'pending'
|
|
})
|
|
.select()
|
|
.single();
|
|
|
|
if (submissionError) throw submissionError;
|
|
|
|
// Create the submission item with actual company data
|
|
const { error: itemError } = await supabase
|
|
.from('submission_items')
|
|
.insert({
|
|
submission_id: submissionData.id,
|
|
item_type: companyType,
|
|
item_data: {
|
|
name: data.name,
|
|
slug: data.slug,
|
|
description: data.description,
|
|
person_type: data.person_type,
|
|
website_url: data.website_url,
|
|
founded_year: data.founded_year,
|
|
headquarters_location: data.headquarters_location,
|
|
company_type: companyType,
|
|
images: processedImages as unknown as Json
|
|
},
|
|
status: 'pending',
|
|
order_index: 0
|
|
});
|
|
|
|
if (itemError) throw itemError;
|
|
|
|
return { submitted: true, submissionId: submissionData.id };
|
|
}
|
|
|
|
export async function submitCompanyUpdate(
|
|
companyId: string,
|
|
data: CompanyFormData,
|
|
userId: string
|
|
) {
|
|
// Fetch existing company data (all fields for original_data)
|
|
const { data: existingCompany, error: fetchError } = await supabase
|
|
.from('companies')
|
|
.select('*')
|
|
.eq('id', companyId)
|
|
.single();
|
|
|
|
if (fetchError) throw fetchError;
|
|
if (!existingCompany) throw new Error('Company not found');
|
|
|
|
// Upload any pending local images first
|
|
let processedImages = data.images;
|
|
if (data.images?.uploaded && data.images.uploaded.length > 0) {
|
|
try {
|
|
const uploadedImages = await uploadPendingImages(data.images.uploaded);
|
|
processedImages = {
|
|
...data.images,
|
|
uploaded: uploadedImages
|
|
};
|
|
} catch (error) {
|
|
console.error(`Failed to upload images for ${existingCompany.company_type} update:`, error);
|
|
throw new Error('Failed to upload images. Please check your connection and try again.');
|
|
}
|
|
}
|
|
|
|
// Create the main submission record
|
|
const { data: submissionData, error: submissionError } = await supabase
|
|
.from('content_submissions')
|
|
.insert({
|
|
user_id: userId,
|
|
submission_type: existingCompany.company_type,
|
|
content: {
|
|
action: 'edit',
|
|
company_id: companyId
|
|
},
|
|
status: 'pending'
|
|
})
|
|
.select()
|
|
.single();
|
|
|
|
if (submissionError) throw submissionError;
|
|
|
|
// Create the submission item with actual company data AND original data
|
|
const { error: itemError } = await supabase
|
|
.from('submission_items')
|
|
.insert({
|
|
submission_id: submissionData.id,
|
|
item_type: existingCompany.company_type,
|
|
item_data: {
|
|
company_id: companyId,
|
|
name: data.name,
|
|
slug: data.slug,
|
|
description: data.description,
|
|
person_type: data.person_type,
|
|
website_url: data.website_url,
|
|
founded_year: data.founded_year,
|
|
headquarters_location: data.headquarters_location,
|
|
images: processedImages as unknown as Json
|
|
},
|
|
original_data: JSON.parse(JSON.stringify(existingCompany)),
|
|
status: 'pending',
|
|
order_index: 0
|
|
});
|
|
|
|
if (itemError) throw itemError;
|
|
|
|
return { submitted: true, submissionId: submissionData.id };
|
|
}
|