Skip to content

Commit

Permalink
feat: implement hosting categories UI
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Rybczonek committed Apr 17, 2024
1 parent 9205a1a commit a93a37b
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 71 deletions.
46 changes: 22 additions & 24 deletions src/components/settings/hostingPreferences/HAppSelectionSection.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,35 @@ import SettingsSection from '../SettingsSection.vue'
import ExclusionSelect from '@/components/settings/hostingPreferences/ExclusionSelect.vue'
import { categories } from '@/constants/categories'
import { countries } from '@/constants/countries'
import type { HostingJurisdictions } from '@/types/types'
import type { HostingJurisdictions, HostingCategories } from '@/types/types'
import { ECriteriaType } from '@/types/types'
const props = defineProps<{
hostingJurisdictions: HostingJurisdictions
isJurisdictionLoading: boolean
hostingCategories: HostingCategories
isLoading: boolean
}>()
const isHostingCategoriesExclusionsBusy = ref(false)
const emit = defineEmits(['update:jurisdiction'])
const emit = defineEmits(['update:jurisdiction', 'update:categories'])
interface Jurisdiction {
// eslint-disable-next-line @typescript-eslint/naming-convention
criteria_type: ECriteriaType
value: string[]
}
interface Categories {
// eslint-disable-next-line @typescript-eslint/naming-convention
criteria_type: ECriteriaType
value: string[]
}
function updateJurisdiction(jurisdiction: Jurisdiction): void {
emit('update:jurisdiction', jurisdiction)
}
function saveHostingCategoriesExclusions(): void {
isHostingCategoriesExclusionsBusy.value = true
// Make an API call to save new selected options
setTimeout(() => {
isHostingCategoriesExclusionsBusy.value = false
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
}, 2000)
function updateCategories(categories: Categories): void {
emit('update:categories', categories)
}
</script>

Expand All @@ -52,7 +51,7 @@ function saveHostingCategoriesExclusions(): void {
:key="props.hostingJurisdictions.timestamp"
label="hosting_preferences.happ_selection.exclude"
:options="countries"
:is-busy="isJurisdictionLoading"
:is-busy="isLoading"
:initially-selected="props.hostingJurisdictions.criteriaType === ECriteriaType.exclude ? props.hostingJurisdictions.value : []"
@save="updateJurisdiction({value: $event, criteria_type: ECriteriaType.exclude})"
/>
Expand All @@ -62,7 +61,7 @@ function saveHostingCategoriesExclusions(): void {
:key="props.hostingJurisdictions.timestamp"
label="hosting_preferences.happ_selection.include"
:options="countries"
:is-busy="isJurisdictionLoading"
:is-busy="isLoading"
:initially-selected="props.hostingJurisdictions.criteriaType === 'include' ? props.hostingJurisdictions.value : []"
@save="updateJurisdiction({value: $event, criteria_type: ECriteriaType.include})"
/>
Expand All @@ -75,18 +74,22 @@ function saveHostingCategoriesExclusions(): void {
</span>
<div class="happ-selection-section__tags-item happ-selection-section__tags-item-exclude">
<ExclusionSelect
:key="props.hostingCategories.timestamp"
label="hosting_preferences.happ_selection.exclude"
:options="categories"
:is-busy="isHostingCategoriesExclusionsBusy"
@save="saveHostingCategoriesExclusions"
:is-busy="isLoading"
:initially-selected="props.hostingCategories.criteriaType === 'include' ? props.hostingCategories.value : []"
@save="updateCategories({value: $event, criteria_type: ECriteriaType.include})"
/>
</div>
<div class="happ-selection-section__tags-item happ-selection-section__tags-item-include">
<ExclusionSelect
:key="props.hostingCategories.timestamp"
label="hosting_preferences.happ_selection.include"
:options="categories"
:is-busy="isHostingCategoriesExclusionsBusy"
@save="saveHostingCategoriesExclusions"
:is-busy="isLoading"
:initially-selected="props.hostingCategories.criteriaType === 'include' ? props.hostingCategories.value : []"
@save="updateCategories({value: $event, criteria_type: ECriteriaType.include})"
/>
</div>
</div>
Expand All @@ -100,11 +103,6 @@ function saveHostingCategoriesExclusions(): void {
}
.happ-selection-section {
&--category {
opacity: 0.5;
pointer-events: none;
}
&__tags {
margin-top: 16px;
Expand Down
52 changes: 8 additions & 44 deletions src/interfaces/HposInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ export interface HostPreferencesResponse {
value: string[]
is_exclusion: boolean
}
categories_prefs: {
value: string[]
is_exclusion: boolean
}
timestamp: number
}

Expand All @@ -137,6 +141,10 @@ export interface DefaultPreferencesPayload {
value: string[]
is_exclusion: boolean
}
categories_prefs: {
value: string[]
is_exclusion: boolean
}
}

type HposHolochainCallResponse =
Expand Down Expand Up @@ -656,50 +664,6 @@ export function useHposInterface(): HposInterface {
}
}

async function getHostingJurisdictions(): Promise<
HposHolochainCallResponse | { error: unknown }
> {
let holoportId = ''

if (window.location.host.split(':')[0] === 'localhost') {
const holoportUrl = `${import.meta.env.VITE_HOLOPORT_URL}` || ''
holoportId = holoportUrl.split('//')[1]?.split('.')[0] ?? ''
} else {
holoportId = window.location.host.split('//')[1]?.split('.')[0] ?? ''
}

const params = {
appId: localStorage.getItem(kCoreAppVersionLSKey),
roleId: 'core-app',
zomeName: 'hha',
fnName: 'get_hosting_jurisdictions',
payload: holoportId
}

try {
const hostingJurisdictions = await hposHolochainCall({
method: 'post',
path: '/zome_call',
pathPrefix: '/api/v2',
responseType: 'arraybuffer',
params
})

return hostingJurisdictions
} catch (error) {
console.error('getHostingJurisdictions encountered an error: ', error)
return {
jurisdiction_prefs: {
value: ['Poland'], // QUESTION: shouldn't this be empty if there is an error?
is_exclusion: false
},
timestamp: 0
}

// return false
}
}

async function checkAuth(
email: string,
password: string,
Expand Down
16 changes: 15 additions & 1 deletion src/pages/HostingPreferences.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const isPaidHostingEnabled = ref(userStore.kycLevel !== EUserKycLevel.two)
const pricesSettings = computed(() => preferencesStore.pricesSettings)
const invoicesSettings = computed(() => preferencesStore.invoicesSettings)
const hostingJurisdictions = computed(() => preferencesStore.hostingJurisdictions)
const hostingCategories = computed(() => preferencesStore.hostingCategories)
const isModalVisible = ref(false)
Expand Down Expand Up @@ -93,11 +94,22 @@ interface HostingJurisdiction {
value: string[]
}
interface HostingCategories {
// eslint-disable-next-line @typescript-eslint/naming-convention
criteria_type: ECriteriaType
value: string[]
}
async function updateHostingJurisdiction(jurisdiction: HostingJurisdiction): Promise<void> {
preferencesStore.updateHostingJurisdiction(jurisdiction)
await setDefaultHostPreferences()
}
async function updateHostingCategories(categories: HostingCategories): Promise<void> {
preferencesStore.updateHostingCategories(categories)
await setDefaultHostPreferences()
}
function onTogglePaidHosting(isToggledOn: boolean): void {
isPaidHostingEnabled.value = isToggledOn
Expand Down Expand Up @@ -157,11 +169,13 @@ async function invoicePaymentDueChanged({ period }: InvoiceDue): Promise<void> {
/>

<HAppSelectionSection
:is-jurisdiction-loading="isUpdating"
:is-loading="isUpdating"
:hosting-jurisdictions="hostingJurisdictions"
:hosting-categories="hostingCategories"
class="hosting-preferences__happ-selection"
data-test-hosting-preferences-happ-selection-section
@update:jurisdiction="updateHostingJurisdiction"
@update:categories="updateHostingCategories"
/>

<GlobalHostingPlanModal
Expand Down
37 changes: 36 additions & 1 deletion src/store/preferences.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { defineStore } from 'pinia'
import { DefaultPreferencesPayload, useHposInterface } from '@/interfaces/HposInterface'
import { isHostPreferencesResponse } from '@/types/predicates'
import type { HostingJurisdictions, InvoicesData, PricesData } from '@/types/types'
import type {
HostingCategories,
HostingJurisdictions,
InvoicesData,
PricesData
} from '@/types/types'
import { ECriteriaType } from '@/types/types'

const { getHostPreferences, setDefaultHAppPreferences } = useHposInterface()
Expand All @@ -13,6 +18,12 @@ interface State {
pricesSettings: PricesData
invoicesSettings: InvoicesData
hostingJurisdictions: HostingJurisdictions
hostingCategories: HostingCategories
}

interface RawHostingCategories {
criteria_type: ECriteriaType
value: string[]
}

export const usePreferencesStore = defineStore('preferences', {
Expand All @@ -36,6 +47,11 @@ export const usePreferencesStore = defineStore('preferences', {
value: [],
criteriaType: ECriteriaType.exclude,
timestamp: 0
},
hostingCategories: {
value: [],
criteriaType: ECriteriaType.exclude,
timestamp: 0
}
}),

Expand Down Expand Up @@ -64,6 +80,10 @@ export const usePreferencesStore = defineStore('preferences', {
jurisdiction_prefs: {
value: this.hostingJurisdictions.value,
is_exclusion: this.hostingJurisdictions.criteriaType === ECriteriaType.exclude
},
categories_prefs: {
value: this.hostingCategories.value,
is_exclusion: this.hostingCategories.criteriaType === ECriteriaType.exclude
}
}

Expand Down Expand Up @@ -104,12 +124,18 @@ export const usePreferencesStore = defineStore('preferences', {
this.hostingJurisdictions.criteriaType = jurisdiction.criteria_type
},

updateHostingCategories(categories: RawHostingCategories): void {
this.hostingCategories.value = categories.value
this.hostingCategories.criteriaType = categories.criteria_type
},

async getHostPreferences(): Promise<void> {
const response = await getHostPreferences()

if (!isHostPreferencesResponse(response)) {
// If the request failed, update the timestamp to trigger a re-render of the selects
this.hostingJurisdictions.timestamp = Date.now()
this.hostingCategories.timestamp = Date.now()
return
}

Expand All @@ -122,6 +148,15 @@ export const usePreferencesStore = defineStore('preferences', {
timestamp: response.timestamp
}

this.hostingCategories = {
value: response.categories_prefs?.value || [],
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
criteriaType: response.categories_prefs?.is_exclusion
? ECriteriaType.exclude
: ECriteriaType.include,
timestamp: response.timestamp
}

const {
max_fuel_before_invoice: invoiceHolofuelThreshold,
max_time_before_invoice: invoiceDurationThreshold,
Expand Down
6 changes: 6 additions & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,9 @@ export interface HostingJurisdictions {
criteriaType: ECriteriaType
timestamp: number
}

export interface HostingCategories {
value: string[]
criteriaType: ECriteriaType
timestamp: number
}
2 changes: 1 addition & 1 deletion ui-common-library

0 comments on commit a93a37b

Please sign in to comment.