Adds new campaign form. Adds fronts and thread types
This commit is contained in:
@@ -19,4 +19,8 @@ export type DocumentAction =
|
||||
doc: AnyDocument;
|
||||
relationships: Relationship[];
|
||||
relatedDocuments: AnyDocument[];
|
||||
}
|
||||
| {
|
||||
type: "removeDocument";
|
||||
docId: DocumentId;
|
||||
};
|
||||
|
||||
@@ -1,6 +1,13 @@
|
||||
import _ from "lodash";
|
||||
import type { AnyDocument, DocumentId, Relationship } from "@/lib/types";
|
||||
import type { DocumentAction } from "./actions";
|
||||
import { ready, loading, unloaded, type DocumentState } from "./state";
|
||||
import {
|
||||
ready,
|
||||
loading,
|
||||
unloaded,
|
||||
type DocumentState,
|
||||
mapResult,
|
||||
} from "./state";
|
||||
import { relationshipsForDocument } from "@/lib/relationships";
|
||||
|
||||
function setLoadingDocument(
|
||||
@@ -65,6 +72,36 @@ function setRelationship(
|
||||
};
|
||||
}
|
||||
|
||||
function removeDocument(
|
||||
docId: DocumentId,
|
||||
state: DocumentState,
|
||||
): DocumentState {
|
||||
const remainingDocs: DocumentState["documents"] = _.omit(state.documents, [
|
||||
docId,
|
||||
]);
|
||||
return {
|
||||
...state,
|
||||
documents: _.mapValues(remainingDocs, (result) => {
|
||||
if (result.type !== "ready") {
|
||||
return result;
|
||||
}
|
||||
return ready({
|
||||
doc: result.value.doc,
|
||||
relationships: _.mapValues(
|
||||
result.value.relationships,
|
||||
(relationshipResult) =>
|
||||
mapResult(relationshipResult, (relationship) => ({
|
||||
...relationship,
|
||||
secondary: relationship.secondary.filter(
|
||||
(relatedId) => relatedId !== docId,
|
||||
),
|
||||
})),
|
||||
),
|
||||
});
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
export function reducer(
|
||||
state: DocumentState,
|
||||
action: DocumentAction,
|
||||
@@ -84,5 +121,7 @@ export function reducer(
|
||||
setDocument(state, action.doc),
|
||||
),
|
||||
);
|
||||
case "removeDocument":
|
||||
return removeDocument(action.docId, state);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,16 @@ export const error = (err: unknown): Result<any> => ({ type: "error", err });
|
||||
export const loading = (): Result<any> => ({ type: "loading" });
|
||||
export const ready = <V>(value: V): Result<V> => ({ type: "ready", value });
|
||||
|
||||
export const mapResult = <A, B>(
|
||||
result: Result<A>,
|
||||
f: (a: A) => B,
|
||||
): Result<B> => {
|
||||
if (result.type === "ready") {
|
||||
return ready(f(result.value));
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
export type DocumentState = {
|
||||
documents: Record<
|
||||
DocumentId,
|
||||
|
||||
Reference in New Issue
Block a user