commit c1161e820138f20cea48fe1850d65cec9375e826
Author: Fantič <58391275+FantaFantic@users.noreply.github.com>
Date:   Wed Oct 18 18:33:41 2023 +0200

    re #10819 ItemView prevent error states

diff --git a/src/components/item/ItemTabBar.tsx b/src/components/item/ItemTabBar.tsx
index c90c63f..a8f7c18 100644
--- a/src/components/item/ItemTabBar.tsx
+++ b/src/components/item/ItemTabBar.tsx
@@ -1,6 +1,6 @@
 import { HStack, CircleIcon, View, ScrollView, Button, Center, Heading, Pressable, Text, Box, Container, VStack, Icon, ArrowBackIcon, ChevronLeftIcon, ChevronRightIcon } from "native-base";
 import { CertaintyWithColors } from "../../stores/reducers/itemSlice";
-import { Concordance } from "../../types/item";
+import { Certainty, Concordance } from "../../types/item";
 import LoadingBox from "../loading/LoadingBox";
 
 // custom render Tab
@@ -46,7 +46,7 @@ const ItemTabBar = (props: { navigationState: { routes: any[] }, concordances: C
                                                 <CircleIcon
                                                     marginLeft="5"
                                                     size="2"
-                                                    color={CertaintyWithColors[concordances[i].cert].color}
+                                                    color={CertaintyWithColors[concordances[i].cert ?? Certainty.Unknown].color}
                                                 />
                                                 <Text marginLeft="5%">
                                                     {i == index ? <Text bold>{concordances[i].id}</Text> : concordances[i].id}
@@ -64,7 +64,7 @@ const ItemTabBar = (props: { navigationState: { routes: any[] }, concordances: C
                                                 <CircleIcon
                                                     marginLeft="42%"
                                                     size="2"
-                                                    color={CertaintyWithColors[concordances[0].cert].color}
+                                                    color={CertaintyWithColors[concordances[0].cert ?? Certainty.Unknown].color}
                                                 />
                                                 <Text marginLeft="2%">
                                                     <Text bold>{concordances[0].id}</Text>
diff --git a/src/components/item/ItemView.tsx b/src/components/item/ItemView.tsx
index c94d7e9..ae2b220 100644
--- a/src/components/item/ItemView.tsx
+++ b/src/components/item/ItemView.tsx
@@ -47,7 +47,7 @@ const ItemDetail = (props: { item: Item, itemLoading: boolean }) => {
                                 </VStack>
                                 <Box marginLeft="auto" marginTop={0}> {/* marginLeft: "auto" pushes the Box to the right */}
                                     <Button variant="outline" size="md">
-                                        {item.concordances[0].id}
+                                        {item?.concordances?.[0]?.id}
                                     </Button>
                                 </Box>
                             </HStack>
@@ -59,8 +59,8 @@ const ItemDetail = (props: { item: Item, itemLoading: boolean }) => {
                                             {item.inventoryItem}
                                         </Text>
                                         <Text italic marginTop="2.5%">
-                                            {item.searchSubjects.map((subject, index) => (
-                                                index < item.searchSubjects.length - 1 ? (subject + ", ") : (subject)
+                                            {item.searchSubjects != undefined && item?.searchSubjects?.map((subject, index) => (
+                                                item.searchSubjects && index < item.searchSubjects.length - 1 ? (subject + ", ") : (subject)
                                             ))}
                                         </Text>
                                     </VStack>}
diff --git a/src/pages/ItemViewPage.tsx b/src/pages/ItemViewPage.tsx
index 31c6611..ebf47ad 100644
--- a/src/pages/ItemViewPage.tsx
+++ b/src/pages/ItemViewPage.tsx
@@ -19,7 +19,7 @@ const ItemViewPage = ({route, navigation}: DrawerScreenProps<RootDrawerParamList
 
   const layout = useWindowDimensions();
 
-  const [routes, setRoutes] = React.useState([
+  const [routes, setRoutes] = React.useState<{key: string, title: string}[]>([
     // initial tab
     { key: 'loading', title: 'Loading item...' },
   ]);
@@ -54,8 +54,7 @@ const ItemViewPage = ({route, navigation}: DrawerScreenProps<RootDrawerParamList
   useEffect(() => {
     dispatch(getItem(route.params.itemId));
     log.debug("ItemViewPage", "useEffect", "getItem", route.params.itemId);
-  }, [])
-
+  }, [route.params.itemId])
 
   // concordances are loaded
   useEffect(() => {
@@ -69,8 +68,8 @@ const ItemViewPage = ({route, navigation}: DrawerScreenProps<RootDrawerParamList
       for (let i = 0; i < concordances.length; i++) {
         let concordance = concordances[i];
         concordanceRoutes.push({
-          key: concordance.id,
-          title: concordance.id
+          key: concordance.id ?? "",
+          title: concordance.id ?? ""
         })
         // _renderSceneDict[concordance.id] = <LoadingBox text={`Loading item ${concordance.id}...`}></LoadingBox>;
       }
@@ -86,7 +85,7 @@ const ItemViewPage = ({route, navigation}: DrawerScreenProps<RootDrawerParamList
 
     if (!itemLoading && item && item.id) {
       if (index == 0) {
-        dispatch(setConcordances(item.concordances));
+        dispatch(setConcordances(item.concordances ?? []));
       }
       // set item notes if item is loaded
       if (item && item.id) {
@@ -100,7 +99,7 @@ const ItemViewPage = ({route, navigation}: DrawerScreenProps<RootDrawerParamList
       console.log("index changed");
       setIndex(newIndex);
       if (concordances && concordances.length > 0) {
-        dispatch(getItem(concordances[newIndex].id));
+        dispatch(getItem(concordances[newIndex].id ?? ""));
       }
     }
   }
diff --git a/src/stores/actions/itemThunks.ts b/src/stores/actions/itemThunks.ts
index acfde50..7a41e57 100644
--- a/src/stores/actions/itemThunks.ts
+++ b/src/stores/actions/itemThunks.ts
@@ -2,7 +2,6 @@ import { createAsyncThunk } from "@reduxjs/toolkit"
 import { getItemRequest } from "../../api/itemservice"
 import { getItemNotesRequest } from "../../api/notesservice"
 import { Certainty, Concordance, Item } from "../../types/item";
-import { Note } from "../../types/note";
 
 
 export const getItem = createAsyncThunk(
@@ -16,33 +15,33 @@ export const getItem = createAsyncThunk(
             // data with image
             if (response.status === 200 && response.data.object.length > 1) {
                 // TODO IMAGES to array :-)
-                const authorName = response.data.object[1]?.name?.[0]?.getty_data?.display_name || null;
+                const authorName = response.data?.object[1]?.name?.[0]?.getty_data?.display_name ?? undefined;
                 const images : {imageUrl: string, title: string}[] = []
-                for(let i = 0; i < response.data.object[1]?.images.length; i++){
-                    images.push({imageUrl: response.data.object[1]?.images?.[i]?.file, title:  response.data.object[1]?.images?.[i]?.text})
+                for(let i = 1; i < response.data.object.length; i++){
+                    images.push({imageUrl: response.data.object[i]?.images?.[0]?.file, title:  response.data.object[i]?.images?.[0]?.text})
                 }
                 
                 const institution = {
-                    name: response.data.object[1]?.institution || null,
-                    inventoryNumber: response.data.object[1]?.id_number || null,
-                    country: response.data.object[1]?.country || null,
-                    city: response.data.object[1]?.city || null
+                    name: response.data?.object[1]?.institution ?? undefined,
+                    inventoryNumber: response.data?.object[1]?.id_number ?? undefined,
+                    country: response.data?.object[1]?.country ?? undefined,
+                    city: response.data?.object[1]?.city ?? undefined
                 };
-                const repository = response.data.object[1]?.repository || null;
-                const provenance = response.data.object[1]?.provenance || null;
-                const description = response.data.object[1]?.physical_description || null;
+                const repository = response.data?.object[1]?.repository ?? undefined;
+                const provenance = response.data?.object[1]?.provenance ?? undefined;
+                const description = response.data?.object[1]?.physical_description ?? undefined;
 
                 return {
                     id: itemId,
                     fullView: true,
                     concordances: response.data.concordances ?
-                        [{ id: itemId, cert: response.data.object[1]?.cert || Certainty.Unknown }].concat(response.data.concordances)
+                        [{ id: itemId, cert: response.data?.object[1]?.cert ?? Certainty.Unknown }].concat(response.data.concordances)
                         :
-                        [{ id: itemId, cert: response.data.object[1]?.cert || Certainty.Unknown }],
-                    authorDisplayName: response.data.object[0]?.name?.[0]?.getty_data?.display_name || null,
-                    workName: response.data.object[0]?.caption || null,
-                    inventoryItem: response.data.text || null,
-                    searchSubjects: response.data.search_subject || null,
+                        [{ id: itemId, cert: response.data?.object[1]?.cert ?? Certainty.Unknown }],
+                    authorDisplayName: response.data.object[0]?.name?.[0]?.getty_data?.display_name ?? undefined,
+                    workName: response.data.object[0]?.caption ?? undefined,
+                    inventoryItem: response.data.text ?? undefined,
+                    searchSubjects: response.data.search_subject ?? undefined,
                     authorName,
                     images,
                     institution,
@@ -57,13 +56,13 @@ export const getItem = createAsyncThunk(
                     id: itemId,
                     fullView: false,
                     concordances: response.data.concordances ?
-                        [{ id: itemId, cert: response.data.object[1]?.cert || Certainty.Unknown }].concat(response.data.concordances)
+                        [{ id: itemId, cert: response.data?.object[1]?.cert ?? Certainty.Unknown }].concat(response.data.concordances)
                         :
-                        [{ id: itemId, cert: response.data.object[1]?.cert || Certainty.Unknown }],
-                    authorDisplayName: response.data.object[0]?.name?.[0]?.getty_data?.display_name || null,
-                    workName: response.data.object[0]?.caption || null,
-                    inventoryItem: response.data.text || null,
-                    searchSubjects: response.data.search_subject || null,
+                        [{ id: itemId, cert: response.data?.object[1]?.cert ?? Certainty.Unknown }],
+                    authorDisplayName: response.data.object[0]?.name?.[0]?.getty_data?.display_name ?? undefined,
+                    workName: response.data.object[0]?.caption ?? undefined,
+                    inventoryItem: response.data.text ?? undefined,
+                    searchSubjects: response.data.search_subject ?? undefined,
                 }
             }
             else {
@@ -115,7 +114,7 @@ export const getItemNotes = createAsyncThunk(
             const response = await getItemNotesRequest(item, relatedComments);
 
             if (response.status === 200) {
-                if (response.data.length > 0) {
+                if (response.data && response.data.length > 0) {
 
                     let notes = [];
                     for (let i = 0; i < response.data.length; i++) {
diff --git a/src/types/item.ts b/src/types/item.ts
index 24221e5..98f0a62 100644
--- a/src/types/item.ts
+++ b/src/types/item.ts
@@ -2,14 +2,14 @@ import { Note } from "./note"
 
 
 export type  Item = {
-    id: string
+    id?: string
 
-    concordances: Concordance[]
+    concordances?: Concordance[]
 
-    authorDisplayName: string
-    workName: string
-    inventoryItem: string
-    searchSubjects: string[]
+    authorDisplayName?: string
+    workName?: string
+    inventoryItem?: string
+    searchSubjects?: string[]
 
     fullView : boolean
 
@@ -37,8 +37,8 @@ export type ItemViewState = {
 }
 
 export type Concordance = {
-    id: string
-    cert: Certainty
+    id?: string
+    cert?: Certainty
 }
 
 export enum Certainty {
