Skip to content

Commit

Permalink
media and upload of image working
Browse files Browse the repository at this point in the history
  • Loading branch information
brentkulwicki committed Sep 25, 2023
1 parent 57b4805 commit fcda48c
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 26 deletions.
19 changes: 19 additions & 0 deletions src/scenes/ProgressReports/EditForm/ProgressReportEdit.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,26 @@ fragment ProgressReportEdit on ProgressReport {
}
media {
items {
variant {
key
label
responsibleRole
}
variantGroup
id
category
media {
altText
caption
file {
id
url
name
mimeType
type
size
}
}
}
uploadableVariants {
key
Expand Down
22 changes: 18 additions & 4 deletions src/scenes/ProgressReports/EditForm/Steps/Media/MediaInfoForm.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
import { Box } from '@mui/material';
import { Box, TextField } from '@mui/material';
import {
ProgressReportMediaCategoryLabels,
ProgressReportMediaCategoryList,
} from '~/api/schema/enumLists';
import { labelFrom } from '~/common';
import { Form, SelectField, TextField } from '~/components/form';
import { Form, SelectField } from '~/components/form';
import { minLength, required } from '~/components/form/validators';

Check warning on line 8 in src/scenes/ProgressReports/EditForm/Steps/Media/MediaInfoForm.tsx

View workflow job for this annotation

GitHub Actions / run

'minLength' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 8 in src/scenes/ProgressReports/EditForm/Steps/Media/MediaInfoForm.tsx

View workflow job for this annotation

GitHub Actions / run

'required' is defined but never used. Allowed unused vars must match /^_/u
import { useCallback, useState } from 'react';

Check warning on line 9 in src/scenes/ProgressReports/EditForm/Steps/Media/MediaInfoForm.tsx

View workflow job for this annotation

GitHub Actions / run

`react` import should occur before import of `~/api/schema/enumLists`
import { ProgressReportMediaCategory } from '~/api/schema.graphql';

Check warning on line 10 in src/scenes/ProgressReports/EditForm/Steps/Media/MediaInfoForm.tsx

View workflow job for this annotation

GitHub Actions / run

`~/api/schema.graphql` import should occur before import of `~/api/schema/enumLists`

// TODO type submitForm
// @ts-ignore

Check failure on line 13 in src/scenes/ProgressReports/EditForm/Steps/Media/MediaInfoForm.tsx

View workflow job for this annotation

GitHub Actions / run

Do not use "@ts-ignore" because it alters compilation errors
export const MediaInfoForm = ({
submitForm,
disabled,
initialValues,
}: {
submitForm: any;
disabled: boolean;
initialValues: {
category: ProgressReportMediaCategory | null;
caption: string | null;
};
}) => {
const [category, setCategory] = useState(initialValues.category);

Check warning on line 26 in src/scenes/ProgressReports/EditForm/Steps/Media/MediaInfoForm.tsx

View workflow job for this annotation

GitHub Actions / run

'setCategory' is assigned a value but never used. Allowed unused vars must match /^_/u
const [caption, setCaption] = useState(initialValues.caption);

Check warning on line 27 in src/scenes/ProgressReports/EditForm/Steps/Media/MediaInfoForm.tsx

View workflow job for this annotation

GitHub Actions / run

'caption' is assigned a value but never used. Allowed unused vars must match /^_/u

Check warning on line 27 in src/scenes/ProgressReports/EditForm/Steps/Media/MediaInfoForm.tsx

View workflow job for this annotation

GitHub Actions / run

'setCaption' is assigned a value but never used. Allowed unused vars must match /^_/u
const updateMedia = useCallback(() => {

Check warning on line 28 in src/scenes/ProgressReports/EditForm/Steps/Media/MediaInfoForm.tsx

View workflow job for this annotation

GitHub Actions / run

'updateMedia' is assigned a value but never used. Allowed unused vars must match /^_/u
submitForm({ variables: {} });
}, []);

Check warning on line 30 in src/scenes/ProgressReports/EditForm/Steps/Media/MediaInfoForm.tsx

View workflow job for this annotation

GitHub Actions / run

React Hook useCallback has a missing dependency: 'submitForm'. Either include it or remove the dependency array. If 'submitForm' changes too often, find the parent component that defines it and wrap that definition in useCallback
return (
<Form onSubmit={submitForm}>
<Box sx={{ p: 1, m: 1, flexGrow: 2 }}>
Expand All @@ -26,7 +38,9 @@ export const MediaInfoForm = ({
options={ProgressReportMediaCategoryList}
variant="outlined"
getOptionLabel={labelFrom(ProgressReportMediaCategoryLabels)}
defaultValue={disabled ? null : ProgressReportMediaCategoryList[0]}
defaultValue={
disabled ? null : category || ProgressReportMediaCategoryList[0]
}
/>
<TextField
variant="outlined"
Expand All @@ -35,8 +49,8 @@ export const MediaInfoForm = ({
disabled={disabled}
placeholder="Enter Photo Caption"
minRows={4}
validate={[required, minLength()]}
margin="none"
defaultValue={initialValues?.caption}

Check warning on line 53 in src/scenes/ProgressReports/EditForm/Steps/Media/MediaInfoForm.tsx

View workflow job for this annotation

GitHub Actions / run

Unnecessary optional chain on a non-nullish value
/>
</Box>
</Form>
Expand Down
31 changes: 31 additions & 0 deletions src/scenes/ProgressReports/EditForm/Steps/Media/MediaStep.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
mutation CreateMedia($input: UploadProgressReportMedia!) {
uploadProgressReportMedia(input: $input) {
id
media {
items {
id
variant {
key
label
responsibleRole
}
category
}
}
}
}

mutation UpdateMedia($input: UpdateProgressReportMedia!) {
updateProgressReportMedia(input: $input) {
id
media {
altText
caption
id
mimeType
url
}
}
}
# placeholder for delete
# placeholder for download
60 changes: 42 additions & 18 deletions src/scenes/ProgressReports/EditForm/Steps/Media/MediaStep.tsx
Original file line number Diff line number Diff line change
@@ -1,43 +1,58 @@
import { useMutation } from '@apollo/client';
import { Box, Typography } from '@mui/material';
import React from 'react';
import { useState } from 'react';
import { Fragment } from 'react';
import { useDropzone } from 'react-dropzone';
import { useUploadFileAsync } from '~/components/files/hooks';
import { DropOverlay } from '~/components/Upload/DropOverlay';
import { StepComponent } from '../step.types';
import { CreateMediaDocument, UpdateMediaDocument } from './MediaStep.graphql';
import { VariantMediaAccordion } from './VariantMediaAccordion';

// TODO find types for actual uploadVariant here
export interface MediaVariantResponse {
item?: string | undefined;
item: any[];
uploadVariant: any;
}
export const MediaStep: StepComponent = ({ report }) => {
// TODO need to replace this with the actual file version and upload
const [currentFile, setCurrentFile] = useState('');
const [createMedia] = useMutation(CreateMediaDocument);
const [updateMedia] = useMutation(UpdateMediaDocument);
const uploadFile = useUploadFileAsync();
//
// const handleFileUpload = async () => {
// console.log('handleFileUpload')
// }

// TODO remove this before going live
console.log(report);
const handleDrop = (files: File[]) => {
console.log(`file dropped`);
console.log(files);
// uploadFile({ files });
const handleFileUpload = async (files: File[]) => {
const [uploadedImageInfo, finalizeUpload] = await uploadFile(files[0]);
if (!uploadedImageInfo) {
finalizeUpload.tap;
return;
}
await createMedia({
variables: {
input: {
file: uploadedImageInfo,
reportId: report.id,
variant: 'published',
},
},
}).then(...finalizeUpload.tap);
// finalizeUpload.tap;
};
const {
getRootProps,
getInputProps,
isDragActive,
open: openFileBrowser,
} = useDropzone({
onDrop: handleDrop,
onDrop: handleFileUpload,

Check failure on line 46 in src/scenes/ProgressReports/EditForm/Steps/Media/MediaStep.tsx

View workflow job for this annotation

GitHub Actions / run

Promise-returning function provided to property where a void return was expected
noClick: true,
noKeyboard: true,
disabled: false,
});

const chooseFileAndUpload = () => {
const file = openFileBrowser();
console.log(file);
};
return (
<div {...getRootProps()}>
<Box sx={{ maxWidth: 'md' }}>
Expand All @@ -49,19 +64,28 @@ export const MediaStep: StepComponent = ({ report }) => {
.reverse()
.map((variant) => {
return (
<React.Fragment key={variant.key}>
<Fragment key={variant.key}>
<DropOverlay isDragActive={isDragActive}>
Drop files to start uploading
</DropOverlay>
<input {...getInputProps()} name="media_file_uploader" />
<input
{...getInputProps()}
name={`upload-media-${variant.key}`}
/>
<Box sx={{ display: 'block' }} key={variant.key}>
<VariantMediaAccordion
updateMedia={updateMedia}
openFileBrowser={openFileBrowser}
response={{ item: undefined, uploadVariant: variant }}
response={{
item: report.media.items.filter(
(item) => item.variant.key === variant.key
),
uploadVariant: variant,
}}
initiallyOpen={false}
/>
</Box>
</React.Fragment>
</Fragment>
);
})}
</Box>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,25 @@ const ImageBox = ({
media,
openFileBrowser,
}: {
media: string | undefined;
media: any[];
openFileBrowser: any;
}) => {
return (
<>
{media ? (
{media.length ? (
// TODO replace with image when it exists
<Box sx={{ border: '1px solid green', p: 1, m: 1 }}>
placeholder for current media imag
<Box
sx={{
borderRadius: '3px',
backgroundColor: 'rgba(0, 0, 0, 0.12)',
p: 1,
pt: 6,
m: 1,
flexGrow: 1.75,
textAlign: 'center',
}}
>
<img src={media[0].media.file.url} alt={media[0].media.altText} />
</Box>
) : (
<Box
Expand All @@ -46,14 +56,17 @@ const ImageBox = ({
);
};

// TODO proper types
export const VariantMediaAccordion = ({
openFileBrowser,
response,
initiallyOpen,
updateMedia,
}: {
openFileBrowser: any;
response: MediaVariantResponse;
initiallyOpen: boolean;
updateMedia: any;
}) => {
const [expanded, setExpanded] = useState(initiallyOpen);

Expand All @@ -76,6 +89,10 @@ export const VariantMediaAccordion = ({
<Box sx={{ display: 'flex', p: '0px 16px' }}>
<ImageBox media={response.item} openFileBrowser={openFileBrowser} />
<MediaInfoForm
initialValues={{
category: response.item[0]?.category,
caption: response.item[0]?.media?.caption,
}}
disabled={!response.item}
submitForm={() => {
console.log('submit');
Expand Down

0 comments on commit fcda48c

Please sign in to comment.