如何使用 NGXS 在动作中设置布尔值 属性

How to set a boolen property value inside of an action with NGXS

我是 NGXS 的新手,需要在操作中设置一个布尔值,但我不确定如何去做。

这是我的界面:

export interface CertificateObject {
  certificateName: string;
  issueDate: Date;
  expiryDate: Date;
  thumbPrint: string;
  serialNumber: string;
  daysToExpire: number;
  aboutToExpire: boolean;
}

这是我的状态模型:

export interface CertificateStateModel {
  certificates: CertificateObject[];
  filteredCertificates: CertificateObject[];
  searchLimitedStateMessage: string;
  emptyCertificateMessage: string;
  searchTerm: string;
}

这是动作:

@Action(CertificateAction.GetAllCertificates)
  getAllCertificates(
    ctx: StateContext<CertificateStateModel>,
    action: CertificateAction.GetAllCertificates
  ) {

    ctx.dispatch(new Busy(true));
    return this.svc.getIACertificates().pipe(
      tap((certsResponse) => {
        ctx.patchState({
          certificates: certsResponse.certificates.map(cert => ({
            ...cert,
          })),
          filteredCertificates: certsResponse.certificates.map(cert => ({
            ...cert,
          })),
        });
        ctx.dispatch(new Busy(false));
      },
      (error: any) => {
        ctx.dispatch([
          new DisplayMessage({type: "error", list: [ "Get All Certificates Error", error.error ]}),
          new Busy(false)
        ]);
      })
    );
  }

我需要评估 daysToExpire 属性 并在 < 61 时将 aboutToExpire 设置为 True。我目前正在模板上执行此操作,需要将逻辑移至操作中,但我迷路了。任何帮助,将不胜感激。谢谢

您可以直接在证书 map 中添加它,如下所示:

ctx.patchState({
  certificates: certsResponse.certificates.map((cert) => ({
    ...cert,
    aboutToExpire: cert.daysToExpire < 61 // <<<< assing it here
  })),
  filteredCertificates: certsResponse.certificates.map((cert) => ({
    ...cert,
    aboutToExpire: cert.daysToExpire < 61 // <<<< assing it here
  })),
});