import Swal from 'sweetalert2'; import AllMapDataStore from '@/stores/allMapData.js'; import ConformanceStore from '@/stores/conformance.js'; import FilesStore from '@/stores/files.js'; import PageAdminStore from '@/stores/pageAdmin.js'; import { useModalStore } from '@/stores/modal.js'; const customClass = { container: '!z-[99999]', popup: '!w-[564px]', title: '!text-xl !font-semibold !mb-2', htmlContainer: '!text-sm !font-normal !h-full !mb-4 !leading-5', inputLabel: '!text-sm !font-normal', input: '!h-8 !text-sm !font-normal !shadow-inner !shadow-black !border-neutral-200', validationMessage: '!bg-neutral-10 !text-danger', confirmButton: '!inline-block !rounded-full !text-sm !font-medium !text-center !align-middle !transition-colors !duration-300 !px-5 !py-2 !w-[100px] !h-[40px]', cancelButton: '!inline-block !rounded-full !text-sm !font-medium !text-center !align-middle !transition-colors !duration-300 !px-5 !py-2 !w-[100px] !h-[40px] ', }; /** * Map Saved * @param { function } addFilterId 後端 API */ export async function saveFilter(addFilterId, next = null) { let fileName = ''; const pageAdminStore = PageAdminStore(); const { value, isConfirmed } = await Swal.fire({ title: 'SAVE NEW FILTER', input: 'text', inputPlaceholder: 'Enter Filter Name.', inputValue: fileName, inputAttributes: { 'maxlength': 200, }, inputValidator: (value) => { if (!value) return 'You need to write something!'; fileName = value; }, icon: 'info', iconHtml: 'cloud_upload', iconColor: '#0099FF', reverseButtons:true, confirmButtonColor: '#0099FF', showCancelButton: true, cancelButtonColor: '#94a3b8', customClass: customClass, }); // 透過回傳值判斷要不要轉址 if(isConfirmed) { // 存檔成功 await addFilterId(fileName); // 顯示儲存完成 if (value) { // Example of value: yes savedSuccessfully(value); } // 清空欄位 fileName = ''; return true; } else { // 點擊取消或空白處,為存檔失敗。 pageAdminStore.keepPreviousPage(); // Not every time we have nontrivial next value if (next !== null) { next(false); } return false; } } /** * Saved Success * @param { string } value File's name */ export async function savedSuccessfully(value) { value = value ? value : ''; await Swal.fire({ title: 'SAVE COMPLETE', html: `${value} has been saved in Lucia.`, timer: 3000, // 停留 3 秒後自動關閉 showConfirmButton: false, icon: 'success', iconColor: '#0099FF', customClass: customClass }) }; /** * leave Map page * @param { function } next 執行完函式後的步驟 * @param { function } addFilterId 後端 API * @param { string } toPath route path * @param { function } logOut 登出函式 */ export async function leaveFilter(next, addFilterId, toPath, logOut) { const allMapDataStore = AllMapDataStore(); const pageAdminStore = PageAdminStore(); const result = await Swal.fire({ title: 'SAVE YOUR FILTER?', html: 'If you want to continue using this filter in any other page, please select [Yes].', icon: 'warning', iconColor: '#FF3366', reverseButtons:true, confirmButtonText: 'Yes', confirmButtonColor: '#FF3366', showCancelButton: true, cancelButtonText: 'No', cancelButtonColor: '#94a3b8', customClass: customClass, }); if(result.isConfirmed) { if(allMapDataStore.createFilterId) { await allMapDataStore.updataFilter(); if(allMapDataStore.isUpdataFilter) { await savedSuccessfully(allMapDataStore.filterName); } } else { // Dangerous, here shows a modal await saveFilter(addFilterId, next); } logOut ? logOut() : next(toPath); } else if(result.dismiss === 'cancel') { // console.log('popup cancel case', ); // Handle page admin issue // console.log("PageAdminStore.activePage", PageAdminStore.activePage); pageAdminStore.keepPreviousPage(); allMapDataStore.tempFilterId = null; logOut ? logOut() : next(toPath); } else if(result.dismiss === 'backdrop') { // console.log('popup backdrop case', ); // Handle page admin issue // console.log("PageAdminStore.activePage", PageAdminStore.activePage); pageAdminStore.keepPreviousPage(); logOut ? null : next(false); } }; /** * Conformance Saved * @param { function } addConformanceCreateCheckId 後端 API */ export async function saveConformance(addConformanceCreateCheckId) { let fileName = ''; const { value, isConfirmed } = await Swal.fire({ title: 'SAVE NEW RULE', input: 'text', inputPlaceholder: 'Enter Rule Name.', inputValue: fileName, inputAttributes: { 'maxlength': 200, }, inputValidator: (value) => { if (!value) return 'You need to write something!'; fileName = value; }, icon: 'info', iconHtml: 'cloud_upload', iconColor: '#0099FF', reverseButtons:true, confirmButtonColor: '#0099FF', showCancelButton: true, cancelButtonColor: '#94a3b8', customClass: customClass, }); // 透過回傳值判斷要不要轉址 if(isConfirmed) { // 存檔成功 await addConformanceCreateCheckId(fileName); // 顯示儲存完成 if (value) savedSuccessfully(value); // 清空欄位 fileName = ''; return true; } else { // 點擊取消或空白處,為存檔失敗。 return false; } } /** * leave Conformance page * @param { function } next 執行完函式後的步驟 * @param { function } addConformanceCreateCheckId 後端 API * @param { string } toPath route path * @param { function } logOut 登出函式 */ export async function leaveConformance(next, addConformanceCreateCheckId, toPath, logOut) { const conformanceStore = ConformanceStore(); const result = await Swal.fire({ title: 'SAVE YOUR RULE?', icon: 'warning', iconColor: '#FF3366', reverseButtons:true, confirmButtonText: 'Yes', confirmButtonColor: '#FF3366', showCancelButton: true, cancelButtonText: 'No', cancelButtonColor: '#94a3b8', customClass: customClass }) if(result.isConfirmed) { if(conformanceStore.conformanceFilterCreateCheckId || conformanceStore.conformanceLogCreateCheckId) { await conformanceStore.updataConformance(); if(conformanceStore.isUpdataConformance) await savedSuccessfully(conformanceStore.conformanceFileName); } else { await saveConformance(addConformanceCreateCheckId); } logOut ? logOut() : next(toPath); } else if(result.dismiss === 'cancel') { conformanceStore.conformanceFilterTempCheckId = null; conformanceStore.conformanceLogTempCheckId = null; logOut ? logOut() : next(toPath); } else if(result.dismiss === 'backdrop') { logOut ? null : next(false); } }; /** * Upload failde First * @param { string } failureType 後端檔案錯誤類型 * @param { string } failureMsg 後端檔案錯誤訊息 */ export async function uploadFailedFirst(failureType, failureMsg, failureLoc) { // msg: 'not in UTF-8' | 'insufficient columns' | 'the csv file is empty' | 'the filename does not ends with .csv' | 'not a CSV file' // type: 'encoding' | 'insufficient_columns' | 'empty' | 'name_suffix' | mime_type let value = ''; switch (failureType) { case 'size': value = 'File size exceeds 90MB.'; break; case 'encoding': value = `Please use UFT-8 for character encoding: (Row #${failureLoc})`; break; case 'insufficient_columns': value = 'Need at least five columns of data.'; break; case 'empty': value = 'Need at least one record of data.'; break; case 'name_suffix': case 'mime_type': value = 'File is not in csv format.'; break; default: value = failureMsg; break; } await Swal.fire({ title: 'IMPORT FAILED', html: value, timer: 3000, // 停留 3 秒後自動關閉 showConfirmButton: false, icon: 'error', iconColor: '#FF3366', customClass: customClass }) }; /** * Upload failde Second * @param { array } detail 後端回傳的失敗訊息 */ export async function uploadFailedSecond(detail) { let srt = ''; let manySrt = ''; detail.forEach(i => { let content = ''; switch (i.type) { case 'too_many': manySrt = 'There are more errors.'; break; case 'unrecognized': content = `
Error(s) detected:
${manySrt} Please check.
Do you really want to delete ${name}?
The following dependent file(s) will also be deleted:
The following file(s) have been deleted by other user(s):