Skip to content

Commit

Permalink
refactor: Removing some extra javascript again
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Nov 5, 2022
1 parent 853ec63 commit 3faf981
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 62 deletions.
15 changes: 15 additions & 0 deletions cmd/fibr/static/scripts/confirm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
document.addEventListener('readystatechange', (event) => {
if (event.target.readyState !== 'complete') {
return;
}

document.querySelectorAll('[data-confirm]').forEach((element) => {
element.addEventListener('click', (e) => {
if (
!confirm(`Are you sure you want to delete ${element.dataset.confirm}?`)
) {
e.preventDefault();
}
});
});
});
19 changes: 19 additions & 0 deletions cmd/fibr/static/scripts/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
document.addEventListener('readystatechange', (event) => {
if (event.target.readyState !== 'complete') {
return;
}

const link = document.getElementById('go-back');

if (link) {
link.setAttribute('href', document.referrer);
link.addEventListener('click', (e) => {
e.preventDefault();
window.addEventListener('popstate', () => {
window.location.reload(true);
});
history.back();
return false;
});
}
});
2 changes: 1 addition & 1 deletion cmd/fibr/static/scripts/index.min.js

Large diffs are not rendered by default.

53 changes: 53 additions & 0 deletions cmd/fibr/static/scripts/webhook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
document.addEventListener('readystatechange', (event) => {
if (event.target.readyState !== 'complete') {
return;
}

const urlLabel = document.getElementById('webhook-url-label');
if (!urlLabel) {
return;
}

const urlWebhook = document.getElementById('webhook-url');
const telegramChatID = document.getElementById('telegram-chat-id');

document
.getElementById('webhook-kind-raw')
.addEventListener('change', (e) => {
if (e.target.value === 'raw') {
urlWebhook.placeholder = 'http://website.com/fibr';
urlLabel.innerHTML = 'URL';
telegramChatID.classList.add('hidden');
}
});

document
.getElementById('webhook-kind-discord')
.addEventListener('change', (e) => {
if (e.target.value === 'discord') {
urlWebhook.placeholder = 'https://discord.com/api/webhooks/...';
urlLabel.innerHTML = 'URL';
telegramChatID.classList.add('hidden');
}
});

document
.getElementById('webhook-kind-slack')
.addEventListener('change', (e) => {
if (e.target.value === 'slack') {
urlWebhook.placeholder = 'https://hooks.slack.com/services/...';
urlLabel.innerHTML = 'URL';
telegramChatID.classList.add('hidden');
}
});

document
.getElementById('webhook-kind-telegram')
.addEventListener('change', (e) => {
if (e.target.value === 'telegram') {
urlLabel.innerHTML = 'Token';
urlWebhook.placeholder = 'Bot token';
telegramChatID.classList.remove('hidden');
}
});
});
20 changes: 0 additions & 20 deletions cmd/fibr/templates/error.html
Original file line number Diff line number Diff line change
@@ -1,26 +1,6 @@
{{ define "error" }}
{{ template "header" . }}

<script type="text/javascript" nonce="{{ .nonce }}">
document.addEventListener('readystatechange', async (event) => {
if (event.target.readyState === 'complete') {
const link = document.getElementById('go-back');

if (link) {
link.setAttribute('href', document.referrer);
link.addEventListener('click', (e) => {
e.preventDefault();
window.addEventListener('popstate', () => {
window.location.reload(true);
});
history.back();
return false;
});
}
}
});
</script>

<header class="header center">
<h1 class="no-margin no-padding">
{{ template "root-link" . }}
Expand Down
19 changes: 1 addition & 18 deletions cmd/fibr/templates/share-list.html
Original file line number Diff line number Diff line change
@@ -1,21 +1,4 @@
{{ define "share-list" }}
<script type="text/javascript" nonce="{{ .nonce }}">
document.addEventListener('readystatechange', async (event) => {
if (event.target.readyState === 'complete') {
{{ range .Shares }}
const shareDelete{{ js .ID }} = document.getElementById('share-delete-{{ .ID }}');
if (shareDelete{{ js .ID }}) {
shareDelete{{ js .ID }}.addEventListener('click', (e) => {
if (!confirm('Are you sure you want to delete share for {{ .Path }}?')) {
e.preventDefault()
}
});
}
{{ end }}
}
});
</script>

