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

Clear image list cache #189

Merged
merged 15 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
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
5 changes: 5 additions & 0 deletions server/assets/css/phoenix.css
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ select {
width: auto;
}

label {
font-weight:300;
display:inline;
}

/* Phoenix promo and logo */
.phx-hero {
text-align: center;
Expand Down
1 change: 1 addition & 0 deletions server/lib/field_hub/file_store.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule FieldHub.FileStore do
@valid_file_variants Application.compile_env(:field_hub, :valid_file_variants)
@index_cache_name Application.compile_env(:field_hub, :file_index_cache_name)
@index_cache_expiration_ms 1000 * 60 * 60 * 24
# @index_cache_expiration_ms 3000
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What did you try here? This line should probably be deleted at the end.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I wanted to see what happened without wait one day.


require Logger

Expand Down
12 changes: 8 additions & 4 deletions server/lib/field_hub_web/controllers/api/project_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ defmodule FieldHubWeb.Api.ProjectController do

@identifier_length Application.compile_env(:field_hub, :max_project_identifier_length)

@moduledoc """
This API controller module handles the HTTP requests for listing, creating or deleting projects.
"""

def index(%{assigns: %{current_user: user_name}} = conn, _params) do
render(conn, "list.json", %{projects: Project.get_all_for_user(user_name)})
end
Expand Down Expand Up @@ -86,12 +90,12 @@ defmodule FieldHubWeb.Api.ProjectController do
end

def delete(conn, %{"project" => id}) do
project = Project.delete(id)
user = User.delete(id)
project_deletion_result = Project.delete(id)
user_deletion_result = User.delete(id)

response_payload = %{
status_project: project,
status_user: user
status_project: project_deletion_result,
status_user: user_deletion_result
}

conn
Expand Down
74 changes: 74 additions & 0 deletions server/lib/field_hub_web/live/project_show_live.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
defmodule FieldHubWeb.ProjectShowLive do
alias FieldHub.Project

alias FieldHubWeb.{
Router.Helpers,
UserAuth,
Expand Down Expand Up @@ -42,6 +44,9 @@ defmodule FieldHubWeb.ProjectShowLive do
|> assign(:project, project)
|> assign(:current_user, user_name)
|> assign(:new_password, "")
|> assign(:confirm_project_name, "")
|> assign(:delete_files, false)
|> assign(:cache,"")
|> read_project_doc()
}

Expand Down Expand Up @@ -112,6 +117,75 @@ defmodule FieldHubWeb.ProjectShowLive do
{:noreply, assign(socket, :new_password, password)}
end

# def handle_event("delete_cache", %{"project" => project} = _values, socket) do
def handle_event("delete_cache", _values, %{assigns: %{project: project}}=socket) do
new_cache = "Delete or update cache in this handle_event"
cache_index = FieldHub.FileStore.file_index(project)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the ticket you do not need to create a new cache, that is already handled by the FileStore internally.

The only thing that has to be triggered from the interface is the FileStore's function to clear the existing cache. If you want to have a look how the file index looks like, you could for example use the Desktop client to push new images into the repository.



IO.inspect(cache_index)
{:noreply, assign(socket, :cache, new_cache)}
# {:noreply, assign(socket, :cache, inspect(cache_index))}
end

def handle_event(
"delete_form_change",
%{
"repeat_project_name_input" => repeated_project_name,
"delete_files_radio" => delete_files
} = _values,
socket
) do
delete_files =
case delete_files do
"delete_files" ->
true

"keep_files" ->
false
end

socket = assign(socket, :confirm_project_name, repeated_project_name)
socket = assign(socket, :delete_files, delete_files)
{:noreply, socket}
end

def handle_event(
"delete",
_values,
%{assigns: %{project: project, current_user: user_name, delete_files: delete_files}} =
socket
) do
socket =
case User.is_admin?(user_name) do
true ->
%{database: :deleted} = Project.delete(project, delete_files)
:deleted = User.delete(project)

if delete_files == false do
{:ok, "Project database`#{project}` has been deleted successfully."}
else
{:ok, "Project database`#{project}` and images have been deleted successfully."}
end

false ->
{:error, "You are not authorized to delete the project."}
end
|> case do
{:ok, msg} ->
socket
|> put_flash(:info, msg)

{:error, msg} ->
socket
|> put_flash(:error, msg)
end

IO.inspect(delete_files)

{:noreply, redirect(socket, to: "/")}
end

def handle_event("generate_password", _values, socket) do
{:noreply, assign(socket, :new_password, CouchService.create_password())}
end
Expand Down
45 changes: 43 additions & 2 deletions server/lib/field_hub_web/live/project_show_live.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
<%= if User.is_admin?(@current_user) do %>
<hr>
<h2>Password change</h2>
<form phx-change="update">
<form id="pwd_form" phx-change="update">
<div class="row">
<div class="column">
<input
Expand All @@ -151,7 +151,48 @@
</div>
</div>
</form>

<hr>
<h2>Cache (XX ko - last update AAAA-MM-DD-00:00:00)</h2>

<button type="button" class="button" phx-click="delete_cache" >Delete/Update cache </button>

<pre>
<%=
"Project: " <> @project

@cache
%>
</pre>
<hr>
<h2>Delete project</h2>
<form id="del_form" phx-change="delete_form_change">
<div class="row">
<div class="column">
<input
type="text"
placeholder="Repeat the project name to delete"
name="repeat_project_name_input"
value={@confirm_project_name}
/>
</div>
</div>

<div class="row">
<div class="column">
<input type="radio" name="delete_files_radio" id="delete_database" value="keep_files" checked={not @delete_files}>
<label for="delete_database">Delete database only</label><br>
<input type="radio" name="delete_files_radio" id="delete_database_and_files" value="delete_files" checked={@delete_files}>
<label for="delete_database_and_files">Delete database and image files</label>

</div>
</div>
<div class="row">
<div class="column">
<button type="button" class="button" phx-click="delete" disabled={@confirm_project_name != @project} style="width:100%">
Delete
</button>
</div>
</div>
</form>
<% end %>
</div>
8 changes: 4 additions & 4 deletions server/test/field_hub_web/live/project_show_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ defmodule FieldHubWeb.ProjectShowLiveTest do
{:ok, %{conn: conn}}
end

test "admin has passwort setting interface", %{conn: conn} do
test "admin has password setting interface", %{conn: conn} do
{:ok, view, _html_on_mount} = live(conn, "/ui/projects/show/#{@project}")

html = render(view)
Expand All @@ -362,7 +362,7 @@ defmodule FieldHubWeb.ProjectShowLiveTest do

html =
view
|> element("form")
|> element("#pwd_form")
|> render_change(%{password: "typed_in_password"})

assert html =~
Expand Down Expand Up @@ -397,7 +397,7 @@ defmodule FieldHubWeb.ProjectShowLiveTest do
new_password = "updated_password"

view
|> element("form")
|> element("#pwd_form")
|> render_change(%{password: new_password})

html =
Expand Down Expand Up @@ -430,7 +430,7 @@ defmodule FieldHubWeb.ProjectShowLiveTest do
User.delete(@user_name)

view
|> element("form")
|> element("#pwd_form")
|> render_change(%{password: "updated_password"})

html =
Expand Down
Loading