-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(client): move aside menu component to server side gen and fi…
…x sync with current entry list when deleting or creating new enttries
- Loading branch information
1 parent
840c688
commit af94722
Showing
6 changed files
with
48 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 13 additions & 82 deletions
95
apps/client/src/components/feature/aside-nav/aside-nav.component.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 23 additions & 59 deletions
82
apps/client/src/components/feature/aside-nav/components/entry-list/entry-list.component.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,33 @@ | ||
"use client"; | ||
|
||
import { EntryService } from "@/models/api/entry"; | ||
import { ClientRoutingService } from "@/models/routing/client"; | ||
import type { Entry } from "@/types/entry"; | ||
import Link from "next/link"; | ||
import { usePathname } from "next/navigation"; | ||
import { useEffect, useState } from "react"; | ||
|
||
export function EntryList() { | ||
const [entryList, setEntryList] = useState<Entry[]>([]); | ||
|
||
useEffect(() => { | ||
async function fetchEntryList(signal: AbortSignal) { | ||
const { entryList, error } = await EntryService.getUserEntyList({ | ||
signal, | ||
}); | ||
|
||
if (error || !entryList || entryList.length === 0) return; | ||
import { EntryItem } from "./entry-item.component"; | ||
import { getCookie } from "@/utils/getCookies"; | ||
|
||
setEntryList(entryList); | ||
} | ||
export async function EntryList() { | ||
const { cookie } = getCookie(); | ||
|
||
const ctrl = new AbortController(); | ||
if (!cookie) return <p>something went wrong</p>; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
fetchEntryList(ctrl.signal); | ||
|
||
return () => { | ||
ctrl.abort(); | ||
}; | ||
}, []); | ||
const { entryList, error } = await EntryService.getUserEntyList({ cookie }); | ||
console.log(entryList); | ||
|
||
return ( | ||
<ul className="w-full flex flex-col gap-2"> | ||
{entryList.length > 0 && | ||
entryList.map((entry) => ( | ||
<li key={entry.id}> | ||
<EntryItem entry={entry} /> | ||
</li> | ||
))} | ||
{!error && | ||
entryList && | ||
entryList.length > 0 && | ||
entryList | ||
.sort((entry, nextEntry) => | ||
new Date(entry.updated_at).getTime() > | ||
new Date(nextEntry.updated_at).getTime() | ||
? -1 | ||
: 1, | ||
) | ||
.map((entry) => ( | ||
<li key={entry.id}> | ||
<EntryItem entry={entry} /> | ||
</li> | ||
))} | ||
{error && <p>something went wrong</p>} | ||
</ul> | ||
); | ||
} | ||
|
||
function EntryItem({ entry }: { entry: Entry }) { | ||
const [isActive, setIsActive] = useState<boolean>(false); | ||
|
||
const pathName = usePathname(); | ||
|
||
useEffect(() => { | ||
if (pathName === ClientRoutingService.app.entries.readById(entry.id)) { | ||
setIsActive(true); | ||
return; | ||
} | ||
|
||
setIsActive(false); | ||
}, [pathName, entry.id]); | ||
|
||
return ( | ||
<Link href={ClientRoutingService.app.entries.readById(entry.id)}> | ||
<article | ||
className={`p-2 hover:bg-zinc-200 ${isActive ? "bg-zinc-200" : ""} rounded-sm transition-all duration-150`} | ||
> | ||
<span className="font-semibold">{entry.title}</span> | ||
</article> | ||
</Link> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters