-
Notifications
You must be signed in to change notification settings - Fork 0
feat(add-data): Add modal to import data via file upload #189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
david-bretaud-dev
wants to merge
4
commits into
main
Choose a base branch
from
david-bretaud-dev/feat-add-modal-to-import-new-data
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
71fb87d
feat(add-data): Add new feature to add data via file upload
david-bretaud-dev 639c37d
fix(add-data): Make layer config agnostic
david-bretaud-dev 9be9e7f
fix(append-data): Fix frontend flow
david-bretaud-dev 87cfef3
fix: Improve translations, add bio layer, fix backend
david-bretaud-dev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,205 @@ | ||
| import { AlertCircle, CheckCircle2, UploadIcon } from "lucide-react"; | ||
| import type { FC } from "react"; | ||
|
|
||
| import { useTranslation } from "@i18n"; | ||
|
|
||
| import { Alert, AlertAction, AlertDescription } from "@ui/alert"; | ||
| import { Button } from "@ui/button"; | ||
| import { | ||
| Dialog, | ||
| DialogContent, | ||
| DialogDescription, | ||
| DialogFooter, | ||
| DialogHeader, | ||
| DialogTitle, | ||
| } from "@ui/dialog"; | ||
| import { Input } from "@ui/input"; | ||
| import { Label } from "@ui/label"; | ||
| import { RadioGroup, RadioGroupItem } from "@ui/radio-group"; | ||
| import { | ||
| Select, | ||
| SelectContent, | ||
| SelectItem, | ||
| SelectTrigger, | ||
| SelectValue, | ||
| } from "@ui/select"; | ||
|
|
||
| import { | ||
| ACCEPTED_EXTENSIONS_BY_KIND, | ||
| RESOURCE_KINDS, | ||
| type ResourceKind, | ||
| } from "./constants"; | ||
| import { useAddData } from "./use-add-data"; | ||
|
|
||
| // The value is the catalog folder name | ||
| export type LayerOptions = Array<{ value: string; translation: string }>; | ||
|
|
||
| type AddDataDialogProps = { | ||
| layerOptions: LayerOptions; | ||
| onOpenChange: (open: boolean) => void; | ||
| open: boolean; | ||
| }; | ||
|
|
||
| export const AddDataDialog: FC<AddDataDialogProps> = ({ | ||
| layerOptions, | ||
| onOpenChange, | ||
| open, | ||
| }) => { | ||
| const { t } = useTranslation("common"); | ||
| const { | ||
| error, | ||
| form, | ||
| isLoading, | ||
| isSuccess, | ||
| kind, | ||
| reset, | ||
| setFile, | ||
| setForm, | ||
| setKind, | ||
| submit, | ||
| } = useAddData(); | ||
|
|
||
| const isFormData = kind === RESOURCE_KINDS.FormData; | ||
|
|
||
| const handleOpenChange = (next: boolean) => { | ||
| if (!next) { | ||
| reset(); | ||
| } | ||
| onOpenChange(next); | ||
| }; | ||
|
|
||
| const handleSubmit = (event: React.FormEvent) => { | ||
| event.preventDefault(); | ||
| submit(); | ||
| }; | ||
|
|
||
| return ( | ||
| <Dialog | ||
| onOpenChange={handleOpenChange} | ||
| open={open} | ||
| > | ||
| <DialogContent className="sm:max-w-md"> | ||
| <DialogHeader> | ||
| <DialogTitle>{t("addData.title")}</DialogTitle> | ||
| <DialogDescription>{t("addData.description")}</DialogDescription> | ||
| </DialogHeader> | ||
|
|
||
| <form | ||
| className="flex flex-col gap-6" | ||
| onSubmit={handleSubmit} | ||
| > | ||
| <div className="grid gap-3"> | ||
| <Label htmlFor="add-data-form">{t("addData.field.form")}</Label> | ||
| <Select | ||
| onValueChange={(value) => setForm(value)} | ||
| value={form} | ||
| > | ||
| <SelectTrigger id="add-data-form"> | ||
| <SelectValue placeholder={t("addData.field.formPlaceholder")} /> | ||
| </SelectTrigger> | ||
| <SelectContent> | ||
| {layerOptions.map(({ value, translation }) => ( | ||
| <SelectItem | ||
| key={value} | ||
| value={value} | ||
| > | ||
| {translation} | ||
| </SelectItem> | ||
| ))} | ||
| </SelectContent> | ||
| </Select> | ||
| </div> | ||
|
|
||
| <div className="grid gap-3"> | ||
| <Label>{t("addData.field.kind")}</Label> | ||
| <RadioGroup | ||
| className="flex gap-6" | ||
| onValueChange={(value) => setKind(value as ResourceKind)} | ||
| value={kind} | ||
| > | ||
| <div className="flex items-center gap-3"> | ||
| <RadioGroupItem | ||
| id="add-data-kind-form" | ||
| value={RESOURCE_KINDS.FormData} | ||
| /> | ||
| <Label | ||
| className="font-normal" | ||
| htmlFor="add-data-kind-form" | ||
| > | ||
| {t("addData.kind.formData")} | ||
| </Label> | ||
| </div> | ||
| <div className="flex items-center gap-3"> | ||
| <RadioGroupItem | ||
| id="add-data-kind-external" | ||
| value={RESOURCE_KINDS.ExternalData} | ||
| /> | ||
| <Label | ||
| className="font-normal" | ||
| htmlFor="add-data-kind-external" | ||
| > | ||
| {t("addData.kind.externalData")} | ||
| </Label> | ||
| </div> | ||
| </RadioGroup> | ||
| </div> | ||
|
|
||
| <div className="grid gap-3"> | ||
| <Label htmlFor="add-data-file">{t("addData.field.file")}</Label> | ||
| <Input | ||
| accept={ACCEPTED_EXTENSIONS_BY_KIND[kind].join(",")} | ||
| className="hover:cursor-pointer" | ||
| id="add-data-file" | ||
| onChange={(event) => setFile(event.target.files?.[0] ?? null)} | ||
| type="file" | ||
| /> | ||
| <p className="text-sm text-muted-foreground"> | ||
| {isFormData | ||
| ? t("addData.hint.formData") | ||
| : t("addData.hint.externalData")} | ||
| </p> | ||
| </div> | ||
|
|
||
| {error && ( | ||
| <Alert variant="destructive"> | ||
| <AlertCircle className="h-4 w-4" /> | ||
| <AlertDescription style={{ "--tw-translate-y": 0 }}> | ||
| {t(`addData.error.${error}`)} | ||
| </AlertDescription> | ||
| </Alert> | ||
| )} | ||
|
|
||
| {isSuccess && ( | ||
| <Alert variant="success"> | ||
| <CheckCircle2 className="h-4 w-4" /> | ||
| <AlertDescription style={{ "--tw-translate-y": 0 }}> | ||
| {t("addData.success")} | ||
| </AlertDescription> | ||
| <AlertAction> | ||
| <Button | ||
| onClick={() => window.location.reload()} | ||
| size="sm" | ||
| variant="default" | ||
| > | ||
| {t("addData.refreshData")} | ||
| </Button> | ||
| </AlertAction> | ||
| </Alert> | ||
| )} | ||
|
|
||
| <DialogFooter> | ||
| <Button | ||
| disabled={isLoading} | ||
| type="submit" | ||
| > | ||
| <UploadIcon /> | ||
| {isLoading | ||
| ? t("addData.button.pending") | ||
| : t("addData.button.submit")} | ||
| </Button> | ||
| </DialogFooter> | ||
| </form> | ||
| </DialogContent> | ||
| </Dialog> | ||
| ); | ||
| }; |
This file contains hidden or 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,31 @@ | ||
| // Kind of resource the uploaded file represents. | ||
| export const RESOURCE_KINDS = { | ||
| ExternalData: "external_data", | ||
| FormData: "form_data", | ||
| } as const; | ||
|
|
||
| export type ResourceKind = (typeof RESOURCE_KINDS)[keyof typeof RESOURCE_KINDS]; | ||
|
|
||
| // coordo loader the backend selects, per resource kind. cf LoaderType | ||
| export const RESOURCE_TYPE_BY_KIND = { | ||
| [RESOURCE_KINDS.ExternalData]: "file", | ||
| [RESOURCE_KINDS.FormData]: "kobotoolbox", | ||
| } as const; | ||
|
|
||
| // Accepted upload extensions per kind (coordo's file / kobotoolbox loaders). | ||
| // Form data is the multi-sheet KoboToolbox answers workbook (.xlsx). | ||
| export const ACCEPTED_EXTENSIONS_BY_KIND = { | ||
| [RESOURCE_KINDS.ExternalData]: [ | ||
| ".csv", | ||
| ".tsv", | ||
| ".tab", | ||
| ".xlsx", | ||
| ".xls", | ||
| ".xlsm", | ||
| ".xlsb", | ||
| ".odf", | ||
| ".ods", | ||
| ".odt", | ||
| ], | ||
| [RESOURCE_KINDS.FormData]: [".csv", ".xlsx"], | ||
| } as const; |
This file contains hidden or 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 @@ | ||
| export { AddDataDialog, type LayerOptions } from "./add-data-dialog"; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.