Skip to content

Commit dd122fa

Browse files
committed
try to fix csv import
1 parent e6b9fce commit dd122fa

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

src/pages/admin/festivals/CSVImportDialog.tsx

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ import { Input } from "@/components/ui/input";
1313
import { Label } from "@/components/ui/label";
1414
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
1515
import { Progress } from "@/components/ui/progress";
16+
import {
17+
Select,
18+
SelectContent,
19+
SelectItem,
20+
SelectTrigger,
21+
SelectValue,
22+
} from "@/components/ui/select";
1623
import { Upload, FileText, Loader2 } from "lucide-react";
1724
import {
1825
importStages,
@@ -38,6 +45,7 @@ export function CSVImportDialog({
3845
const [isImporting, setIsImporting] = useState(false);
3946
const [stagesFile, setStagesFile] = useState<File | null>(null);
4047
const [setsFile, setSetsFile] = useState<File | null>(null);
48+
const [timezone, setTimezone] = useState("Europe/Lisbon");
4149
const [progress, setProgress] = useState({ current: 0, total: 0, label: "" });
4250
const { toast } = useToast();
4351
const queryClient = useQueryClient();
@@ -114,6 +122,7 @@ export function CSVImportDialog({
114122
const setsResult = await importSets(
115123
setsData,
116124
editionId,
125+
timezone,
117126
(current, total) => {
118127
setProgress({
119128
current,
@@ -223,6 +232,29 @@ export function CSVImportDialog({
223232
</div>
224233
)}
225234
</div>
235+
<div className="space-y-2">
236+
<Label htmlFor="timezone-select">Timezone</Label>
237+
<Select value={timezone} onValueChange={setTimezone}>
238+
<SelectTrigger>
239+
<SelectValue placeholder="Select timezone" />
240+
</SelectTrigger>
241+
<SelectContent>
242+
<SelectItem value="Europe/Lisbon">Europe/Lisbon</SelectItem>
243+
<SelectItem value="Europe/Madrid">Europe/Madrid</SelectItem>
244+
<SelectItem value="Europe/London">Europe/London</SelectItem>
245+
<SelectItem value="America/New_York">
246+
America/New_York
247+
</SelectItem>
248+
<SelectItem value="America/Los_Angeles">
249+
America/Los_Angeles
250+
</SelectItem>
251+
<SelectItem value="UTC">UTC</SelectItem>
252+
</SelectContent>
253+
</Select>
254+
<p className="text-xs text-muted-foreground">
255+
Select the timezone that the CSV times are in
256+
</p>
257+
</div>
226258
<p className="text-sm text-muted-foreground">
227259
Expected columns: artist_names, stage_name, name (optional),
228260
time_start (optional), time_end (optional), description

src/services/csvImportService.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { supabase } from "@/integrations/supabase/client";
22
import { generateSlug } from "@/lib/slug";
3+
import { fromZonedTime } from "date-fns-tz";
34

45
export interface StageImportData {
56
name: string;
@@ -14,6 +15,20 @@ export interface SetImportData {
1415
description?: string;
1516
}
1617

18+
// Helper function to convert local time string to UTC for database storage
19+
function convertLocalTimeToUTC(
20+
timeString: string | undefined,
21+
timezone: string,
22+
): string | null {
23+
if (!timeString) return null;
24+
25+
// Parse the time string and interpret it as being in the specified timezone
26+
// First create a date object assuming the time is in the target timezone
27+
const utcDate = fromZonedTime(timeString, timezone);
28+
29+
return utcDate.toISOString();
30+
}
31+
1732
export interface ImportResult {
1833
success: boolean;
1934
message: string;
@@ -137,6 +152,7 @@ export async function importStages(
137152
export async function importSets(
138153
sets: SetImportData[],
139154
editionId: string,
155+
timezone: string = "UTC",
140156
onProgress?: (completed: number, total: number) => void,
141157
): Promise<ImportResult> {
142158
try {
@@ -227,6 +243,10 @@ export async function importSets(
227243
.eq("festival_edition_id", editionId)
228244
.limit(1);
229245

246+
// Convert times to UTC for database storage
247+
const utcTimeStart = convertLocalTimeToUTC(set.time_start, timezone);
248+
const utcTimeEnd = convertLocalTimeToUTC(set.time_end, timezone);
249+
230250
let createdSetId = "";
231251
let setError;
232252

@@ -236,8 +256,8 @@ export async function importSets(
236256
const { error } = await supabase
237257
.from("sets")
238258
.update({
239-
time_start: set.time_start || null,
240-
time_end: set.time_end || null,
259+
time_start: utcTimeStart,
260+
time_end: utcTimeEnd,
241261
description: set.description || null,
242262
archived: false,
243263
})
@@ -255,8 +275,8 @@ export async function importSets(
255275
slug: generateSlug(setName),
256276
stage_id: stage.id,
257277
festival_edition_id: editionId,
258-
time_start: set.time_start || null,
259-
time_end: set.time_end || null,
278+
time_start: utcTimeStart,
279+
time_end: utcTimeEnd,
260280
description: set.description || null,
261281
archived: false,
262282
created_by: userId,

0 commit comments

Comments
 (0)