added possible-user-tasks

This commit is contained in:
z1glr
2025-01-28 22:23:12 +00:00
parent f7fbc4dfc2
commit 2c005e61a6
13 changed files with 338 additions and 232 deletions

View File

@@ -26,17 +26,21 @@ export interface TaskAssignment {
userName: string | null;
}
export interface User {
export interface StateUser {
userName: string;
admin: boolean;
}
export type User = StateUser & {
possibleTasks: number[];
};
export type UserAddModify = User & {
password: string;
};
interface Zustand {
user: User | null;
user: StateUser | null;
tasks?: Task[];
availabilities?: Availability[];
patch: (zustand?: Partial<Zustand>) => void;

View File

@@ -1,7 +1,7 @@
"use client";
import { apiCall } from "@/lib";
import zustand from "@/Zustand";
import zustand, { StateUser } from "@/Zustand";
import { Spinner } from "@heroui/react";
import { usePathname, useRouter } from "next/navigation";
import React, { useEffect, useState } from "react";
@@ -23,10 +23,7 @@ export default function Main({ children }: { children: React.ReactNode }) {
void (async () => {
let loggedIn = false;
const welcomeResult = await apiCall<{
userName: string;
loggedIn: boolean;
}>("GET", "welcome");
const welcomeResult = await apiCall<StateUser>("GET", "welcome");
if (welcomeResult.ok) {
try {

View File

@@ -1,11 +1,13 @@
import {
AllString,
classNames,
getTasks,
validatePassword as validatePassword,
} from "@/lib";
import zustand, { User, UserAddModify } from "@/Zustand";
import {
Checkbox,
CheckboxGroup,
Form,
Input,
Modal,
@@ -14,6 +16,7 @@ import {
ModalFooter,
ModalHeader,
} from "@heroui/react";
import { useAsyncList } from "@react-stately/data";
import React, { FormEvent, useState } from "react";
export default function UserEditor(props: {
@@ -26,8 +29,19 @@ export default function UserEditor(props: {
onSubmit: (user: UserAddModify) => void;
}) {
const [name, setName] = useState(props.value?.userName ?? "");
const [admin, setAdmin] = useState(props.value?.admin ?? false);
const [password, setPassword] = useState("");
const [admin, setAdmin] = useState(props.value?.admin ?? false);
const [possibleTasks, setPossibleTasks] = useState<string[]>(
props.value?.possibleTasks.map((t) => t.toString()) ?? [],
);
const tasks = useAsyncList({
async load() {
return {
items: await getTasks(),
};
},
});
const pwErrors = validatePassword(password);
@@ -39,6 +53,7 @@ export default function UserEditor(props: {
const data = {
...formData,
possibleTasks: possibleTasks.map((t) => parseInt(t)),
admin: formData.admin !== undefined,
};
@@ -120,6 +135,17 @@ export default function UserEditor(props: {
>
Admin
</Checkbox>
<CheckboxGroup
label="Assignable Tasks"
value={possibleTasks}
onValueChange={setPossibleTasks}
>
{tasks.items.map((task) => (
<Checkbox key={task.taskID} value={task.taskID?.toString()}>
{task.taskName}
</Checkbox>
))}
</CheckboxGroup>
</ModalBody>
<ModalFooter>{props.footer}</ModalFooter>
</ModalContent>

View File

@@ -2,7 +2,7 @@
import CheckboxIcon from "@/components/CheckboxIcon";
import { apiCall } from "@/lib";
import zustand from "@/Zustand";
import zustand, { StateUser } from "@/Zustand";
import {
ViewFilled,
ViewOffFilled,
@@ -21,7 +21,7 @@ export default function Login() {
async function sendLogin(e: FormEvent<HTMLFormElement>) {
const data = Object.fromEntries(new FormData(e.currentTarget));
const result = await apiCall("POST", "login", undefined, data);
const result = await apiCall<StateUser>("POST", "login", undefined, data);
if (result.ok) {
// add the user-info to the zustand