Skip to content

Commit

Permalink
feat: share link skeleton.
Browse files Browse the repository at this point in the history
  • Loading branch information
puni9869 committed Nov 10, 2024
1 parent 391b801 commit f7e390a
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 24 deletions.
1 change: 1 addition & 0 deletions config/local.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
}
},
"enableSsl": false,
"shareDataOverMail": false,
"authentication": {
"enableLogin": true,
"enableRegistration": true,
Expand Down
19 changes: 10 additions & 9 deletions frontend/app.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { NavItemSelected, SideNavCollapse, AddNewWebLinkInit,WebLinkActionsInit } from './home/index.js';
import { DeleteAccountModelInit, DisableMyAccountInit } from './setting/index.js';
import {NavItemSelected, SideNavCollapse, AddNewWebLinkInit, WebLinkActionsInit, ShareLinkInit} from './home/index.js';
import {DeleteAccountModelInit, DisableMyAccountInit} from './setting/index.js';

document.addEventListener('DOMContentLoaded', function () {
NavItemSelected();
SideNavCollapse();
DeleteAccountModelInit();
DisableMyAccountInit();
AddNewWebLinkInit();
WebLinkActionsInit();
console.info('App is loaded');
NavItemSelected();
SideNavCollapse();
DeleteAccountModelInit();
DisableMyAccountInit();
AddNewWebLinkInit();
WebLinkActionsInit();
ShareLinkInit();
console.info('App is loaded');
}, false);
27 changes: 25 additions & 2 deletions frontend/home/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,8 @@ export function WebLinkActionsInit() {
['#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();
RefreshPage();
});
});
});
Expand Down Expand Up @@ -142,3 +141,27 @@ async function WebLinkActions(data) {
}
}

export function ShareLinkInit() {
document.querySelectorAll('#share').forEach((elm) => {
elm.addEventListener('click', async (e) => (await ShareLink(e.target.dataset)));
});
}

async function ShareLink(data) {
if (!data?.id) {
return;
}
console.info("ShareLink is called", data);
const url = `/share/${data.id}`;
try {
const response = await fetch(url, {method: 'GET'});
if (!response.ok) {
throw new Error(`Response status: ${response.status}`);
}
const resp = await response.text();
document.getElementById('share-content').innerHTML = resp;
} catch (error) {
console.error(error.message);
}
}

30 changes: 23 additions & 7 deletions server/home/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func Home(c *gin.Context) {
var urls []models.Url
db := database.Db()
result := db.Where("created_by =? and is_active = ? and is_deleted = ?", currentlyLoggedIn.(string), true, false).
Order("updated_at desc").
Order("id desc").
Limit(100).
Find(&urls)
if result.RowsAffected > 0 {
Expand All @@ -63,7 +63,7 @@ func Favourite(c *gin.Context) {
var urls []models.Url
db := database.Db()
result := db.Where("created_by =? and is_active = ? and is_deleted = ? and is_fav =? ", currentlyLoggedIn.(string), true, false, true).
Order("updated_at desc").
Order("id desc").
Limit(100).
Find(&urls)
if result.RowsAffected > 0 {
Expand All @@ -80,7 +80,7 @@ func Archived(c *gin.Context) {
var urls []models.Url
db := database.Db()
result := db.Where("created_by =? and is_active = ? and is_deleted = ? and is_archived =? ", currentlyLoggedIn.(string), true, false, true).
Order("updated_at desc").
Order("id desc").
Limit(100).
Find(&urls)
if result.RowsAffected > 0 {
Expand Down Expand Up @@ -129,8 +129,7 @@ func Actions(c *gin.Context) {
}

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 {
Expand All @@ -156,13 +155,30 @@ func Actions(c *gin.Context) {
}
updates["IsDeleted"] = !value
}
var url models.Url

db := database.Db()
db.Model(&url).Updates(updates)
db.Model(&models.Url{}).Where("id = ? and created_by = ? ", requestBody["id"], currentlyLoggedIn.(string)).Updates(updates)

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

func Share(c *gin.Context) {
log := logger.NewLogger()
//var err error
session := sessions.Default(c)
currentlyLoggedIn := session.Get(middlewares.Userkey)
id := c.Param("id")
var url *models.Url
db := database.Db()
result := db.Where("id =? and created_by =? and is_active = ?", id, currentlyLoggedIn.(string), true).First(&url)
if result.RowsAffected != 1 {
c.JSON(http.StatusNotFound, map[string]string{"Status": "NOT_OK", "Message": "Not found."})
return
}
log.Info(url)
c.HTML(http.StatusOK, "share.tmpl", nil)
}

func Favicon(c *gin.Context) {
c.String(http.StatusOK, "OK")
}
Expand Down
1 change: 1 addition & 0 deletions server/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func RegisterRoutes(r *gin.Engine, sessionStore session.Store) {
authRouters.GET("/favourite", home.Favourite)
authRouters.GET("/archived", home.Archived)
authRouters.GET("/trash", home.Trash)
authRouters.GET("/share/:id", home.Share)

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

Expand Down
2 changes: 1 addition & 1 deletion templates/home/archived.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{{template "widget/search" .}}
{{template "widget/add_link" .}}
</div>
{{if (gt .Count 0)}}
{{if and .Count (gt .Count 0)}}
{{template "home/url_container" .}}
{{else}}
{{template "widget/no_data" .}}
Expand Down
2 changes: 1 addition & 1 deletion templates/home/favourite.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{{template "widget/search" .}}
{{template "widget/add_link" .}}
</div>
{{if (gt .Count 0)}}
{{if and .Count (gt .Count 0)}}
{{template "home/url_container" .}}
{{else}}
{{template "widget/no_data" .}}
Expand Down
2 changes: 1 addition & 1 deletion templates/home/home.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{{template "widget/search" .}}
{{template "widget/add_link" .}}
</div>
{{if (gt .Count 0)}}
{{if and .Count (gt .Count 0)}}
{{template "home/url_container" .}}
{{else}}
{{template "widget/no_data" .}}
Expand Down
2 changes: 1 addition & 1 deletion templates/home/trash.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{{template "widget/search" .}}
{{template "widget/add_link" .}}
</div>
{{if (gt .Count 0)}}
{{if and .Count (gt .Count 0)}}
{{template "home/url_container" .}}
{{else}}
{{template "widget/no_data" .}}
Expand Down
5 changes: 4 additions & 1 deletion templates/home/url_actions.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@
</span>

<span id="share" 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">
<svg data-id="{{.ID}}" 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
data-id="{{.ID}}"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="1"
Expand All @@ -60,4 +61,6 @@
</svg>
</span>
</div>

<div id="share-content"></div>
{{end}}
2 changes: 1 addition & 1 deletion templates/widget/add_link.tmpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{define "widget/add_link"}}
<div class="relative z-10 mx-right h-auto group">
<div class="relative z-10 mx-right h-auto group">
<div class="flex items-center px-2 py-1 text-gray-700 group select-none hover:cursor-pointer hover:text-indigo-500">
<p id="add-weblink">Add Link</p>
</div>
Expand Down
62 changes: 62 additions & 0 deletions templates/widget/share.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<div class="relative z-10 mx-right h-auto group visible">
<div class="flex items-center px-2 py-1 text-gray-700 group select-none hover:cursor-pointer hover:text-indigo-500">
<p id="add-weblink">Add Link</p>
</div>
</div>
<!-- Modal Background -->
<div id="add-weblink-modal" class="fixed inset-0 bg-gray-900 bg-opacity-50 flex justify-center pt-10 hidden">
<!-- Modal Content -->
<div class="fixed bg-white w-full max-w-md mx-4 sm:mx-6 md:mx-8 lg:mx-12 p-6 sm:p-8 rounded-lg shadow-lg">
<!-- Close Button (Cross Icon) -->
<button class="absolute top-4 right-4 text-gray-500 hover:text-black-700" id="close-modal">
<svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
</svg>
</button>

<h2 class="text-sm sm:text-sm font-bold mb-4">Add Link</h2>

<!-- Input Box -->
<input autocomplete="on" required autofocus type="text" id="add-weblink-input-box" placeholder="Enter link" class="w-full p-1 border rounded mb-3 focus:outline-none focus:ring-2 focus:ring-indigo-500">
<p class="text-base text-red-600 text-left hidden" id="weblink-err"></p>
<div class="flex select-none">
<div class="colors flex items-center" id="tags">
<ul>
<li>
<label>
<input type="radio" name="tag" value="black" id="tag"/>
<span class="swatch" style="background-color: #222;"></span>
</label>
</li>
<li>
<label>
<input type="radio" name="tag" value="yellow" id="tag"/>
<span class="swatch" style="background-color: #ffde21;"></span>
</label>
</li>
<li>
<label>
<input type="radio" name="tag" value="red" id="tag"/>
<span class="swatch" style="background-color: #ffa896;"></span>
</label>
</li>
<li>
<label>
<input type="radio" name="tag" value="blue" id="tag"/>
<span class="swatch" style="background-color: #6e8cd5;"></span>
</label>
</li>
<li>
<label>
<input type="radio" name="tag" value="green" id="tag"/>
<span class="swatch" style="background-color: #44c28d;"></span>
</label>
</li>
</ul>
</div>
<div class="flex flex-1 justify-end">
<button class="bg-blue-500 text-white text-sm px-4 py-2 rounded hover:bg-blue-600" id="add-link-btn">Add Link</button>
</div>
</div>
</div>
</div>

0 comments on commit f7e390a

Please sign in to comment.