reformat w/ prettier

feat/vaults
Tomáš Mládek 2021-10-29 17:24:24 +02:00
parent 30189b42a5
commit ea1b05d491
1 changed files with 125 additions and 104 deletions

View File

@ -3,11 +3,17 @@ import { fetcher } from "@/utils";
import useSWRV from "swrv";
import { computed, ComputedRef, Ref } from "vue";
export function useEntity(address: string | (() => string), condition?: () => Boolean) {
export function useEntity(
address: string | (() => string),
condition?: () => Boolean
) {
const { data, error, mutate } = useSWRV<ListingResult, unknown>(
() => (condition === undefined || condition()) ? `/api/obj/${typeof address === "string" ? address : address()}` : null,
fetcher, { revalidateOnFocus: false }
() =>
condition === undefined || condition()
? `/api/obj/${typeof address === "string" ? address : address()}`
: null,
fetcher,
{ revalidateOnFocus: false }
);
const entries = computed(() => {
@ -39,14 +45,20 @@ export function useEntity(address: string | (() => string), condition?: () => Bo
data,
error,
mutate
}
};
}
export function query(query: string | (() => string), condition?: () => Boolean) {
export function query(
query: string | (() => string),
condition?: () => Boolean
) {
const { data, error, mutate } = useSWRV<ListingResult, unknown>(
() => (condition === undefined || condition()) ? `/api/obj?query=${typeof query === "string" ? query : query()}` : null,
fetcher, { revalidateOnFocus: false }
() =>
condition === undefined || condition()
? `/api/obj?query=${typeof query === "string" ? query : query()}`
: null,
fetcher,
{ revalidateOnFocus: false }
);
const result = computed(() => {
@ -66,7 +78,7 @@ export function query(query: string | (() => string), condition?: () => Boolean)
data,
error,
mutate
}
};
}
interface EntityIdentification {
@ -74,28 +86,32 @@ interface EntityIdentification {
value: string;
}
export function identify(attributes: ComputedRef<[string, IEntry][]>): ComputedRef<EntityIdentification[]> {
export function identify(
attributes: ComputedRef<[string, IEntry][]>
): ComputedRef<EntityIdentification[]> {
// Get all identities of the object
const isEntries = computed(() => {
return attributes.value
.filter(([_, entry]) => entry.attribute === "IS")
.map(([_, entry]) => entry.value.c);
})
});
// Out of those, retrieve their TYPE_ID entries
const { data: typeIdListing } = query(() => {
return isEntries.value && `(matches (in ${isEntries.value.map((e) => `"${e}"`).join(" ")}) "TYPE_ID" ?)`;
return (
isEntries.value &&
`(matches (in ${isEntries.value
.map(e => `"${e}"`)
.join(" ")}) "TYPE_ID" ?)`
);
});
const typeIdAttributes: ComputedRef<[string, string][]> = computed(() => {
return Object.values(typeIdListing.value || {}).map((entry) => {
return Object.values(typeIdListing.value || {}).map(entry => {
return [entry.entity, entry.value.c];
});
});
// Finally, filter own object's attributes according to TYPE_IDs
return computed(() => {
// For each identity/TYPE_ID pair
@ -109,15 +125,20 @@ export function identify(attributes: ComputedRef<[string, IEntry][]>): ComputedR
return {
type,
value: attr.value.c
}
};
});
})
}).flat();
.flat();
});
}
export function asDict(attributes: [string, IEntry][]): { [key: string]: string } {
export function asDict(
attributes: [string, IEntry][]
): { [key: string]: string } {
const result = {} as { [key: string]: string };
attributes.map(([_, attribute]) => attribute).forEach((attribute) => {
attributes
.map(([_, attribute]) => attribute)
.forEach(attribute => {
result[attribute.attribute] = attribute.value.c;
});
return result;