Skip to content

Commit

Permalink
refactor: rewrite promises to async/await
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Dec 17, 2023
1 parent 7f19a3c commit 12d60ae
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 99 deletions.
3 changes: 2 additions & 1 deletion nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ server {
# Administration API
rewrite admin/api/category/permissions /admin/api/index.php last;
rewrite admin/api/category/update-order /admin/api/index.php last;
rewrite admin/api/configuration/faqs-sorting-key /admin/api/index.php last;
rewrite admin/api/configuration/faqs-sorting-key /admin/api/index.php last;
rewrite admin/api/configuration/faqs-sorting-order /admin/api/index.php last;
rewrite admin/api/configuration/list /admin/api/index.php last;
rewrite admin/api/configuration/send-test-mail /admin/api/index.php last;
rewrite admin/api/configuration/templates /admin/api/index.php last;
Expand Down
2 changes: 1 addition & 1 deletion phpmyfaq/.htaccess
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ RewriteRule api/setup/update-database api/index.php [L,QSA
RewriteRule admin/api/category/permissions admin/api/index.php [L,QSA]
RewriteRule admin/api/category/update-order admin/api/index.php [L,QSA]
RewriteRule admin/api/configuration/faqs-sorting-key admin/api/index.php [L,QSA]
RewriteRule admin/api/configuration/faqs-sorting-options admin/api/index.php [L,QSA]
RewriteRule admin/api/configuration/faqs-sorting-order admin/api/index.php [L,QSA]
RewriteRule admin/api/configuration/list admin/api/index.php [L,QSA]
RewriteRule admin/api/configuration/send-test-mail admin/api/index.php
RewriteRule admin/api/configuration/templates admin/api/index.php
Expand Down
193 changes: 97 additions & 96 deletions phpmyfaq/admin/assets/src/configuration/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,30 @@ export const handleConfiguration = async () => {
configTabList.forEach((element) => {
const configTabTrigger = new Tab(element);
element.addEventListener('shown.bs.tab', async (event) => {
console.log('tab switch');
event.preventDefault();
let target = event.target.getAttribute('href');
fetchConfiguration(target).then(async () => {
switch (target) {
case '#main':
await handleTranslation();
await handleTemplates();
break;
case '#records':
await handleFaqsSortingKeys();
break;
}
tabLoaded = true;
configTabTrigger.show();
});
await fetchConfiguration(target);

switch (target) {
case '#main':
await handleTranslation();
await handleTemplates();
break;
case '#records':
await handleFaqsSortingKeys();
await handleFaqsSortingOrder();
break;
}

tabLoaded = true;
configTabTrigger.show();
});
});

if (!tabLoaded) {
fetchConfiguration('#main').then(async () => {
await handleTranslation();
await handleTemplates();
});
await fetchConfiguration('#main');
await handleTranslation();
await handleTemplates();
}
}
};
Expand All @@ -69,99 +69,100 @@ export const handleTemplates = async () => {

export const handleFaqsSortingKeys = async () => {
const faqsOrderSelectBox = document.getElementsByName('edit[records.orderby]');

if (faqsOrderSelectBox !== null) {
const currentValue = faqsOrderSelectBox[0].dataset.pmfConfigurationCurrentValue;
const options = await fetchFaqsSortingKeys(currentValue);
faqsOrderSelectBox[0].insertAdjacentHTML('beforeend', options);
}
};

export const handleFaqsSortingOrder = async () => {
const faqsOrderSelectBox = document.getElementsByName('edit[records.sortby]');
if (faqsOrderSelectBox !== null) {
const currentValue = faqsOrderSelectBox[0].dataset.pmfConfigurationCurrentValue;
const options = await fetchFaqsSortingOrder(currentValue);
faqsOrderSelectBox[0].insertAdjacentHTML('beforeend', options);
}
};

const fetchConfiguration = async (target) => {
//fetch(`index.php?action=ajax&ajax=configuration-list&conf=${target.substr(1)}`)
fetch(`./api/configuration/list/${target.substr(1)}`)
.then(
(response) => {
if (response.ok) {
return response.text();
}
throw new Error('Request failed!');
},
(networkError) => {
console.log(networkError.message);
}
)
.then((html) => {
const tabContent = document.querySelector(target);
while (tabContent.firstChild) {
tabContent.removeChild(tabContent.firstChild);
}
tabContent.innerHTML = html.toString();
})
.catch((error) => {
console.error(error);
});
try {
const response = await fetch(`./api/configuration/list/${target.substring(1)}`);

if (!response.ok) {
console.error('Request failed!');
return;
}

const html = await response.text();
const tabContent = document.querySelector(target);

while (tabContent.firstChild) {
tabContent.removeChild(tabContent.firstChild);
}

tabContent.innerHTML = html.toString();
} catch (error) {
console.error(error.message);
}
};

const fetchTranslations = async () => {
return await fetch(`./api/configuration/translations`)
.then(
(response) => {
if (response.ok) {
return response.text();
}
throw new Error('Request failed!');
},
(networkError) => {
console.log(networkError.message);
}
)
.then((html) => {
return html;
})
.catch((error) => {
console.error(error);
});
try {
const response = await fetch(`./api/configuration/translations`);

if (!response.ok) {
console.error('Request failed!');
return;
}

return await response.text();
} catch (error) {
console.error(error.message);
}
};

const fetchTemplates = async () => {
return await fetch(`./api/configuration/templates`)
.then(
(response) => {
if (response.ok) {
return response.text();
}
throw new Error('Request failed!');
},
(networkError) => {
console.log(networkError.message);
}
)
.then((html) => {
return html;
})
.catch((error) => {
console.error(error);
});
try {
const response = await fetch(`./api/configuration/templates`);

if (!response.ok) {
console.error('Request failed!');
return;
}

return await response.text();
} catch (error) {
console.error(error.message);
}
};

const fetchFaqsSortingKeys = async (currentValue) => {
return await fetch(`./api/configuration/faqs-sorting-key/${currentValue}`)
.then(
(response) => {
if (response.ok) {
return response.text();
}
throw new Error('Request failed!');
},
(networkError) => {
console.log(networkError.message);
}
)
.then((html) => {
return html;
})
.catch((error) => {
console.error(error);
});
try {
const response = await fetch(`./api/configuration/faqs-sorting-key/${currentValue}`);

if (!response.ok) {
console.error('Request failed!');
return;
}

return await response.text();
} catch (error) {
console.error(error.message);
}
};

const fetchFaqsSortingOrder = async (currentValue) => {
try {
const response = await fetch(`./api/configuration/faqs-sorting-order/${currentValue}`);

if (!response.ok) {
console.error('Request failed!');
return;
}

return await response.text();
} catch (error) {
console.error(error.message);
}
};
1 change: 1 addition & 0 deletions phpmyfaq/admin/footer.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,6 @@
<?php
}
?>
<script src="../assets/dist/backend.js?<?= time(); ?>"></script>
</body>
</html>
1 change: 0 additions & 1 deletion phpmyfaq/admin/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,6 @@
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="../assets/dist/admin.css">

<script src="../assets/dist/backend.js?<?= time(); ?>"></script>
<script src="assets/js/configuration.js"></script>
<link rel="shortcut icon" href="../assets/themes/<?= Template::getTplSetName(); ?>/img/favicon.ico">
</head>
Expand Down

0 comments on commit 12d60ae

Please sign in to comment.