Skip to content

Commit 28aa3cc

Browse files
authored
Merge pull request #14 from Typext/feature/implementing-use-context
Feature/implementing use context
2 parents c483381 + 6bc4491 commit 28aa3cc

File tree

10 files changed

+111
-75
lines changed

10 files changed

+111
-75
lines changed

src/contexts/MainContext.tsx

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,64 @@
1-
import React, { createContext } from 'react';
1+
import React, { createContext, useState } from 'react';
22

3-
interface IMainContext {
3+
import {
4+
ITopic,
5+
IAddressAndHour,
6+
IProjectInfo,
7+
ISubject,
8+
} from 'pages/Main/components/MinuteViewer/components/Minute/DTOs';
9+
10+
interface IMainProvider {
411
children: React.ReactNode;
512
}
613

7-
export const MainContext = createContext({});
14+
interface IMainContextData {
15+
topics: Array<ITopic>;
16+
setTopics: Function;
17+
showSchedule: Boolean;
18+
setShowSchedule: Function;
19+
addressAndHour: IAddressAndHour;
20+
setAddressAndHour: Function;
21+
projectInfo: IProjectInfo;
22+
setProjectInfo: Function;
23+
subjects: ISubject[];
24+
setSubjects: Function;
25+
distributions: string[];
26+
setDistributions: Function;
27+
}
28+
29+
export const MainContext = createContext({} as IMainContextData);
30+
31+
export const MainProvider: React.FC<IMainProvider> = ({ children }: IMainProvider) => {
32+
const [topics, setTopics] = useState<ITopic[]>([]);
33+
const [showSchedule, setShowSchedule] = useState(false);
34+
const [addressAndHour, setAddressAndHour] = useState<IAddressAndHour>({
35+
local: '',
36+
startDate: '',
37+
startHour: '',
38+
});
39+
const [projectInfo, setProjectInfo] = useState<IProjectInfo>({
40+
projectName: '',
41+
members: [],
42+
});
43+
const [subjects, setSubjects] = useState<ISubject[]>([]);
44+
const [distributions, setDistributions] = useState<string[]>([]);
845

9-
export const MainProvider: React.FC<IMainContext> = ({ children }: IMainContext) => {
1046
return (
11-
<MainContext.Provider value="teste">
47+
<MainContext.Provider value={{
48+
topics,
49+
setTopics,
50+
showSchedule,
51+
setShowSchedule,
52+
addressAndHour,
53+
setAddressAndHour,
54+
projectInfo,
55+
setProjectInfo,
56+
subjects,
57+
setSubjects,
58+
distributions,
59+
setDistributions,
60+
}}
61+
>
1262
{children}
1363
</MainContext.Provider>
1464
);

src/pages/Main/components/Distributions/Distributions.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import React, { useState } from 'react';
1+
import React, { useContext, useState } from 'react';
22
import { message } from 'antd';
33

4+
import { MainContext } from 'contexts/MainContext';
5+
46
import Input from 'components/Input/Input';
57
import Button from 'components/Button/Button';
68

@@ -10,12 +12,9 @@ import ScrollBox from 'components/ScrollBox/ScrollBox';
1012
import BoxInformation from 'components/BoxInformation/BoxInformation';
1113
import StyledDistributions from './styles';
1214

13-
interface DistributionsProps {
14-
setDistributions: Function
15-
distributions: Array<string>
16-
}
15+
const Distributions = () => {
16+
const { distributions, setDistributions } = useContext(MainContext);
1717

18-
const Distributions = ({ setDistributions, distributions } : DistributionsProps) => {
1918
const [distribution, setDistribution] = useState<string>('');
2019

2120
const handleCleanFields = () => {

src/pages/Main/components/Initial/Initial.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
/* eslint-disable prettier/prettier */
2-
import React, { useState, useEffect } from 'react';
2+
import React, { useContext, useEffect, useState } from 'react';
3+
4+
import { MainContext } from 'contexts/MainContext';
5+
36
import Input from 'components/Input/Input';
47
import { StyledInitial } from './styles';
58

6-
interface IInitialProps {
7-
setAddressAndHour: Function
8-
}
9+
const Initial = () => {
10+
const { setAddressAndHour } = useContext(MainContext);
911

10-
const Initial = ({ setAddressAndHour }: IInitialProps) => {
1112
const [local, setLocal] = useState<string>('');
1213
const [startHour, setStartHour] = useState<string>('');
1314
const [startDate, setStartDate] = useState<string>('');

src/pages/Main/components/OptionButtons/index.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import React from 'react';
1+
import React, { useContext } from 'react';
2+
3+
import { MainContext } from 'contexts/MainContext';
24

35
import Button from 'components/Button/Button';
46

@@ -7,16 +9,16 @@ import { getMode } from 'services/api';
79
import { Container } from './styles';
810

911
interface OptionButtonsProps {
10-
setShowSchedule: Function;
1112
setShowMinute: Function;
1213
handleGenerateMinute: Function;
1314
}
1415

1516
const OptionButtons: React.FC<OptionButtonsProps> = ({
16-
setShowSchedule,
1717
setShowMinute,
1818
handleGenerateMinute,
1919
}: OptionButtonsProps) => {
20+
const { setShowSchedule } = useContext(MainContext);
21+
2022
const isNotProduction = getMode();
2123

2224
const handleOpenMinuteModal = () => {

src/pages/Main/components/ProjectInformation/ProjectInformation.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import React, { useState, useEffect } from 'react';
1+
import React, { useState, useEffect, useContext } from 'react';
2+
3+
import { MainContext } from 'contexts/MainContext';
24

35
import Button from 'components/Button/Button';
46
import Input from 'components/Input/Input';
@@ -19,11 +21,9 @@ interface IMembers {
1921
enterprise: string;
2022
}
2123

22-
interface ProjectProps {
23-
setProjectInfo: Function;
24-
}
24+
const ProjectInformation = () => {
25+
const { setProjectInfo } = useContext(MainContext);
2526

26-
const ProjectInformation = ({ setProjectInfo }: ProjectProps) => {
2727
const [members, setMembers] = useState<IMembers[]>([]);
2828

2929
const [projectName, setProjectName] = useState<string>('');

src/pages/Main/components/ScheduleModal/index.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import React from 'react';
1+
import React, { useContext } from 'react';
2+
3+
import { MainContext } from 'contexts/MainContext';
24

35
import scheduleImage from 'assets/calendar.svg';
46
import exit_icon from 'assets/exit_logo_red.svg';
57

68
import { Container } from './styles';
79

8-
interface IScheduleModalProps {
9-
setShowSchedule: Function;
10-
}
10+
const ScheduleModal = () => {
11+
const { setShowSchedule } = useContext(MainContext);
1112

12-
const ScheduleModal = ({ setShowSchedule }: IScheduleModalProps) => {
1313
const handleCloseModal = () => {
1414
if (setShowSchedule) setShowSchedule();
1515
};

src/pages/Main/components/Subjects/Subjects.tsx

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
1-
import React, { useState } from 'react';
2-
import ScrollBox from 'components/ScrollBox/ScrollBox';
1+
import React, { useContext, useState } from 'react';
2+
3+
import { MainContext } from 'contexts/MainContext';
34

5+
import ScrollBox from 'components/ScrollBox/ScrollBox';
46
import Button from 'components/Button/Button';
57
import StyledSubjects from './styles';
68

79
import SubjectModal from './components/SubjectModal';
810

9-
import { ISubject } from '../MinuteViewer/components/Minute/DTOs';
10-
11-
interface ISubjectsProps {
12-
subjects: Array<ISubject>;
13-
setSubjects: Function;
14-
}
11+
const Subjects = () => {
12+
const { subjects, setSubjects } = useContext(MainContext);
1513

16-
const Subjects = ({ subjects, setSubjects }: ISubjectsProps) => {
1714
const [openSubjectModal, setOpenSubjectModal] = useState(false);
1815

1916
const handleOpenSubjectModal = () => {

src/pages/Main/components/Topics/Topics.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
/* eslint-disable prettier/prettier */
2-
import React, { useState } from 'react';
2+
import React, { useContext, useState } from 'react';
33
import { message } from 'antd';
44

5+
import { MainContext } from 'contexts/MainContext';
6+
57
import Input from 'components/Input/Input';
68
import Button from 'components/Button/Button';
79
import BoxInformation from 'components/BoxInformation/BoxInformation';
@@ -12,12 +14,9 @@ import StyledTopics from './styles';
1214

1315
import { ITopic } from '../MinuteViewer/components/Minute/DTOs';
1416

15-
interface ITopicsProps {
16-
setTopics: Function;
17-
topics: Array<ITopic>;
18-
}
17+
const Topics = () => {
18+
const { topics, setTopics } = useContext(MainContext);
1919

20-
const Topics = ({ setTopics, topics }: ITopicsProps) => {
2120
const [topicName, setTopicName] = useState<string>('');
2221

2322
const handleCleanFields = () => {

src/pages/Main/index.tsx

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import React, { useState } from 'react';
1+
import React, { useContext, useState } from 'react';
22

3-
import { MainProvider } from 'contexts/MainContext';
3+
import { MainContext, MainProvider } from 'contexts/MainContext';
44

55
import Header from 'components/Header/Header';
66
import Initial from './components/Initial/Initial';
@@ -17,31 +17,22 @@ import ScheduleModal from './components/ScheduleModal';
1717
import Container from './styles';
1818

1919
import {
20-
IProjectInfo,
21-
ISubject,
22-
ITopic,
23-
IAddressAndHour,
2420
IMinute,
2521
} from './components/MinuteViewer/components/Minute/DTOs';
2622

2723
const Main = () => {
24+
const {
25+
topics,
26+
showSchedule,
27+
addressAndHour,
28+
projectInfo,
29+
subjects,
30+
distributions,
31+
} = useContext(MainContext);
32+
2833
const [showMinute, setShowMinute] = useState(false);
29-
const [showSchedule, setShowSchedule] = useState(false);
3034

3135
const [minute, setMinute] = useState<IMinute>();
32-
const [addressAndHour, setAddressAndHour] = useState<IAddressAndHour>({
33-
local: '',
34-
startDate: '',
35-
startHour: '',
36-
});
37-
const [projectInfo, setProjectInfo] = useState<IProjectInfo>({
38-
projectName: '',
39-
members: [],
40-
});
41-
42-
const [topics, setTopics] = useState<ITopic[]>([]);
43-
const [subjects, setSubjects] = useState<ISubject[]>([]);
44-
const [distributions, setDistributions] = useState<string[]>([]);
4536

4637
const handleGenerateMinute = () => {
4738
setMinute({
@@ -61,22 +52,19 @@ const Main = () => {
6152
<MinuteViewer setShowMinute={setShowMinute} minute={minute} />
6253
)}
6354

64-
{showSchedule && <ScheduleModal setShowSchedule={setShowSchedule} />}
55+
{showSchedule && <ScheduleModal />}
6556

6657
<Container>
67-
<Initial setAddressAndHour={setAddressAndHour} />
68-
<ProjectInformation setProjectInfo={setProjectInfo} />
69-
<Topics setTopics={setTopics} topics={topics} />
58+
<Initial />
59+
<ProjectInformation />
7060

71-
<Subjects subjects={subjects} setSubjects={setSubjects} />
72-
<Distributions
73-
setDistributions={setDistributions}
74-
distributions={distributions}
75-
/>
61+
<Topics />
62+
63+
<Subjects />
64+
<Distributions />
7665
<OptionButtons
7766
setShowMinute={setShowMinute}
7867
handleGenerateMinute={handleGenerateMinute}
79-
setShowSchedule={setShowSchedule}
8068
/>
8169
</Container>
8270
</MainProvider>

src/routes.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import RecoveryPassword from 'pages/RecoveryPassword';
21
import React from 'react';
2+
import RecoveryPassword from 'pages/RecoveryPassword';
33
import { Switch, Route, BrowserRouter } from 'react-router-dom';
44

55
import NewPassword from 'pages/NewPassword';

0 commit comments

Comments
 (0)