Skip to content

Commit

Permalink
feat: actions update for is_fav, is_archived, is_deleted pages.
Browse files Browse the repository at this point in the history
  • Loading branch information
puni9869 committed Nov 9, 2024
1 parent 54c05ce commit 391b801
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 10 deletions.
3 changes: 2 additions & 1 deletion frontend/app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NavItemSelected, SideNavCollapse, AddNewWebLinkInit } from './home/index.js';
import { NavItemSelected, SideNavCollapse, AddNewWebLinkInit,WebLinkActionsInit } from './home/index.js';
import { DeleteAccountModelInit, DisableMyAccountInit } from './setting/index.js';

document.addEventListener('DOMContentLoaded', function () {
Expand All @@ -7,5 +7,6 @@ document.addEventListener('DOMContentLoaded', function () {
DeleteAccountModelInit();
DisableMyAccountInit();
AddNewWebLinkInit();
WebLinkActionsInit();
console.info('App is loaded');
}, false);
43 changes: 43 additions & 0 deletions frontend/home/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,46 @@ async function AddNewLink(webLink, selectedTag) {
console.error(error.message);
}
}

export function WebLinkActionsInit() {
const actions = document.querySelector('#weblink-actions');
if (!actions) {
return;
}

['#move-to-favourite', '#move-to-archive', '#move-to-trash'].forEach((actionSelector) => {
document.querySelectorAll(actionSelector).forEach((elm) => {
elm.addEventListener('click', async (e) => {
console.info(e.target);
await WebLinkActions(e.target.dataset);
// RefreshPage();
});
});
});
}


async function WebLinkActions(data) {
console.info("Action is called", data);
const url = '/actions';
try {
const headers = new Headers();
headers.append('Content-Type', 'application/json');

const response = await fetch(url, {
method: 'PUT',
headers: headers,
body: JSON.stringify(data)
});
if (!response.ok) {
throw new Error(`Response status: ${response.status}`);
}
const resp = await response.json();
if (resp?.Status === "OK") {
console.info(resp?.Message);
}
} catch (error) {
console.error(error.message);
}
}

60 changes: 58 additions & 2 deletions server/home/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package home

import (
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"github.com/puni9869/pinmyblogs/models"
"github.com/puni9869/pinmyblogs/pkg/database"
"github.com/puni9869/pinmyblogs/pkg/logger"
"github.com/puni9869/pinmyblogs/pkg/spider"
"github.com/puni9869/pinmyblogs/server/middlewares"
"github.com/puni9869/pinmyblogs/types/forms"

Check failure on line 11 in server/home/home.go

View workflow job for this annotation

GitHub Actions / checks

"github.com/puni9869/pinmyblogs/types/forms" imported and not used (typecheck)
"net/http"

"github.com/gin-gonic/gin"
"strconv"
)

func AddWeblink(c *gin.Context) {
Expand Down Expand Up @@ -107,6 +107,62 @@ func Trash(c *gin.Context) {
c.HTML(http.StatusOK, "trash.tmpl", gin.H{"HasError": false, "Urls": urls, "Count": result.RowsAffected})
}

func Actions(c *gin.Context) {
log := logger.NewLogger()
var err error
session := sessions.Default(c)
currentlyLoggedIn := session.Get(middlewares.Userkey)

var requestBody map[string]string
err = c.ShouldBindJSON(&requestBody)
if err != nil {
log.WithError(err).Error("Bad request body")
c.JSON(http.StatusBadRequest, gin.H{"Status": "NOT_OK", "Errors": "Bad values"})
return
}

u64, err := strconv.ParseUint(requestBody["id"], 10, 64)
if err != nil {
log.WithError(err).Error("Bad request body", u64)
c.JSON(http.StatusBadRequest, gin.H{"Status": "NOT_OK", "Errors": "Bad values"})
return
}

var updates = make(map[string]any)
updates["ID"] = requestBody["id"]
updates["CreatedBy"] = currentlyLoggedIn.(string)
if val, ok := requestBody["isFav"]; ok {
value, err := strconv.ParseBool(val)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"Status": "NOT_OK", "Errors": "Bad values"})
return
}
updates["IsFav"] = !value
}

if val, ok := requestBody["isArchived"]; ok {
value, err := strconv.ParseBool(val)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"Status": "NOT_OK", "Errors": "Bad values"})
return
}
updates["IsArchived"] = !value
}

if val, ok := requestBody["isDeleted"]; ok {
value, err := strconv.ParseBool(val)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"Status": "NOT_OK", "Errors": "Bad values"})
}
updates["IsDeleted"] = !value
}
var url models.Url
db := database.Db()
db.Model(&url).Updates(updates)

