Files
api/web/store/trial.ts
Michael Datelle 72860e71fe test: create tests for stores batch 3 (#1358)
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **New Features**
- Added comprehensive test coverage for the purchase, replaceRenew,
modal, notifications, theme, trial, unraidApi, unraidApiSettings,
updateOs, updateOsActions, updateOsChangelog, activationCode, and
callbackActions stores.
- Exposed callback error state in the callbackActions store for external
access.
  - Made error state publicly accessible in the replaceRenew store.

- **Tests**
- Introduced new test files covering state, getters, actions, and side
effects for multiple stores including modal, notifications, purchase,
replaceRenew, theme, trial, unraidApi, unraidApiSettings, updateOs,
updateOsActions, updateOsChangelog, activationCode, and callbackActions.
- Enhanced existing test suites with additional mocks, reactive state
handling, and expanded test cases for improved coverage and robustness.

- **Refactor**
- Improved code clarity and readability in modal, notifications,
purchase, replaceRenew, trial, theme, updateOsActions, callbackActions,
and unraidApi stores through import reorganization and formatting
adjustments.
- Updated imports to include reactive and computed utilities for
enhanced state management in several stores.
- Standardized import styles and streamlined store definitions in the
unraidApiSettings store.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: mdatelle <mike@datelle.net>
2025-04-16 17:06:52 -04:00

96 lines
3.0 KiB
TypeScript

import { computed, ref, watch } from 'vue';
import { createPinia, defineStore, setActivePinia } from 'pinia';
import type { ExternalPayload, TrialExtend, TrialStart } from '@unraid/shared-callbacks';
import type { StartTrialResponse } from '~/composables/services/keyServer';
import { addPreventClose, removePreventClose } from '~/composables/preventClose';
import { startTrial } from '~/composables/services/keyServer';
import { useCallbackActionsStore } from '~/store/callbackActions';
import { useDropdownStore } from '~/store/dropdown';
import { useServerStore } from '~/store/server';
/**
* @see https://stackoverflow.com/questions/73476371/using-pinia-with-vue-js-web-components
* @see https://github.com/vuejs/pinia/discussions/1085
*/
setActivePinia(createPinia());
export const useTrialStore = defineStore('trial', () => {
const callbackActionsStore = useCallbackActionsStore();
const dropdownStore = useDropdownStore();
const serverStore = useServerStore();
type TrialStatus = 'failed' | 'ready' | TrialExtend | TrialStart | 'success';
const trialStatus = ref<TrialStatus>('ready');
const trialModalLoading = computed(
() => trialStatus.value === 'trialExtend' || trialStatus.value === 'trialStart'
);
const trialModalVisible = computed(
() =>
trialStatus.value === 'failed' ||
trialStatus.value === 'trialExtend' ||
trialStatus.value === 'trialStart'
);
const requestTrial = async (type?: TrialExtend | TrialStart) => {
try {
const payload = {
guid: serverStore.guid,
timestamp: Math.floor(Date.now() / 1000),
};
const response: StartTrialResponse = await startTrial(payload);
if (!response.license) {
trialStatus.value = 'failed';
return console.error('[requestTrial]', 'No license returned', response);
}
// manually create a payload to mimic a callback for key installs
const trialStartData: ExternalPayload = {
actions: [
{
keyUrl: response.license,
type: type ?? 'trialStart',
},
],
sender: window.location.href,
type: 'forUpc',
};
trialStatus.value = 'success';
return callbackActionsStore.saveCallbackData(trialStartData);
} catch (error) {
trialStatus.value = 'failed';
console.error('[requestTrial]', error);
}
};
const setTrialStatus = (status: TrialStatus) => {
trialStatus.value = status;
};
watch(trialStatus, (newVal) => {
// opening
if (newVal === 'trialExtend' || newVal === 'trialStart') {
addPreventClose();
dropdownStore.dropdownHide(); // close the dropdown when the trial modal is opened
setTimeout(() => {
requestTrial(newVal);
}, 1500);
}
// allow closure
if (newVal === 'failed' || newVal === 'success') {
removePreventClose();
}
});
return {
// State
trialModalLoading,
trialModalVisible,
trialStatus,
// Actions
requestTrial,
setTrialStatus,
};
});