Skip to content

Commit

Permalink
Created a callback function for Urlloader.tsx so that Uploaded.tsx is…
Browse files Browse the repository at this point in the history
… updated after URL upload
  • Loading branch information
auksesir committed Sep 7, 2023
1 parent d4c62a2 commit 1cff6f7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
25 changes: 20 additions & 5 deletions frontend/src/renderer/pages/file-upload/Upload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,24 @@ export function Upload() {
// React state to store the files
const [files, setFiles] = useState<FileData[]>([]);

// Callback function to update the files state when files are added in the Dropzone component
const handleFilesUploaded = (uploadedFiles: File[]) => {
setFiles((prevFiles) => [...prevFiles, ...uploadedFiles]);
};
// Callback function to update the files state when files are added in the Dropzone and Urlloader component
const handleFilesUploaded = (uploadedFiles: (File[] | string[])) => {
const newFiles: FileData[] = uploadedFiles.map((item) => {
if (typeof item === 'string') {
// Convert string to FileData object
return {
name: item,
type: 'website',
size: 0 // Placeholder
};
} else {
// If it's already a FileData object, keep it as is
return item;
}
});

setFiles((prevFiles) => [...prevFiles, ...newFiles]);
};

// useEffect(() => { // Using useEffect hook to call API on component mount
// axios.get<FileData[]>(`${config.REACT_APP_BACKEND_URL}/files`)
Expand Down Expand Up @@ -50,7 +64,7 @@ export function Upload() {

return (
<div style={{marginTop: '-100px'}}>
<Urlloader/>
<Urlloader onFilesUploaded={handleFilesUploaded}/>
<DropzoneButton onFilesUploaded={handleFilesUploaded}/>
<UploadedTable data={files}/>
</div>
Expand All @@ -59,3 +73,4 @@ export function Upload() {

export default Upload;


Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,4 @@ export function DropzoneButton({ onFilesUploaded }: { onFilesUploaded: (uploaded
);
}

export default DropzoneButton;
export default DropzoneButton;
12 changes: 4 additions & 8 deletions frontend/src/renderer/pages/file-upload/components/Urlloader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import { useEffect, useState } from 'react';
import { apiCall } from '../../../utils/api';
import { useForm } from '@mantine/form';
import { Notifications } from '@mantine/notifications';
import { check } from 'prettier';

export function UrlLoader() {
export function UrlLoader({ onFilesUploaded }: { onFilesUploaded: (uploadedFiles: string[]) => void }) {
const [sitemap, setSitemap] = useState(false);

const form = useForm({
Expand All @@ -28,7 +27,7 @@ const form = useForm({
const handleSuccess = (values) => {
console.log(values);
handleUrlUpload();

form.setFieldValue('URL', '');
};

const handleFail = (errors) => {
Expand All @@ -43,16 +42,14 @@ const form = useForm({
console.log(payload);
apiCall('/files/website', 'POST', payload)
.then((response) => {
console.log(response.data);

Notifications.show({
title: 'URL Upload Successful',
message: 'The URL was successfully uploaded.',
color: 'green'
});

form.setFieldValue('URL', '');
setSitemap(false);

onFilesUploaded([payload.website]);
})
.catch((error) => {
console.log('Error fetching config:', error);
Expand Down Expand Up @@ -88,7 +85,6 @@ const form = useForm({
<Checkbox
label={'Index whole sitemap'}
style={{ marginTop: '10px' }}
checked={sitemap}
onChange={(event) => setSitemap(event.currentTarget.checked)}
/>
<Button type="submit">Upload</Button>
Expand Down

0 comments on commit 1cff6f7

Please sign in to comment.