Initial commit: NestJS NextJS pnpm monorepo
This commit is contained in:
commit
610dcf202b
175 files changed
+25172
No files matched your search
@@ -0,0 +1,66 @@
|
||||
"use client"
|
||||
|
||||
import { useState } from "react";
|
||||
import { LocationTreeNode } from "../../types/inventory";
|
||||
|
||||
type Props = {
|
||||
node: LocationTreeNode;
|
||||
selectedId: string | null;
|
||||
focusedId: string | null;
|
||||
openMap: Record<string, boolean>;
|
||||
setOpenMap: React.Dispatch<React.SetStateAction<Record<string, boolean>>>;
|
||||
setFocusedId: (id: string) => void;
|
||||
onSelect: (id: string ) => void;
|
||||
};
|
||||
|
||||
export function LocationTreeNodeItem({ node, selectedId, focusedId, openMap, setOpenMap, setFocusedId, onSelect }: Props) {
|
||||
|
||||
const hasChildren = node.children.length > 0;
|
||||
const isOpen = openMap[node._id];
|
||||
|
||||
return (
|
||||
<div className="ml-4" >
|
||||
<div className="flex items-center gap-2">
|
||||
{hasChildren && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setOpenMap(m => ({ ...m, [node._id]: !isOpen }))}
|
||||
className="w-6"
|
||||
>
|
||||
{isOpen ? "▼" : "▶"}
|
||||
</button>
|
||||
)}
|
||||
|
||||
{!hasChildren && <span className="w-6" />}
|
||||
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => onSelect(node._id)}
|
||||
onFocus={() => setFocusedId(node._id)}
|
||||
className={`text-left px-1 rounded ${selectedId === node._id ? "font-bold text-blue-600" : ""
|
||||
} ${focusedId === node._id ? "bg-blue-100" : ""
|
||||
}`}
|
||||
>
|
||||
{node.name}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{isOpen && hasChildren && (
|
||||
<ul className="ml-6 mt-1 border-l pl-2">
|
||||
{node.children.map(child => (
|
||||
<LocationTreeNodeItem
|
||||
key={child._id}
|
||||
node={child}
|
||||
selectedId={selectedId}
|
||||
focusedId={focusedId}
|
||||
openMap={openMap}
|
||||
setOpenMap={setOpenMap}
|
||||
setFocusedId={setFocusedId}
|
||||
onSelect={onSelect}
|
||||
/>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react"
|
||||
import { Controller, useForm } from "react-hook-form"
|
||||
import { InputTextR } from "../ui/inputTextR";
|
||||
import { emptyLocation, Location, LocationCreate, LocationMutaion, LocationTreeNode, LocationUpdate } from "../../types/inventory";
|
||||
import { locationCreate, locationTree, locationUpdate } from "../../actions/location.actions";
|
||||
|
||||
import { useRouter } from "next/navigation"
|
||||
import { sites } from "../../_config/sites";
|
||||
import { LocationTreeView } from "./location.treeView";
|
||||
|
||||
interface Params {
|
||||
initData?: Location
|
||||
debug?: boolean
|
||||
|
||||
}
|
||||
|
||||
export const LocationForm = ({ initData, debug }: Params) => {
|
||||
|
||||
const [locationTreeData, setLocationTree] = useState<LocationTreeNode[]>()
|
||||
|
||||
useEffect(() => {
|
||||
const load = async () => {
|
||||
|
||||
let d = await locationTree();
|
||||
setLocationTree(d);
|
||||
}
|
||||
load();
|
||||
|
||||
if (initData) {
|
||||
const { _id, name, description, parentLocationId } = initData;
|
||||
setId(_id);
|
||||
setModeUpdate(true);
|
||||
reset({ name, description, parentLocationId })
|
||||
}
|
||||
|
||||
}, [initData])
|
||||
|
||||
|
||||
const router = useRouter();
|
||||
const {
|
||||
register,
|
||||
handleSubmit,
|
||||
formState: { errors },
|
||||
watch,
|
||||
control,
|
||||
getFieldState,
|
||||
|
||||
reset }
|
||||
= useForm<LocationMutaion>({ defaultValues: emptyLocation })
|
||||
|
||||
const [modeUpdate, setModeUpdate] = useState(false);
|
||||
const [id, setId] = useState<string | undefined>();
|
||||
const [error, setError] = useState<string | undefined>();
|
||||
|
||||
|
||||
|
||||
const onSubmit = async (data: LocationMutaion) => {
|
||||
setError(undefined);
|
||||
|
||||
if (modeUpdate) {
|
||||
if (id) {
|
||||
locationUpdate(id, data);
|
||||
} else {
|
||||
setError('Etwas ist falsch, keine ID');
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
const res = await locationCreate(data);
|
||||
/* if (!res.ok) {
|
||||
setError("_id")
|
||||
return
|
||||
}
|
||||
*/
|
||||
router.push(sites.location.url)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="w-120 flex flex-col p-4 pt-10 ps-10" >
|
||||
{modeUpdate ? <div>Update</div> : <div>Anlegen</div>}
|
||||
<div className="border-1 p-4">
|
||||
|
||||
<form onSubmit={handleSubmit(onSubmit)} className="max-w-md mx-auto">
|
||||
<InputTextR
|
||||
label="Lagerort Bezeichnung"
|
||||
{...register("name", { required: "Pflichtfeld" })}
|
||||
error={errors.name?.message}
|
||||
/>
|
||||
|
||||
<InputTextR
|
||||
label="Beschreibung"
|
||||
{...register("description")}
|
||||
error={errors.description?.message}
|
||||
/>
|
||||
{locationTreeData &&
|
||||
<Controller
|
||||
name="parentLocationId"
|
||||
control={control}
|
||||
rules={{ required: false }}
|
||||
render={({ field }) => (
|
||||
<LocationTreeView data={locationTreeData}
|
||||
onSelect={(id) => field.onChange(id)}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
}
|
||||
|
||||
<div>
|
||||
{error && <div>{error}</div>}
|
||||
{errors.name && <div>{errors.name.message}</div>}
|
||||
</div>
|
||||
|
||||
<button
|
||||
className="bg-blue-500 text-white p-2 rounded mt-2 hover:bg-blue-600"
|
||||
type="submit">
|
||||
Speichern
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
{debug &&
|
||||
<div>
|
||||
<div>{watch("name")}</div>
|
||||
<pre>touched:{getFieldState("name").isTouched ? 'true' : 'false'}</pre>
|
||||
<pre>{JSON.stringify(watch(), null, 2)}</pre>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
"use client"
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import { LocationTreeNode } from "../../types/inventory";
|
||||
import { LocationTreeNodeItem } from "./LocationTreeNodeItem";
|
||||
|
||||
|
||||
|
||||
type Props = {
|
||||
data: LocationTreeNode[];
|
||||
onSelect: (node: LocationTreeNode) => void;
|
||||
};
|
||||
|
||||
|
||||
export function LocationTreeView({ data, onSelect }: Props) {
|
||||
const [selectedId, setSelectedId] = useState<string | null>(null);
|
||||
const [focusedId, setFocusedId] = useState<string | null>(null);
|
||||
const [openMap, setOpenMap] = useState<Record<string, boolean>>({});
|
||||
|
||||
// sichtbare Nodes flach machen
|
||||
const visibleNodes = useMemo(() => {
|
||||
const result: LocationTreeNode[] = [];
|
||||
|
||||
function walk(nodes: LocationTreeNode[]) {
|
||||
for (const n of nodes) {
|
||||
result.push(n);
|
||||
if (openMap[n._id]) {
|
||||
walk(n.children);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
walk(data);
|
||||
return result;
|
||||
}, [data, openMap]);
|
||||
|
||||
const handleSelect = (id:string)=>{
|
||||
let node = visibleNodes.find((node)=> node._id === id)
|
||||
console.log('nodex')
|
||||
if(node) onSelect(node);
|
||||
}
|
||||
|
||||
// initial focus
|
||||
useEffect(() => {
|
||||
if (!focusedId && visibleNodes.length > 0) {
|
||||
setFocusedId(visibleNodes[0]._id);
|
||||
}
|
||||
console.log(visibleNodes)
|
||||
}, [visibleNodes, focusedId]);
|
||||
|
||||
function handleKeyDown(e: React.KeyboardEvent) {
|
||||
if (!focusedId) return;
|
||||
|
||||
const index = visibleNodes.findIndex(n => n._id === focusedId);
|
||||
|
||||
if (e.key === "ArrowDown") {
|
||||
console.log('down')
|
||||
e.preventDefault();
|
||||
const next = visibleNodes[index + 1];
|
||||
if (next) setFocusedId(next._id);
|
||||
}
|
||||
|
||||
if (e.key === "ArrowUp") {
|
||||
console.log('up')
|
||||
e.preventDefault();
|
||||
const prev = visibleNodes[index - 1];
|
||||
if (prev) setFocusedId(prev._id);
|
||||
}
|
||||
|
||||
if (e.key === "ArrowRight") {
|
||||
console.log('right')
|
||||
e.preventDefault();
|
||||
setOpenMap(m => ({ ...m, [focusedId]: true }));
|
||||
}
|
||||
|
||||
if (e.key === "ArrowLeft") {
|
||||
console.log('left')
|
||||
e.preventDefault();
|
||||
setOpenMap(m => ({ ...m, [focusedId]: false }));
|
||||
}
|
||||
|
||||
if (e.key === "Enter") {
|
||||
e.preventDefault();
|
||||
setSelectedId(focusedId);
|
||||
handleSelect(focusedId);
|
||||
}
|
||||
if (e.key === "Space") {
|
||||
e.preventDefault();
|
||||
setSelectedId(focusedId);
|
||||
handleSelect(focusedId);
|
||||
}
|
||||
console.log(e.key)
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
tabIndex={0}
|
||||
onKeyDown={handleKeyDown}
|
||||
className="border p-2 rounded outline-none focus:ring"
|
||||
>
|
||||
{data.map(node => (
|
||||
<LocationTreeNodeItem
|
||||
key={node._id}
|
||||
node={node}
|
||||
selectedId={selectedId}
|
||||
focusedId={focusedId}
|
||||
openMap={openMap}
|
||||
setOpenMap={setOpenMap}
|
||||
setFocusedId={setFocusedId}
|
||||
onSelect={(id) => { setSelectedId(id); setFocusedId(id); handleSelect(id); }} />
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { LocationTreeNode, Location } from "../../types/inventory";
|
||||
import { LocationTreeView } from "./location.treeView";
|
||||
import { locationFindById } from "../../actions/location.actions";
|
||||
import { LocationForm } from "./location.form";
|
||||
|
||||
|
||||
interface Params {
|
||||
data: LocationTreeNode[]
|
||||
}
|
||||
|
||||
export function LocationView({ data }: Params) {
|
||||
const [selectedLocation, setSelectedLocation] = useState<Location | undefined>();
|
||||
|
||||
const handleSelect = async (node: LocationTreeNode) => {
|
||||
console.log('node',node);
|
||||
// const node = await locationFindById(value);
|
||||
setSelectedLocation(() => node);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="w-120 flex flex-col p-4 pt-10 ps-10">
|
||||
<div>xxx</div>
|
||||
<LocationTreeView data={data} onSelect={(item) => handleSelect(item)}></LocationTreeView>
|
||||
{selectedLocation &&
|
||||
<LocationForm initData={selectedLocation}></LocationForm>
|
||||
}
|
||||
</div>
|
||||
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user