Skip to content

Commit

Permalink
feat(file): preview image after uploading
Browse files Browse the repository at this point in the history
  • Loading branch information
ngyngcphu committed Oct 12, 2023
1 parent acdd30e commit d739480
Show file tree
Hide file tree
Showing 13 changed files with 95 additions and 9 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion __mock-server__/file-server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const app = express();
global.__basedir = __dirname;

var corsOptions = {
origin: 'http://localhost:3000'
origin: 'http://localhost:3000',
credentials: true
};

app.use(cors(corsOptions));
Expand Down
22 changes: 14 additions & 8 deletions src/components/home/ChooseFileBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useState } from 'react';
import { Dialog, DialogBody } from '@material-tailwind/react';
import { ArrowUpTrayIcon } from '@heroicons/react/24/outline';
import { useOrderWorkflowBox } from '@components/order';
import { useFileStore } from '@states/home';
import { useOrderWorkflowStore } from '@states/order';

export function useChooseFileBox() {
Expand All @@ -10,12 +11,21 @@ export function useChooseFileBox() {

const ChooseFileBox = () => {
const { setOrderStep } = useOrderWorkflowStore();
const { uploadFile } = useFileStore();

const handleOpenBox = () => setOpenBox(!openBox);
const handleUploadDocument = async (event: React.ChangeEvent<HTMLInputElement>) => {
if (event.target.files) {
await uploadFile(event.target.files[0]);
setOpenBox(false);
openOrderWorkflowBox();
setOrderStep(1);
}
};

return (
<>
<Dialog
size='xs'
className='2xl:max-w-fit 2xl:w-fit 2xl:min-w-fit lg:max-w-fit lg:w-fit lg:min-w-fit max-w-fit w-fit min-w-fit'
open={openBox}
handler={handleOpenBox}
Expand All @@ -25,7 +35,7 @@ export function useChooseFileBox() {
htmlFor='dropzone-file'
className='flex flex-col items-center w-[310px] h-[148px] gap-4 p-4 lg:w-[384px] lg:h-[190px] lg:p-7 border-2 border-blue/1 border-dashed rounded-lg cursor-pointer bg-blue-50 hover:bg-blue-100'
>
<div className='flex gap-4'>
<div className='flex items-center gap-4'>
<ArrowUpTrayIcon
strokeWidth={2}
className='bg-blue/1 text-white rounded-full w-[40px] h-[40px] p-2 lg:w-[48px] lg:h-[48px] lg:p-3'
Expand All @@ -35,7 +45,7 @@ export function useChooseFileBox() {
</span>
</div>

<div className='text-sm lg:text-base gap-1'>
<div className='text-sm lg:text-base'>
<span className='font-semibold h-[32px]'>Allowed formats:</span> .doc, .docx, .xls,
.xlsx, .ppt, .jpg, .png, .pdf
<div className='text-sm lg:text-base'>
Expand All @@ -47,11 +57,7 @@ export function useChooseFileBox() {
id='dropzone-file'
type='file'
className='hidden'
onChange={() => {
setOpenBox(false);
openOrderWorkflowBox();
setOrderStep(1);
}}
onChange={handleUploadDocument}
/>
</label>
</DialogBody>
Expand Down
4 changes: 4 additions & 0 deletions src/components/order/UploadDocumentForm.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { Button } from '@material-tailwind/react';
import { useFileStore } from '@states/home';
import { useOrderWorkflowStore } from '@states/order';

// Tan's first-task in here.
export function UploadDocumentForm() {
const { setOrderStep } = useOrderWorkflowStore();
const { fileTarget } = useFileStore();
console.log(fileTarget);

return (
<>
<div>UploadDocumentForm</div>
{fileTarget && <img src={fileTarget.url} alt='docs' width='30%' />}
<Button onClick={() => setOrderStep(2)}>Save</Button>
</>
);
Expand Down
1 change: 1 addition & 0 deletions src/constants/errorMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const FILE_NOT_FOUND = 'File not found!';
1 change: 1 addition & 0 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* @file Automatically generated by barrelsby.
*/

export * from './errorMessage';
export * from './home';
export * from './menuBar';
export * from './screen';
5 changes: 5 additions & 0 deletions src/services/common/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ export const mockServer = axios.create({
withCredentials: true
});

export const mockFileServer = axios.create({
baseURL: 'http://localhost:3003',
withCredentials: true
});

export async function invoke<R = unknown, D = unknown>(call: Promise<AxiosResponse<R, D>>) {
try {
const response = await call;
Expand Down
16 changes: 16 additions & 0 deletions src/services/home/file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { mockFileServer, invoke } from '@services/common';

export const fileService = {
upload: (file: File) => {
const formData = new FormData();
formData.append('file', file);
return invoke<{ message: string }>(
mockFileServer.post('/upload', formData, {
headers: {
'Content-Type': 'multipart/form-data'
}
})
);
},
getAll: () => invoke<FileData[]>(mockFileServer.get('/files'))
};
1 change: 1 addition & 0 deletions src/services/home/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* @file Automatically generated by barrelsby.
*/

export * from './file';
export * from './order';
export * from './slide';
export * from './userInfo';
37 changes: 37 additions & 0 deletions src/states/home/file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { create } from 'zustand';
import { FILE_NOT_FOUND } from '@constants';
import { fileService } from '@services/home';

export const useFileStore = create<FileStore>()((set, get) => ({
fileStatus: 'UNINIT',
fileAll: [],
fileTarget: {
name: '',
url: ''
},
uploadFile: async (file) => {
set(() => ({ fileStatus: 'PENDING' }));
try {
await fileService.upload(file);
await get().getAllFiles();
const fileTarget = get().getFileByName(file.name);
set(() => ({ fileTarget: fileTarget, fileStatus: 'SUCCESS' }));
} catch (err) {
set(() => ({ fileStatus: 'REJECT' }));
}
},
getAllFiles: async () => {
set(() => ({ fileStatus: 'PENDING' }));
try {
const fileAll = await fileService.getAll();
set(() => ({ fileAll: fileAll, fileStatus: 'SUCCESS' }));
} catch (err) {
set(() => ({ fileStatus: 'REJECT' }));
}
},
getFileByName: (name) => {
const file = get().fileAll.find((item) => item.name === name);
if (!file) throw new Error(FILE_NOT_FOUND);
return file;
}
}));
1 change: 1 addition & 0 deletions src/states/home/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* @file Automatically generated by barrelsby.
*/

export * from './file';
export * from './order';
export * from './slide';
export * from './userInfo';
13 changes: 13 additions & 0 deletions src/types/home/file.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
type FileData = {
name: string;
url: string;
};

type FileStore = {
fileStatus: StoreStatus;
fileAll: FileData[];
fileTarget: FileData;
uploadFile: (file: File) => Promise<void>;
getAllFiles: () => Promise<void>;
getFileByName: (name: string) => FileData;
};

0 comments on commit d739480

Please sign in to comment.