sportsRunning and sportsWalking











up vote
0
down vote

favorite












There are two functions, sportsRunning and sportsWalking, that will render different text. For this, I am using an if condition at the bottom to display corresponding div:



{sportsDescription.sportsDate && sportsDescription.sportsDate.length > 0 && sportsDescription.sportsDate !== '2199-01-01' ?
this.sportsWalking() : this.sportsRunning()}`


Can you tell me how I can refactor the code?



sportsRunning() {
const sportsFire = this.props.profile.firstName;
return (
<div className="sports-table action-shadow">
<h4>{`Want some advice${sportsFire && sportsFire.length > 0 ? `, ${sportsFire}` : ''}?`}</h4>
<p>Stay focused by setting a target date for completion of your sports.</p>
<div className="secondary-links section_content">
<a href="javascript:;" onClick={this.editHandler}>Set Target Date</a>
</div>
</div>
);
}

sportsWalking() {
const sportsFire = this.props.profile.firstName;
return (
<div className="sports-table action-shadow">
<h4>{`Want a helpful tip${sportsFire && sportsFire.length > 0 ? `, ${sportsFire}` : ''}?`}</h4>
<p>Add some momentum to your account by setting up automatic recurring deposits.</p>
<div className="secondary-links section_content">
<a href="javascript:;" onClick={this.editHandler}>Set Up Automatic Deposits</a>
</div>
</div>
);
}

render() {
const {sportsDescription, data} = this.props;
const sportsAmount = parseFloat(sportsDescription.sportsAmount);
const accumulatedAmount = data.summary.total || 0;

return (
<div>
<div className="content-box action-shadow">
{
sportsDescription.error ?
this.renderProgressError() :
this.renderProgress({
accumulatedAmount,
sportsAmount
})
}
<div className="section_content">
<h2>Your Goal Progress</h2>
<p>You have accumulated <strong className={accumulatedAmount >= 0 ? null : 'negative'}>
{moneyFormat(accumulatedAmount)}</strong> towards your sports of <strong>
{moneyFormat(sportsAmount)}</strong>.
</p>
{
sportsDescription.sportsDate && sportsDescription.sportsDate.length > 0 &&
sportsDescription.sportsDate !== '2199-01-01' && this.renderGoalDate(sportsDescription.sportsDate)
}
<div className="secondary-links section_content">
<a href="javascript:;" onClick={this.editHandler}>Edit Goal Details</a>
</div>
</div>
</div>
{sportsDescription.sportsDate && sportsDescription.sportsDate.length > 0 && sportsDescription.sportsDate !== '2199-01-01' ?
this.sportsWalking() : this.sportsRunning()}
</div>
);









share|improve this question
























  • Welcome to CR! Looks like the formatting is a bit off. Take a look at the editing help page for all the information you need to fix the formatting of your post.
    – Mathieu Guindon
    Aug 10 '16 at 21:30












  • Can you tell us, what you think could be refactored? Because both functions return different texts, but with the same layout.. If the layout will not change you could create pureRenderComponent ala (props) => (<div></div>) But if the structure will change at any point, then its fine to keep it separately, then you will have less trouble to customize it
    – webdeb
    Aug 10 '16 at 22:12












  • @webdeb layout will not change...but your solution is confusing for me...can you updated in my code
    – user3854268
    Aug 11 '16 at 13:57










  • @user3854268 check out my answer
    – webdeb
    Aug 11 '16 at 14:29















up vote
0
down vote

favorite












There are two functions, sportsRunning and sportsWalking, that will render different text. For this, I am using an if condition at the bottom to display corresponding div:



{sportsDescription.sportsDate && sportsDescription.sportsDate.length > 0 && sportsDescription.sportsDate !== '2199-01-01' ?
this.sportsWalking() : this.sportsRunning()}`


Can you tell me how I can refactor the code?



sportsRunning() {
const sportsFire = this.props.profile.firstName;
return (
<div className="sports-table action-shadow">
<h4>{`Want some advice${sportsFire && sportsFire.length > 0 ? `, ${sportsFire}` : ''}?`}</h4>
<p>Stay focused by setting a target date for completion of your sports.</p>
<div className="secondary-links section_content">
<a href="javascript:;" onClick={this.editHandler}>Set Target Date</a>
</div>
</div>
);
}

