Skip to content

Commit 82f10d4

Browse files
committed
Refactor javascript following Locality of Behaviour (LoB) principle
1 parent 6842e09 commit 82f10d4

18 files changed

+281
-274
lines changed

internal/view/static/js/alpine-components/github-repo-info.js

-77
This file was deleted.

internal/view/static/js/app.js

-4
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,10 @@ import { initThemeHelper } from './init-theme-helper.js'
22
import { initSweetAlert2 } from './init-sweetalert2.js'
33
import { initNotyf } from './init-notyf.js'
44
import { initHTMX } from './init-htmx.js'
5-
import { initAlpineComponents } from './init-alpine-components.js'
65
import { initHelpers } from './init-helpers.js'
7-
import { initDashboardAsideScroll } from './dashboard-aside-scroll.js'
86

97
initThemeHelper()
108
initSweetAlert2()
119
initNotyf()
1210
initHTMX()
13-
initAlpineComponents()
1411
initHelpers()
15-
initDashboardAsideScroll()

internal/view/static/js/dashboard-aside-scroll.js

-22
This file was deleted.

internal/view/static/js/init-alpine-components.js

-15
This file was deleted.

internal/view/web/component/change_theme_button.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type ChangeThemeButtonParams struct {
1616

1717
func ChangeThemeButton(params ChangeThemeButtonParams) gomponents.Node {
1818
return html.Div(
19-
alpine.XData("changeThemeButton"),
19+
alpine.XData("alpineChangeThemeButton()"),
2020
alpine.XCloak(),
2121

2222
components.Classes{

internal/view/static/js/alpine-components/change-theme-button.js internal/view/web/component/change_theme_button.inc.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
export const changeThemeButton = {
2-
name: 'changeThemeButton',
3-
fn: () => ({
1+
window.alpineChangeThemeButton = function () {
2+
return {
43
theme: '',
54

65
loadTheme () {
@@ -16,5 +15,5 @@ export const changeThemeButton = {
1615
init () {
1716
this.loadTheme()
1817
}
19-
})
18+
}
2019
}

internal/view/web/component/options_dropdown.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
func OptionsDropdown(children ...gomponents.Node) gomponents.Node {
1212
return html.Div(
1313
html.Class("inline-block"),
14-
alpine.XData("options_dropdown"),
14+
alpine.XData("alpineOptionsDropdown()"),
1515
alpine.XOn("mouseenter", "open()"),
1616
alpine.XOn("mouseleave", "close()"),
1717
html.Button(

internal/view/static/js/alpine-components/options-dropdown.js internal/view/web/component/options_dropdown.inc.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
export const optionsDropdown = {
2-
name: 'options_dropdown',
3-
fn: () => ({
1+
window.alpineOptionsDropdown = function () {
2+
return {
43
isOpen: false,
54
buttonEl: null,
65
contentEl: null,
@@ -43,5 +42,5 @@ export const optionsDropdown = {
4342
this.contentEl.style.top = `${buttonRect.top - contentHeight}px`
4443
}
4544
}
46-
})
45+
}
4746
}

internal/view/web/component/star_on_github.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
func StarOnGithub(size size) gomponents.Node {
1212
return html.A(
13-
alpine.XData("githubRepoInfo"),
13+
alpine.XData("alpineStarOnGithub()"),
1414
alpine.XCloak(),
1515
components.Classes{
1616
"btn btn-neutral": true,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
window.alpineStarOnGithub = function () {
2+
return {
3+
stars: null,
4+
5+
async init () {
6+
const stars = await this.getStars()
7+
if (stars !== null) {
8+
this.stars = stars
9+
}
10+
},
11+
12+
async getStars () {
13+
const cacheKey = 'pbw-gh-stars'
14+
15+
const cachedJSON = localStorage.getItem(cacheKey)
16+
if (cachedJSON) {
17+
const cached = JSON.parse(cachedJSON)
18+
if (Date.now() - cached.timestamp < 2 * 60 * 1000) {
19+
return cached.value
20+
}
21+
}
22+
23+
const url = 'https://api.github.com/repos/eduardolat/pgbackweb'
24+
try {
25+
const response = await fetch(url)
26+
if (!response.ok) {
27+
return null
28+
}
29+
const data = await response.json()
30+
const value = data.stargazers_count
31+
const dataToCache = JSON.stringify({
32+
value,
33+
timestamp: Date.now()
34+
})
35+
localStorage.setItem(cacheKey, dataToCache)
36+
return value
37+
} catch {
38+
return null
39+
}
40+
}
41+
}
42+
}

internal/view/web/dashboard/summary/index.go

+1-106
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ import (
44
"fmt"
55
"net/http"
66

7-
lucide "github.com/eduardolat/gomponents-lucide"
87
"github.com/eduardolat/pgbackweb/internal/database/dbgen"
98
"github.com/eduardolat/pgbackweb/internal/util/echoutil"
109
"github.com/eduardolat/pgbackweb/internal/view/reqctx"
11-
"github.com/eduardolat/pgbackweb/internal/view/web/alpine"
1210
"github.com/eduardolat/pgbackweb/internal/view/web/component"
1311
"github.com/eduardolat/pgbackweb/internal/view/web/layout"
1412
"github.com/google/uuid"
@@ -201,111 +199,8 @@ func indexPage(
201199
BgColors: []string{blueColor, greenColor, redColor},
202200
}),
203201
),
204-
html.Div(
205-
alpine.XData("genericSlider(4)"),
206-
alpine.XCloak(),
207-
html.Class("mt-6 flex flex-col justify-center items-center mx-auto"),
208-
component.H2Text("How to use PG Back Web"),
209-
component.CardBox(component.CardBoxParams{
210-
Class: "mt-4 space-y-4 max-w-[600px]",
211-
Children: []gomponents.Node{
212-
html.Div(
213-
html.Class("flex justify-center"),
214-
html.Ul(
215-
html.Class("steps"),
216-
html.Li(
217-
html.Class("step"),
218-
alpine.XBind("class", "currentSlide >= 1 ? 'step-primary' : ''"),
219-
gomponents.Text("Create database"),
220-
),
221-
html.Li(
222-
html.Class("step"),
223-
alpine.XBind("class", "currentSlide >= 2 ? 'step-primary' : ''"),
224-
gomponents.Text("Create destination"),
225-
),
226-
html.Li(
227-
html.Class("step"),
228-
alpine.XBind("class", "currentSlide >= 3 ? 'step-primary' : ''"),
229-
gomponents.Text("Create backup"),
230-
),
231-
html.Li(
232-
html.Class("step"),
233-
alpine.XBind("class", "currentSlide >= 4 ? 'step-primary' : ''"),
234-
gomponents.Text("Wait for executions"),
235-
),
236-
),
237-
),
238-
239-
html.Div(
240-
alpine.XShow("currentSlide === 1"),
241-
component.H3Text("Create database"),
242-
component.PText(`
243-
To create a database, click on the "Databases" menu item on the
244-
left sidebar. Then click on the "Create database" button. Fill
245-
in the form and click on the "Save" button. You can create as
246-
many databases as you want to backup.
247-
`),
248-
),
249-
250-
html.Div(
251-
alpine.XShow("currentSlide === 2"),
252-
component.H3Text("Create S3 destination (optional)"),
253-
component.PText(`
254-
To create a destination, click on the "Destinations" menu item on
255-
the left sidebar. Then click on the "Create destination" button.
256-
Fill in the form and click on the "Save" button. You can create
257-
as many destinations as you want to store the backups. If you
258-
don't want to use S3 destinations and store the backups locally,
259-
you can skip this step.
260-
`),
261-
),
262-
263-
html.Div(
264-
alpine.XShow("currentSlide === 3"),
265-
component.H3Text("Create backup"),
266-
component.PText(`
267-
To create a backup you need to have at least one database and one
268-
destination. Click on the "Backups" menu item on the left sidebar.
269-
Then click on the "Create backup" button. Fill in the form and
270-
click on the "Save" button. You can create as many backups as you
271-
want including any combination of databases and destinations.
272-
`),
273-
),
274202

275-
html.Div(
276-
alpine.XShow("currentSlide === 4"),
277-
component.H3Text("Wait for executions"),
278-
component.PText(`
279-
When your backup is created and active, the system will start
280-
creating executions based on the schedule you defined. You can
281-
also create executions manually by clicking the "Run backup now"
282-
button on the backups list. You can see the executions on the
283-
"Executions" menu item on the left sidebar and then click on the
284-
"Show details" button to see the details, logs, and download or
285-
delete the backup file.
286-
`),
287-
),
288-
289-
html.Div(
290-
html.Class("mt-4 flex justify-between"),
291-
html.Button(
292-
alpine.XBind("disabled", "!hasPrevSlide"),
293-
alpine.XOn("click", "prevSlide"),
294-
html.Class("btn btn-neutral btn-ghost"),
295-
lucide.ChevronLeft(),
296-
component.SpanText("Previous"),
297-
),
298-
html.Button(
299-
alpine.XBind("disabled", "!hasNextSlide"),
300-
alpine.XOn("click", "nextSlide"),
301-
html.Class("btn btn-neutral btn-ghost"),
302-
component.SpanText("Next"),
303-
lucide.ChevronRight(),
304-
),
305-
),
306-
},
307-
}),
308-
),
203+
indexHowTo(),
309204
}
310205

311206
return layout.Dashboard(reqCtx, layout.DashboardParams{

0 commit comments

Comments
 (0)