Skip to content

Commit

Permalink
Added table view for the user
Browse files Browse the repository at this point in the history
  • Loading branch information
LobbyLobster committed Oct 18, 2023
1 parent 737dbf7 commit 547fce3
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 51 deletions.
17 changes: 10 additions & 7 deletions controller/admin_controller.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
package controller

import (
"net/http"

"github.com/gin-gonic/gin"
ginsession "github.com/go-session/gin-session"
)

type TableData struct {
TableColumnNum int
}

func HandleAdmin(ctx *gin.Context) {
// store := ginsession.FromContext(ctx)
// _, ok := store.Get("user")
// if !ok {
// // User is not logged in, redirect them to the login page
// ctx.Redirect(http.StatusTemporaryRedirect, "/login")
// return
// }
store := ginsession.FromContext(ctx)
_, ok := store.Get("user")
if !ok {
// User is not logged in, redirect them to the login page
ctx.Redirect(http.StatusTemporaryRedirect, "/login")
return
}

ctx.HTML(200, "admin.html", nil)
}
14 changes: 7 additions & 7 deletions controller/login_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
)

func HandleLogin(ctx *gin.Context) {
// store := ginsession.FromContext(ctx)
// _, ok := store.Get("user")
// if ok {
// // User is not logged in, redirect them to the login page
// ctx.Redirect(http.StatusTemporaryRedirect, "/")
// return
// }
store := ginsession.FromContext(ctx)
_, ok := store.Get("user")
if ok {
// User is not logged in, redirect them to the login page
ctx.Redirect(http.StatusTemporaryRedirect, "/")
return
}
ctx.HTML(200, "login.html", nil)
}

