Skip to content

Commit

Permalink
(feat): user deletion via ui
Browse files Browse the repository at this point in the history
  • Loading branch information
camwhit-e committed Nov 1, 2024
1 parent 5ac5ca7 commit 5f7ec87
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 3 deletions.
33 changes: 33 additions & 0 deletions app/Console/Commands/CreateUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Influx\Console\Commands;

use Influx\Models\User;
use Illuminate\Support\Str;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Hash;

class CreateUser extends Command
{
protected $signature = 'user:create';
protected $description = 'Create a new user and return login details';

public function handle()
{
$isSuperuser = $this->confirm('Should this user be a superuser?');
$username = $this->ask('What is the username?');
$email = $this->ask('What is the email?');
$password = $this->secret('What is the password?');

$user = User::create([
'name' => $username,
'email' => $email,
'password' => Hash::make($password),
'is_superuser' => $isSuperuser,
]);

$this->table(['ID', 'Username', 'Email', 'Superuser'], [
[$user->id, $user->name, $user->email, $user->is_superuser ? 'Yes' : 'No'],
]);
}
}
17 changes: 17 additions & 0 deletions app/Http/Controllers/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Inertia\Response;
use Influx\Models\User;
use Illuminate\Http\Request;
use Illuminate\Http\RedirectResponse;
use Illuminate\Contracts\Hashing\Hasher;

class UsersController extends Controller
Expand Down Expand Up @@ -91,4 +92,20 @@ public function update(Request $request, int $id): Response

return Inertia::render('Users/Edit', ['user' => $user]);
}

/**
* Delete a user account.
*/
public function delete(int $id): RedirectResponse
{
$user = User::findOrFail($id);

try {
$user->delete();
} catch (\Exception $ex) {
throw new \Exception($ex->getMessage());
};

return redirect()->intended(route('users.index', absolute: false));;
}
}
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions resources/js/Pages/Users/DeleteUserForm.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { useState } from 'react';
import Modal from '@/Components/Modal';
import { router } from '@inertiajs/react';
import DangerButton from '@/Components/DangerButton';
import SecondaryButton from '@/Components/SecondaryButton';

export default function DeleteUserForm({ id }: { id: number }) {
const [open, setOpen] = useState(false);
const [submitting, setSubmitting] = useState(false);

const deleteUser = () => {
setSubmitting(true);

router.delete(`/users/${id}`, {
onError: (error) => console.error(error)
});
};

return (
<>
<DangerButton onClick={() => setOpen(true)} className={'mr-4'}>
Delete
</DangerButton>

<Modal show={open} onClose={() => setOpen(false)}>
<div className="p-6">
<h2 className="text-lg font-medium text-gray-900 dark:text-gray-100">
Are you sure?
</h2>

<p className="mt-1 text-sm text-gray-600 dark:text-gray-400">
Deleting this user account will revoke their access immediately.
</p>

<div className="mt-6 flex justify-end">
<SecondaryButton type={'button'} onClick={() => setOpen(false)}>
Cancel
</SecondaryButton>

<DangerButton type={'button'} className="ms-3" disabled={submitting} onClick={deleteUser}>
Delete Account
</DangerButton>
</div>
</div>
</Modal>
</>
);
}
7 changes: 6 additions & 1 deletion resources/js/Pages/Users/UserForm.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import DangerButton from '@/Components/DangerButton';
import PrimaryButton from '@/Components/PrimaryButton';
import { User } from '@/types';
import { router } from '@inertiajs/react';
import { useState } from 'react';
import DeleteUserForm from './DeleteUserForm';

export default function UserForm({ user }: { user?: User }) {
const [values, setValues] = useState({
Expand Down Expand Up @@ -106,7 +108,10 @@ export default function UserForm({ user }: { user?: User }) {
</div>
</div>
<div className={'text-right'}>
<PrimaryButton type={'submit'}>Submit</PrimaryButton>
{user && (
<DeleteUserForm id={user!.id} />
)}
<PrimaryButton type={'submit'}>{user ? 'Update' : 'Create'} User</PrimaryButton>
</div>
</form>
);
Expand Down
2 changes: 1 addition & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
Route::get('/', function () {
return Inertia::render('Welcome', [
'canLogin' => Route::has('login'),
'canRegister' => Route::has('register'),
'laravelVersion' => Application::VERSION,
'phpVersion' => PHP_VERSION,
]);
Expand All @@ -28,6 +27,7 @@

Route::get('/{user:id}', [UsersController::class, 'view'])->name('users.view');
Route::put('/{user:id}', [UsersController::class, 'update'])->name('users.update');
Route::delete('/{user:id}', [UsersController::class, 'delete'])->name('users.delete');
});

Route::prefix('/profile')->group(function () {
Expand Down

0 comments on commit 5f7ec87

Please sign in to comment.