Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add modals for CRUD operations #48

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
733b249
Add input/display fields to the modal according to the action
NicoBiernat Oct 9, 2024
e233aa7
Change user state type to non-optional
NicoBiernat Oct 9, 2024
3be4b3c
Only show password field description on edit action
NicoBiernat Oct 9, 2024
ddb2bed
Split UserModal into different modals for Add, Details, Edit and Delete
NicoBiernat Oct 16, 2024
180cc79
Merge branch 'main' into user-role-key-management
NicoBiernat Oct 16, 2024
4932d26
Wrap callbacks in useCallback and refactor
NicoBiernat Oct 16, 2024
e2dba42
Merge branch 'main' into user-role-key-management
NicoBiernat Oct 16, 2024
c2a342d
Make fields in types required
NicoBiernat Oct 19, 2024
65b4ba7
Add api implementation for user CRUD
NicoBiernat Oct 19, 2024
14d545b
Fix ModelContext use getAllUsers instead of getPublicUsers
NicoBiernat Oct 19, 2024
b6510e2
Fetch data and access the API
NicoBiernat Oct 19, 2024
e20ba74
Implement remaining API routes in AuthContext
NicoBiernat Nov 12, 2024
efe5934
Add views for roles and registration keys
NicoBiernat Nov 12, 2024
097b589
Fix request loop caused by useEffect dependency on user that also set…
NicoBiernat Nov 12, 2024
202ba32
Hide unused options card
NicoBiernat Feb 11, 2025
c125b9e
Fix error handling when fetching metrics
NicoBiernat Feb 11, 2025
9c12d88
Add mouse input toggle
NicoBiernat Feb 11, 2025
99a400e
Hide admin area in sidebar from normal users and rename Monitor -> Mo…
NicoBiernat Feb 11, 2025
d7633e4
Add roles field to the generated API user type and convert the roles …
NicoBiernat Feb 11, 2025
baf9a36
Implement mouse events (and dragging) with window coordinates
NicoBiernat Feb 11, 2025
ca03b48
Implement the monitoring view to display the metrics data interactively
NicoBiernat Feb 11, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/api/auth/types/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,25 @@ export interface User {
permanentApiToken?: boolean;
registrationKey?: RegistrationKey;
}

export function newUninitializedUser(): User {
return {
id: 0,
username: '',
email: '',
roles: [],
createdAt: new Date(0),
updatedAt: new Date(0),
lastSeen: new Date(0),
permanentApiToken: false,
registrationKey: {
id: 0,
key: '',
description: '',
createdAt: new Date(0),
updatedAt: new Date(0),
expiresAt: new Date(0),
permanent: false,
},
};
}
105 changes: 105 additions & 0 deletions src/components/UserAddModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { User } from '@luna/api/auth/types';
import {
Button,
Checkbox,
Input,
Modal,
ModalBody,
ModalContent,
ModalFooter,
ModalHeader,
} from '@nextui-org/react';
import { useEffect, useState } from 'react';

export interface UserAddModalProps {
show: boolean;
setShow: (show: boolean) => void;
NicoBiernat marked this conversation as resolved.
Show resolved Hide resolved
}

