Support not-present relationships
This commit is contained in:
@@ -30,6 +30,9 @@ export function DocumentView({
|
|||||||
if (v.type === "ready") {
|
if (v.type === "ready") {
|
||||||
return v.value.secondary.length.toString();
|
return v.value.secondary.length.toString();
|
||||||
}
|
}
|
||||||
|
if (v.type === "empty") {
|
||||||
|
return "0";
|
||||||
|
}
|
||||||
return "...";
|
return "...";
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -1,14 +1,15 @@
|
|||||||
import _ from "lodash";
|
import { relationshipsForDocument } from "@/lib/relationships";
|
||||||
import type { AnyDocument, DocumentId, Relationship } from "@/lib/types";
|
import type { AnyDocument, DocumentId, Relationship } from "@/lib/types";
|
||||||
|
import _ from "lodash";
|
||||||
import type { DocumentAction } from "./actions";
|
import type { DocumentAction } from "./actions";
|
||||||
import {
|
import {
|
||||||
ready,
|
empty,
|
||||||
loading,
|
loading,
|
||||||
|
mapResult,
|
||||||
|
ready,
|
||||||
unloaded,
|
unloaded,
|
||||||
type DocumentState,
|
type DocumentState,
|
||||||
mapResult,
|
|
||||||
} from "./state";
|
} from "./state";
|
||||||
import { relationshipsForDocument } from "@/lib/relationships";
|
|
||||||
|
|
||||||
function setLoadingDocument(
|
function setLoadingDocument(
|
||||||
docId: DocumentId,
|
docId: DocumentId,
|
||||||
@@ -47,6 +48,36 @@ function setDocument(state: DocumentState, doc: AnyDocument): DocumentState {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function setAllRelationshipsEmpty(
|
||||||
|
docId: DocumentId,
|
||||||
|
state: DocumentState,
|
||||||
|
): DocumentState {
|
||||||
|
const prevDocResult = state.documents[docId];
|
||||||
|
if (prevDocResult?.type !== "ready") {
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
const prevDoc = prevDocResult.value.doc;
|
||||||
|
const relationships = prevDocResult.value.relationships;
|
||||||
|
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
documents: {
|
||||||
|
...state.documents,
|
||||||
|
[docId]: ready({
|
||||||
|
...prevDocResult.value,
|
||||||
|
relationships: Object.fromEntries(
|
||||||
|
relationshipsForDocument(prevDoc).map((relType) =>
|
||||||
|
relationships[relType]?.type === "ready"
|
||||||
|
? [relType, relationships[relType]]
|
||||||
|
: [relType, empty()],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
function setRelationship(
|
function setRelationship(
|
||||||
docId: DocumentId,
|
docId: DocumentId,
|
||||||
state: DocumentState,
|
state: DocumentState,
|
||||||
@@ -118,8 +149,11 @@ export function reducer(
|
|||||||
setDocument,
|
setDocument,
|
||||||
action.relationships.reduce(
|
action.relationships.reduce(
|
||||||
setRelationship.bind(null, action.doc.id),
|
setRelationship.bind(null, action.doc.id),
|
||||||
|
setAllRelationshipsEmpty(
|
||||||
|
action.doc.id,
|
||||||
setDocument(state, action.doc),
|
setDocument(state, action.doc),
|
||||||
),
|
),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
case "removeDocument":
|
case "removeDocument":
|
||||||
return removeDocument(action.docId, state);
|
return removeDocument(action.docId, state);
|
||||||
|
|||||||
@@ -10,11 +10,13 @@ export type Result<V> =
|
|||||||
| { type: "unloaded" }
|
| { type: "unloaded" }
|
||||||
| { type: "error"; err: unknown }
|
| { type: "error"; err: unknown }
|
||||||
| { type: "loading" }
|
| { type: "loading" }
|
||||||
|
| { type: "empty" }
|
||||||
| { type: "ready"; value: V };
|
| { type: "ready"; value: V };
|
||||||
|
|
||||||
export const unloaded = (): Result<any> => ({ type: "unloaded" });
|
export const unloaded = (): Result<any> => ({ type: "unloaded" });
|
||||||
export const error = (err: unknown): Result<any> => ({ type: "error", err });
|
export const error = (err: unknown): Result<any> => ({ type: "error", err });
|
||||||
export const loading = (): Result<any> => ({ type: "loading" });
|
export const loading = (): Result<any> => ({ type: "loading" });
|
||||||
|
export const empty = (): Result<any> => ({ type: "empty" });
|
||||||
export const ready = <V>(value: V): Result<V> => ({ type: "ready", value });
|
export const ready = <V>(value: V): Result<V> => ({ type: "ready", value });
|
||||||
|
|
||||||
export const mapResult = <A, B>(
|
export const mapResult = <A, B>(
|
||||||
|
|||||||
Reference in New Issue
Block a user