-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPackageLikeAction.tsx
48 lines (41 loc) · 1.27 KB
/
PackageLikeAction.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"use client";
import { useFormToaster } from "@thunderstore/cyberstorm-forms";
import {
ApiAction,
packageLikeActionSchema,
} from "@thunderstore/ts-api-react-actions";
import { packageLike } from "@thunderstore/thunderstore-api";
export function PackageLikeAction(props: {
isLoggedIn: boolean;
packageName: string;
namespace: string;
isLiked: boolean;
currentUserUpdateTrigger: () => Promise<void>;
}) {
const { onSubmitSuccess, onSubmitError } = useFormToaster({
successMessage: `${props.isLiked ? "Unliked" : "Liked"} package ${
props.packageName
}`,
errorMessage: props.isLoggedIn
? "Unknown error occurred. The error has been logged"
: "You must be logged in to like a package!",
});
function onActionSuccess() {
props.currentUserUpdateTrigger();
onSubmitSuccess();
}
function onActionError() {
onSubmitError();
}
const onSubmit = ApiAction({
schema: packageLikeActionSchema,
meta: { namespace_id: props.namespace, package_name: props.packageName },
endpoint: packageLike,
onSubmitSuccess: onActionSuccess,
onSubmitError: onActionError,
});
return function () {
onSubmit({ target_state: props.isLiked ? "unrated" : "rated" });
};
}
PackageLikeAction.displayName = "PackageLikeAction";