Skip to content

Commit 8bcfd3b

Browse files
Joel Rozenvaibhavsingh97
authored andcommitted
feat(sponsors): add component for adding a sponsor to an event (freeCodeCamp#140)
* feat(add a sponsor): add component for adding a sponsor to an event freeCodeCamp#97 * style(client): fix whitespace * refactor(add sponsor): convert to typescript also make changes as per reviewer comments * refactor(add sponsor): adding basic styling to form using styled-components also addressed reviewer comments * chore(add-sponsor): fix spelling mistakes * refactor(add-sponsor): add isomporphic-fetch and refactor to pass linting tests also changed error handling / success response handling * fix(eslintrd): ignore prop-types in eslint * fix(add sponsor component): rename interfaces to begin with 'I' also remove return type of FC * refactor(add sponsor): split styles and interfaces into seperate files * refactor(add sponsor): folder restructure * refactor(addsponsor.tsx): fix submit function to use try/catch * Update .eslintrc.json * refactor(add sponsor): make sponsor type a typed enum * refactor(add sponsor form): rewrite to use react-hook-form as per new project specs 163 * fix(add sponsor) remove console log * fix linter being weird
1 parent a027e82 commit 8bcfd3b

File tree

8 files changed

+298
-226
lines changed

8 files changed

+298
-226
lines changed

.eslintrc.json

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
"prettier/@typescript-eslint",
77
"plugin:prettier/recommended"
88
],
9-
"plugins": ["react", "@typescript-eslint", "prettier"],
9+
"plugins": [
10+
"react",
11+
"@typescript-eslint",
12+
"prettier"
13+
],
1014
"env": {
1115
"node": true,
1216
"es6": true
@@ -22,7 +26,9 @@
2226
"env": {
2327
"jest": true
2428
},
25-
"plugins": ["jest"]
29+
"plugins": [
30+
"jest"
31+
]
2632
}
2733
],
2834
"rules": {
@@ -32,7 +38,8 @@
3238
"@typescript-eslint/no-explicit-any": 0,
3339
"@typescript-eslint/no-unused-vars": ["error", { "argsIgnorePattern": "^_" }],
3440
"prettier/prettier": ["error", { "singleQuote": true }],
35-
"react/display-name": 0
41+
"react/display-name": 0,
42+
"react/prop-types": 0
3643
},
3744
"settings": {
3845
"react": {

client/components/AddSponsor.tsx

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import * as React from 'react';
2+
import fetch from 'isomorphic-fetch';
3+
import useForm from 'react-hook-form';
4+
5+
import {
6+
Form,
7+
Input,
8+
ResponseDiv,
9+
SubmitBtn,
10+
} from 'client/styles/components/AddSponsor';
11+
import {
12+
IAddSponsorProps,
13+
ISponsorData,
14+
} from 'client/interfaces/components/AddSponsor';
15+
16+
const AddSponsor: React.FC<IAddSponsorProps> = ({ eventId, chapterId }) => {
17+
const [responseMsg, setResponseMsg] = React.useState('');
18+
19+
const { register, handleSubmit, errors } = useForm();
20+
21+
const onSubmit = async data => {
22+
const { name, website, type }: ISponsorData = data;
23+
try {
24+
await fetch(`/${chapterId}/events/${eventId}/sponsors`, {
25+
// TODO: create route
26+
method: 'post',
27+
body: {
28+
name,
29+
website,
30+
type,
31+
},
32+
});
33+
setResponseMsg(`${name} has been added as a ${type} sponsor.`);
34+
} catch (e) {
35+
setResponseMsg('Uh oh, something went wrong.');
36+
// TODO: more descriptive error messages
37+
}
38+
};
39+
40+
return (
41+
<>
42+
{(responseMsg || errors) && <ResponseDiv>{responseMsg}</ResponseDiv>}
43+
<Form onSubmit={handleSubmit(onSubmit)}>
44+
<label>Sponsor Name: </label>
45+
<Input
46+
name="name"
47+
type="text"
48+
placeholder="Glitter and Sparkle Co"
49+
ref={register}
50+
required
51+
/>
52+
<label>Sponsor Website: </label>
53+
<Input
54+
name="website"
55+
type="text"
56+
placeholder="www.glitter.co"
57+
ref={register}
58+
required
59+
/>
60+
<label>Sponsor Type: </label>
61+
<select name="type" required ref={register}>
62+
<option value="FOOD">Food</option>
63+
<option value="BEVERAGE">Beverage</option>
64+
<option value="OTHER">Other</option>
65+
</select>
66+
<SubmitBtn type="submit">Add Sponsor</SubmitBtn>
67+
</Form>
68+
</>
69+
);
70+
};
71+
72+
export default AddSponsor;

client/components/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
import SomeComponent from './SomeComponent';
2+
import AddSponsor from './AddSponsor';
23

3-
export { SomeComponent };
4+
export { SomeComponent, AddSponsor };
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export interface IAddSponsorProps {
2+
eventId: string;
3+
chapterId: string;
4+
}
5+
6+
export enum SponsorType {
7+
FOOD,
8+
BEVERAGE,
9+
OTHER,
10+
}
11+
export interface ISponsorData {
12+
name: string;
13+
website: string;
14+
type: SponsorType;
15+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import styled from 'styled-components';
2+
3+
export const Form = styled.form`
4+
display: flex;
5+
flex-direction: column;
6+
max-width: 25%;
7+
`;
8+
9+
export const Input = styled.input`
10+
margin-bottom: 15px;
11+
`;
12+
13+
export const SubmitBtn = styled.button`
14+
margin-top: 15px;
15+
`;
16+
17+
export const ResponseDiv = styled.div`
18+
margin: 15px 0;
19+
`;
20+
// TODO: Style div based on response reveived

0 commit comments

Comments
 (0)