forked from trent_larson/crowd-funder-for-time-pwa
refactor(claims): extract offer fulfillment logic to utility function
Created extractOfferFulfillment utility in libs/util.ts to handle both array and single object cases for fulfills field. Updated ClaimView and ConfirmGiftView to use the shared utility, eliminating code duplication and improving maintainability.
This commit is contained in:
@@ -159,6 +159,41 @@ export const isGiveAction = (
|
|||||||
return isGiveClaimType(veriClaim.claimType);
|
return isGiveClaimType(veriClaim.claimType);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export interface OfferFulfillment {
|
||||||
|
offerHandleId: string;
|
||||||
|
offerType: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extract offer fulfillment information from the fulfills field
|
||||||
|
* Handles both array and single object cases
|
||||||
|
*/
|
||||||
|
export const extractOfferFulfillment = (fulfills: any): OfferFulfillment | null => {
|
||||||
|
if (!fulfills) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle both array and single object cases
|
||||||
|
let offerFulfill = null;
|
||||||
|
|
||||||
|
if (Array.isArray(fulfills)) {
|
||||||
|
// Find the Offer in the fulfills array
|
||||||
|
offerFulfill = fulfills.find((item) => item["@type"] === "Offer");
|
||||||
|
} else if (fulfills["@type"] === "Offer") {
|
||||||
|
// fulfills is a single Offer object
|
||||||
|
offerFulfill = fulfills;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (offerFulfill) {
|
||||||
|
return {
|
||||||
|
offerHandleId: offerFulfill.identifier,
|
||||||
|
offerType: offerFulfill["@type"],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
|
||||||
export const shortDid = (did: string) => {
|
export const shortDid = (did: string) => {
|
||||||
if (did.startsWith("did:peer:")) {
|
if (did.startsWith("did:peer:")) {
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -733,32 +733,9 @@ export default class ClaimView extends Vue {
|
|||||||
* Extract offer fulfillment information from the fulfills array
|
* Extract offer fulfillment information from the fulfills array
|
||||||
*/
|
*/
|
||||||
extractOfferFulfillment() {
|
extractOfferFulfillment() {
|
||||||
if (!this.detailsForGive?.fullClaim?.fulfills) {
|
this.detailsForGiveOfferFulfillment = libsUtil.extractOfferFulfillment(
|
||||||
this.detailsForGiveOfferFulfillment = null;
|
this.detailsForGive?.fullClaim?.fulfills
|
||||||
return;
|
);
|
||||||
}
|
|
||||||
|
|
||||||
const fulfills = this.detailsForGive.fullClaim.fulfills;
|
|
||||||
|
|
||||||
// Handle both array and single object cases
|
|
||||||
let offerFulfill = null;
|
|
||||||
|
|
||||||
if (Array.isArray(fulfills)) {
|
|
||||||
// Find the Offer in the fulfills array
|
|
||||||
offerFulfill = fulfills.find((item) => item["@type"] === "Offer");
|
|
||||||
} else if (fulfills["@type"] === "Offer") {
|
|
||||||
// fulfills is a single Offer object
|
|
||||||
offerFulfill = fulfills;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (offerFulfill) {
|
|
||||||
this.detailsForGiveOfferFulfillment = {
|
|
||||||
offerHandleId: offerFulfill.identifier,
|
|
||||||
offerType: offerFulfill["@type"],
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
this.detailsForGiveOfferFulfillment = null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// =================================================
|
// =================================================
|
||||||
|
|||||||
@@ -718,32 +718,9 @@ export default class ConfirmGiftView extends Vue {
|
|||||||
* Extract offer fulfillment information from the fulfills array
|
* Extract offer fulfillment information from the fulfills array
|
||||||
*/
|
*/
|
||||||
private extractOfferFulfillment() {
|
private extractOfferFulfillment() {
|
||||||
if (!this.giveDetails?.fullClaim?.fulfills) {
|
this.giveDetailsOfferFulfillment = libsUtil.extractOfferFulfillment(
|
||||||
this.giveDetailsOfferFulfillment = null;
|
this.giveDetails?.fullClaim?.fulfills
|
||||||
return;
|
);
|
||||||
}
|
|
||||||
|
|
||||||
const fulfills = this.giveDetails.fullClaim.fulfills;
|
|
||||||
|
|
||||||
// Handle both array and single object cases
|
|
||||||
let offerFulfill = null;
|
|
||||||
|
|
||||||
if (Array.isArray(fulfills)) {
|
|
||||||
// Find the Offer in the fulfills array
|
|
||||||
offerFulfill = fulfills.find((item) => item["@type"] === "Offer");
|
|
||||||
} else if (fulfills["@type"] === "Offer") {
|
|
||||||
// fulfills is a single Offer object
|
|
||||||
offerFulfill = fulfills;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (offerFulfill) {
|
|
||||||
this.giveDetailsOfferFulfillment = {
|
|
||||||
offerHandleId: offerFulfill.identifier,
|
|
||||||
offerType: offerFulfill["@type"],
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
this.giveDetailsOfferFulfillment = null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user