Untitled

mail@pastecode.io avatar
unknown
typescript
7 months ago
3.4 kB
6
Indexable
Never
import { createSlice } from '@reduxjs/toolkit';

import { difficultToAnswerId } from 'constants/answer-ids';

import { RootState } from './store';
import { QuestionsState, QuestionResponse } from './types';

export const initialState: QuestionsState = {
  questions: [],
  comment: null,
};

const questionsSlice = createSlice({
  name: 'questions',
  initialState,
  reducers: {
    saveQuestions(state: QuestionsState, { payload }: { payload: Array<QuestionResponse> }) {
      state.questions = payload;
    },
    saveComment(state: QuestionsState, { payload }: { payload: string }) {
      state.comment = payload;
    },
    setAnswerId(
      state: QuestionsState,
      { payload }: { payload: { questionId: number; answerId?: number; minExtraPickOptions?: number } },
    ) {
      const { questionId, answerId, minExtraPickOptions } = payload;
      const question = state.questions.find((q) => q.questionId === questionId);

      if (question) {
        question.answerId = answerId;
        question.minExtraPickOptions = minExtraPickOptions;
      }
    },
    setAnswerComment(
      state: QuestionsState,
      { payload }: { payload: { questionId: number; comment?: string; commentRequired?: boolean } },
    ) {
      const { questionId, comment, commentRequired } = payload;
      const question = state.questions.find((q) => q.questionId === questionId);

      if (question) {
        question.comment = comment;
        question.commentRequired = commentRequired;
      }
    },
    setFillState(state: QuestionsState, { payload }: { payload: { questionId: number; isCompleted?: boolean } }) {
      const { questionId, isCompleted } = payload;
      const question = state.questions.find((questionResponse) => questionResponse.questionId === questionId);
      const isFilled = isCompleted || !!(question?.answerId && (question?.comment || !question?.commentRequired));

      if (question) {
        question.isFilled = isFilled;
      }
    },
    setComment(state: QuestionsState, { payload }: { payload: { comment: string } }) {
      const { comment } = payload;

      state.comment = comment;
    },
  },
});

export const {
  actions: { saveQuestions, saveComment, setAnswerId, setAnswerComment, setComment, setFillState },
  reducer: questionsReducer,
} = questionsSlice;

export const getQuestions = ({ questions }: RootState) => questions;

export const getGeneralQuestions = ({ questions }: RootState) =>
  questions.questions.filter((question) => !question.isChild);

export const getSelectedAdditionalQuestionsIds = ({ questions }: RootState) =>
  questions.questions
    .filter((question) => question.isChild && question.answerId && question.answerId !== difficultToAnswerId)
    .map(({ questionId }) => questionId);

export const getExtraAdditionalQuestionsIds = ({ questions }: RootState) =>
  questions.questions.filter((question) => question.isExtra).map(({ questionId }) => questionId);

export const getAnswerByQuestionId =
  (questionId: number) =>
  ({ questions }: RootState) =>
    questions.questions.find((question) => question.questionId === questionId);

export const getComment = ({ questions }: RootState) => questions.comment;

export const getAnswers = ({ questions }: RootState) => questions.questions;