doUploadFileToComment: function()

in src/components/attachments-row/attachment-actions.ts [149:213]


    doUploadFileToComment: function (
      isArticle: boolean = false,
      files: NormalizedAttachment[],
      entity: IssueFull | Article | IssueCreate,
      comment: IssueComment,
    ) {
      return async (
        dispatch: (arg0: any) => any,
        getState: StateGetter,
        getApi: ApiGetter,
      ) => {
        const api: Api = getApi();
        const attachApi: ArticlesAPI | IssueAPI = isArticle ? api.articles : api.issue;
        const isCommentDraft: boolean = hasType.commentDraft(comment);
        const [error, addedAttachments]: [CustomError | null, Attachment[]] = await until(
          files.map((file: NormalizedAttachment) => attachApi.attachFileToComment(
            entity.id,
            file,
            isCommentDraft ? undefined : comment.id
          )),
          true,
        ) as [CustomError | null, Attachment[]];

        if (error) {
          notifyError(error);
          return [];
        } else {
          log.info(`Attachment Actions: File attached to the ${isArticle ? 'Article' : 'Issue'} Comment`);
          usage.trackEvent(
            isArticle ? ANALYTICS_ARTICLE_PAGE_STREAM : ANALYTICS_ISSUE_STREAM_SECTION,
            'Attach image success',
          );
          dispatch(actions.stopImageAttaching());
          dispatch(actions.toggleAttachFileDialog(false));
          const visibility: Visibility | undefined = (
            files[0].visibility || (hasType.visibilityLimited(comment.visibility) ? comment.visibility : null)
          );
          if (visibility) {
            const [err, attachmentsWithVisibility]: [CustomError | null, Attachment[]] = await until(
              addedAttachments.map(
                (attach: Attachment) => {
                  return attachApi.updateCommentAttachmentVisibility(
                    entity.id, attach, visibility, isCommentDraft
                  );
                }
              ),
            ) as [CustomError | null, Attachment[]];
            if (!err) {
              const visibilityMap: Record<string, Visibility> = attachmentsWithVisibility.reduce(
                (akk, attach: Attachment) => ({
                  ...akk,
                  [attach.id]: attach.visibility,
                }), {});
              return addedAttachments.map((attach: Attachment) => {
                return Object.assign(
                  attach,
                  visibilityMap[attach.id] ? {visibility: visibilityMap[attach.id]} : {}
                );
              });
            }
          }
          return addedAttachments;
        }
      };
    },