Skip to content

Commit

Permalink
Add server action to examples
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandrHoroshih committed Feb 14, 2024
1 parent 7efc934 commit d8ebaee
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 6 deletions.
7 changes: 5 additions & 2 deletions apps/playground-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"eslint-config-next": "13.3.0",
"mvp.css": "^1.12.0",
"next": "14.1.0",
"patronum": "^2.0.0",
"patronum": "^2.2.0",
"postcss": "8.4.21",
"react": "18.2.0",
"react-dom": "18.2.0",
Expand All @@ -37,5 +37,8 @@
"devDependencies": {
"fs-extra": "^11.1.1",
"zx": "^7.2.1"
}
},
"browserslist": [
"last 2 years"
]
}
8 changes: 4 additions & 4 deletions apps/playground-app/pnpm-lock.yaml

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

2 changes: 2 additions & 0 deletions apps/playground-app/src/features/layout/view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Link from "next/link";

import { AsyncCounter } from "#root/features/async-counter";
import { MemoUpdatesBugCheck } from "#root/features/memo-check";
import { ServerAction } from "#root/features/server-action";

const links = [
{
Expand Down Expand Up @@ -30,6 +31,7 @@ export function PageLayout(props: React.PropsWithChildren<{}>) {
</h1>
<small>Playground app to research Next.js + Effector</small>
<MemoUpdatesBugCheck />
<ServerAction />
</div>
<AsyncCounter />
<nav style={{ marginBottom: 0 }}>
Expand Down
12 changes: 12 additions & 0 deletions apps/playground-app/src/features/server-action/hash-value.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"use server";
import { createHash } from "node:crypto";

export const hashValue = async (value: string) => {

/**
* Simulates server-side activities like db queries, etc.
*/
await new Promise((resolve) => setTimeout(resolve, 1000));

return createHash("sha256").update(value).digest("hex");
};
1 change: 1 addition & 0 deletions apps/playground-app/src/features/server-action/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { ServerAction } from "./ui";
20 changes: 20 additions & 0 deletions apps/playground-app/src/features/server-action/model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { createEffect, createEvent, createStore, sample } from "effector";
import { debounce } from "patronum";

import { hashValue } from "./hash-value";

const hashValueFx = createEffect(async (value: string) => hashValue(value));

export const $hashedValue = createStore<string>("").on(
hashValueFx.doneData,
(_, value) => value
);

export const $pending = hashValueFx.pending;

export const valueChanged = createEvent<string>();

sample({
clock: debounce(valueChanged, 500),
target: hashValueFx,
});
27 changes: 27 additions & 0 deletions apps/playground-app/src/features/server-action/ui.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"use client";
import { useUnit } from "effector-react";

import { $hashedValue, $pending, valueChanged } from "./model";

export function ServerAction() {
const [hashedValue, pending, changed] = useUnit([
$hashedValue,
$pending,
valueChanged,
]);

return (
<div
style={{
maxWidth: "300px",
margin: "auto",
}}
>
<h3>Server action example</h3>
<input type="text" onChange={(e) => changed(e.target.value)} />
<div>
Server-hashed value: {pending ? "Hashing..." : hashedValue.slice(0, 5)}
</div>
</div>
);
}

0 comments on commit d8ebaee

Please sign in to comment.