sportsWalking() {
const sportsFire = this.props.profile.firstName;
return (
<div className="sports-table action-shadow">
<h4>{`Want a helpful tip${sportsFire && sportsFire.length > 0 ? `, ${sportsFire}` : ''}?`}</h4>
<p>Add some momentum to your account by setting up automatic recurring deposits.</p>
<div className="secondary-links section_content">
<a href="javascript:;" onClick={this.editHandler}>Set Up Automatic Deposits</a>
</div>
</div>
);
}

render() {
const {sportsDescription, data} = this.props;
const sportsAmount = parseFloat(sportsDescription.sportsAmount);
const accumulatedAmount = data.summary.total || 0;

return (
<div>
<div className="content-box action-shadow">
{
sportsDescription.error ?
this.renderProgressError() :
this.renderProgress({
accumulatedAmount,
sportsAmount
})
}
<div className="section_content">
<h2>Your Goal Progress</h2>
<p>You have accumulated <strong className={accumulatedAmount >= 0 ? null : 'negative'}>
{moneyFormat(accumulatedAmount)}</strong> towards your sports of <strong>
{moneyFormat(sportsAmount)}</strong>.
</p>
{
sportsDescription.sportsDate && sportsDescription.sportsDate.length > 0 &&
sportsDescription.sportsDate !== '2199-01-01' && this.renderGoalDate(sportsDescription.sportsDate)
}
<div className="secondary-links section_content">
<a href="javascript:;" onClick={this.editHandler}>Edit Goal Details</a>
</div>
</div>
</div>
{sportsDescription.sportsDate && sportsDescription.sportsDate.length > 0 && sportsDescription.sportsDate !== '2199-01-01' ?
this.sportsWalking() : this.sportsRunning()}
</div>
);









share|improve this question
























  • Welcome to CR! Looks like the formatting is a bit off. Take a look at the editing help page for all the information you need to fix the formatting of your post.
    – Mathieu Guindon
    Aug 10 '16 at 21:30












  • Can you tell us, what you think could be refactored? Because both functions return different texts, but with the same layout.. If the layout will not change you could create pureRenderComponent ala (props) => (<div></div>) But if the structure will change at any point, then its fine to keep it separately, then you will have less trouble to customize it
    – webdeb
    Aug 10 '16 at 22:12












  • @webdeb layout will not change...but your solution is confusing for me...can you updated in my code
    – user3854268
    Aug 11 '16 at 13:57










  • @user3854268 check out my answer
    – webdeb
    Aug 11 '16 at 14:29













up vote
0
down vote

favorite









up vote
0
down vote

favorite











There are two functions, sportsRunning and sportsWalking, that will render different text. For this, I am using an if condition at the bottom to display corresponding div:



{sportsDescription.sportsDate && sportsDescription.sportsDate.length > 0 && sportsDescription.sportsDate !== '2199-01-01' ?
this.sportsWalking() : this.sportsRunning()}`


Can you tell me how I can refactor the code?



sportsRunning() {
const sportsFire = this.props.profile.firstName;
return (
<div className="sports-table action-shadow">
<h4>{`Want some advice${sportsFire && sportsFire.length > 0 ? `, ${sportsFire}` : ''}?`}</h4>
<p>Stay focused by setting a target date for completion of your sports.</p>
<div className="secondary-links section_content">
<a href="javascript:;" onClick={this.editHandler}>Set Target Date</a>
</div>
</div>
);
}

sportsWalking() {
const sportsFire = this.props.profile.firstName;
return (
<div className="sports-table action-shadow">
<h4>{`Want a helpful tip${sportsFire && sportsFire.length > 0 ? `, ${sportsFire}` : ''}?`}</h4>
<p>Add some momentum to your account by setting up automatic recurring deposits.</p>
<div className="secondary-links section_content">
<a href="javascript:;" onClick={this.editHandler}>Set Up Automatic Deposits</a>
</div>
</div>
);
}

render() {
const {sportsDescription, data} = this.props;
const sportsAmount = parseFloat(sportsDescription.sportsAmount);
const accumulatedAmount = data.summary.total || 0;

return (
<div>
<div className="content-box action-shadow">
{
sportsDescription.error ?
this.renderProgressError() :
this.renderProgress({
accumulatedAmount,
sportsAmount
})
}
<div className="section_content">
<h2>Your Goal Progress</h2>
<p>You have accumulated <strong className={accumulatedAmount >= 0 ? null : 'negative'}>
{moneyFormat(accumulatedAmount)}</strong> towards your sports of <strong>
{moneyFormat(sportsAmount)}</strong>.
</p>
{
sportsDescription.sportsDate && sportsDescription.sportsDate.length > 0 &&
sportsDescription.sportsDate !== '2199-01-01' && this.renderGoalDate(sportsDescription.sportsDate)
}
<div className="secondary-links section_content">
<a href="javascript:;" onClick={this.editHandler}>Edit Goal Details</a>
</div>
</div>
</div>
{sportsDescription.sportsDate && sportsDescription.sportsDate.length > 0 && sportsDescription.sportsDate !== '2199-01-01' ?
this.sportsWalking() : this.sportsRunning()}
</div>
);









share|improve this question















There are two functions, sportsRunning and sportsWalking, that will render different text. For this, I am using an if condition at the bottom to display corresponding div:



{sportsDescription.sportsDate && sportsDescription.sportsDate.length > 0 && sportsDescription.sportsDate !== '2199-01-01' ?
this.sportsWalking() : this.sportsRunning()}`


Can you tell me how I can refactor the code?



sportsRunning() {
const sportsFire = this.props.profile.firstName;
return (
<div className="sports-table action-shadow">
<h4>{`Want some advice${sportsFire && sportsFire.length > 0 ? `, ${sportsFire}` : ''}?`}</h4>
<p>Stay focused by setting a target date for completion of your sports.</p>
<div className="secondary-links section_content">
<a href="javascript:;" onClick={this.editHandler}>Set Target Date</a>
</div>
</div>
);
}

sportsWalking() {
const sportsFire = this.props.profile.firstName;
return (
<div className="sports-table action-shadow">
<h4>{`Want a helpful tip${sportsFire && sportsFire.length > 0 ? `, ${sportsFire}` : ''}?`}</h4>
<p>Add some momentum to your account by setting up automatic recurring deposits.</p>
<div className="secondary-links section_content">
<a href="javascript:;" onClick={this.editHandler}>Set Up Automatic Deposits</a>
</div>
</div>
);
}

render() {
const {sportsDescription, data} = this.props;
const sportsAmount = parseFloat(sportsDescription.sportsAmount);
const accumulatedAmount = data.summary.total || 0;

return (
<div>
<div className="content-box action-shadow">
{
sportsDescription.error ?
this.renderProgressError() :
this.renderProgress({
accumulatedAmount,
sportsAmount
})
}
<div className="section_content">
<h2>Your Goal Progress</h2>
<p>You have accumulated <strong className={accumulatedAmount >= 0 ? null : 'negative'}>
{moneyFormat(accumulatedAmount)}</strong> towards your sports of <strong>
{moneyFormat(sportsAmount)}</strong>.
</p>
{
sportsDescription.sportsDate && sportsDescription.sportsDate.length > 0 &&
sportsDescription.sportsDate !== '2199-01-01' && this.renderGoalDate(sportsDescription.sportsDate)
}
<div className="secondary-links section_content">
<a href="javascript:;" onClick={this.editHandler}>Edit Goal Details</a>
</div>
</div>
</div>
{sportsDescription.sportsDate && sportsDescription.sportsDate.length > 0 && sportsDescription.sportsDate !== '2199-01-01' ?
this.sportsWalking() : this.sportsRunning()}
</div>
);






beginner html react.js jsx






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 '16 at 2:58









200_success

127k15148412




127k15148412










asked Aug 10 '16 at 21:01









user3854268

13




13












  • Welcome to CR! Looks like the formatting is a bit off. Take a look at the editing help page for all the information you need to fix the formatting of your post.
    – Mathieu Guindon
    Aug 10 '16 at 21:30












  • Can you tell us, what you think could be refactored? Because both functions return different texts, but with the same layout.. If the layout will not change you could create pureRenderComponent ala (props) => (<div></div>) But if the structure will change at any point, then its fine to keep it separately, then you will have less trouble to customize it
    – webdeb
    Aug 10 '16 at 22:12












  • @webdeb layout will not change...but your solution is confusing for me...can you updated in my code
    – user3854268
    Aug 11 '16 at 13:57










  • @user3854268 check out my answer
    – webdeb
    Aug 11 '16 at 14:29


















  • Welcome to CR! Looks like the formatting is a bit off. Take a look at the editing help page for all the information you need to fix the formatting of your post.
    – Mathieu Guindon
    Aug 10 '16 at 21:30












  • Can you tell us, what you think could be refactored? Because both functions return different texts, but with the same layout.. If the layout will not change you could create pureRenderComponent ala (props) => (<div></div>) But if the structure will change at any point, then its fine to keep it separately, then you will have less trouble to customize it
    – webdeb
    Aug 10 '16 at 22:12












  • @webdeb layout will not change...but your solution is confusing for me...can you updated in my code
    – user3854268
    Aug 11 '16 at 13:57










  • @user3854268 check out my answer
    – webdeb
    Aug 11 '16 at 14:29
















Welcome to CR! Looks like the formatting is a bit off. Take a look at the editing help page for all the information you need to fix the formatting of your post.
– Mathieu Guindon
Aug 10 '16 at 21:30






Welcome to CR! Looks like the formatting is a bit off. Take a look at the editing help page for all the information you need to fix the formatting of your post.
– Mathieu Guindon
Aug 10 '16 at 21:30














Can you tell us, what you think could be refactored? Because both functions return different texts, but with the same layout.. If the layout will not change you could create pureRenderComponent ala (props) => (<div></div>) But if the structure will change at any point, then its fine to keep it separately, then you will have less trouble to customize it
– webdeb
Aug 10 '16 at 22:12






Can you tell us, what you think could be refactored? Because both functions return different texts, but with the same layout.. If the layout will not change you could create pureRenderComponent ala (props) => (<div></div>) But if the structure will change at any point, then its fine to keep it separately, then you will have less trouble to customize it
– webdeb
Aug 10 '16 at 22:12














@webdeb layout will not change...but your solution is confusing for me...can you updated in my code
– user3854268
Aug 11 '16 at 13:57




@webdeb layout will not change...but your solution is confusing for me...can you updated in my code
– user3854268
Aug 11 '16 at 13:57












@user3854268 check out my answer
– webdeb
Aug 11 '16 at 14:29




@user3854268 check out my answer
– webdeb
Aug 11 '16 at 14:29










1 Answer
1






active

oldest

votes

















up vote
0
down vote













This is what I mean with pureRenderComponent:



const SportsMoving = ({editHandler, sportsDescription, ...props }) => {

const isRunning = sportsDescription.sportsDate && sportsDescription.sportsDate.length > 0 && sportsDescription.sportsDate !== '2199-01-01';
const sportFire = props.profile.firstName || "";

return (
<div className="sports-table action-shadow">
<h4>
{
isRunning
? `Want some advice ${sportFire}?`
: `Want a helpful tip ${sportsFire}?`
}
</h4>
<p>
{
isRunning
? "Stay focused by setting a target date for completion of your sports."
: "Add some momentum to your account by setting up automatic recurring deposits."
}
</p>
<div className="secondary-links section_content">
<a href="javascript:;" onClick={editHandler}>
{
isRunning
? "Set Target Date"
: "Set Up Automatic Deposits"
}
</a>
</div>
</div>
)
}


now in your parentComponent you can use it like so:



     ...
</div>
<SportsMoving editHandler={this.editHandler} ...props />
</div>





share|improve this answer























    Your Answer





    StackExchange.ifUsing("editor", function () {
    return StackExchange.using("mathjaxEditing", function () {
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    });
    });
    }, "mathjax-editing");

    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "196"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f138385%2fsportsrunning-and-sportswalking%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    This is what I mean with pureRenderComponent:



    const SportsMoving = ({editHandler, sportsDescription, ...props }) => {

    const isRunning = sportsDescription.sportsDate && sportsDescription.sportsDate.length > 0 && sportsDescription.sportsDate !== '2199-01-01';
    const sportFire = props.profile.firstName || "";

    return (
    <div className="sports-table action-shadow">
    <h4>
    {
    isRunning
    ? `Want some advice ${sportFire}?`
    : `Want a helpful tip ${sportsFire}?`
    }
    </h4>
    <p>
    {
    isRunning
    ? "Stay focused by setting a target date for completion of your sports."
    : "Add some momentum to your account by setting up automatic recurring deposits."
    }
    </p>
    <div className="secondary-links section_content">
    <a href="javascript:;" onClick={editHandler}>
    {
    isRunning
    ? "Set Target Date"
    : "Set Up Automatic Deposits"
    }
    </a>
    </div>
    </div>
    )
    }


    now in your parentComponent you can use it like so:



         ...
    </div>
    <SportsMoving editHandler={this.editHandler} ...props />
    </div>





    share|improve this answer



























      up vote
      0
      down vote













      This is what I mean with pureRenderComponent:



      const SportsMoving = ({editHandler, sportsDescription, ...props }) => {

      const isRunning = sportsDescription.sportsDate && sportsDescription.sportsDate.length > 0 && sportsDescription.sportsDate !== '2199-01-01';
      const sportFire = props.profile.firstName || "";

      return (
      <div className="sports-table action-shadow">
      <h4>
      {
      isRunning
      ? `Want some advice ${sportFire}?`
      : `Want a helpful tip ${sportsFire}?`
      }
      </h4>
      <p>
      {
      isRunning
      ? "Stay focused by setting a target date for completion of your sports."
      : "Add some momentum to your account by setting up automatic recurring deposits."
      }
      </p>
      <div className="secondary-links section_content">
      <a href="javascript:;" onClick={editHandler}>
      {
      isRunning
      ? "Set Target Date"
      : "Set Up Automatic Deposits"
      }
      </a>
      </div>
      </div>
      )
      }


      now in your parentComponent you can use it like so:



           ...
      </div>
      <SportsMoving editHandler={this.editHandler} ...props />
      </div>





      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        This is what I mean with pureRenderComponent:



        const SportsMoving = ({editHandler, sportsDescription, ...props }) => {

        const isRunning = sportsDescription.sportsDate && sportsDescription.sportsDate.length > 0 && sportsDescription.sportsDate !== '2199-01-01';
        const sportFire = props.profile.firstName || "";

        return (
        <div className="sports-table action-shadow">
        <h4>
        {
        isRunning
        ? `Want some advice ${sportFire}?`
        : `Want a helpful tip ${sportsFire}?`
        }
        </h4>
        <p>
        {
        isRunning
        ? "Stay focused by setting a target date for completion of your sports."
        : "Add some momentum to your account by setting up automatic recurring deposits."
        }
        </p>
        <div className="secondary-links section_content">
        <a href="javascript:;" onClick={editHandler}>
        {
        isRunning
        ? "Set Target Date"
        : "Set Up Automatic Deposits"
        }
        </a>
        </div>
        </div>
        )
        }


        now in your parentComponent you can use it like so:



             ...
        </div>
        <SportsMoving editHandler={this.editHandler} ...props />
        </div>





        share|improve this answer














        This is what I mean with pureRenderComponent:



        const SportsMoving = ({editHandler, sportsDescription, ...props }) => {

        const isRunning = sportsDescription.sportsDate && sportsDescription.sportsDate.length > 0 && sportsDescription.sportsDate !== '2199-01-01';
        const sportFire = props.profile.firstName || "";

        return (
        <div className="sports-table action-shadow">
        <h4>
        {
        isRunning
        ? `Want some advice ${sportFire}?`
        : `Want a helpful tip ${sportsFire}?`
        }
        </h4>
        <p>
        {
        isRunning
        ? "Stay focused by setting a target date for completion of your sports."
        : "Add some momentum to your account by setting up automatic recurring deposits."
        }
        </p>
        <div className="secondary-links section_content">
        <a href="javascript:;" onClick={editHandler}>
        {
        isRunning
        ? "Set Target Date"
        : "Set Up Automatic Deposits"
        }
        </a>
        </div>
        </div>
        )
        }


        now in your parentComponent you can use it like so:



             ...
        </div>
        <SportsMoving editHandler={this.editHandler} ...props />
        </div>






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Apr 9 '17 at 15:03

























        answered Aug 11 '16 at 14:28









        webdeb

        48324




        48324






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Code Review Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            Use MathJax to format equations. MathJax reference.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f138385%2fsportsrunning-and-sportswalking%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Сан-Квентин

            8-я гвардейская общевойсковая армия

            Алькесар