Ignore:
Timestamp:
11/09/21 14:45:09 (3 years ago)
Author:
Стојков Марко <mst@…>
Branches:
dev
Children:
74ad056
Parents:
7e7cc4c
Message:

Answer question

Location:
src/Clients/Angular/finki-chattery/src
Files:
4 added
11 edited

Legend:

Unmodified
Added
Removed
  • src/Clients/Angular/finki-chattery/src/app/core/state/question-facade.service.ts

    r7e7cc4c r48f727d  
    1414import { AuthService } from '../services';
    1515import {
     16  AnswerQuestion,
    1617  EffectStartedWorking,
    1718  GetPreviewQuestionsLatest,
     
    99100  }
    100101
     102  public answerQuestion(questionUid: string, text: string): void {
     103    this.dispatchEffect(new AnswerQuestion(questionUid, text));
     104  }
     105
    101106  private fetchPreviewQuestionsLatest(): void {
    102107    this.dispatchEffect(new GetPreviewQuestionsLatest());
  • src/Clients/Angular/finki-chattery/src/app/core/state/question-state/question-state-request.models.ts

    r7e7cc4c r48f727d  
    88  constructor(public text: string) {}
    99}
     10
     11export class AnswerQuestionRequest {
     12  constructor(public text: string) {}
     13}
  • src/Clients/Angular/finki-chattery/src/app/core/state/question-state/question.actions.ts

    r7e7cc4c r48f727d  
    33
    44import {
     5  AnswerQuestionStateViewModel,
    56  AnswerResponseQuestionStateViewModel,
    67  PreviewQuestionViewModel,
     
    2627  VoteAnswer = '[Question] Vote answer',
    2728  VoteAnswerSuccess = '[Question] Vote answer Success',
     29  AnswerQuestion = '[Question] AnswerQuestion',
     30  AnswerQuestionSuccess = '[Question] AnswerQuestion Success',
    2831  EffectStartedWorking = '[Question] Effect Started Working',
    2932  EffectFinishedWorking = '[Question] Effect Finished Working',
    3033  EffectFinishedWorkingError = '[Question] Effect Finished Working error'
     34}
     35
     36export class AnswerQuestion implements Action {
     37  readonly type = QuestionActionTypes.AnswerQuestion;
     38
     39  constructor(public questionUid: string, public text: string) {}
     40}
     41
     42export class AnswerQuestionSuccess implements Action {
     43  readonly type = QuestionActionTypes.AnswerQuestionSuccess;
     44
     45  constructor(public payload: AnswerQuestionStateViewModel) {}
    3146}
    3247
     
    141156  | SetCorrectAnswerSuccess
    142157  | RespondToAnswerSuccess
     158  | AnswerQuestionSuccess
    143159  | EffectStartedWorking
    144160  | EffectFinishedWorking
  • src/Clients/Angular/finki-chattery/src/app/core/state/question-state/question.effects.ts

    r7e7cc4c r48f727d  
    88import { NotificationService } from '../../services/notification.service';
    99import { QuestionFacadeService } from '../question-facade.service';
    10 import { RespondToAnswerRequest, VoteAnswerRequest } from './question-state-request.models';
     10import { AnswerQuestionRequest, RespondToAnswerRequest, VoteAnswerRequest } from './question-state-request.models';
    1111import {
     12  AnswerQuestionStateResponse,
    1213  AnswerResponseQuestionStateResponse,
    1314  PreviewQuestionResponse,
     
    1617} from './question-state-response.models';
    1718import {
     19  AnswerQuestion,
     20  AnswerQuestionSuccess,
    1821  EffectFinishedWorking,
    1922  EffectFinishedWorkingError,
     
    165168    );
    166169  });
     170
     171  answerQuestion$ = createEffect(() => {
     172    return this.actions$.pipe(
     173      ofType<AnswerQuestion>(QuestionActionTypes.AnswerQuestion),
     174      mergeMap((action) => {
     175        return this.api
     176          .post<AnswerQuestionStateResponse>(`v1/questions/${action.questionUid}/answers`, new AnswerQuestionRequest(action.text))
     177          .pipe(
     178            tap((state) => this.notification.successNotification('success-answer')),
     179            switchMap((state) => [
     180              new AnswerQuestionSuccess(QuestionMapper.ToAnswerQuestionStateViewModel(state)),
     181              new EffectFinishedWorking()
     182            ]),
     183            catchError((err) => [new EffectFinishedWorkingError(err)])
     184          );
     185      })
     186    );
     187  });
    167188}
  • src/Clients/Angular/finki-chattery/src/app/core/state/question-state/question.mapper.ts

    r7e7cc4c r48f727d  
    1515import { TranslateFromJsonService } from 'src/app/shared-app/services';
    1616import {
     17  AnswerQuestionStateResponse,
    1718  AnswerResponseQuestionStateResponse,
    1819  PreviewQuestionResponse,
     
    2930
    3031    if (questionStateResponse.answersResponse.length > 0) {
    31       answers = questionStateResponse.answersResponse.map((x) => {
    32         let answerResponses: AnswerResponseQuestionStateViewModel[] = [];
    33 
    34         if (x.answerResponsesResponse.length > 0) {
    35           answerResponses = x.answerResponsesResponse.map((y) => {
    36             const answerResponseStudent = new AnswerResponseStudentQuestionStateViewModel(
    37               y.studentResponse.uid,
    38               y.studentResponse.index,
    39               y.studentResponse.imageUrl,
    40               y.studentResponse.reputation
    41             );
    42 
    43             return new AnswerResponseQuestionStateViewModel(y.uid, y.text, moment(y.createdOn), answerResponseStudent);
    44           });
    45         }
    46 
    47         const answerStudent = new AnswerStudentQuestionStateViewModel(
    48           x.studentResponse.uid,
    49           x.studentResponse.index,
    50           x.studentResponse.imageUrl,
    51           x.studentResponse.reputation
    52         );
    53 
    54         return new AnswerQuestionStateViewModel(
    55           x.uid,
    56           x.text,
    57           x.correctAnswer,
    58           moment(x.createdOn),
    59           x.votesCount,
    60           answerStudent,
    61           answerResponses
    62         );
    63       });
     32      answers = questionStateResponse.answersResponse.map((x) => QuestionMapper.ToAnswerQuestionStateViewModel(x));
    6433    }
    6534
     
    136105    return new AnswerResponseQuestionStateViewModel(response.uid, response.text, moment(response.createdOn), answerResponseStudent);
    137106  }
     107
     108  public static ToAnswerQuestionStateViewModel(response: AnswerQuestionStateResponse): AnswerQuestionStateViewModel {
     109    let answerResponses: AnswerResponseQuestionStateViewModel[] = [];
     110
     111    if (response.answerResponsesResponse.length > 0) {
     112      answerResponses = response.answerResponsesResponse.map((y) => {
     113        const answerResponseStudent = new AnswerResponseStudentQuestionStateViewModel(
     114          y.studentResponse.uid,
     115          y.studentResponse.index,
     116          y.studentResponse.imageUrl,
     117          y.studentResponse.reputation
     118        );
     119
     120        return new AnswerResponseQuestionStateViewModel(y.uid, y.text, moment(y.createdOn), answerResponseStudent);
     121      });
     122    }
     123
     124    const answerStudent = new AnswerStudentQuestionStateViewModel(
     125      response.studentResponse.uid,
     126      response.studentResponse.index,
     127      response.studentResponse.imageUrl,
     128      response.studentResponse.reputation
     129    );
     130
     131    return new AnswerQuestionStateViewModel(
     132      response.uid,
     133      response.text,
     134      response.correctAnswer,
     135      moment(response.createdOn),
     136      response.votesCount,
     137      answerStudent,
     138      answerResponses
     139    );
     140  }
    138141}
  • src/Clients/Angular/finki-chattery/src/app/core/state/question-state/question.reducers.ts

    r7e7cc4c r48f727d  
    114114      };
    115115    }
     116    case QuestionActionTypes.AnswerQuestionSuccess: {
     117      if (state.question) {
     118        return {
     119          ...state,
     120          question: {
     121            ...state.question,
     122            answers: [...state.question.answers, action.payload]
     123          }
     124        };
     125      }
     126
     127      return {
     128        ...state
     129      };
     130    }
    116131    case QuestionActionTypes.EffectStartedWorking: {
    117132      return {
  • src/Clients/Angular/finki-chattery/src/app/shared-app/components/components.ts

    r7e7cc4c r48f727d  
    55import { TextEditorComponent } from './generic/text-editor/text-editor.component';
    66import { VoteComponent } from './generic/vote/vote.component';
     7import { AnswerQuestionComponent } from './question/answer-question/answer-question.component';
    78import { AskQuestionSharedComponent } from './question/ask-question-shared/ask-question-shared.component';
    89import { PreviewQuestionDisplayComponent } from './question/preview-question-display/preview-question-display.component';
     
    2627  PreviewQuestionFullComponent,
    2728  TextEditorComponent,
    28   RespondToAnswerDialogComponent
     29  RespondToAnswerDialogComponent,
     30  AnswerQuestionComponent
    2931];
  • src/Clients/Angular/finki-chattery/src/app/shared-app/components/question/preview-question-full/preview-question-full.component.html

    r7e7cc4c r48f727d  
    4040  </mat-card-content>
    4141  <mat-card-actions>
     42    <app-button (action)="scrollToBottom()" [buttonType]="ButtonType.CallToAction">{{
     43      'preview-question-full-answer' | translate
     44    }}</app-button>
    4245    <app-button appShareLink [buttonType]="ButtonType.Basic">{{ 'share-link' | translate }}</app-button>
    4346  </mat-card-actions>
  • src/Clients/Angular/finki-chattery/src/app/shared-app/components/question/preview-question-full/preview-question-full.component.ts

    r7e7cc4c r48f727d  
    1414
    1515  ngOnInit(): void {}
     16
     17  scrollToBottom(): void {
     18    window.scrollTo(0, document.body.scrollHeight);
     19  }
    1620}
  • src/Clients/Angular/finki-chattery/src/app/shared-app/components/question/question-preview/question-preview.component.html

    r7e7cc4c r48f727d  
    5959    </mat-card>
    6060  </ng-container>
     61  <app-answer-question></app-answer-question>
    6162</div>
  • src/Clients/Angular/finki-chattery/src/assets/translations/en.json

    r7e7cc4c r48f727d  
    6363  "submit-button": "Submit",
    6464  "header-student-questions": "Your questions",
     65  "success-answer": "Successfully answered question",
     66  "preview-question-full-answer": "Answer question",
     67  "answer-question-title": "Give your answer to the question",
     68  "answer-question-button": "Answer",
     69  "StudentDoesNotOwnQuestion": "You do not own this question",
    6570  "AnswerAlreadyUpvoted": "You have already upvoted this answer",
    6671  "AnswerAlreadyDownvoted": "You have already downvoted this answer",
Note: See TracChangeset for help on using the changeset viewer.