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