Skip to content

Commit

Permalink
Convert API handlers hook to TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
man90es committed Mar 5, 2024
1 parent c489707 commit 2772709
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"devDependencies": {
"@babel/core": "^7.23.9",
"@babel/eslint-parser": "^7.23.10",
"@bakaso/fkclient": "^1.6.0",
"@bakaso/fkclient": "^1.6.1",
"@types/node": "^20.11.16",
"@vue/cli-plugin-babel": "^5.0.8",
"@vue/cli-plugin-eslint": "^5.0.8",
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 18 additions & 6 deletions src/hooks/useAPIHandlers.js → src/hooks/useAPIHandlers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { getProps } from "@/utils"
import { useStore } from "vuex"
import { useToast } from "vue-toast-notification"
import type { Post, Thread } from "@/store"
import type FKClient from "@bakaso/fkclient"

export default function useAPIHandlers(API) {
export default function useAPIHandlers(API: FKClient) {
const store = useStore()
const toast = useToast()

Expand All @@ -23,12 +25,12 @@ export default function useAPIHandlers(API) {

store.commit("updateThreadList", {
...getProps(what, ["boardName", "count", "page"]),
payload: data.map(thread => thread.id),
payload: data.map((thread: Thread) => thread.id),
})

store.commit("updateThreads", data)

for (let thread of data) {
for (const thread of data) {
store.commit("updatePostList", {
threadId: thread.id,
payload: [thread.head.id],
Expand Down Expand Up @@ -64,13 +66,13 @@ export default function useAPIHandlers(API) {
// Feed
store.commit("updateFeed", {
...getProps(what, ["boardName", "count", "page"]),
payload: data.map(post => post.id),
payload: data.map((post: Post) => post.id),
})
} else {
// Thread
store.commit("updatePostList", {
...getProps(what, ["threadId", "count", "page"]),
payload: data.map(post => post.id),
payload: data.map((post: Post) => post.id),
})
}

Expand All @@ -88,6 +90,10 @@ export default function useAPIHandlers(API) {
API.addInMessageListener(
({ event, error }) => "created" === event && !error,
({ type, data }) => {
if (!type) {
return
}

const action = ({
board: "pushBoard",
post: "pushPost",
Expand All @@ -101,7 +107,13 @@ export default function useAPIHandlers(API) {
API.addInMessageListener(
({ event }) => "deleted" === event,
({ type, data }) => {
if (!type) {
return
}

const action = ({
// TODO: Handle board deletion
board: undefined,
post: "unregisterPost",
thread: "unregisterThread",
})[type]
Expand All @@ -113,7 +125,7 @@ export default function useAPIHandlers(API) {
API.addInMessageListener(
({ error }) => Boolean(error),
({ error }) => {
const text = error.description || `Error ${error.message} occured` || "Unexpected error occured"
const text = error!.description || `Error ${error!.message} occured` || "Unexpected error occured"
toast.error(text, { position: "top-right" })
}
)
Expand Down
4 changes: 2 additions & 2 deletions src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ type Board = {
name: string
}

type Thread = {
export type Thread = {
id: number
boardName: Board["name"]
head: Post
posts: number
}

type Post = {
export type Post = {
id: number
threadId: Thread["id"]
}
Expand Down

0 comments on commit 2772709

Please sign in to comment.