@baqhub/sdk-react
Version:
The official React SDK for the BAQ federated app platform.
124 lines (123 loc) • 4.41 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.recordByVersion = recordByVersion;
exports.recordByKey = recordByKey;
exports.findRecordByKey = findRecordByKey;
exports.findRecordByQuery = findRecordByQuery;
exports.findEntityRecord = findEntityRecord;
exports.findStandingRecord = findStandingRecord;
exports.findStandingDecision = findStandingDecision;
exports.updateStandingDecision = updateStandingDecision;
const sdk_1 = require("@baqhub/sdk");
function pickRecord(local, root) {
if (!local) {
return root;
}
if (!root) {
return local;
}
if (local.createdAt > root.createdAt) {
return local;
}
return root;
}
function recordByVersion(versions) {
return (version) => {
return versions[version];
};
}
function recordByKey(entity, proxyEntity) {
return (getState) => (key) => {
const state = getState();
const record = (() => {
const localRecord = state[proxyEntity]?.dictionary[key];
if (proxyEntity === entity) {
return localRecord;
}
return pickRecord(localRecord, state[entity]?.dictionary[key]);
})();
if (!record || "noContent" in record) {
throw new Error("This record does not exist: " + key);
}
return record;
};
}
function findRecordByKey(entity, proxyEntity) {
return (getState) => (key) => {
const record = (() => {
const state = getState();
const localRecord = state[proxyEntity]?.dictionary[key];
if (proxyEntity === entity) {
return localRecord;
}
return pickRecord(localRecord, state[entity]?.dictionary[key]);
})();
if ("noContent" in record) {
throw new Error("This record does not exist: " + key);
}
return record;
};
}
function findRecordByQuery(entity, proxyEntity) {
return (getState) => (query) => {
const state = getState();
const findRecord = (findEntity) => {
const records = sdk_1.Query.filter({ ...query, pageSize: 1 }, state[findEntity]?.list || []);
if (records.length > 1) {
throw new Error("Multiple records found.");
}
return records[0];
};
if (proxyEntity === entity) {
return findRecord(proxyEntity);
}
return pickRecord(findRecord(proxyEntity), findRecord(entity));
};
}
function findEntityRecord(entity, proxyEntity) {
const findRecord = findRecordByQuery(entity, proxyEntity);
return (getState) => (targetEntity) => {
return findRecord(getState)({
sources: [
sdk_1.RecordSource.SELF,
sdk_1.RecordSource.NOTIFICATION,
sdk_1.RecordSource.SUBSCRIPTION,
sdk_1.RecordSource.RESOLUTION,
],
filter: sdk_1.Q.and(sdk_1.Q.author(targetEntity), sdk_1.Q.type(sdk_1.EntityRecord)),
});
};
}
function findStandingRecord(entity) {
const findRecord = findRecordByQuery(entity, entity);
return (getState) => (publisherEntity) => {
return findRecord(getState)({
sources: [sdk_1.RecordSource.SELF],
filter: sdk_1.Q.and(sdk_1.Q.author(entity), sdk_1.Q.type(sdk_1.StandingRecord), sdk_1.Q.entity("content.publisher", publisherEntity)),
});
};
}
function findStandingDecision(entity) {
const findRecord = findStandingRecord(entity);
return (getState) => (publisherEntity) => {
const standingRecord = findRecord(getState)(publisherEntity);
if (!standingRecord) {
return sdk_1.StandingDecision.UNDECIDED;
}
return standingRecord.content.decision;
};
}
function updateStandingDecision(entity) {
const findRecord = findStandingRecord(entity);
return (getState, updateRecord) => (publisherEntity, decision) => {
const standingRecord = findRecord(getState)(publisherEntity);
const newContent = {
publisher: sdk_1.EntityLink.new(publisherEntity),
decision,
};
const newStandingRecord = standingRecord
? sdk_1.StandingRecord.update(entity, standingRecord, newContent)
: sdk_1.StandingRecord.new(entity, newContent);
updateRecord([newStandingRecord]);
};
}