Skip to content

Commit 37840ce

Browse files
authored
feat(word-editor): implement new word action (#39)
This Pull Request implements the `add` functionality for word on the word editor. The `add` operation takes the current input entered into the editor on submit, perform a submit operation with the `"add"` action through the `handleSubmitWord` handler and routes to the `/editor` page after successful word addition operation. ### Changes Made - Removed `WordEditor` component integration in `/editor` route; prepping it as the jargons editor (a dashboard 😉) - Added the "add new word" route for adding new word to dictionary at `/editor/new` - Integrated `WordEditor` and it `SubmitButton` component to the add new word route - Integrated the `writeNewWord` function into the `handleSubmitWord` handler with logic to execute it only when word-editor `action` is `"new"` ```js if (action === "new") { const newWord = await writeNewWord(userOctokit, forkedRepoDetails, { title, content }, { env: "browser" }); console.log("New word added: ", newWord); } ``` - Modified the `handleSubmitWord` handler to move the `updateExistingWord` function integration to the conditions of executing only when `action` is `"edit"` in contrast to the `"new"` - Added a new utils function `captilizeText` (quick one from chatGPT 😉); which is used to capitalize the title text incases where the entered word title is in lowercase ![image](https://github.com/babblebey/jargons.dev/assets/25631971/b042ab7c-9250-408a-ad84-fe61ba74d03b) - Used the `trim()`method on the entered word `title` to remove whitespaces from input ### Screencast https://github.com/babblebey/jargons.dev/assets/25631971/0f6d00a8-c239-4da0-85cb-08cd9e688c76
1 parent 1768772 commit 37840ce

5 files changed

Lines changed: 80 additions & 20 deletions

File tree

src/components/islands/word-editor.jsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import useRouter from "../../lib/hooks/use-router.js";
55
import useWordEditor from "../../lib/hooks/use-word-editor.js";
66
import { $isWordSubmitLoading } from "../../lib/stores/dictionary.js";
77
import handleSubmitWord from "../../lib/handlers/handle-submit-word.js";
8+
import { capitalizeText } from "../../lib/utils/index.js";
89

910
export default function WordEditor({ title = "", content = "", metadata = {}, action, octokitAuths }) {
1011
return (
@@ -53,7 +54,7 @@ function Editor({ eTitle, eContent, eMetadata, className, submitHandler, action,
5354
async function handleOnSubmit() {
5455
$isWordSubmitLoading.set(true);
5556
await submitHandler(octokitAuths, action, {
56-
title,
57+
title: capitalizeText(title.trim()),
5758
content,
5859
metadata: eMetadata
5960
});
@@ -102,7 +103,7 @@ function Preview({ className, ...props }) {
102103
<div className="w-full">
103104
{title && (
104105
<h1 className="text-4xl font-black">
105-
{ title }
106+
{ capitalizeText(title.trim()) }
106107
</h1>
107108
)}
108109
</div>

src/lib/handlers/handle-submit-word.js

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { forkRepository } from "../fork.js";
33
import { createBranch } from "../branch.js";
44
import { submitWord } from "../submit-word.js";
55
import { normalizeAsUrl } from "../utils/index.js";
6-
import { updateExistingWord } from "../word-editor.js";
76
import { PROJECT_REPO_DETAILS } from "../../../constants.js";
7+
import { updateExistingWord, writeNewWord } from "../word-editor.js";
88

99
/**
1010
* Submit Handler for word editor, handles submit function based on action
@@ -40,16 +40,29 @@ export default async function handleSubmitWord(octokitAuths, action, { title, co
4040
repoChangeBranchRef: branch.ref
4141
}
4242

43-
// update existing word
44-
const word = await updateExistingWord(userOctokit, forkedRepoDetails, {
45-
title,
46-
content,
47-
path: metadata.path,
48-
sha: metadata.sha
49-
}, {
50-
env: "browser"
51-
});
52-
console.log("Word updated: ", word);
43+
// update existing word - if action is "edit"
44+
if (action === "edit") {
45+
const updatedWord = await updateExistingWord(userOctokit, forkedRepoDetails, {
46+
title,
47+
content,
48+
path: metadata.path,
49+
sha: metadata.sha
50+
}, {
51+
env: "browser"
52+
});
53+
console.log("Word updated: ", updatedWord);
54+
}
55+
56+
// add new word - if action is "new"
57+
if (action === "new") {
58+
const newWord = await writeNewWord(userOctokit, forkedRepoDetails, {
59+
title,
60+
content
61+
}, {
62+
env: "browser"
63+
});
64+
console.log("New word added: ", newWord);
65+
}
5366

5467
// submit the edit in new pr
5568
const wordSubmission = await submitWord(

src/lib/utils/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,13 @@ export function isObjectEmpty(object) {
4848
*/
4949
export function resolveEditorActionFromPathname(pathname) {
5050
return pathname.slice(1).split("/")[1].toLowerCase();
51+
}
52+
53+
/**
54+
* Capitalize text
55+
* @param {string} text
56+
* @returns {string}
57+
*/
58+
export function capitalizeText(text) {
59+
return text.split(" ").map(word => word.charAt(0).toUpperCase() + word.slice(1)).join(" ");
5160
}

src/pages/editor/index.astro

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
---
22
import doAuth from "../../lib/actions/do-auth";
3-
import { $userData } from "../../lib/stores/user.js";
43
import BaseLayout from "../../layouts/base.astro";
54
import Navbar from "../../components/navbar.astro";
6-
import WordEditor from "../../components/islands/word-editor.jsx";
5+
import { $userData } from "../../lib/stores/user.js";
76
87
const { url: { pathname }, redirect } = Astro;
98
@@ -18,12 +17,14 @@ $userData.set(userData);
1817
class="flex flex-col w-full min-h-screen overflow-hidden"
1918
>
2019
<Navbar>
21-
<a class="no-underline text-white bg-gray-900 hover:bg-gray-700 focus:ring-4 focus:ring-gray-600 font-medium rounded-lg text-base px-5 py-2.5 text-center ml-1 sm:ml-3">
22-
Publish
23-
</a>
20+
2421
</Navbar>
2522

26-
<main class="flex grow px-5 md:px-6 pb-4">
27-
<WordEditor client:load />
23+
<main class="flex flex-col grow px-5 md:px-6 pb-4">
24+
<h1>
25+
The Jargons Editor - Your Dashboard
26+
</h1>
27+
28+
<a href="/editor/new">Add New Word</a>
2829
</main>
2930
</BaseLayout>

src/pages/editor/new/index.astro

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
import BaseLayout from "../../../layouts/base.astro";
3+
import doAuth from "../../../lib/actions/do-auth.js";
4+
import Navbar from "../../../components/navbar.astro";
5+
import { $userData } from "../../../lib/stores/user.js";
6+
import doOctokitAuth from "../../../lib/actions/do-octokit-auth";
7+
import { resolveEditorActionFromPathname } from "../../../lib/utils/index.js";
8+
import WordEditor, { SubmitButton as WordEditorSubmitButton } from "../../../components/islands/word-editor.jsx";
9+
10+
const { url: { pathname }, redirect } = Astro;
11+
12+
const { isAuthed, authedData: userData } = await doAuth(Astro);
13+
if (!isAuthed) return redirect(`/login?return_to=${encodeURIComponent(pathname)}`);
14+
// @ts-expect-error
15+
$userData.set(userData);
16+
17+
const octokitAuths = doOctokitAuth(Astro);
18+
const action = resolveEditorActionFromPathname(pathname);
19+
---
20+
21+
<BaseLayout
22+
pageTitle="Dictionry"
23+
class="flex flex-col w-full min-h-screen overflow-hidden"
24+
>
25+
<Navbar>
26+
<WordEditorSubmitButton client:load />
27+
</Navbar>
28+
29+
<main class="flex grow px-5 md:px-6 pb-4">
30+
<WordEditor
31+
action={action}
32+
octokitAuths={octokitAuths}
33+
client:load
34+
/>
35+
</main>
36+
</BaseLayout>

0 commit comments

Comments
 (0)