From af0258c8aaa529e71323315655b4faeec7e709d3 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sat, 3 Sep 2022 07:50:16 +0200 Subject: [PATCH] Refactor list creation to use request.js --- .../retrospring/features/lists/create.ts | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/app/javascript/retrospring/features/lists/create.ts b/app/javascript/retrospring/features/lists/create.ts index 12ff95ec..52eb9d1f 100644 --- a/app/javascript/retrospring/features/lists/create.ts +++ b/app/javascript/retrospring/features/lists/create.ts @@ -1,4 +1,4 @@ -import Rails from '@rails/ujs'; +import { post } from '@rails/request.js'; import { showNotification, showErrorNotification } from 'utilities/notifications'; import I18n from 'retrospring/i18n'; @@ -6,25 +6,26 @@ export function createListHandler(event: Event): void { const button = event.target as HTMLButtonElement; const input = document.querySelector('input#new-list-name'); - Rails.ajax({ - url: '/ajax/create_list', - type: 'POST', - data: new URLSearchParams({ + post('/ajax/create_list', { + body: { name: input.value, user: button.dataset.user - }).toString(), - success: (data) => { + }, + contentType: 'application/json' + }) + .then(async response => { + const data = await response.json; + if (data.success) { document.querySelector('#lists-list ul.list-group').insertAdjacentHTML('beforeend', data.render); } showNotification(data.message, data.success); - }, - error: (data, status, xhr) => { - console.log(data, status, xhr); + }) + .catch(err => { + console.log(err); showErrorNotification(I18n.translate('frontend.error.message')); - } - }); + }); } export function createListInputHandler(event: KeyboardEvent): void {