Rewrite Story Validation in Typescript | Steemit Condenser Official

in Steem Dev11 months ago

Hello Programmers,

I hope you all are well. I am very well sure that many new developers are working on Steem and they want to build some useful tools for Steem Blockchain. Our team is working on SteemPro Mobile App for the last 6 months, During this time frame we also launch SteemPro

storyValidation.png

To educate the new developers and also to the whole steemit users, We arrange this bi-weekly post in which we explore the code of official Steemit. You can get the GitHub repository here.

We will also modify the code to convert it into TypeScript and make the necessary changes to work with the SDS APIs provided by @steemchiller.

File Path

steemit/condenser/src/app/components/elements/ReplyEditor.jsx

Class

ReplyEditor

Old Code

validation: values => {
                let bodyValidation = null;
                if (!values.body) {
                    bodyValidation = tt('g.required');
                }
                if (
                    values.body &&
                    new Blob([values.body]).size >= maxKb * 1024 - 256
                ) {
                    bodyValidation = `Post body exceeds ${maxKb * 1024 -
                        256} bytes.`;
                }
                return {
                    title:
                        isStory &&
                        (!values.title || values.title.trim() === ''
                            ? tt('g.required')
                            : values.title.length > 255
                              ? tt('reply_editor.shorten_title')
                              : null),
                    tags: isStory && validateTagInput(values.tags, !isEdit),
                    body: bodyValidation,
                };
            },

Working

This function is used to check the length of a post/comment body in the Steem blockchain. There is a specific length for the Post body and the Comment body. Let's rewrite and make it simple to use in our SteemPro app development.

Code in Typescript

export const validateCommentBody = (
  body: string,
  isStory: boolean,
): true | string => {
  const maxKb = isStory ? 64 : 16;
  const maxLength = maxKb * 1024;
  console.log(body.length);
  if (new Blob([body]).size >= maxKb * 1024 - 256) {
    return `Exceeds maximum length (${maxKb}KB)`;
  }

  return true;
};

What's new

We create a new function validateCommentBody to validate the both Post and Comment body length. We set two params body with the typed string and isStory with type boolean. If is story is true then we consider the body of the post or else the body of a comment. here is the simple usage in the SteemPro Mobile app.

image.png

SteemPro Official

@steempro.com
https://www.steempro.com

▀▄▀▄▀▄ T̳̿͟͞h̳̿͟͞a̳̿͟͞n̳̿͟͞k̳̿͟͞s̳̿͟͞ ̳̿͟͞f̳̿͟͞o̳̿͟͞r̳̿͟͞ ̳̿͟͞R̳̿͟͞e̳̿͟͞a̳̿͟͞d̳̿͟͞i̳̿͟͞n̳̿͟͞g̳̿͟͞ ▄▀▄▀▄▀

Cc: @blacks
Cc: @rme
Cc: @hungry-griffin
Cc: @steemchiller
Cc: @steemcurator01


Best Regards @faisalamin

Sort:  

This post has been featured in the latest edition of Steem News...

Coin Marketplace

STEEM 0.28
TRX 0.11
JST 0.034
BTC 66038.71
ETH 3178.89
USDT 1.00
SBD 4.05