-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
158 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { createContext, useContext, useEffect, useState } from 'react' | ||
|
||
import Router from 'next/router' | ||
|
||
import QuestionsRepository from 'repositories/questionsRepository' | ||
|
||
import type { ReactNode } from 'react' | ||
import type { Question } from 'types/entities' | ||
|
||
type Questions = Question[] | ||
type QuestionsAnswers = { id: string; answer: number }[] | ||
type AnswerQuestion = (questionId: string, answer: number) => void | ||
|
||
interface AuthContextData { | ||
questions: Questions | ||
questionsAnswers: QuestionsAnswers | ||
isLoading: boolean | ||
answerQuestion: AnswerQuestion | ||
} | ||
|
||
interface AuthProviderProps { | ||
children: ReactNode | ||
} | ||
|
||
const questionsRepository = new QuestionsRepository() | ||
|
||
const QuestionsContext = createContext<AuthContextData>({ | ||
questions: [], | ||
questionsAnswers: [], | ||
isLoading: true, | ||
answerQuestion: () => undefined | ||
}) | ||
|
||
export const QuestionsProvider = ({ children }: AuthProviderProps) => { | ||
const [questions, setQuestions] = useState<Questions>([]) | ||
const [questionsAnswers, setQuestionsAnswers] = useState<QuestionsAnswers>([]) | ||
const [isLoading, setIsLoading] = useState(true) | ||
|
||
const answerQuestion: AnswerQuestion = (questionId, answer) => { | ||
setQuestionsAnswers([ | ||
...questionsAnswers.filter(({ id }) => id !== questionId), | ||
{ | ||
id: questionId, | ||
answer | ||
} | ||
]) | ||
} | ||
|
||
const fetchQuestions = async () => { | ||
try { | ||
const { questions } = await questionsRepository.getQuestions() | ||
setQuestions(questions) | ||
} catch { | ||
Router.push('/') | ||
} finally { | ||
setIsLoading(false) | ||
} | ||
} | ||
|
||
useEffect(() => { | ||
fetchQuestions() | ||
}, []) | ||
|
||
return ( | ||
<QuestionsContext.Provider | ||
value={{ | ||
questions, | ||
questionsAnswers, | ||
isLoading, | ||
answerQuestion | ||
}} | ||
> | ||
{children} | ||
</QuestionsContext.Provider> | ||
) | ||
} | ||
|
||
export const useQuestions = () => { | ||
const context = useContext(QuestionsContext) | ||
|
||
return context | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,24 @@ | ||
import Ranger from 'components/Ranger' | ||
|
||
import { useQuestions } from 'contexts/QuestionsContext' | ||
|
||
import type { NextPage } from 'next' | ||
|
||
const HomePage: NextPage = () => { | ||
const QuestionsPage: NextPage = () => { | ||
const { questions, answerQuestion } = useQuestions() | ||
|
||
return ( | ||
<> | ||
<Ranger optionsSize={3} side='both' /> | ||
{questions.map((question) => ( | ||
<Ranger | ||
key={question.id} | ||
optionsSize={3} | ||
side='both' | ||
onSelect={(answer) => answerQuestion(question.id, answer)} | ||
/> | ||
))} | ||
</> | ||
) | ||
} | ||
|
||
export default HomePage | ||
export default QuestionsPage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import Http from 'services/Http' | ||
|
||
import type { GetQuestionsResponse } from 'types/requests' | ||
|
||
export default class QuestionsRepository extends Http { | ||
async all() { | ||
return await this.get('surveys/frontend-interview') | ||
getQuestions() { | ||
return this.get<GetQuestionsResponse>('surveys/frontend-interview') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export type Question = { | ||
id: string | ||
createdAt: string | ||
title: string | ||
category: Category | ||
surveyId?: string | ||
} | ||
|
||
export type Category = { | ||
id: number | ||
name: string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import type { Question } from './entities' | ||
|
||
export type GetQuestionsResponse = { | ||
surveyId: number | ||
questions: Question[] | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2221,7 +2221,7 @@ prettier@^2.6.2: | |
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.6.2.tgz#e26d71a18a74c3d0f0597f55f01fb6c06c206032" | ||
integrity sha512-PkUpF+qoXTqhOeWL9fu7As8LXsIUZ1WYaJiY/a7McAQzxjk82OF0tibkFXVCDImZtWxbvojFjerkiLb0/q8mew== | ||
|
||
prop-types@^15.7.2, prop-types@^15.8.1: | ||
prop-types@^15.8.1: | ||
version "15.8.1" | ||
resolved "https://registry.yarnpkg.com/prop-types/-/prop-types-15.8.1.tgz#67d87bf1a694f48435cf332c24af10214a3140b5" | ||
integrity sha512-oj87CgZICdulUohogVAR7AjlC0327U4el4L6eAvOqCeudMDVU0NThNaV+b9Df4dXgSP1gXMTnPdhfe/2qDH5cg== | ||
|
@@ -2248,31 +2248,11 @@ [email protected]: | |
loose-envify "^1.1.0" | ||
scheduler "^0.22.0" | ||
|
||
react-fast-compare@^3.1.1: | ||
version "3.2.0" | ||
resolved "https://registry.yarnpkg.com/react-fast-compare/-/react-fast-compare-3.2.0.tgz#641a9da81b6a6320f270e89724fb45a0b39e43bb" | ||
integrity sha512-rtGImPZ0YyLrscKI9xTpV8psd6I8VAtjKCzQDlzyDvqJA8XOW78TXYQwNRNd8g8JZnDu8q9Fu/1v4HPAVwVdHA== | ||
|
||
react-helmet@^6.1.0: | ||
version "6.1.0" | ||
resolved "https://registry.yarnpkg.com/react-helmet/-/react-helmet-6.1.0.tgz#a750d5165cb13cf213e44747502652e794468726" | ||
integrity sha512-4uMzEY9nlDlgxr61NL3XbKRy1hEkXmKNXhjbAIOVw5vcFrsdYbH2FEwcNyWvWinl103nXgzYNlns9ca+8kFiWw== | ||
dependencies: | ||
object-assign "^4.1.1" | ||
prop-types "^15.7.2" | ||
react-fast-compare "^3.1.1" | ||
react-side-effect "^2.1.0" | ||
|
||
react-is@^16.13.1, react-is@^16.7.0: | ||
version "16.13.1" | ||
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4" | ||
integrity sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ== | ||
|
||
react-side-effect@^2.1.0: | ||
version "2.1.1" | ||
resolved "https://registry.yarnpkg.com/react-side-effect/-/react-side-effect-2.1.1.tgz#66c5701c3e7560ab4822a4ee2742dee215d72eb3" | ||
integrity sha512-2FoTQzRNTncBVtnzxFOk2mCpcfxQpenBMbk5kSVBg5UcPqV9fRbgY2zhb7GTWWOlpFmAxhClBDlIq8Rsubz1yQ== | ||
|
||
[email protected]: | ||
version "18.1.0" | ||
resolved "https://registry.yarnpkg.com/react/-/react-18.1.0.tgz#6f8620382decb17fdc5cc223a115e2adbf104890" | ||
|
06b3215
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
company-questions – ./
company-questions-git-master-mikael-r.vercel.app
company-questions-mikael-r.vercel.app
company-questions.vercel.app