Skip to content

Commit

Permalink
Fix label values not updating between edit sessions and update save m…
Browse files Browse the repository at this point in the history
…ethod #147
  • Loading branch information
johnatawnclementawn authored and jacobtylerwalls committed Jan 17, 2025
1 parent df95e91 commit 1983128
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 34 deletions.
20 changes: 20 additions & 0 deletions arches_lingo/src/arches_lingo/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,26 @@ export const fetchSchemeLabel = async (schemeId: string) => {
return parsed;
};

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

export const deleteSchemeLabelTile = async (
schemeId: string,
tileId: string,
Expand Down
69 changes: 47 additions & 22 deletions arches_lingo/src/arches_lingo/components/generic/LabelEditor.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<script setup lang="ts">
import { inject, onMounted, ref, toRaw, type Ref } from "vue";
import { computed, inject, onMounted, ref, toRaw, toRef, type Ref } from "vue";
import Button from "primevue/button";
import { useGettext } from "vue3-gettext";
import { useRoute } from "vue-router";
import { useRoute, useRouter } from "vue-router";
import { useToast } from "primevue/usetoast";
import {
createScheme,
createSchemeLabel,
fetchControlledListOptions,
fetchGroupRdmSystemList,
fetchPersonRdmSystemList,
Expand All @@ -26,8 +28,10 @@ import {
LABEL_CONTROLLED_LIST,
LANGUAGE_CONTROLLED_LIST,
METATYPES_CONTROLLED_LIST,
NEW,
selectedLanguageKey,
STATUSES_CONTROLLED_LIST,
UPDATED,
} from "@/arches_lingo/constants.ts";
import type {
Expand All @@ -36,12 +40,14 @@ import type {
ControlledListItemResult,
ResourceInstanceReference,
ResourceInstanceResult,
SchemeInstance,
} from "@/arches_lingo/types.ts";
import type { Language } from "@/arches_vue_utils/types.ts";
const emit = defineEmits(["update"]);
const emit = defineEmits([UPDATED]);
const toast = useToast();
const route = useRoute();
const router = useRouter();
const selectedLanguage = inject(selectedLanguageKey) as Ref<Language>;
const { $gettext } = useGettext();
Expand All @@ -53,7 +59,13 @@ const props = withDefaults(
value: () => ({}) as AppellativeStatus,
},
);
const value = ref(props.value);
const valueRef = toRef(props, "value");
const formValue = computed({
get: () => valueRef.value,
set: (newVal: AppellativeStatus) => {
valueRef.value = newVal;
},
});
const languageOptions = ref<ControlledListItem[]>([]);
const typeOptions = ref<ControlledListItem[]>([]);
Expand All @@ -64,14 +76,14 @@ const groupAndPersonOptions = ref<ResourceInstanceReference[]>();
const textualWorkOptions = ref<ResourceInstanceReference[]>();
function onUpdateString(node: keyof AppellativeStatus, val: string) {
(value.value[node] as unknown) = toRaw(val);
(formValue.value[node] as unknown) = toRaw(val);
}
function onUpdateReferenceDatatype(
node: keyof AppellativeStatus,
val: ControlledListItem[],
) {
(value.value[node] as unknown) = val.map((item) => toRaw(item));
(formValue.value[node] as unknown) = val.map((item) => toRaw(item));
}
function onUpdateResourceInstance(
Expand All @@ -83,18 +95,31 @@ function onUpdateResourceInstance(
const selectedOptions = options.filter((option) =>
val.includes(option.resourceId),
);
(value.value[node] as unknown) = selectedOptions;
(formValue.value[node] as unknown) = selectedOptions;
}
}
async function save() {
try {
await updateSchemeLabel(
route.params.id as string,
value.value.tileid as string,
value.value,
);
emit("update");
if (route.params.id === NEW) {
const newSchemeInstance: SchemeInstance = {
appellative_status: [toRaw(formValue.value)],
};
const updated = await createScheme(newSchemeInstance);
await router.push({
name: "scheme",
params: { id: updated.resourceinstanceid },
});
} else if (!formValue.value.tileid) {
await createSchemeLabel(route.params.id as string, formValue.value);
} else {
await updateSchemeLabel(
route.params.id as string,
formValue.value.tileid as string,
formValue.value,
);
}
emit(UPDATED);
} catch (error) {
toast.add({
severity: ERROR,
Expand Down Expand Up @@ -176,7 +201,7 @@ onMounted(async () => {
<!-- Label Language: reference datatype -->
<label for="">{{ $gettext("Label Language") }}</label>
<ReferenceDatatype
:value="value?.appellative_status_ascribed_name_language"
:value="formValue?.appellative_status_ascribed_name_language"
:mode="EDIT"
:multi-value="false"
:options="languageOptions"
Expand All @@ -191,7 +216,7 @@ onMounted(async () => {
<!-- Label Type: reference datatype -->
<label for="">{{ $gettext("Label Type") }}</label>
<ReferenceDatatype
:value="value?.appellative_status_ascribed_relation"
:value="formValue?.appellative_status_ascribed_relation"
:mode="EDIT"
:multi-value="false"
:options="typeOptions"
Expand All @@ -206,7 +231,7 @@ onMounted(async () => {
<!-- Label Status: reference datatype -->
<label for="">{{ $gettext("Label Status") }}</label>
<ReferenceDatatype
:value="value?.appellative_status_status"
:value="formValue?.appellative_status_status"
:mode="EDIT"
:multi-value="false"
:options="statusOptions"
Expand All @@ -217,7 +242,7 @@ onMounted(async () => {
<!-- Label Status Metatype: reference datatype -->
<label for="">{{ $gettext("Label Metatype") }}</label>
<ReferenceDatatype
:value="value?.appellative_status_status_metatype"
:value="formValue?.appellative_status_status_metatype"
:mode="EDIT"
:multi-value="false"
:options="metatypeOptions"
Expand All @@ -233,7 +258,7 @@ onMounted(async () => {
<!-- Label Temporal Validity Start: date -->
<label for="">{{ $gettext("Label Temporal Validity Start") }}</label>
<DateDatatype
:value="value?.appellative_status_timespan_begin_of_the_begin"
:value="formValue?.appellative_status_timespan_begin_of_the_begin"
:mode="EDIT"
@update="
(val) =>
Expand All @@ -247,7 +272,7 @@ onMounted(async () => {
<!-- Label Temporal Validity End: date -->
<label for="">{{ $gettext("Label Temporal Validity End") }}</label>
<DateDatatype
:value="value?.appellative_status_timespan_end_of_the_end"
:value="formValue?.appellative_status_timespan_end_of_the_end"
:mode="EDIT"
@update="
(val) =>
Expand All @@ -261,7 +286,7 @@ onMounted(async () => {
<!-- Contributor: resource instance -->
<label for="">{{ $gettext("Contributor") }}</label>
<ResourceInstanceRelationships
:value="value?.appellative_status_data_assignment_actor"
:value="formValue?.appellative_status_data_assignment_actor"
:mode="EDIT"
:options="groupAndPersonOptions"
@update="
Expand All @@ -276,7 +301,7 @@ onMounted(async () => {
<!-- Sources: resource instance -->
<label for="">{{ $gettext("Sources") }}</label>
<ResourceInstanceRelationships
:value="value?.appellative_status_data_assignment_object_used"
:value="formValue?.appellative_status_data_assignment_object_used"
:mode="EDIT"
:options="textualWorkOptions"
@update="
Expand All @@ -291,7 +316,7 @@ onMounted(async () => {
<!-- Warrant Type: reference datatype -->
<label for="">{{ $gettext("Warrant Type") }}</label>
<ReferenceDatatype
:value="value?.appellative_status_data_assignment_type"
:value="formValue?.appellative_status_data_assignment_type"
:mode="EDIT"
:multi-value="false"
:options="eventTypeOptions"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script setup lang="ts">
import { useGettext } from "vue3-gettext";
import { onMounted, ref } from "vue";
import { computed, onMounted, ref } from "vue";
import { useRoute } from "vue-router";
import { useToast } from "primevue/usetoast";
Expand Down Expand Up @@ -36,18 +36,22 @@ const metaStringLabel: MetaStringText = {
noRecords: $gettext("No scheme labels were found."),
};
withDefaults(
const props = withDefaults(
defineProps<{
mode?: DataComponentMode;
tileId?: string | null;
args?: Array<object>;
}>(),
{
mode: VIEW,
tileId: null, // editor arg specifying what tile to operate on.
},
);
const schemeInstance = ref<SchemeInstance>();
const appellativeStatusToEdit = computed(() => {
return schemeInstance.value?.appellative_status?.find(
(tile) => tile.tileid === props.tileId,
);
});
defineExpose({ getSectionValue });
Expand Down Expand Up @@ -181,15 +185,10 @@ function update() {
</SchemeReportSection>
</div>
<div v-if="mode === EDIT">
<div
v-for="appellative_status in schemeInstance?.appellative_status"
:key="appellative_status.tileid"
>
<LabelEditor
:value="appellative_status"
@update="update"
></LabelEditor>
</div>
<LabelEditor
:value="appellativeStatusToEdit"
@update="update"
/>
</div>
</template>
<style scoped>
Expand Down
1 change: 1 addition & 0 deletions arches_lingo/templates/arches_urls.htm
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
api_schemes="{% url 'schemes-list-create' %}"
api_scheme_label='(resourceid)=>{return "{% url "api-scheme-label" "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa" %}".replace("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", resourceid)}'
api_scheme_label_tile='(resourceid, tileid)=>{return "{% url "api-scheme-label-tile" "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa" "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaab"%}".replace("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", resourceid).replace("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaab", tileid)}'
api_scheme_label_create="{% url 'api-scheme-label-create' %}"
api_scheme_note='(resourceid)=>{return "{% url "api-scheme-note" "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa" %}".replace("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", resourceid)}'
api_scheme_note_tile='(resourceid, tileid)=>{return "{% url "api-scheme-note-tile" "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa" "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaab" %}".replace("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", resourceid).replace("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaab", tileid)}'
api_scheme_creation='(resourceid)=>{return "{% url "api-scheme-creation" "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa" %}".replace("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", resourceid)}'
Expand Down
6 changes: 6 additions & 0 deletions arches_lingo/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
SchemeDetailView,
SchemeLabelTileView,
SchemeLabelView,
SchemeLabelCreateView,
SchemeListCreateView,
SchemeNamespaceView,
SchemeNoteTileView,
Expand Down Expand Up @@ -70,6 +71,11 @@
SchemeLabelTileView.as_view(),
name="api-scheme-label-tile",
),
path(
"api/scheme/label",
SchemeLabelCreateView.as_view(),
name="api-scheme-label-create",
),
path(
"api/scheme/<uuid:pk>/note",
SchemeNoteView.as_view(),
Expand Down
5 changes: 5 additions & 0 deletions arches_lingo/views/api/pythonic_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ class SchemeLabelTileView(ArchesModelAPIMixin, RetrieveUpdateDestroyAPIView):
serializer_class = SchemeLabelTileSerializer


class SchemeLabelCreateView(ArchesModelAPIMixin, ListCreateAPIView):
permission_classes = [RDMAdministrator]
serializer_class = SchemeLabelTileSerializer


class SchemeNoteView(ArchesModelAPIMixin, RetrieveUpdateDestroyAPIView):
permission_classes = [RDMAdministrator]
serializer_class = SchemeNoteSerializer
Expand Down

0 comments on commit 1983128

Please sign in to comment.