diff --git a/app/graphql/resolvers/axes_query_resolver.rb b/app/graphql/resolvers/axes_query_resolver.rb new file mode 100644 index 0000000..099083e --- /dev/null +++ b/app/graphql/resolvers/axes_query_resolver.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true +module Resolvers + class AxesQueryResolver + def initialize(context) + @context = context + end + + def resolve + AxisPolicy::Scope.new(@context[:current_user], Axis).resolve + end + end + end + \ No newline at end of file diff --git a/app/graphql/types/query_type.rb b/app/graphql/types/query_type.rb index 010a527..ffdd9e2 100644 --- a/app/graphql/types/query_type.rb +++ b/app/graphql/types/query_type.rb @@ -6,6 +6,7 @@ module Types field :questions, QuestionType.connection_type, null: false do argument :where, Inputs::QuestionWhereInput, required: false end + field :axes, AxisType.connection_type, null: false field :subjects, SubjectType.connection_type, null: false field :categories, CategoryType.connection_type, null: false field :reviewers, UserType.connection_type, null: false @@ -16,6 +17,10 @@ module Types Resolvers::QuestionsQueryResolver.new(Question, context: context, where: where).resolve end + def axes + Resolvers::AxesQueryResolver.new(context).resolve + end + def subjects Resolvers::SubjectsQueryResolver.new(context).resolve end diff --git a/app/javascript/__generated__/graphql-schema.ts b/app/javascript/__generated__/graphql-schema.ts index 72f6aa8..1e1be79 100644 --- a/app/javascript/__generated__/graphql-schema.ts +++ b/app/javascript/__generated__/graphql-schema.ts @@ -25,6 +25,27 @@ export type Axis = { subjects: Array; }; +/** The connection type for Axis. */ +export type AxisConnection = { + __typename?: 'AxisConnection'; + /** A list of edges. */ + edges: Array; + /** A list of nodes. */ + nodes: Array; + /** Information to aid in pagination. */ + pageInfo: PageInfo; + totalCount: Scalars['Int']['output']; +}; + +/** An edge in a connection. */ +export type AxisEdge = { + __typename?: 'AxisEdge'; + /** A cursor for use in pagination. */ + cursor: Scalars['String']['output']; + /** The item at the end of the edge. */ + node?: Maybe; +}; + export type Category = { __typename?: 'Category'; id: Scalars['ID']['output']; @@ -181,6 +202,7 @@ export type PageInfo = { export type Query = { __typename?: 'Query'; + axes: AxisConnection; categories: CategoryConnection; currentUser?: Maybe; /** Fetches an object given its ID. */ @@ -194,6 +216,14 @@ export type Query = { }; +export type QueryAxesArgs = { + after?: InputMaybe; + before?: InputMaybe; + first?: InputMaybe; + last?: InputMaybe; +}; + + export type QueryCategoriesArgs = { after?: InputMaybe; before?: InputMaybe; diff --git a/app/javascript/__generated__/schema.graphql b/app/javascript/__generated__/schema.graphql index 508296e..5111097 100644 --- a/app/javascript/__generated__/schema.graphql +++ b/app/javascript/__generated__/schema.graphql @@ -4,6 +4,42 @@ type Axis { subjects: [Subject!]! } +""" +The connection type for Axis. +""" +type AxisConnection { + """ + A list of edges. + """ + edges: [AxisEdge!]! + + """ + A list of nodes. + """ + nodes: [Axis!]! + + """ + Information to aid in pagination. + """ + pageInfo: PageInfo! + totalCount: Int! +} + +""" +An edge in a connection. +""" +type AxisEdge { + """ + A cursor for use in pagination. + """ + cursor: String! + + """ + The item at the end of the edge. + """ + node: Axis +} + type Category { id: ID! name: String! @@ -238,6 +274,27 @@ type PageInfo { } type Query { + axes( + """ + Returns the elements in the list that come after the specified cursor. + """ + after: String + + """ + Returns the elements in the list that come before the specified cursor. + """ + before: String + + """ + Returns the first _n_ elements from the list. + """ + first: Int + + """ + Returns the last _n_ elements from the list. + """ + last: Int + ): AxisConnection! categories( """ Returns the elements in the list that come after the specified cursor. diff --git a/app/javascript/pages/assessment/NewAssessement.tsx b/app/javascript/pages/assessment/NewAssessement.tsx index 1ecbaaa..47fccbf 100644 --- a/app/javascript/pages/assessment/NewAssessement.tsx +++ b/app/javascript/pages/assessment/NewAssessement.tsx @@ -1,23 +1,21 @@ import { gql, useQuery } from "@apollo/client"; import React, { useState } from "react"; +import { FaTrash } from "react-icons/fa"; import { Controller, useForm } from "react-hook-form"; -import { Query } from "../../__generated__/graphql-schema"; + +import { Axis, Query } from "../../__generated__/graphql-schema"; import { Button, Card, Input, Navigator } from '../../components'; type NewAssessementForm = { - subjectWeights: Record + axisWeights: Record } const NEW_ASSESSEMENT_DATA_QUERY = gql` query NewAssessementDataQuery { - categories { + axes { nodes { id name - subjects { - id - name - } } } } @@ -25,24 +23,30 @@ const NEW_ASSESSEMENT_DATA_QUERY = gql` export const NewAssessement = () => { const { data } = useQuery(NEW_ASSESSEMENT_DATA_QUERY) + const axes = data?.axes.nodes const [subjectsIds, setSubjectsIds] = useState([]) - const subjectForm = useForm<{ subjectId: string }>() const { register, control, watch } = useForm({ mode: 'onBlur' }) + - const handleSubjectFormSubmit = (data: { - subjectId: string - }) => { - setSubjectsIds(prev => [...prev, data.subjectId]) - subjectForm.reset(); + const addAxisForm = useForm<{ axisId: string }>() + + const handleAddAxis = (formData: { axisId: string }) => { + setSubjectsIds(prev => [...prev, formData.axisId]) + addAxisForm.reset(); } - if (!data?.categories) { - return null; + const handleRemoveAxis = (axisId: string) => { + setSubjectsIds(prev => prev.filter((axis => axis !== axisId))) } + if (!axes?.length) return null + + const notSelectedAxis: Axis[] = axes.filter((axis) => !subjectsIds.includes(axis.id)) + const selectedAxis: Axis[] = axes.filter((axis) => subjectsIds.includes(axis.id)) + return ( <> @@ -61,117 +65,116 @@ export const NewAssessement = () => { - +
- +
- {data.categories.nodes.map(category => ( - category.subjects.find(subject => subjectsIds.includes(subject.id)) ? -
-
{category.name}
- {category.subjects.map(subject => - subjectsIds.includes(subject.id) - ?
-
{subject.name}
-
- Fácil - - {watch(`subjectWeights.easy.${subject.id}`) ?? 5} - -
- ( - - )} - /> + {selectedAxis.map(axis => ( +
+
+
+
{axis.name}
+
handleRemoveAxis(axis.id)}>
+
+
+ Fácil + + {watch(`axisWeights.easy.${axis.id}`) ?? 5} + +
+ ( + + )} + /> -
+
-
-
- Médio - - {watch(`subjectWeights.medium.${subject.id}`) ?? 5} - -
- ( - - )} - /> +
+
+ Médio + + {watch(`axisWeights.medium.${axis.id}`) ?? 5} + +
+ ( + + )} + /> -
+
-
-
- Difícil - - {watch(`subjectWeights.hard.${subject.id}`) ?? 5} - -
- ( - - )} - /> +
+
+ Difícil + + {watch(`axisWeights.hard.${axis.id}`) ?? 5} + +
+ ( + + )} + /> -
+
-
-
- : null - )} +
- : null +
))}
diff --git a/app/policies/axis_policy.rb b/app/policies/axis_policy.rb new file mode 100644 index 0000000..196acd9 --- /dev/null +++ b/app/policies/axis_policy.rb @@ -0,0 +1,7 @@ +class AxisPolicy < ApplicationPolicy + class Scope < Scope + def resolve + scope.all + end + end +end