Skip to content

Commit

Permalink
Add user management screen
Browse files Browse the repository at this point in the history
Fixes #2

Add user management functionality to the settings page.

* **Beam.Client/Pages/Settings.razor**
  - Add a user management section with a list of users and options to add, edit, and delete users.
  - Add a form to add new users.
  - Add buttons to edit and delete existing users.
  - Update the `OnInitializedAsync` method to fetch the list of users.
  - Add methods `AddUser`, `EditUser`, and `DeleteUser` to handle user management actions.
  - Add a `prompt` method to get user input for editing user names.

* **Beam.Client/Services/DataService.cs**
  - Add methods `AddUser`, `EditUser`, and `DeleteUser` to manage users.
  - Add a method `GetUsers` to fetch the list of users.
  - Add an event `UpdatedUsers` to notify when the user list is updated.

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/octodemo/BeamCodespacesDemo/issues/2?shareId=XXXX-XXXX-XXXX-XXXX).
  • Loading branch information
Dedac committed Sep 23, 2024
1 parent 11bfad8 commit 43480e1
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
57 changes: 56 additions & 1 deletion Beam.Client/Pages/Settings.razor
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,72 @@
<button @onclick="UpdateUser" type="button" class="btn btn-outline-secondary">Update</button>
</div>
</div>

<h3>User Management</h3>
<div>
<ul>
@foreach (var user in users)
{
<li>
@user.Name
<button @onclick="(() => EditUser(user))" class="btn btn-outline-secondary">Edit</button>
<button @onclick="(() => DeleteUser(user))" class="btn btn-outline-danger">Delete</button>
</li>
}
</ul>
</div>

<h4>Add New User</h4>
<div class="input-group">
<input @bind="newUserName" class="form-control" placeholder="Enter new user name" />
<div class="input-group-append">
<button @onclick="AddUser" type="button" class="btn btn-outline-secondary">Add</button>
</div>
</div>

<Beam.Animation.AnimatedBeam />

@code{
private string name = "";
private string newUserName = "";
private List<User> users = new List<User>();

protected override void OnInitialized()
protected override async Task OnInitializedAsync()
{
name = data.CurrentUser.Name;
users = await data.GetUsers();
}

async Task UpdateUser()
{
await data.GetOrCreateUser(name);
}

async Task AddUser()
{
await data.AddUser(newUserName);
newUserName = "";
users = await data.GetUsers();
}

async Task EditUser(User user)
{
var newName = prompt("Enter new name for user", user.Name);
if (!string.IsNullOrEmpty(newName))
{
await data.EditUser(user.Id, newName);
users = await data.GetUsers();
}
}

async Task DeleteUser(User user)
{
await data.DeleteUser(user.Id);
users = await data.GetUsers();
}

private string? prompt(string message, string defaultValue)
{
return Microsoft.JSInterop.JSRuntime.Current.InvokeAsync<string>("prompt", message, defaultValue).Result;
}
}
23 changes: 23 additions & 0 deletions Beam.Client/Services/DataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public DataService(BeamApiService apiService)

public event Action? UdpatedFrequencies;
public event Action? UpdatedRays;
public event Action? UpdatedUsers;

public async Task GetFrequencies()
{
Expand Down Expand Up @@ -102,5 +103,27 @@ public async Task<List<Ray>> GetUserRays(string name)
return await _apiService.UserRays(name ?? CurrentUser.Name);
}

public async Task AddUser(string name)
{
await _apiService.AddUser(new User() { Name = name });
UpdatedUsers?.Invoke();
}

public async Task EditUser(int id, string newName)
{
await _apiService.EditUser(id, newName);
UpdatedUsers?.Invoke();
}

public async Task DeleteUser(int id)
{
await _apiService.DeleteUser(id);
UpdatedUsers?.Invoke();
}

public async Task<List<User>> GetUsers()
{
return await _apiService.GetUsers();
}
}
}

0 comments on commit 43480e1

Please sign in to comment.