Skip to content

Commit 01f1c6c

Browse files
committed
feat: add swr
1 parent 0ff2ec7 commit 01f1c6c

File tree

6 files changed

+143
-26
lines changed

6 files changed

+143
-26
lines changed

apps/web/@/messages/en.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,5 +111,11 @@
111111
"turn_publish": "Turn publish",
112112
"post_created": "Post created successfully",
113113
"post_updated": "Post updated successfully"
114+
},
115+
"upload": {
116+
"asset_management": "Asset Management",
117+
"upload_image": "Upload Image",
118+
"select": "Select",
119+
"upload": "Upload"
114120
}
115121
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import React, { createContext, useCallback, useContext, useReducer } from "react"
2+
3+
import { OrderBy } from "database"
4+
5+
// newest
6+
// oldest
7+
// name A-Z
8+
// name Z-A
9+
export const OrderField = {
10+
newest: "createdAt",
11+
oldest: "createdAt",
12+
nameAsc: "name",
13+
nameDesc: "name",
14+
}
15+
16+
export type FileManagerState = {
17+
selectedFiles: File[]
18+
search: string
19+
orderBy: OrderBy
20+
order: (typeof OrderField)[keyof typeof OrderField]
21+
}
22+
export type FileManagerContextType = FileManagerState & {
23+
setSelectedFiles: (files: File[]) => void
24+
setSearch: (search: string) => void
25+
setOrder: (order: "asc" | "desc") => void
26+
setOrderBy: (orderBy: string) => void
27+
}
28+
29+
const FileManagerContext = createContext<FileManagerContextType | null>(null)
30+
31+
export const useFileManager = () => {
32+
const context = useContext(FileManagerContext)
33+
if (!context) {
34+
throw new Error("useFileManager must be used within a FileManagerProvider")
35+
}
36+
return context
37+
}
38+
39+
type FileManagerAction = {
40+
type: "SET_SELECTED_FILES" | "SET_SEARCH" | "SET_ORDER" | "SET_ORDER_BY"
41+
payload: any
42+
}
43+
44+
function fileManagerReducer(state: FileManagerState, action: FileManagerAction): FileManagerState {
45+
switch (action.type) {
46+
case "SET_SELECTED_FILES":
47+
return { ...state, selectedFiles: action.payload }
48+
case "SET_SEARCH":
49+
return { ...state, search: action.payload }
50+
case "SET_ORDER":
51+
return { ...state, order: action.payload }
52+
case "SET_ORDER_BY":
53+
return { ...state, orderBy: action.payload }
54+
default:
55+
return state
56+
}
57+
}
58+
59+
const FileManagerContainer = ({ children }: { children: React.ReactNode }) => {
60+
const [state, dispatch] = useReducer(fileManagerReducer, {
61+
selectedFiles: [],
62+
search: "",
63+
order: OrderField.newest,
64+
orderBy: "asc",
65+
})
66+
67+
const setSelectedFiles = useCallback((files: File[]) => {
68+
dispatch({ type: "SET_SELECTED_FILES", payload: files })
69+
}, [])
70+
71+
const setSearch = useCallback((search: string) => {
72+
dispatch({ type: "SET_SEARCH", payload: search })
73+
}, [])
74+
75+
const setOrder = useCallback((order: OrderBy) => {
76+
dispatch({ type: "SET_ORDER", payload: order })
77+
}, [])
78+
79+
const setOrderBy = useCallback((orderBy: string) => {
80+
dispatch({ type: "SET_ORDER_BY", payload: orderBy })
81+
}, [])
82+
83+
return (
84+
<FileManagerContext.Provider
85+
value={{
86+
...state,
87+
setSelectedFiles,
88+
setSearch,
89+
setOrder,
90+
setOrderBy,
91+
}}
92+
>
93+
{children}
94+
</FileManagerContext.Provider>
95+
)
96+
}
97+
98+
export default FileManagerContainer

apps/web/@/molecules/upload/index.tsx

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { ReactNode, useState } from "react"
22

3+
import { useTranslations } from "next-intl"
34
import {
45
Button,
56
Dialog,
@@ -11,40 +12,36 @@ import {
1112
} from "ui"
1213

1314
import AssetManagement from "./AssetsManagement"
15+
import FileManagerContainer from "./FileManagerContainer"
1416
import UploadImageButton from "./UploadImageButton"
1517

1618
interface UploadProps {
1719
children: ReactNode
1820
}
1921

2022
const Upload: React.FC<UploadProps> = ({ children }) => {
21-
// const [isOpen, setIsOpen] = useState(false)
22-
23-
// const handleOpenChange = (open: boolean) => {
24-
// setIsOpen(open)
25-
// }
23+
const t = useTranslations("common")
2624

2725
return (
28-
<Dialog
29-
// open={isOpen}
30-
// onOpenChange={handleOpenChange}
31-
>
32-
<DialogTrigger asChild>{children}</DialogTrigger>
33-
<DialogContent className="w-full max-w-[800px]">
34-
<DialogHeader className="flex flex-row items-center gap-4">
35-
<DialogTitle className="mb-0">Asset Management</DialogTitle>
36-
<UploadImageButton />
37-
</DialogHeader>
38-
39-
<div className="h-[300px] overflow-scroll">
40-
<AssetManagement />
41-
</div>
42-
43-
<DialogFooter>
44-
<Button>Select</Button>
45-
</DialogFooter>
46-
</DialogContent>
47-
</Dialog>
26+
<FileManagerContainer>
27+
<Dialog>
28+
<DialogTrigger asChild>{children}</DialogTrigger>
29+
<DialogContent className="w-full max-w-[800px]">
30+
<DialogHeader className="flex flex-row items-center gap-4">
31+
<DialogTitle className="mb-0">Asset Management</DialogTitle>
32+
<UploadImageButton />
33+
</DialogHeader>
34+
35+
<div className="h-[300px] overflow-scroll">
36+
<AssetManagement />
37+
</div>
38+
39+
<DialogFooter>
40+
<Button>Select</Button>
41+
</DialogFooter>
42+
</DialogContent>
43+
</Dialog>
44+
</FileManagerContainer>
4845
)
4946
}
5047

apps/web/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
"slate-hyperscript": "^0.100.0",
7070
"slate-react": "^0.107.1",
7171
"slugify": "^1.6.6",
72+
"swr": "^2.2.5",
7273
"tailwind-merge": "^1.14.0",
7374
"tailwindcss": "^3.2.4",
7475
"tailwindcss-animate": "^1.0.7",

packages/database/src/index.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@ import { createPost, getPost, getPosts, updatePost, updatePostStatus } from "./p
44
import { TCreatePostInput, TPostActionType, TPostItem } from "./posts/selects"
55
import { TGetPostsRequest, TGetPostsResponse } from "./posts/type"
66
import prisma from "./prisma"
7-
import { ActionReturnType } from "./shared/type"
7+
import {
8+
ActionReturnType,
9+
DEFAULT_LIMIT,
10+
DEFAULT_PAGE,
11+
OrderBy,
12+
PeriodValues,
13+
TGetListResponse,
14+
} from "./shared/type"
815
import { createTag, getTag, getTags, getTopTags } from "./tags/queries"
916
import type { TTagItem, TTagListItem } from "./tags/selects"
1017

@@ -51,4 +58,9 @@ export type {
5158

5259
// Shared
5360
ActionReturnType,
61+
OrderBy,
62+
TGetListResponse,
63+
PeriodValues,
64+
DEFAULT_LIMIT,
65+
DEFAULT_PAGE,
5466
}

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)