<div id="share-list" class="modal">
<div class="modal-content">
<h2 class="flex flex-center header no-margin">
Expand Down Expand Up @@ -70,7 +53,7 @@ <h2 class="flex flex-center header no-margin">
<input type="hidden" name="type" value="share" />
<input type="hidden" name="method" value="DELETE" />
<input type="hidden" name="id" value="{{ .ID }}" />
<button id="share-delete-{{ .ID }}" type="submit" class="button button-icon" alt="Delete">
<button id="share-delete-{{ .ID }}" type="submit" class="button button-icon" title="Delete" data-confirm="share for {{ .Path }}">
<img class="icon" src="{{ url "/svg/times?fill=crimson" }}" alt="Delete">
</button>
</form>
Expand Down
19 changes: 1 addition & 18 deletions cmd/fibr/templates/webhook-list.html
Original file line number Diff line number Diff line change
@@ -1,21 +1,4 @@
{{ define "webhook-list" }}
<script type="text/javascript" nonce="{{ .nonce }}">
document.addEventListener('readystatechange', async (event) => {
if (event.target.readyState === 'complete') {
{{ range .Webhooks }}
const webhookDelete{{ js .ID }} = document.getElementById('webhook-delete-{{ .ID }}');
if (webhookDelete{{ js .ID }}) {
webhookDelete{{ js .ID }}.addEventListener('click', (e) => {
if (!confirm('Are you sure you want to delete webhook for {{ .Pathname }}?')) {
e.preventDefault()
}
});
}
{{ end }}
}
});
</script>

<div id="webhook-list" class="modal">
<div class="modal-content">
<h2 class="flex flex-center header no-margin">
Expand Down Expand Up @@ -83,7 +66,7 @@ <h2 class="flex flex-center header no-margin">
<input type="hidden" name="type" value="webhook" />
<input type="hidden" name="method" value="DELETE" />
<input type="hidden" name="id" value="{{ .ID }}" />
<button id="webhook-delete-{{ .ID }}" type="submit" class="button button-icon" title="Delete">
<button id="webhook-delete-{{ .ID }}" type="submit" class="button button-icon" title="Delete" data-confirm="webhook for {{ .Pathname }}">
<img class="icon" src="{{ url "/svg/times?fill=crimson" }}" alt="Delete">
</button>
</form>
Expand Down
5 changes: 3 additions & 2 deletions pkg/share/share.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@ func (a *App) loadShares(ctx context.Context) error {
}

func (a *App) refresh(ctx context.Context) error {
var err error
if a.shares, err = provider.LoadJSON[map[string]provider.Share](ctx, a.storageApp, shareFilename); err != nil {
if shares, err := provider.LoadJSON[map[string]provider.Share](ctx, a.storageApp, shareFilename); err != nil {
if !absto.IsNotExist(err) {
return err
}
Expand All @@ -161,6 +160,8 @@ func (a *App) refresh(ctx context.Context) error {
}

return provider.SaveJSON(ctx, a.storageApp, shareFilename, &a.shares)
} else {
a.shares = shares
}

return nil
Expand Down
6 changes: 3 additions & 3 deletions pkg/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,7 @@ func (a *App) Start(ctx context.Context) {
}

func (a *App) loadWebhooks(ctx context.Context) error {
var err error

if a.webhooks, err = provider.LoadJSON[map[string]provider.Webhook](ctx, a.storageApp, webhookFilename); err != nil {
if webhooks, err := provider.LoadJSON[map[string]provider.Webhook](ctx, a.storageApp, webhookFilename); err != nil {
if !absto.IsNotExist(err) {
return err
}
Expand All @@ -147,6 +145,8 @@ func (a *App) loadWebhooks(ctx context.Context) error {
}

return provider.SaveJSON(ctx, a.storageApp, webhookFilename, &a.webhooks)
} else {
a.webhooks = webhooks
}

return nil
Expand Down

0 comments on commit 3faf981

Please sign in to comment.