Skip to content
This repository has been archived by the owner on Jun 9, 2023. It is now read-only.

Commit

Permalink
feat: conditionally load fetch proxy lib for browser/server
Browse files Browse the repository at this point in the history
* fix linting errors
  • Loading branch information
phoenisx committed Nov 18, 2019
1 parent f6f352e commit 0f518c7
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 73 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
.next/
next.config.js
client/@types/
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"prettier"
],
"env": {
"browser": true,
"node": true,
"es6": true
},
Expand Down
3 changes: 0 additions & 3 deletions client/@types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,3 @@ interface Window {
__REDUX_DEVTOOLS_EXTENSION_COMPOSE__: any;
__REACT_DEVTOOLS_GLOBAL_HOOK__: any;
}

// export some usefull utils for type modifications...
type Subtract<T, K> = T extends K ? T : never;
24 changes: 7 additions & 17 deletions client/components/AddSponsor.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as React from 'react';
import fetch from 'isomorphic-fetch';
import useForm from 'react-hook-form';

import {
Expand All @@ -8,29 +7,20 @@ import {
ResponseDiv,
SubmitBtn,
} from 'client/styles/components/AddSponsor';
import {
IAddSponsorProps,
ISponsorData,
} from 'client/interfaces/components/AddSponsor';

const AddSponsor: React.FC<IAddSponsorProps> = ({ eventId, chapterId }) => {
const AddSponsor: React.FC = () => {
const [responseMsg, setResponseMsg] = React.useState('');

const { register, handleSubmit, errors } = useForm();

// TODO: Get data from store
// const eventId = useSelector(state => state.selectedChapter.eventId);
// const chapterId = useSelector(state => state.selectedChapter.id);

const onSubmit = async data => {
const { name, website, type }: ISponsorData = data;
try {
await fetch(`/${chapterId}/events/${eventId}/sponsors`, {
// TODO: create route
method: 'post',
body: {
name,
website,
type,
},
});
setResponseMsg(`${name} has been added as a ${type} sponsor.`);
// await dispatch(sponsorActions.submit(eventId, chapterId));
setResponseMsg(`${data.name} has been added as a ${data.type} sponsor.`);
} catch (e) {
setResponseMsg('Uh oh, something went wrong.');
// TODO: more descriptive error messages
Expand Down
5 changes: 1 addition & 4 deletions client/interfaces/components/AddSponsor.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
export interface IAddSponsorProps {
eventId: string;
chapterId: string;
}
// export interface IAddSponsorProps {}

export enum SponsorType {
FOOD,
Expand Down
15 changes: 12 additions & 3 deletions client/services/http-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,26 @@
* This Class is a very minimal wrapper around fetch
* to make API calls easy.
*/
import fetch from 'isomorphic-unfetch';

import { RequestInfo, RequestInit, Response } from 'node-fetch';

let fetch: (url: RequestInfo, init?: RequestInit) => Promise<Response>;

if (typeof window === 'undefined') {
fetch = require('node-fetch');
} else {
fetch = window.fetch ? window.fetch : require('whatwg-fetch');
}

export class HttpService<V = any> {
public static baseUrl = '/api/v1'; // TODO: need to create some ENV for this
public static baseHeaders = {
'Content-Type': 'application/json',
};

public stringifyParams(params: Object) {
public stringifyParams(params: Record<string, string>) {
return Object.keys(params).reduce((acc, key, i) => {
return params.hasOwnProperty(key)
return Object.prototype.hasOwnProperty.call(params, key)
? `${acc}${i !== 0 ? '&' : '?'}${key}=${params[key]}`
: acc;
}, '');
Expand Down
40 changes: 2 additions & 38 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,11 @@
"express-async-handler": "^1.1.4",
"express-response-errors": "^1.0.4",
"fork-ts-checker-webpack-plugin": "^1.5.1",
"isomorphic-fetch": "^2.2.1",
"immer": "^3.2.0",
"isomorphic-unfetch": "^3.0.0",
"module-alias": "^2.2.2",
"morgan": "^1.9.1",
"next": "^9.1.1",
"node-fetch": "^2.6.0",
"pg": "^7.12.1",
"pg-hstore": "^2.3.3",
"react": "^16.10.2",
Expand All @@ -64,7 +63,8 @@
"sequelize-typescript": "^1.0.0",
"styled-components": "^4.4.0",
"ts-node": "^8.4.1",
"typescript": "^3.6.4"
"typescript": "^3.6.4",
"whatwg-fetch": "^3.0.0"
},
"devDependencies": {
"@testing-library/react": "^9.3.0",
Expand Down
6 changes: 1 addition & 5 deletions pages/add-sponsor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@ import * as React from 'react';
import { AddSponsor } from 'client/components';

const AddSponsorPage = () => {
// TODO - Dynamically get Chapter and EventId s
const eventId = '123abc';
const chapterId = 'abc123';

return (
<>
<AddSponsor eventId={eventId} chapterId={chapterId} />
<AddSponsor />
{
// TODO: page designs and styling
}
Expand Down

0 comments on commit 0f518c7

Please sign in to comment.