Skip to content

Commit

Permalink
Save / update scheme labels #147
Browse files Browse the repository at this point in the history
  • Loading branch information
johnatawnclementawn authored and jacobtylerwalls committed Jan 17, 2025
1 parent e33b026 commit 6a9627e
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 32 deletions.
24 changes: 22 additions & 2 deletions arches_lingo/src/arches_lingo/api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import arches from "arches";
import Cookies from "js-cookie";

import type { SchemeInstance } from "@/arches_lingo/types";
import type { AppellativeStatus, SchemeInstance } from "@/arches_lingo/types";

function getToken() {
const token = Cookies.get("csrftoken");
Expand Down Expand Up @@ -101,6 +100,27 @@ export const deleteSchemeLabelTile = async (
}
};

export const updateSchemeLabel = async (
schemeId: string,
tileId: string,
appellative_status: AppellativeStatus,
) => {
const response = await fetch(
arches.urls.api_scheme_label_tile(schemeId, tileId),
{
method: "PATCH",
headers: {
"X-CSRFTOKEN": getToken(),
"Content-Type": "application/json",
},
body: JSON.stringify(appellative_status),
},
);
const parsed = await response.json();
if (!response.ok) throw new Error(parsed.message || response.statusText);
return parsed;
};

export const fetchSchemeNotes = async (schemeId: string) => {
const response = await fetch(arches.urls.api_scheme_note(schemeId));
const parsed = await response.json();
Expand Down
81 changes: 66 additions & 15 deletions arches_lingo/src/arches_lingo/components/generic/LabelEditor.vue
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
<script setup lang="ts">
import { defineProps, inject, onMounted, ref, type Ref } from "vue";
import { defineProps, inject, onMounted, ref, toRaw, type Ref } from "vue";
import Button from "primevue/button";
import { useGettext } from "vue3-gettext";
import { useRoute } from "vue-router";
import { useToast } from "primevue/usetoast";
import {
fetchControlledListOptions,
fetchGroupRdmSystemList,
fetchPersonRdmSystemList,
fetchTextualWorkRdmSystemList,
updateSchemeLabel,
} from "@/arches_lingo/api.ts";
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 {
selectedLanguageKey,
EDIT,
LANGUAGE_CONTROLLED_LIST,
ERROR,
EVENT_TYPES_CONTROLLED_LIST,
LABEL_CONTROLLED_LIST,
STATUSES_CONTROLLED_LIST,
LANGUAGE_CONTROLLED_LIST,
METATYPES_CONTROLLED_LIST,
EVENT_TYPES_CONTROLLED_LIST,
selectedLanguageKey,
STATUSES_CONTROLLED_LIST,
} from "@/arches_lingo/constants.ts";
import type {
Expand All @@ -30,7 +37,12 @@ import type {
} 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 selectedLanguage = inject(selectedLanguageKey) as Ref<Language>;
const { $gettext } = useGettext();
const props = withDefaults(
defineProps<{
value?: AppellativeStatus;
Expand All @@ -49,8 +61,32 @@ const eventTypeOptions = ref<ControlledListItem[]>([]);
const groupAndPersonOptions = ref<ResourceInstanceReference[]>();
const textualWorkOptions = ref<ResourceInstanceReference[]>();
function onUpdate(newValue: string) {
console.log(newValue);
function onUpdate(
node: keyof AppellativeStatus,
val: ControlledListItem[] | ResourceInstanceReference[] | string,
) {
if (Array.isArray(val)) {
(value.value[node] as unknown) = val.map((item) => toRaw(item));
} else {
(value.value[node] as unknown) = toRaw(val);
}
}
async function save() {
try {
await updateSchemeLabel(
route.params.id as string,
value.value.tileid as string,
value.value,
);
emit("update");
} catch (error) {
toast.add({
severity: ERROR,
summary: $gettext("Error saving scheme"),
detail: (error as Error).message,
});
}
}
async function getControlledListOptions(
Expand Down Expand Up @@ -117,7 +153,9 @@ onMounted(async () => {
<NonLocalizedString
:value="value?.appellative_status_ascribed_name_content ?? ''"
:mode="EDIT"
@update="onUpdate"
@update="
(val) => onUpdate('appellative_status_ascribed_name_content', val)
"
/>
<!-- Label Language: reference datatype -->
<label for="">{{ $gettext("Label Language") }}</label>
Expand All @@ -126,7 +164,9 @@ onMounted(async () => {
:mode="EDIT"
:multi-value="false"
:options="languageOptions"
@update="onUpdate"
@update="
(val) => onUpdate('appellative_status_ascribed_name_language', val)
"
/>
<!-- Label Type: reference datatype -->
<label for="">{{ $gettext("Label Type") }}</label>
Expand All @@ -135,7 +175,7 @@ onMounted(async () => {
:mode="EDIT"
:multi-value="false"
:options="typeOptions"
@update="onUpdate"
@update="(val) => onUpdate('appellative_status_ascribed_relation', val)"
/>
<!-- Label Status: reference datatype -->
<label for="">{{ $gettext("Label Status") }}</label>
Expand All @@ -144,7 +184,7 @@ onMounted(async () => {
:mode="EDIT"
:multi-value="false"
:options="statusOptions"
@update="onUpdate"
@update="(val) => onUpdate('appellative_status_status', val)"
/>
<!-- Label Status Metatype: reference datatype -->
<label for="">{{ $gettext("Label Metatype") }}</label>
Expand All @@ -153,7 +193,7 @@ onMounted(async () => {
:mode="EDIT"
:multi-value="false"
:options="metatypeOptions"
@update="onUpdate"
@update="(val) => onUpdate('appellative_status_status_metatype', val)"
/>
<!-- Label Temporal Validity Start: date -->
<label for="">{{ $gettext("Label Temporal Validity Start") }}</label>
Expand All @@ -167,15 +207,20 @@ onMounted(async () => {
:value="value?.appellative_status_data_assignment_actor"
:mode="EDIT"
:options="groupAndPersonOptions"
@update="onUpdate"
@update="
(val) => onUpdate('appellative_status_data_assignment_actor', val)
"
/>
<!-- Sources: resource instance -->
<label for="">{{ $gettext("Sources") }}</label>
<ResourceInstanceRelationships
:value="value?.appellative_status_data_assignment_object_used"
:mode="EDIT"
:options="textualWorkOptions"
@update="onUpdate"
@update="
(val) =>
onUpdate('appellative_status_data_assignment_object_used', val)
"
/>
<!-- Warrant Type: reference datatype -->
<label for="">{{ $gettext("Warrant Type") }}</label>
Expand All @@ -184,6 +229,12 @@ onMounted(async () => {
:mode="EDIT"
:multi-value="false"
:options="eventTypeOptions"
@update="onUpdate"
@update="
(val) => onUpdate('appellative_status_data_assignment_type', val)
"
/>
<Button
:label="$gettext('Update')"
@click="save"
></Button>
</template>
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import {
OPEN_EDITOR,
NEW,
VIEW,
UPDATED,
} from "@/arches_lingo/constants.ts";
import { deleteSchemeLabelTile, fetchSchemeLabel } from "@/arches_lingo/api.ts";
import SchemeReportSection from "@/arches_lingo/components/scheme/report/SchemeSection.vue";
import LabelEditor from "@/arches_lingo/components/generic/LabelEditor.vue";
import MetaStringViewer from "@/arches_lingo/components/generic/MetaStringViewer.vue";
import ResourceInstanceRelationships from "@/arches_lingo/components/generic/ResourceInstanceRelationships.vue";
import ReferenceDatatype from "@/arches_lingo/components/generic/ReferenceDatatype.vue";
import LabelEditor from "@/arches_lingo/components/generic/LabelEditor.vue";
import SchemeReportSection from "@/arches_lingo/components/scheme/report/SchemeSection.vue";
import type {
AppellativeStatus,
Expand All @@ -40,8 +41,6 @@ withDefaults(
mode?: DataComponentMode;
tileId?: string | null;
args?: Array<object>;
// todo for Johnathan - if obj empty, create new tile
// if obj has values, load those values into the form
}>(),
{
mode: VIEW,
Expand All @@ -52,7 +51,7 @@ const schemeInstance = ref<SchemeInstance>();
defineExpose({ getSectionValue });
const emits = defineEmits([OPEN_EDITOR]);
const emit = defineEmits([OPEN_EDITOR, UPDATED]);
onMounted(() => {
getSectionValue();
Expand Down Expand Up @@ -104,7 +103,7 @@ function editSectionValue(tileId: string) {
(tile) => tile.tileid === tileId,
);
if (appellativeStatus && appellativeStatus.tileid === tileId) {
emits(OPEN_EDITOR, appellativeStatus.tileid);
emit(OPEN_EDITOR, appellativeStatus.tileid);
} else {
toast.add({
severity: ERROR,
Expand All @@ -114,21 +113,17 @@ function editSectionValue(tileId: string) {
}
}
// async function save() {
// // todo for Johnathan. This function will save the values of the form back to arches.
// }
// async function update() {
// // todo for Johnathan. This function will handle the update emit when the user changes values in your form - you store those values in this section.
// }
function update() {
emit(UPDATED);
}
</script>

<template>
<div v-if="mode === VIEW">
<SchemeReportSection
:title-text="$gettext('Scheme Labels')"
:button-text="$gettext('Add New Scheme Label')"
@open-editor="emits(OPEN_EDITOR)"
@open-editor="emit(OPEN_EDITOR)"
>
<MetaStringViewer
:meta-strings="schemeInstance?.appellative_status"
Expand Down Expand Up @@ -190,7 +185,10 @@ function editSectionValue(tileId: string) {
v-for="appellative_status in schemeInstance?.appellative_status"
:key="appellative_status.tileid"
>
<LabelEditor :value="appellative_status"></LabelEditor>
<LabelEditor
:value="appellative_status"
@update="update"
></LabelEditor>
</div>
</div>
</template>
Expand Down

0 comments on commit 6a9627e

Please sign in to comment.