Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change lists in resource relationships widget #186

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions arches_lingo/src/arches_lingo/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ export const fetchUser = async () => {
return parsed;
};

export const fetchRelatableResources = async (
graphSlug: string,
nodeAlias: string,
page: number,
) => {
const response = await fetch(
`${arches.urls.api_relatable_resources(graphSlug, nodeAlias)}?page=${page}`,
);

const parsed = await response.json();
if (!response.ok) throw new Error(parsed.message || response.statusText);
return parsed;
};

export const fetchLingoResources = async (graphSlug: string) => {
const response = await fetch(arches.urls.api_lingo_resources(graphSlug));
const parsed = await response.json();
Expand Down
89 changes: 13 additions & 76 deletions arches_lingo/src/arches_lingo/components/generic/LabelEditor.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script setup lang="ts">
import { computed, inject, onMounted, ref, toRaw, toRef, useId } from "vue";
import { computed, onMounted, ref, toRaw, toRef, useId } from "vue";

import Button from "primevue/button";
import { useGettext } from "vue3-gettext";
Expand All @@ -8,41 +8,28 @@ import { useToast } from "primevue/usetoast";

import { fetchLists } from "@/arches_references/api.ts";

import {
createScheme,
fetchLingoResources,
upsertLingoTile,
} from "@/arches_lingo/api.ts";
import { createScheme, upsertLingoTile } from "@/arches_lingo/api.ts";

import DateDatatype from "@/arches_lingo/components/generic/DateDatatype.vue";
import NonLocalizedString from "@/arches_lingo/components/generic/NonLocalizedString.vue";
import ReferenceDatatype from "@/arches_lingo/components/generic/ReferenceDatatype.vue";
import ResourceInstanceRelationships from "@/arches_lingo/components/generic/ResourceInstanceRelationships.vue";

import {
EDIT,
ERROR,
NEW,
selectedLanguageKey,
} from "@/arches_lingo/constants.ts";
import { EDIT, ERROR, NEW } from "@/arches_lingo/constants.ts";

import type { Ref } from "vue";
import type {
AppellativeStatus,
ControlledListResult,
ControlledListItem,
ControlledListItemResult,
ResourceInstanceReference,
ResourceInstanceResult,
SchemeInstance,
} from "@/arches_lingo/types.ts";
import type { Language } from "@/arches_vue_utils/types.ts";

const emit = defineEmits(["update"]);
const toast = useToast();
const route = useRoute();
const router = useRouter();
const selectedLanguage = inject(selectedLanguageKey) as Ref<Language>;
const { $gettext } = useGettext();

const props = withDefaults(
Expand All @@ -66,8 +53,6 @@ const typeOptions = ref<ControlledListItem[]>([]);
const statusOptions = ref<ControlledListItem[]>([]);
const metatypeOptions = ref<ControlledListItem[]>([]);
const eventTypeOptions = ref<ControlledListItem[]>([]);
const groupAndPersonOptions = ref<ResourceInstanceReference[]>();
const textualWorkOptions = ref<ResourceInstanceReference[]>();

const labelId = useId();
const labelLanguageId = useId();
Expand Down Expand Up @@ -121,15 +106,9 @@ function onUpdateReferenceDatatype(

function onUpdateResourceInstance(
node: keyof AppellativeStatus,
val: string[],
options: ResourceInstanceReference[],
val: ResourceInstanceReference[],
) {
if (val.length > 0) {
const selectedOptions = options.filter((option) =>
val.includes(option.resourceId),
);
(formValue.value[node] as unknown) = selectedOptions;
}
(formValue.value[node] as unknown) = val;
}

async function save() {
Expand Down Expand Up @@ -200,51 +179,9 @@ async function getControlledLists() {
});
}

async function getResourceInstanceOptions(
fetchOptions: () => Promise<ResourceInstanceResult[]>,
): Promise<ResourceInstanceReference[]> {
let options;
try {
options = await fetchOptions();
} catch (error) {
toast.add({
severity: ERROR,
summary: $gettext("Error"),
detail:
error instanceof Error
? error.message
: $gettext("Could not fetch the resource instance options"),
});
return [];
}
const results = options.map((option: ResourceInstanceResult) => {
const result: ResourceInstanceReference = {
display_value: option.descriptors[selectedLanguage.value.code].name,
resourceId: option.resourceinstanceid,
ontologyProperty: "ac41d9be-79db-4256-b368-2f4559cfbe55",
inverseOntologyProperty: "ac41d9be-79db-4256-b368-2f4559cfbe55",
};
return result;
});
return results;
}
async function initializeSelectOptions() {
onMounted(async () => {
getControlledLists();
groupAndPersonOptions.value = await getResourceInstanceOptions(() =>
fetchLingoResources("group"),
);
groupAndPersonOptions.value = [
...(groupAndPersonOptions.value || []),
...(await getResourceInstanceOptions(() =>
fetchLingoResources("person"),
)),
];
textualWorkOptions.value = await getResourceInstanceOptions(() =>
fetchLingoResources("textual_work"),
);
}

onMounted(initializeSelectOptions);
});
</script>

<template>
Expand Down Expand Up @@ -344,29 +281,29 @@ onMounted(initializeSelectOptions);
<ResourceInstanceRelationships
:value="formValue?.appellative_status_data_assignment_actor"
:mode="EDIT"
:options="groupAndPersonOptions"
:pt-aria-labeled-by="labelContributorId"
graph-slug="scheme"
node-alias="appellative_status_data_assignment_actor"
@update="
(val) =>
(val: ResourceInstanceReference[]) =>
onUpdateResourceInstance(
'appellative_status_data_assignment_actor',
val,
groupAndPersonOptions ?? [],
)
"
/>
<p :id="labelSourcesId">{{ $gettext("Sources") }}</p>
<ResourceInstanceRelationships
:value="formValue?.appellative_status_data_assignment_object_used"
:mode="EDIT"
:options="textualWorkOptions"
graph-slug="scheme"
node-alias="appellative_status_data_assignment_object_used"
:pt-aria-labeled-by="labelSourcesId"
@update="
(val) =>
(val: ResourceInstanceReference[]) =>
onUpdateResourceInstance(
'appellative_status_data_assignment_object_used',
val,
textualWorkOptions ?? [],
)
"
/>
Expand Down
97 changes: 21 additions & 76 deletions arches_lingo/src/arches_lingo/components/generic/NoteEditor.vue
Original file line number Diff line number Diff line change
@@ -1,54 +1,33 @@
<script setup lang="ts">
import {
computed,
inject,
onMounted,
ref,
toRaw,
toRef,
useId,
type Ref,
} from "vue";
import { computed, onMounted, ref, toRaw, toRef, useId } from "vue";

import Button from "primevue/button";
import { useGettext } from "vue3-gettext";
import { useRoute, useRouter } from "vue-router";
import { useToast } from "primevue/usetoast";

import {
createScheme,
fetchLingoResources,
upsertLingoTile,
} from "@/arches_lingo/api.ts";
import { createScheme, upsertLingoTile } from "@/arches_lingo/api.ts";
import { fetchLists } from "@/arches_references/api.ts";
import DateDatatype from "@/arches_lingo/components/generic/DateDatatype.vue";
import NonLocalizedString from "@/arches_lingo/components/generic/NonLocalizedString.vue";
import ReferenceDatatype from "@/arches_lingo/components/generic/ReferenceDatatype.vue";
import ResourceInstanceRelationships from "@/arches_lingo/components/generic/ResourceInstanceRelationships.vue";

import {
EDIT,
ERROR,
NEW,
selectedLanguageKey,
} from "@/arches_lingo/constants.ts";
import { EDIT, ERROR, NEW } from "@/arches_lingo/constants.ts";

import type {
ControlledListItem,
ControlledListItemResult,
ControlledListResult,
ResourceInstanceReference,
ResourceInstanceResult,
SchemeInstance,
SchemeStatement,
} from "@/arches_lingo/types.ts";
import type { Language } from "@/arches_vue_utils/types.ts";

const emit = defineEmits(["update"]);
const toast = useToast();
const route = useRoute();
const router = useRouter();
const selectedLanguage = inject(selectedLanguageKey) as Ref<Language>;
const { $gettext } = useGettext();

const props = withDefaults(
Expand Down Expand Up @@ -76,8 +55,6 @@ const languageOptions = ref<ControlledListItem[]>([]);
const typeOptions = ref<ControlledListItem[]>([]);
const metatypeOptions = ref<ControlledListItem[]>([]);
const eventTypeOptions = ref<ControlledListItem[]>([]);
const groupAndPersonOptions = ref<ResourceInstanceReference[]>();
const textualWorkOptions = ref<ResourceInstanceReference[]>();

const labelId = useId();
const labelLanguageId = useId();
Expand Down Expand Up @@ -125,15 +102,9 @@ function onUpdateReferenceDatatype(

function onUpdateResourceInstance(
node: keyof SchemeStatement,
val: string[],
options: ResourceInstanceReference[],
val: ResourceInstanceReference[],
) {
if (val.length > 0) {
const selectedOptions = options.filter((option) =>
val.includes(option.resourceId),
);
(formValue.value[node] as unknown) = selectedOptions;
}
(formValue.value[node] as unknown) = val ?? [];
}

async function getControlledLists() {
Expand Down Expand Up @@ -204,36 +175,8 @@ async function save() {
}
}

async function getResourceInstanceOptions(
fetchOptions: () => Promise<ResourceInstanceResult[]>,
): Promise<ResourceInstanceReference[]> {
const options = await fetchOptions();
const results = options.map((option: ResourceInstanceResult) => {
const result: ResourceInstanceReference = {
display_value: option.descriptors[selectedLanguage.value.code].name,
resourceId: option.resourceinstanceid,
ontologyProperty: "ac41d9be-79db-4256-b368-2f4559cfbe55",
inverseOntologyProperty: "ac41d9be-79db-4256-b368-2f4559cfbe55",
};
return result;
});
return results;
}

async function initializeSelectOptions() {
getControlledLists();
groupAndPersonOptions.value = await getResourceInstanceOptions(() =>
fetchLingoResources("group"),
);
groupAndPersonOptions.value = [
...(groupAndPersonOptions.value || []),
...(await getResourceInstanceOptions(() =>
fetchLingoResources("person"),
)),
];
textualWorkOptions.value = await getResourceInstanceOptions(() =>
fetchLingoResources("textual_work"),
);
}
</script>

Expand All @@ -245,6 +188,7 @@ async function initializeSelectOptions() {
:mode="EDIT"
@update="(val) => onUpdateString('statement_content_n1', val)"
/>

<!-- Statement Language: reference datatype -->
<label :for="labelLanguageId">{{ $gettext("Statement Language") }}</label>
<ReferenceDatatype
Expand All @@ -257,6 +201,7 @@ async function initializeSelectOptions() {
(val) => onUpdateReferenceDatatype('statement_language_n1', val)
"
/>

<!-- Statement Type: reference datatype -->
<label :for="labelTypeId">{{ $gettext("Statement Type") }}</label>
<ReferenceDatatype
Expand Down Expand Up @@ -323,33 +268,32 @@ async function initializeSelectOptions() {
<ResourceInstanceRelationships
:value="formValue?.statement_data_assignment_actor"
:mode="EDIT"
:options="groupAndPersonOptions"
graph-slug="scheme"
node-alias="statement_data_assignment_actor"
:pt-aria-labeled-by="labelContributorId"
@update="
(val) =>
onUpdateResourceInstance(
'statement_data_assignment_actor',
val,
groupAndPersonOptions ?? [],
)
@updated="
(val: ResourceInstanceReference[]) =>
onUpdateResourceInstance('statement_data_assignment_actor', val)
"
/>

<!-- Sources: resource instance -->
<label :for="labelSourcesId">{{ $gettext("Sources") }}</label>
<ResourceInstanceRelationships
:value="formValue?.statement_data_assignment_object_used"
:mode="EDIT"
:options="textualWorkOptions"
graph-slug="scheme"
node-alias="statement_data_assignment_object_used"
:pt-aria-labeled-by="labelSourcesId"
@update="
(val) =>
@updated="
(val: ResourceInstanceReference[]) =>
onUpdateResourceInstance(
'statement_data_assignment_object_used',
val,
textualWorkOptions ?? [],
)
"
/>

<!-- Warrant Type: reference datatype -->
<label :for="labelWarrantId">{{ $gettext("Warrant Type") }}</label>
<ReferenceDatatype
Expand All @@ -358,11 +302,12 @@ async function initializeSelectOptions() {
:multi-value="false"
:options="eventTypeOptions"
:pt-aria-labeled-by="labelWarrantId"
@update="
(val) =>
@updated="
(val: ControlledListItem[]) =>
onUpdateReferenceDatatype('statement_data_assignment_type', val)
"
/>

<Button
:label="$gettext('Update')"
@click="save"
Expand Down
Loading