GithubHelp home page GithubHelp logo

react2github / java-mod-9-lab-2 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from learn-co-curriculum/java-mod-9-lab-2

0.0 1.0 0.0 78 KB

License: Other

TypeScript 73.06% HTML 18.23% CSS 0.26% Java 8.45%

java-mod-9-lab-2's Introduction

Lab 2

Instructions

Using what you've learned about components and services, create a new component that will display the number of messages sent by the active user.

Here are a few hints:

  1. Your new component can be positioned below the user message component at the bottom of the conversation history panel
  2. It can be a simple label that shows the text "total message(s): " and then the number of messages sent by the active user
  3. You already have a service that handles the sending of messages - integrate with that service to get your component access to the information it's looking for

Here is the solution:

  1. In your messaging-data.service.ts, you already have a addUserMessage() function - no messages get sent through your application without going through this function.
  2. Furthermore, that function already emits an event after it's done handling the requested message
  3. So all we need to do in our new component is subscribe to the existing event and use the corresponding data to update a local variable with the active number of messages
  4. We then simply use {{ }} notation to bind to that variable in our view

Here is the code for this solution:

Note: we used the following CLI command to generate the new component: ng g c application-component/conversation-history-component/message-count-component

  • Inject the MessagingDataService into the message count component:
import { Component, OnInit } from "@angular/core";
import { MessagingDataService } from "src/app/messaging-data.service";
import { Message } from "src/app/message.model";

@Component({
  selector: "app-message-count-component",
  templateUrl: "./message-count-component.component.html",
  styleUrls: ["./message-count-component.component.css"],
})
export class MessageCountComponentComponent implements OnInit {
  sentMessageCount = 0;

  constructor(private messagingSvce: MessagingDataService) {}

  ngOnInit(): void {
    this.messagingSvce.userMessagesChanged.subscribe((messages: Message[]) => {
      this.sentMessageCount = messages.length;
    });
  }
}
  • Add our simple text to the view for our message count component:
total message(s): {{ sentMessageCount }}
  • Add the message count component to the conversation history component in conversation-history-comopnent.component.html:
<div class="container">
  <div class="row">
    <div class="col-12 p-3">Ludovic, Jessica</div>
  </div>
  <div class="row">
    <div class="col-12 border p-3">
      <app-conversation-thread-component></app-conversation-thread-component>
    </div>
  </div>
</div>

<div class="container">
  <div class="row">
    <div class="col-12 p-3">
      <app-send-message-component></app-send-message-component>
    </div>
  </div>
</div>

<div class="container">
  <div class="row">
    <div class="col-12 p-3">
      <app-message-count-component></app-message-count-component>
    </div>
  </div>
</div>
  • Remove the message-count-component.component.spec.ts file, since we won't be writing unit tests for this component

java-mod-9-lab-2's People

Contributors

react2github avatar alveem avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.