export function UserAddModal({ show, setShow }: UserAddModalProps) {
const [user, setUser] = useState<User>({ username: '' });
const [password, setPassword] = useState('');

// initialize/reset modal state
useEffect(() => {
setUser({
username: '',
});
setPassword('');
}, [show]);
NicoBiernat marked this conversation as resolved.
Show resolved Hide resolved

const addUser = () => {
const payload = {
username: user.username,
password,
email: user.email,
permanent_api_token: user.permanentApiToken,
};
console.log('adding user:', payload);
// TODO: call POST /users
// TODO: feedback from the request (success, error)
onOpenChange(false);
};
NicoBiernat marked this conversation as resolved.
Show resolved Hide resolved

const onOpenChange = (isOpen: boolean) => {
if (!isOpen) {
setUser({ username: '' });
setPassword('');
}
NicoBiernat marked this conversation as resolved.
Show resolved Hide resolved
setShow(isOpen);
};
NicoBiernat marked this conversation as resolved.
Show resolved Hide resolved

return (
<Modal isOpen={show} onOpenChange={onOpenChange}>
<ModalContent>
{onClose => (
<>
<ModalHeader>Add User</ModalHeader>
<ModalBody>
<Input
label="Username"
value={user.username}
onValueChange={username => {
if (!user) return;
setUser({ ...user, username });
}}
/>
<Input
type="password"
label="Password"
value={password}
onValueChange={setPassword}
/>
<Input
label="E-Mail"
value={user.email}
onValueChange={email => {
if (!user) return;
setUser({ ...user, email });
}}
/>
<Checkbox
isSelected={user.permanentApiToken}
onValueChange={permanentApiToken => {
if (!user) return;
setUser({
...user,
permanentApiToken,
});
}}
>
Permanent API Token
</Checkbox>
</ModalBody>
<ModalFooter>
<Button color="success" onPress={addUser}>
Add
</Button>
<Button onPress={onClose}>Cancel</Button>
</ModalFooter>
</>
)}
</ModalContent>
</Modal>
);
}
149 changes: 149 additions & 0 deletions src/components/UserDeleteModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import {
newUninitializedUser,
RegistrationKey,
Role,
User,
} from '@luna/api/auth/types';
import {
Button,
Checkbox,
Input,
Modal,
ModalBody,
ModalContent,
ModalFooter,
ModalHeader,
} from '@nextui-org/react';
import { useEffect, useState } from 'react';

export interface UserDeleteModalProps {
id: number;
show: boolean;
setShow: (show: boolean) => void;
}

export function UserDeleteModal({ id, show, setShow }: UserDeleteModalProps) {
const [user, setUser] = useState<User>(newUninitializedUser());

// initialize modal state
useEffect(() => {
// only initialize when the modal is shown
if (!show) return;

// TODO: remove test data and query the API
const now = new Date();
// TODO: call GET /users/<id>/roles
const roles: Role[] = [
{
id: 1,
name: 'Testrole',
createdAt: now,
updatedAt: now,
},
];
// TODO: call GET /users/<id>
const registrationKey: RegistrationKey = {
id: 1,
key: 'Test-Registration-Key',
description: 'Test-Registration-Key for testing purposes',
createdAt: now,
updatedAt: now,
expiresAt: now,
permanent: false,
};
const user: User = {
username: 'Testuser',
email: '[email protected]',
roles,
createdAt: now,
updatedAt: now,
lastSeen: now,
permanentApiToken: false,
registrationKey,
};

setUser(user);
}, [id, show]);

const deleteUser = () => {
NicoBiernat marked this conversation as resolved.
Show resolved Hide resolved
console.log('deleting user with id', id);
// TODO: call DELETE /users/<id>
// TODO: feedback from the request (success, error)
onOpenChange(false);
};

const onOpenChange = (isOpen: boolean) => {
if (!isOpen) {
setUser(newUninitializedUser());
}
setShow(isOpen);
};

return (
<Modal isOpen={show} onOpenChange={onOpenChange}>
<ModalContent>
{onClose => (
<>
<ModalHeader>Delete User</ModalHeader>
<ModalBody>
<Input label="ID" value={id.toString()} isDisabled />

<Input
label="Username"
value={user.username}
onValueChange={username => {
if (!user) return;
setUser({ ...user, username });
}}
isDisabled
/>
<Input
label="E-Mail"
value={user.email}
onValueChange={email => {
if (!user) return;
setUser({ ...user, email });
}}
isDisabled
/>
<Input
label="Created At"
value={user.createdAt?.toLocaleString()}
isDisabled
/>
<Input
label="Updated At"
value={user.updatedAt?.toLocaleString()}
isDisabled
/>
<Input
label="Last Login"
value={user.lastSeen?.toLocaleString()}
isDisabled
/>
<Checkbox
isSelected={user.permanentApiToken}
onValueChange={permanentApiToken => {
if (!user) return;
setUser({
...user,
permanentApiToken,
});
}}
isDisabled
>
Permanent API Token
</Checkbox>
</ModalBody>
<ModalFooter>
<Button color="danger" onPress={deleteUser}>
Delete
</Button>
<Button onPress={onClose}>Cancel</Button>
</ModalFooter>
</>
)}
</ModalContent>
</Modal>
);
}
Loading