c.JSON(http.StatusOK, gin.H{"Status": "OK", "Message": "Weblink updated."})
}

func Favicon(c *gin.Context) {
c.String(http.StatusOK, "OK")
}
Expand Down
2 changes: 2 additions & 0 deletions server/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ func RegisterRoutes(r *gin.Engine, sessionStore session.Store) {
authRouters.GET("/archived", home.Archived)
authRouters.GET("/trash", home.Trash)

authRouters.PUT("/actions", home.Actions)

authRouters.POST("/new",
middlewares.Bind(forms.WeblinkRequest{}),
home.AddWeblink,
Expand Down
20 changes: 13 additions & 7 deletions templates/home/url_actions.tmpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{define "home/url_actions"}}
<div class="flex flex-row align-start items-start select-none gap-x-1">
<div class="flex flex-row align-start items-start select-none gap-x-1" id="weblink-actions">
<span id="move-to-pinned" data-id="{{.ID}}" class="hover:bg-gray-100 rounded p-1 group cursor-pointer">
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 text-gray-500 group-hover:text-indigo-500" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-width="2" stroke-linejoin="round" d="M6 6h.008v.008H6V6Z" />
Expand All @@ -11,19 +11,23 @@
/>
</svg>
</span>
<span id="move-to-favourite" data-id="{{.ID}}" class="hover:bg-gray-100 rounded p-1 group cursor-pointer">
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 text-gray-500 group-hover:text-indigo-500" fill="{{if .IsFav}}black{{else}}none{{end}}" viewBox="0 0 24 24" stroke="currentColor">
<span id="move-to-favourite" data-id="{{.ID}}" class="hover:bg-gray-100 rounded p-1 group cursor-pointer" data-is-fav="{{if .IsFav}}true{{else}}false{{end}}">
<svg data-is-fav="{{if .IsFav}}true{{else}}false{{end}}" data-id="{{.ID}}" xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 text-gray-500 group-hover:text-indigo-500" fill="{{if .IsFav}}black{{else}}none{{end}}" viewBox="0 0 24 24" stroke="currentColor">
<path
data-id="{{.ID}}"
data-is-fav="{{if .IsFav}}true{{else}}false{{end}}"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="1"
d="M21 8.25c0-2.485-2.099-4.5-4.688-4.5-1.935 0-3.597 1.126-4.312 2.733-.715-1.607-2.377-2.733-4.313-2.733C5.1 3.75 3 5.765 3 8.25c0 7.22 9 12 9 12s9-4.78 9-12Z"
/>
</svg>
</span>
<span id="move-to-archive" data-id="{{.ID}}" class="hover:bg-gray-100 rounded p-1 group cursor-pointer">
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 text-gray-500 group-hover:text-indigo-500" fill="{{if .IsArchived}}black{{else}}none{{end}}" viewBox="0 0 24 24" stroke="currentColor">
<span id="move-to-archive" data-id="{{.ID}}" class="hover:bg-gray-100 rounded p-1 group cursor-pointer" data-is-archived="{{if .IsArchived}}true{{else}}false{{end}}">
<svg data-id="{{.ID}}" data-is-archived="{{if .IsArchived}}true{{else}}false{{end}}" xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 text-gray-500 group-hover:text-indigo-500" fill="{{if .IsArchived}}black{{else}}none{{end}}" viewBox="0 0 24 24" stroke="currentColor">
<path
data-id="{{.ID}}"
data-is-archived="{{if .IsArchived}}true{{else}}false{{end}}"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="1"
Expand All @@ -32,9 +36,11 @@
</svg>
</span>

<span id="move-to-trash" data-id="{{.ID}}" class="hover:bg-gray-100 rounded p-1 group cursor-pointer">
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 text-gray-500 group-hover:text-indigo-500" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<span id="move-to-trash" data-id="{{.ID}}" class="hover:bg-gray-100 rounded p-1 group cursor-pointer" data-is-deleted="{{if .IsDeleted}}true{{else}}false{{end}}">
<svg data-is-deleted="{{if .IsDeleted}}true{{else}}false{{end}}" data-id="{{.ID}}" xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 text-gray-500 group-hover:text-indigo-500" fill="{{if .IsDeleted}}black{{else}}none{{end}}" viewBox="0 0 24 24" stroke="currentColor">
<path
data-id="{{.ID}}"
data-is-deleted="{{if .IsDeleted}}true{{else}}false{{end}}"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="1"
Expand Down

0 comments on commit 391b801

Please sign in to comment.