Expand Down
22 changes: 17 additions & 5 deletions controller/search_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,25 @@ import (

func HandleSearch(ctx *gin.Context) {
searchQuery := ctx.PostForm("search-input")
exact := ctx.Query("exact")
var data internal.PageData

if searchQuery == "" {
HandleQueue(ctx)
return
}
if exact == "true" {

sourceCreds := internal.Credentials{
Server: ctx.Query("source_server"),
Account: ctx.Query("source_account"),
}
destCreds := internal.Credentials{
Server: ctx.Query("destination_server"),
Account: ctx.Query("destination_account"),
}

data := internal.GetSearchData(searchQuery)
data = internal.GetSearchData("", true, sourceCreds, destCreds)
} else {

data = internal.GetSearchData(searchQuery, false, internal.Credentials{}, internal.Credentials{})
}

ctx.HTML(200, "tbody.html", data)
}
13 changes: 8 additions & 5 deletions internal/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ import (
"github.com/lithammer/fuzzysearch/fuzzy"
)

func GetSearchData(searchQuery string) PageData {
results := searchInQueue(searchQuery)
func GetSearchData(searchQuery string, isExact bool, sourceDetails, destinationDetails Credentials) PageData {
var results []*Task
if isExact {
results = searchExactCredentials(sourceDetails, destinationDetails)
} else {
results = searchInQueue(searchQuery)
}

data := PageData{
Index: 1,
Expand Down Expand Up @@ -69,10 +74,8 @@ func searchExactCredentials(sourceDetails, destinationDetails Credentials) []*Ta
task := e.Value.(*Task)
if task.SourceAccount == sourceDetails.Account &&
task.SourceServer == sourceDetails.Server &&
task.SourcePassword == sourceDetails.Password &&
task.DestinationAccount == destinationDetails.Account &&
task.DestinationServer == destinationDetails.Server &&
task.DestinationPassword == destinationDetails.Password {
task.DestinationServer == destinationDetails.Server {
results = append(results, task)
}
}
Expand Down
4 changes: 2 additions & 2 deletions internal/sync_loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ func processPendingTasks() {
continue
}

//syncIMAP(task)
simulateTask(task)
syncIMAP(task)
// simulateTask(task)
time.Sleep(100 * time.Millisecond)
}
}
Expand Down
2 changes: 1 addition & 1 deletion templates/admin_navbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<div class="navbar-center" x-data="{ searchInput: '' }">
<input x-model="searchInput" x-on:input.debounce.500ms="updateHxTrigger()" type="text" id="search-input"
name="search-input" class="input-ghost-primary input form-control" placeholder="Search"
hx-post="/api/search?page=1" hx-trigger="keyup changed delay:250ms, search" hx-target="#table-body"
hx-post="/api/search?page=1&exact=false" hx-trigger="keyup changed delay:250ms, search" hx-target="#table-body"
hx-swap="innerHTML transition:true">
</div>

Expand Down
44 changes: 20 additions & 24 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ <h2 class="text-2xl font-semibold mb-4">Source Details</h2>
<div class="grid gap-4 form-group">
<div class="form-field">
<label for="source_server" class="block font-semibold">Server</label>
<input type="text" id="source_server" name="source_server" placeholder="Source Server"
<input type="text" x-model="source_server" id="source_server" name="source_server" placeholder="Source Server"
class="input max-w-full" value="{{.SourceDetails.Server}}">
</div>
<div>
<label for="source_account" class="block font-semibold">Account</label>
<input type="text" id="source_account" name="source_account" placeholder="Source Account"
<input type="text" x-model="source_account" id="source_account" name="source_account" placeholder="Source Account"
class="input max-w-full" value="{{.SourceDetails.Account}}">
</div>
<div>
Expand All @@ -88,13 +88,13 @@ <h2 class="text-2xl font-semibold mb-4">Destination Details</h2>
<div class="grid gap-4 form-group">
<div class="form-field">
<label for="destination_server" class="block font-semibold">Server</label>
<input type="text" id="destination_server" name="destination_server"
<input type="text" x-model="destination_server" id="destination_server" name="destination_server"
placeholder="Destination Server" class="input max-w-full"
value="{{.DestinationDetails.Server}}">
</div>
<div>
<label for="destination_account" class="block font-semibold">Account</label>
<input type="text" id="destination_account" name="destination_account"
<input type="text" x-model="destination_account" id="destination_account" name="destination_account"
placeholder="Destination Account" class="input max-w-full"
value="{{.DestinationDetails.Account}}">
</div>
Expand All @@ -121,22 +121,9 @@ <h2 class="text-2xl font-semibold">User queue</h2>
form="credentials_form"></input>
</div>
<div class="grid gap-4">
<table class="table table-hover table-compact">
<thead>
<tr>
<th>Index</th>
<th>Source Server</th>
<th>Source Account</th>
<th>Destination Server</th>
<th>Destination Account</th>
<th>Status</th>
<th>Actions</th>
</tr>
</thead>
<tbody>

</tbody>
</table>
<table class="table-hover table-compact table" id="queue-table">
{{template "table_user.html"}}
</table>
</div>
</div>
</div>
Expand Down Expand Up @@ -226,20 +213,29 @@ <h2 class="text-2xl font-semibold">User queue</h2>

document.addEventListener('alpine:init', () => {
Alpine.data('data', () => ({
source_server: '',
source_account: '',
destination_server: '',
destination_account: '',

source_validated: false,
destination_validated: false,
showQueue(){
if (this.source_validated === true && this.destination_validated === true) {
htmx.ajax('POST', "/api/search?exact=true&source_server="+this.source_server+"&source_account="+this.source_account+"&destination_server="+this.destination_server+"&destination_account="+this.destination_account+"", {target:'#table-body',swap:'innerHTML'})
}
},
update_source_status(result) {
this.source_validated = result
this.source_validated = result;
this.showQueue();
},
update_destination_status(result) {
this.destination_validated = result
this.destination_validated = result;
this.showQueue();
},
}))
})



</script>

</body>
14 changes: 14 additions & 0 deletions templates/table_user.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<thead>
<tr>
<th>Index</th>
<th>Source Server</th>
<th>Source Account</th>
<th>Destination Server</th>
<th>Destination Account</th>
<th>Status</th>
<th>Actions</th>
</tr>
</thead>
<tbody id="table-body">
{{template "tbody.html" .}}
</tbody>

0 comments on commit 547fce3

Please sign in to comment.