Skip to content

Commit 552e9c0

Browse files
committed
Added fragments API
1 parent 92971cf commit 552e9c0

File tree

11 files changed

+146
-2
lines changed

11 files changed

+146
-2
lines changed

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@ecency/sdk",
33
"private": false,
4-
"version": "1.0.37",
4+
"version": "1.0.38",
55
"type": "module",
66
"license": "MIT",
77
"main": "./dist/ecency-sdk.umd.js",
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from "./queries";
22
export * from "./types";
3+
export * from "./mutations";
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { CONFIG, getAccessToken, getQueryClient } from "@/modules/core";
2+
import { useMutation } from "@tanstack/react-query";
3+
import { Fragment } from "../types";
4+
import { getFragmentsQueryOptions } from "../queries";
5+
6+
export function useAddFragment(username: string) {
7+
return useMutation({
8+
mutationKey: ["posts", "add-fragment", username],
9+
mutationFn: async ({ title, body }: { title: string; body: string }) => {
10+
const response = await fetch(
11+
CONFIG.privateApiHost + "/private-api/fragments-add",
12+
{
13+
method: "POST",
14+
body: JSON.stringify({
15+
code: getAccessToken(username),
16+
title,
17+
body,
18+
}),
19+
headers: {
20+
"Content-Type": "application/json",
21+
},
22+
}
23+
);
24+
return response.json() as Promise<Fragment>;
25+
},
26+
onSuccess(response) {
27+
getQueryClient().setQueryData<Fragment[]>(
28+
getFragmentsQueryOptions(username).queryKey,
29+
(data) => [response, ...(data ?? [])]
30+
);
31+
},
32+
});
33+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { CONFIG, getAccessToken, getQueryClient } from "@/modules/core";
2+
import { useMutation } from "@tanstack/react-query";
3+
import { Fragment } from "../types";
4+
import { getFragmentsQueryOptions } from "../queries";
5+
6+
export function useEditFragment(username: string, fragmentId: string) {
7+
return useMutation({
8+
mutationKey: ["posts", "edit-fragment", username, fragmentId],
9+
mutationFn: async ({ title, body }: { title: string; body: string }) => {
10+
const response = await fetch(
11+
CONFIG.privateApiHost + "/private-api/fragments-update",
12+
{
13+
method: "POST",
14+
body: JSON.stringify({
15+
code: getAccessToken(username),
16+
id: fragmentId,
17+
title,
18+
body,
19+
}),
20+
headers: {
21+
"Content-Type": "application/json",
22+
},
23+
}
24+
);
25+
return response.json() as Promise<Fragment>;
26+
},
27+
onSuccess(response) {
28+
getQueryClient().setQueryData<Fragment[]>(
29+
getFragmentsQueryOptions(username).queryKey,
30+
(data) => {
31+
if (!data) {
32+
return [];
33+
}
34+
35+
const index = data.findIndex(({ id }) => id === fragmentId);
36+
if (index >= 0) {
37+
data[index] = response;
38+
}
39+
40+
return [...data];
41+
}
42+
);
43+
},
44+
});
45+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from "./add-fragment";
2+
export * from "./edit-fragment";
3+
export * from "./remove-fragment";
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { CONFIG, getAccessToken, getQueryClient } from "@/modules/core";
2+
import { useMutation } from "@tanstack/react-query";
3+
import { Fragment } from "../types";
4+
import { getFragmentsQueryOptions } from "../queries";
5+
6+
export function useRemoveFragment(username: string, fragmentId: string) {
7+
return useMutation({
8+
mutationKey: ["posts", "remove-fragment", username],
9+
mutationFn: async () =>
10+
fetch(CONFIG.privateApiHost + "/private-api/fragments-delete", {
11+
method: "POST",
12+
body: JSON.stringify({
13+
code: getAccessToken(username),
14+
id: fragmentId,
15+
}),
16+
headers: {
17+
"Content-Type": "application/json",
18+
},
19+
}),
20+
onSuccess() {
21+
getQueryClient().setQueryData<Fragment[]>(
22+
getFragmentsQueryOptions(username).queryKey,
23+
(data) => [...(data ?? [])].filter(({ id }) => id !== fragmentId)
24+
);
25+
},
26+
});
27+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { CONFIG, getAccessToken } from "@/modules/core";
2+
import { queryOptions } from "@tanstack/react-query";
3+
import { Fragment } from "../types";
4+
5+
export function getFragmentsQueryOptions(username: string) {
6+
return queryOptions({
7+
queryKey: ["posts", "fragments", username],
8+
queryFn: async () => {
9+
const response = await fetch(
10+
CONFIG.privateApiHost + "/private-api/fragments",
11+
{
12+
method: "POST",
13+
body: JSON.stringify({
14+
code: getAccessToken(username),
15+
}),
16+
headers: {
17+
"Content-Type": "application/json",
18+
},
19+
}
20+
);
21+
22+
return response.json() as Promise<Fragment[]>;
23+
},
24+
enabled: !!username,
25+
});
26+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from "./get-trending-tags-query-options";
2+
export * from "./get-fragments-query-options";
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export interface Fragment {
2+
id: string;
3+
title: string;
4+
body: string;
5+
created: string;
6+
modified: string;
7+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from "./trending-tag";
2+
export * from "./fragment";

packages/wallets/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@ecency/wallets",
33
"private": false,
4-
"version": "1.2.11",
4+
"version": "1.2.12",
55
"type": "module",
66
"license": "MIT",
77
"main": "./dist/ecency-sdk.umd.js",

0 commit comments

Comments
 (0)