-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: coupons import csv with hardcoded offer_id
- Loading branch information
1 parent
a0c9ab3
commit a2492f1
Showing
5 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
code,validityTo | ||
H1D1,01/06/24 | ||
H1D2,02/06/24 | ||
H1D3,03/06/24 | ||
H1D4,04/06/24 | ||
H1D5,05/06/24 | ||
H1D6,06/06/24 | ||
H1D7,07/06/24 | ||
H1D8,08/06/24 | ||
H1D9,09/06/24 | ||
H1D10,10/06/24 | ||
H1D11,11/06/24 | ||
H1D12,12/06/24 | ||
H1D13,13/06/24 | ||
H1D14,14/06/24 | ||
H1D15,15/06/24 | ||
H1D16,16/06/24 | ||
H1D17,17/06/24 | ||
H1D18,18/06/24 | ||
H1D19,19/06/24 | ||
H1D20,20/06/24 | ||
H1D21,21/06/24 | ||
H1D22,22/06/24 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { Box, Button } from "@chakra-ui/react"; | ||
import Papa from "papaparse"; | ||
import type { Props } from "payload/components/views/List"; | ||
import React, { useCallback } from "react"; | ||
import { api } from "~/utils/api"; | ||
import { convertFrenchDateToEnglish } from "~/utils/tools"; | ||
|
||
export type ImportCouponsProps = {}; | ||
|
||
const ImportCoupons = ({ hasCreatePermission, resetParams }: Props) => { | ||
if (!hasCreatePermission) return; | ||
|
||
const { mutate: createCoupons } = api.coupon.create.useMutation({ | ||
onSuccess() { | ||
resetParams(); | ||
}, | ||
}); | ||
|
||
const handleFileChange = useCallback( | ||
(event: React.ChangeEvent<HTMLInputElement>) => { | ||
const file = event.target.files?.[0]; | ||
|
||
if (file) { | ||
Papa.parse(file, { | ||
complete: (result) => { | ||
createCoupons( | ||
result.data | ||
.filter( | ||
(row: any) => | ||
row.code && | ||
row.validityTo && | ||
convertFrenchDateToEnglish(row.validityTo) | ||
) | ||
.map((row: any) => { | ||
const englishDate = convertFrenchDateToEnglish( | ||
row.validityTo | ||
); | ||
|
||
return { | ||
code: row.code, | ||
validityTo: new Date(englishDate as string).toISOString(), | ||
status: "available", | ||
offer: 1, | ||
}; | ||
}) | ||
); | ||
}, | ||
header: true, // Si votre CSV a une ligne d'en-tête | ||
dynamicTyping: true, // Convertit automatiquement les valeurs en types appropriés | ||
}); | ||
} | ||
}, | ||
[] | ||
); | ||
|
||
return ( | ||
<Box> | ||
<input | ||
id="csvInput" | ||
type="file" | ||
accept=".csv" | ||
onChange={handleFileChange} | ||
style={{ display: "none" }} | ||
/> | ||
<Button as="label" htmlFor="csvInput" cursor="pointer"> | ||
Importer un CSV | ||
</Button> | ||
</Box> | ||
); | ||
}; | ||
|
||
export default ImportCoupons; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export const convertFrenchDateToEnglish = ( | ||
frenchDate: string | ||
): string | null => { | ||
const match = frenchDate.match(/^(\d{2})\/(\d{2})\/(\d{2})$/); | ||
|
||
if (match) { | ||
const [, day, month, year] = match.map(Number); | ||
|
||
if (year !== undefined && month !== undefined && day !== undefined) { | ||
// Creating a Date object with a four-digit year | ||
const englishFormattedDate = new Date(2000 + year, month - 1, day); | ||
|
||
// Using toISOString to get the date in ISO format (YYYY-MM-DD) | ||
return englishFormattedDate.toISOString().split("T")[0] || null; | ||
} | ||
} | ||
|
||
// Return null if the input format is incorrect | ||
return null; | ||
}; |