Skip to content

Commit

Permalink
Add upload list and fetch metadata for selected uploaded items
Browse files Browse the repository at this point in the history
  • Loading branch information
AdityaKhatri committed Feb 26, 2024
1 parent 482dc9b commit 4fec259
Show file tree
Hide file tree
Showing 10 changed files with 600 additions and 130 deletions.
23 changes: 19 additions & 4 deletions app/components/GalleryFileUpload/index.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import React, { useCallback } from 'react';
import React, { useState, useCallback } from 'react';
import { FileInput, useAlert } from '@the-deep/deep-ui';
import { IoCloudUpload } from 'react-icons/io5';
import { gql, useMutation } from '@apollo/client';
import { removeNull } from '@togglecorp/toggle-form';
import { _cs } from '@togglecorp/fujs';

import {
FileUploadMutation,
FileUploadMutationVariables,
GalleryFileType,
} from '#generated/types';

import styles from './styles.css';

const UPLOAD_FILE = gql`
mutation FileUpload(
$data: FileUploadInputType!,
Expand All @@ -31,11 +34,12 @@ const UPLOAD_FILE = gql`
}
`;
interface Props {
onSuccess: (file: NonNullable<GalleryFileType>) => void;
acceptFileType?: '.pdf' | 'image/*';
onSuccess: (file: NonNullable<GalleryFileType>, uploadedFile: File | undefined) => void;
acceptFileType?: '.pdf' | 'image/*' | '.xlsx' | '.csv';
title?: string;
disabled?: boolean;
projectIds?: string[];
buttonOnly?: boolean;
}

function FileUpload(props: Props) {
Expand All @@ -45,8 +49,11 @@ function FileUpload(props: Props) {
acceptFileType,
disabled,
projectIds,
buttonOnly,
} = props;

const alert = useAlert();
const [uploadedFile, setUploadedFile] = useState<File>();

const [
uploadFile,
Expand Down Expand Up @@ -76,6 +83,7 @@ function FileUpload(props: Props) {
const resultRemoveNull = removeNull(result);
onSuccess(
resultRemoveNull,
uploadedFile,
);
}
},
Expand All @@ -93,6 +101,7 @@ function FileUpload(props: Props) {
if (!value) {
return;
}
setUploadedFile(value);

uploadFile({
variables: {
Expand All @@ -115,17 +124,23 @@ function FileUpload(props: Props) {

return (
<FileInput
className={_cs(
styles.galleryFileUpload,
buttonOnly && styles.buttonOnly,
)}
name={undefined}
value={null}
onChange={handleFileInputChange}
status={undefined}
overrideStatus
title={title}
title={buttonOnly ? undefined : title}
label={buttonOnly ? undefined : title}
maxFileSize={100}
accept={acceptFileType}
disabled={loading || disabled}
>
<IoCloudUpload />
{buttonOnly ? title : undefined}
</FileInput>
);
}
Expand Down
5 changes: 5 additions & 0 deletions app/components/GalleryFileUpload/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.gallery-file-upload {
&.button-only {
--height-border: 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React from 'react';
import { _cs } from '@togglecorp/fujs';
import {
RawButton,
} from '@the-deep/deep-ui';

import styles from './styles.css';

interface Props {
datasetId: string;
title: string;
active: boolean;
onClick: (datasetId: string) => void;
}

function DatasetItem(props: Props) {
const {
title,
datasetId,
onClick,
active,
} = props;

return (
<RawButton
name={datasetId}
onClick={onClick}
className={_cs(
styles.frameworkItem,
active && styles.selected,
)}
>
<div className={styles.title}>
{title}
</div>
</RawButton>
);
}

export default DatasetItem;
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.framework-item {
display: flex;
flex-direction: column;
border-bottom: var(--dui-width-separator-thin) solid var(--dui-color-separator);
background-color: var(--dui-color-background-information);
padding: var(--dui-spacing-small) var(--dui-spacing-medium);
width: 100%;
text-align: left;
gap: var(--dui-spacing-small);

.title {
font-weight: var(--dui-font-weight-bold);
}

&.selected {
background-color: var(--dui-color-surface-information);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';

import {
ExpandableContainer,
Heading,
TextOutput,
SelectInput,
} from '@the-deep/deep-ui';
Expand Down Expand Up @@ -59,13 +59,17 @@ interface Props {
index: number,
) => void | undefined;
index: number;
disabled?: boolean;
readOnly?: boolean;
}

function VariableItem(props: Props) {
const {
column,
setVariableValue,
index,
disabled,
readOnly,
} = props;

const setFieldValue = useFormObject(
Expand All @@ -83,30 +87,31 @@ function VariableItem(props: Props) {
?.enums?.AnalysisReportVariableSerializerType;

return (
<ExpandableContainer
key={column.clientId}
heading={column.name}
headingClassName={styles.columnHeading}
className={styles.expandableContainer}
headerClassName={styles.columnHeader}
spacing="compact"
headingSize="extraSmall"
contentClassName={styles.columnContent}
>
<div className={styles.variableItem}>
<Heading
className={styles.heading}
size="extraSmall"
>
{column.name}
</Heading>
<TextOutput
className={styles.heading}
label="Completeness"
value="90%"
/>
<SelectInput
className={styles.heading}
name="type"
options={columnDataTypeOptions ?? undefined}
label="Column data type"
keySelector={enumKeySelector}
labelSelector={enumLabelSelector}
value={column.type}
onChange={setFieldValue}
disabled={disabled}
readOnly={readOnly}
/>
</ExpandableContainer>
</div>
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
.expandable-container {
border: none;
.variable-item {
display: flex;
align-items: flex-end;
gap: var(--dui-spacing-extra-small);

.header-description {
display: flex;
gap: var(--dui-spacing-small);
.heading {
width: 10rem;
}
}
.column-header {
background-color: transparent;
color: var(--dui-color-text);

.column-heading {
color: var(--dui-color-text);
.completeness {
width: 10rem;
}
.type {
width: 20rem;
}
}
.column-content {
display: flex;
gap: var(--dui-spacing-medium);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import {
useFormObject,
useFormArray,
} from '@togglecorp/toggle-form';
import {
utils,
type WorkSheet,
} from 'xlsx';

import VariableItem from './VariableItem';
import { type SheetType, type VariableType } from '..';
Expand All @@ -26,6 +30,9 @@ interface Props {
index: number
) => void;
index: number;
workSheet: WorkSheet | undefined;
disabled?: boolean;
readOnly?: boolean;
}

const defaultSheetItem = (): SheetType => ({
Expand All @@ -36,7 +43,10 @@ function SheetItem(props: Props) {
const {
item,
setSheetValue,
workSheet,
index,
disabled,
readOnly,
} = props;

const setFieldValue = useFormObject(
Expand All @@ -45,6 +55,23 @@ function SheetItem(props: Props) {
defaultSheetItem,
);

const handleHeaderRowChange = useCallback((newHeaderRow: number | undefined) => {
setFieldValue(newHeaderRow, 'headerRow');
if (!workSheet || !newHeaderRow) {
return;
}
const rawData = utils.sheet_to_json(workSheet, { header: 1 });
const columns = (rawData[newHeaderRow - 1] as string[]).map((rawItem) => ({
clientId: randomString(),
name: rawItem,
type: 'TEXT' as const,
}));
setFieldValue(columns, 'variables');
}, [
setFieldValue,
workSheet,
]);

const {
setValue: setVariableValue,
} = useFormArray('variables', setFieldValue);
Expand All @@ -58,7 +85,11 @@ function SheetItem(props: Props) {
column: datum,
setVariableValue,
index: variableIndex,
disabled,
readOnly,
}), [
disabled,
readOnly,
setVariableValue,
],
);
Expand All @@ -78,12 +109,16 @@ function SheetItem(props: Props) {
label="Name"
value={item?.name}
onChange={setFieldValue}
disabled={disabled}
readOnly
/>
<NumberInput
name="headerRow"
label="Header Row"
value={item.headerRow}
onChange={setFieldValue}
onChange={handleHeaderRowChange}
disabled={disabled}
readOnly={readOnly}
/>
</>
)}
Expand All @@ -97,6 +132,7 @@ function SheetItem(props: Props) {
filtered={false}
errored={false}
pending={false}
borderBetweenItem
/>
</ExpandableContainer>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
.variables {
display: flex;
flex-direction: column;
gap: var(--dui-spacing-small);
flex-shrink: 0;
gap: var(--dui-spacing-extra-small);
}
Loading

0 comments on commit 4fec259

Please sign in to comment.