4
This commit is contained in:
parent
2312fc3d75
commit
9fa9f88c01
File diff suppressed because one or more lines are too long
@ -6,7 +6,7 @@ const fs = require('fs');
|
|||||||
const maxSize = 10 * 1024 * 1024;
|
const maxSize = 10 * 1024 * 1024;
|
||||||
|
|
||||||
// Ensure uploads directory exists
|
// Ensure uploads directory exists
|
||||||
const uploadDir = path.join(__dirname, '../../uploads');
|
const uploadDir = path.join(__dirname, '../../uploads/documents');
|
||||||
if (!fs.existsSync(uploadDir)) {
|
if (!fs.existsSync(uploadDir)) {
|
||||||
fs.mkdirSync(uploadDir, { recursive: true });
|
fs.mkdirSync(uploadDir, { recursive: true });
|
||||||
}
|
}
|
||||||
|
|||||||
@ -82,7 +82,7 @@ router.post('/', processFile, wrapAsync(async (req, res) => {
|
|||||||
}
|
}
|
||||||
// Attach file URL if file was uploaded
|
// Attach file URL if file was uploaded
|
||||||
if (req.file) {
|
if (req.file) {
|
||||||
data.fileurl = `/uploads/${req.file.filename}`;
|
data.fileurl = `/uploads/documents/${req.file.filename}`;
|
||||||
}
|
}
|
||||||
await DocumentsService.create(data, req.currentUser, true, link.host);
|
await DocumentsService.create(data, req.currentUser, true, link.host);
|
||||||
const payload = true;
|
const payload = true;
|
||||||
@ -180,7 +180,7 @@ router.put('/:id', processFile, wrapAsync(async (req, res) => {
|
|||||||
// Attach file URL if file was uploaded
|
// Attach file URL if file was uploaded
|
||||||
if (req.file) {
|
if (req.file) {
|
||||||
data.fileurl = `/uploads/${req.file.filename}`;
|
data.fileurl = `/uploads/${req.file.filename}`;
|
||||||
}
|
data.fileurl = `/uploads/documents/${req.file.filename}`;
|
||||||
await DocumentsService.update(data, req.params.id, req.currentUser);
|
await DocumentsService.update(data, req.params.id, req.currentUser);
|
||||||
res.status(200).send(true);
|
res.status(200).send(true);
|
||||||
}));
|
}));
|
||||||
@ -194,11 +194,6 @@ router.put('/:id', processFile, wrapAsync(async (req, res) => {
|
|||||||
* 500:
|
* 500:
|
||||||
* description: Some server error
|
* description: Some server error
|
||||||
*/
|
*/
|
||||||
router.put('/:id', wrapAsync(async (req, res) => {
|
|
||||||
await DocumentsService.update(req.body.data, req.body.id, req.currentUser);
|
|
||||||
const payload = true;
|
|
||||||
res.status(200).send(payload);
|
|
||||||
}));
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @swagger
|
* @swagger
|
||||||
|
|||||||
@ -65,18 +65,20 @@ export const loadColumns = async (
|
|||||||
editable: true,
|
editable: true,
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
field: 'fileurl',
|
field: 'fileurl',
|
||||||
headerName: 'Fileurl',
|
headerName: 'File',
|
||||||
flex: 1,
|
flex: 1,
|
||||||
minWidth: 120,
|
minWidth: 120,
|
||||||
filterable: false,
|
filterable: false,
|
||||||
headerClassName: 'datagrid--header',
|
headerClassName: 'datagrid--header',
|
||||||
cellClassName: 'datagrid--cell',
|
cellClassName: 'datagrid--cell',
|
||||||
|
renderCell: (params: GridValueGetterParams) =>
|
||||||
editable: true,
|
params.value ? (
|
||||||
|
<a href={params.value as string} download>
|
||||||
|
Download
|
||||||
|
</a>
|
||||||
|
) : null,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
|
|||||||
@ -41,6 +41,8 @@ const EditDocumentsPage = () => {
|
|||||||
|
|
||||||
}
|
}
|
||||||
const [initialValues, setInitialValues] = useState(initVals)
|
const [initialValues, setInitialValues] = useState(initVals)
|
||||||
|
const [file, setFile] = useState<File | null>(null)
|
||||||
|
|
||||||
|
|
||||||
const { documents } = useAppSelector((state) => state.documents)
|
const { documents } = useAppSelector((state) => state.documents)
|
||||||
|
|
||||||
@ -63,10 +65,13 @@ const EditDocumentsPage = () => {
|
|||||||
setInitialValues(newInitialVal);
|
setInitialValues(newInitialVal);
|
||||||
}
|
}
|
||||||
}, [documents])
|
}, [documents])
|
||||||
|
|
||||||
const handleSubmit = async (data) => {
|
const handleSubmit = async (data) => {
|
||||||
await dispatch(update({ id: id, data }))
|
const formData = new FormData();
|
||||||
await router.push('/documents/documents-list')
|
formData.append('title', data.title);
|
||||||
|
formData.append('document_type', data.document_type);
|
||||||
|
if (file) formData.append('file', file);
|
||||||
|
await dispatch(update({ id: id, data: formData }));
|
||||||
|
await router.push('/documents/documents-list');
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@ -111,14 +116,24 @@ const EditDocumentsPage = () => {
|
|||||||
</Field>
|
</Field>
|
||||||
</FormField>
|
</FormField>
|
||||||
|
|
||||||
<FormField
|
<FormField label="File" labelFor="file">
|
||||||
label="Fileurl"
|
{initialValues.fileurl && (
|
||||||
|
<a
|
||||||
|
href={initialValues.fileurl}
|
||||||
|
download
|
||||||
|
className="block mb-2 text-blue-600 hover:underline"
|
||||||
>
|
>
|
||||||
<Field
|
Download current file
|
||||||
name="fileurl"
|
</a>
|
||||||
placeholder="Fileurl"
|
)}
|
||||||
|
<input
|
||||||
|
id="file"
|
||||||
|
name="file"
|
||||||
|
type="file"
|
||||||
|
onChange={e => setFile(e.currentTarget.files ? e.currentTarget.files[0] : null)}
|
||||||
/>
|
/>
|
||||||
</FormField>
|
</FormField>
|
||||||
|
<BaseDivider />
|
||||||
|
|
||||||
<BaseDivider />
|
<BaseDivider />
|
||||||
<BaseButtons>
|
<BaseButtons>
|
||||||
|
|||||||
@ -1,6 +1,4 @@
|
|||||||
import { mdiChartTimelineVariant } from '@mdi/js'
|
import React, { ReactElement, useState } from 'react';
|
||||||
import Head from 'next/head'
|
|
||||||
import React, { ReactElement } from 'react'
|
|
||||||
import CardBox from '../../components/CardBox'
|
import CardBox from '../../components/CardBox'
|
||||||
import LayoutAuthenticated from '../../layouts/Authenticated'
|
import LayoutAuthenticated from '../../layouts/Authenticated'
|
||||||
import SectionMain from '../../components/SectionMain'
|
import SectionMain from '../../components/SectionMain'
|
||||||
@ -36,11 +34,19 @@ const initialValues = {
|
|||||||
const DocumentsNew = () => {
|
const DocumentsNew = () => {
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
const dispatch = useAppDispatch()
|
const dispatch = useAppDispatch()
|
||||||
|
const [file, setFile] = useState<File | null>(null)
|
||||||
|
|
||||||
const handleSubmit = async (data) => {
|
|
||||||
await dispatch(create(data))
|
const handleSubmit = async (data) => {
|
||||||
await router.push('/documents/documents-list')
|
const formData = new FormData();
|
||||||
|
formData.append('title', data.title);
|
||||||
|
formData.append('document_type', data.document_type);
|
||||||
|
if (file) {
|
||||||
|
formData.append('file', file);
|
||||||
}
|
}
|
||||||
|
await dispatch(create(formData));
|
||||||
|
await router.push('/documents/documents-list');
|
||||||
|
}
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Head>
|
<Head>
|
||||||
@ -82,15 +88,16 @@ const DocumentsNew = () => {
|
|||||||
<option value="Plan">Plan</option>
|
<option value="Plan">Plan</option>
|
||||||
|
|
||||||
</Field>
|
</Field>
|
||||||
|
<FormField label="File" labelFor="file">
|
||||||
|
<input
|
||||||
|
id="file"
|
||||||
|
name="file"
|
||||||
|
type="file"
|
||||||
|
onChange={e => setFile(e.currentTarget.files ? e.currentTarget.files[0] : null)}
|
||||||
|
/>
|
||||||
</FormField>
|
</FormField>
|
||||||
|
|
||||||
<FormField
|
<BaseDivider />
|
||||||
label="Fileurl"
|
|
||||||
>
|
|
||||||
<Field
|
|
||||||
name="fileurl"
|
|
||||||
placeholder="Fileurl"
|
|
||||||
/>
|
|
||||||
</FormField>
|
</FormField>
|
||||||
|
|
||||||
<BaseDivider />
|
<BaseDivider />
|
||||||
|
|||||||
@ -61,7 +61,7 @@ const DocumentsView = () => {
|
|||||||
|
|
||||||
<div className={'mb-4'}>
|
<div className={'mb-4'}>
|
||||||
<p className={'block font-bold mb-2'}>Fileurl</p>
|
<p className={'block font-bold mb-2'}>Fileurl</p>
|
||||||
<p>{documents?.fileurl}</p>
|
<p><a href={documents?.fileurl} download>Download</a></p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<BaseDivider />
|
<BaseDivider />
|
||||||
|
|||||||
@ -65,21 +65,24 @@ export const deleteItem = createAsyncThunk('documents/deleteDocuments', async (i
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
export const create = createAsyncThunk('documents/createDocuments', async (data: any, { rejectWithValue }) => {
|
export const create = createAsyncThunk('documents/createDocuments', async (payload: any, { rejectWithValue }) => {
|
||||||
try {
|
try {
|
||||||
const result = await axios.post(
|
let result;
|
||||||
'documents',
|
if (payload instanceof FormData) {
|
||||||
{ data }
|
// FormData upload
|
||||||
)
|
result = await axios.post('documents', payload);
|
||||||
return result.data
|
} else {
|
||||||
|
// JSON fallback
|
||||||
|
result = await axios.post('documents', { data: payload });
|
||||||
|
}
|
||||||
|
return result.data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (!error.response) {
|
if (!error.response) {
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
||||||
return rejectWithValue(error.response.data);
|
return rejectWithValue(error.response.data);
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
|
|
||||||
export const uploadCsv = createAsyncThunk(
|
export const uploadCsv = createAsyncThunk(
|
||||||
'documents/uploadCsv',
|
'documents/uploadCsv',
|
||||||
@ -108,19 +111,23 @@ export const uploadCsv = createAsyncThunk(
|
|||||||
|
|
||||||
export const update = createAsyncThunk('documents/updateDocuments', async (payload: any, { rejectWithValue }) => {
|
export const update = createAsyncThunk('documents/updateDocuments', async (payload: any, { rejectWithValue }) => {
|
||||||
try {
|
try {
|
||||||
const result = await axios.put(
|
let result;
|
||||||
`documents/${payload.id}`,
|
const { id, data } = payload;
|
||||||
{ id: payload.id, data: payload.data }
|
if (data instanceof FormData) {
|
||||||
)
|
// Multipart upload
|
||||||
return result.data
|
result = await axios.put(`documents/${id}`, data);
|
||||||
|
} else {
|
||||||
|
// JSON fallback
|
||||||
|
result = await axios.put(`documents/${id}`, { id, data });
|
||||||
|
}
|
||||||
|
return result.data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (!error.response) {
|
if (!error.response) {
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
||||||
return rejectWithValue(error.response.data);
|
return rejectWithValue(error.response.data);
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
|
|
||||||
export const documentsSlice = createSlice({
|
export const documentsSlice = createSlice({
|
||||||
name: 'documents',
|
name: 'documents',
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user