ui_migration refactor(store) Remove lingering getters at wot.js pinia store
This commit is contained in:
parent
ace7ae9855
commit
f8c4ffe62b
1 changed files with 41 additions and 42 deletions
|
|
@ -9,6 +9,10 @@ export const useWotStore = defineStore(
|
|||
const thingDescriptions = ref({});
|
||||
const servient = ref(null);
|
||||
const helpers = ref(null);
|
||||
|
||||
// Getters
|
||||
const thingList = computed(() => Object.keys(thingDescriptions.value));
|
||||
|
||||
// Actions
|
||||
function addThingDescription(thingName, thingDescription) {
|
||||
thingDescriptions.value[thingName] = thingDescription;
|
||||
|
|
@ -42,58 +46,53 @@ export const useWotStore = defineStore(
|
|||
}
|
||||
}
|
||||
|
||||
const thingList = computed(() => Object.keys(thingDescriptions.value));
|
||||
|
||||
const thingAvailable = computed(() => (thingName) => {
|
||||
function thingAvailable(thingName) {
|
||||
return thingName in thingDescriptions.value;
|
||||
});
|
||||
}
|
||||
|
||||
const thingAffordanceAvailable = computed(() => (thing, affordanceType, affordance) => {
|
||||
function thingAffordanceAvailable(thing, affordanceType, affordance) {
|
||||
const td = thingDescriptions.value[thing];
|
||||
if (!td || !td[affordanceType]) return false;
|
||||
return affordance in td[affordanceType];
|
||||
});
|
||||
}
|
||||
|
||||
const thingFormUrl = computed(
|
||||
() =>
|
||||
(thing, affordanceType, affordance, op, allowUndefined = true) => {
|
||||
// Find the URL for a particular operation
|
||||
const td = thingDescriptions.value[thing];
|
||||
if (!td) {
|
||||
if (allowUndefined) return undefined;
|
||||
throw `Could not find form for ${affordanceType} ${thing}/${affordance} with op ${op}`;
|
||||
}
|
||||
const affordances = td[affordanceType];
|
||||
if (!affordances || !(affordance in affordances)) {
|
||||
if (allowUndefined) return undefined;
|
||||
throw `Could not find form for ${affordanceType} ${thing}/${affordance} with op ${op}`;
|
||||
}
|
||||
let href = findFormHref(affordances[affordance], op);
|
||||
if (href === undefined) {
|
||||
if (allowUndefined) return undefined;
|
||||
throw `Could not find form for ${affordanceType} ${thing}/${affordance} with op ${op}`;
|
||||
}
|
||||
// If we've found an href, prepend the `base` URL if appropriate
|
||||
if (href.startsWith("http")) return href;
|
||||
if ("base" in td) {
|
||||
let base = td.base;
|
||||
if (href.startsWith("/")) href = href.slice(1);
|
||||
if (!base.endsWith("/")) base += "/";
|
||||
return base + href;
|
||||
}
|
||||
return href;
|
||||
},
|
||||
);
|
||||
function thingFormUrl(thing, affordanceType, affordance, op, allowUndefined = true) {
|
||||
// Find the URL for a particular operation
|
||||
const td = thingDescriptions.value[thing];
|
||||
if (!td) {
|
||||
if (allowUndefined) return undefined;
|
||||
throw `Could not find form for ${affordanceType} ${thing}/${affordance} with op ${op}`;
|
||||
}
|
||||
const affordances = td[affordanceType];
|
||||
if (!affordances || !(affordance in affordances)) {
|
||||
if (allowUndefined) return undefined;
|
||||
throw `Could not find form for ${affordanceType} ${thing}/${affordance} with op ${op}`;
|
||||
}
|
||||
let href = findFormHref(affordances[affordance], op);
|
||||
if (href === undefined) {
|
||||
if (allowUndefined) return undefined;
|
||||
throw `Could not find form for ${affordanceType} ${thing}/${affordance} with op ${op}`;
|
||||
}
|
||||
// If we've found an href, prepend the `base` URL if appropriate
|
||||
if (href.startsWith("http")) return href;
|
||||
if ("base" in td) {
|
||||
let base = td.base;
|
||||
if (href.startsWith("/")) href = href.slice(1);
|
||||
if (!base.endsWith("/")) base += "/";
|
||||
return base + href;
|
||||
}
|
||||
return href;
|
||||
}
|
||||
|
||||
const thingPropertyUrl = computed(() => (thing, property, op, allowUndefined) => {
|
||||
function thingPropertyUrl(thing, property, op, allowUndefined) {
|
||||
// Find the URL for a particular property
|
||||
return thingFormUrl.value(thing, "properties", property, op, allowUndefined);
|
||||
});
|
||||
return thingFormUrl(thing, "properties", property, op, allowUndefined);
|
||||
}
|
||||
|
||||
const thingActionUrl = computed(() => (thing, action, op, allowUndefined) => {
|
||||
function thingActionUrl(thing, action, op, allowUndefined) {
|
||||
// Find the URL for a particular action
|
||||
return thingFormUrl.value(thing, "actions", action, op, allowUndefined);
|
||||
});
|
||||
return thingFormUrl(thing, "actions", action, op, allowUndefined);
|
||||
}
|
||||
|
||||
return {
|
||||
thingDescriptions,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue