mirror of
https://github.com/myfatemi04/wheelshare-frontend.git
synced 2025-04-21 11:20:17 -04:00
add carpoolprovider
This commit is contained in:
parent
c1f192fb1e
commit
16a0ae696f
|
@ -8,11 +8,13 @@
|
||||||
"@testing-library/jest-dom": "^5.11.4",
|
"@testing-library/jest-dom": "^5.11.4",
|
||||||
"@testing-library/react": "^11.1.0",
|
"@testing-library/react": "^11.1.0",
|
||||||
"@testing-library/user-event": "^12.1.10",
|
"@testing-library/user-event": "^12.1.10",
|
||||||
|
"@types/immutable": "^3.8.7",
|
||||||
"@types/node": "^14.14.37",
|
"@types/node": "^14.14.37",
|
||||||
"@types/react": "^17.0.3",
|
"@types/react": "^17.0.3",
|
||||||
"@types/react-dom": "^17.0.9",
|
"@types/react-dom": "^17.0.9",
|
||||||
"@types/react-router-dom": "^5.1.7",
|
"@types/react-router-dom": "^5.1.7",
|
||||||
"dotenv": "^8.2.0",
|
"dotenv": "^8.2.0",
|
||||||
|
"immutable": "^4.0.0-rc.14",
|
||||||
"react": "^17.0.2",
|
"react": "^17.0.2",
|
||||||
"react-bootstrap": "^1.5.2",
|
"react-bootstrap": "^1.5.2",
|
||||||
"react-dom": "^17.0.2",
|
"react-dom": "^17.0.2",
|
||||||
|
|
72
src/components/Carpool/CarpoolProvider.tsx
Normal file
72
src/components/Carpool/CarpoolProvider.tsx
Normal file
|
@ -0,0 +1,72 @@
|
||||||
|
import { createContext, ReactNode, useMemo, useState } from 'react';
|
||||||
|
import * as immutable from 'immutable';
|
||||||
|
import { useEffect } from 'react';
|
||||||
|
import { getCarpool } from '../api';
|
||||||
|
|
||||||
|
class Member extends immutable.Record({
|
||||||
|
id: 0,
|
||||||
|
name: '',
|
||||||
|
}) {}
|
||||||
|
|
||||||
|
class Invitation extends immutable.Record({
|
||||||
|
user: new Member(),
|
||||||
|
isRequest: false,
|
||||||
|
}) {}
|
||||||
|
|
||||||
|
class CarpoolState extends immutable.Record({
|
||||||
|
id: 0,
|
||||||
|
name: '',
|
||||||
|
members: immutable.List<Member>(),
|
||||||
|
invitations: immutable.List<Invitation>(),
|
||||||
|
}) {}
|
||||||
|
|
||||||
|
type Subscriber = (state: CarpoolState) => void;
|
||||||
|
|
||||||
|
class CarpoolSDK {
|
||||||
|
private _state = new CarpoolState();
|
||||||
|
get state() {
|
||||||
|
return this._state;
|
||||||
|
}
|
||||||
|
set state(state: CarpoolState) {
|
||||||
|
this._state = state;
|
||||||
|
}
|
||||||
|
private subscribers: Subscriber[] = [];
|
||||||
|
subscribe(subscriber: Subscriber) {
|
||||||
|
this.subscribers.push(subscriber);
|
||||||
|
return () => {
|
||||||
|
this.subscribers = this.subscribers.filter((s) => s !== subscriber);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const CarpoolContext = createContext({
|
||||||
|
sdk: new CarpoolSDK(),
|
||||||
|
carpool: new CarpoolState(),
|
||||||
|
});
|
||||||
|
|
||||||
|
export default function CarpoolProvider({
|
||||||
|
id,
|
||||||
|
children,
|
||||||
|
}: {
|
||||||
|
id: number;
|
||||||
|
children: ReactNode;
|
||||||
|
}) {
|
||||||
|
const [carpool, setCarpool] = useState(new CarpoolState());
|
||||||
|
const sdk = useMemo(() => new CarpoolSDK(), []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
getCarpool(id).then((carpool) => {});
|
||||||
|
}, [id]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const remove = sdk.subscribe(setCarpool);
|
||||||
|
|
||||||
|
return () => remove();
|
||||||
|
}, [sdk]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<CarpoolContext.Provider value={{ sdk, carpool }}>
|
||||||
|
{children}
|
||||||
|
</CarpoolContext.Provider>
|
||||||
|
);
|
||||||
|
}
|
|
@ -1,5 +1,5 @@
|
||||||
import { GroupPreview } from './GroupJoinerLink';
|
import { GroupPreview } from './GroupJoinerLink';
|
||||||
import { IInvitation, IEventSignup } from './types';
|
import { IInvitation, IEventSignup, ICarpool } from './types';
|
||||||
|
|
||||||
const base = process.env.REACT_APP_API_DOMAIN + 'api';
|
const base = process.env.REACT_APP_API_DOMAIN + 'api';
|
||||||
|
|
||||||
|
@ -163,7 +163,7 @@ export async function getReceivedInvitationsAndRequests() {
|
||||||
)) as IInvitation[];
|
)) as IInvitation[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getCarpool(id: number) {
|
export async function getCarpool(id: number): Promise<ICarpool> {
|
||||||
return await get('/carpools/' + id);
|
return await get('/carpools/' + id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
17
yarn.lock
17
yarn.lock
|
@ -2622,6 +2622,13 @@
|
||||||
resolved "https://registry.npmjs.org/@types/html-minifier-terser/-/html-minifier-terser-5.1.1.tgz"
|
resolved "https://registry.npmjs.org/@types/html-minifier-terser/-/html-minifier-terser-5.1.1.tgz"
|
||||||
integrity sha512-giAlZwstKbmvMk1OO7WXSj4OZ0keXAcl2TQq4LWHiiPH2ByaH7WeUzng+Qej8UPxxv+8lRTuouo0iaNDBuzIBA==
|
integrity sha512-giAlZwstKbmvMk1OO7WXSj4OZ0keXAcl2TQq4LWHiiPH2ByaH7WeUzng+Qej8UPxxv+8lRTuouo0iaNDBuzIBA==
|
||||||
|
|
||||||
|
"@types/immutable@^3.8.7":
|
||||||
|
version "3.8.7"
|
||||||
|
resolved "https://registry.yarnpkg.com/@types/immutable/-/immutable-3.8.7.tgz#536d33d30f3f3d9a6d4642a219419fd82af633fb"
|
||||||
|
integrity sha1-U20z0w8/PZptRkKiGUGf2Cr2M/s=
|
||||||
|
dependencies:
|
||||||
|
immutable "*"
|
||||||
|
|
||||||
"@types/invariant@^2.2.33":
|
"@types/invariant@^2.2.33":
|
||||||
version "2.2.34"
|
version "2.2.34"
|
||||||
resolved "https://registry.npmjs.org/@types/invariant/-/invariant-2.2.34.tgz"
|
resolved "https://registry.npmjs.org/@types/invariant/-/invariant-2.2.34.tgz"
|
||||||
|
@ -6693,6 +6700,16 @@ immer@8.0.1:
|
||||||
resolved "https://registry.npmjs.org/immer/-/immer-8.0.1.tgz"
|
resolved "https://registry.npmjs.org/immer/-/immer-8.0.1.tgz"
|
||||||
integrity sha512-aqXhGP7//Gui2+UrEtvxZxSquQVXTpZ7KDxfCcKAF3Vysvw0CViVaW9RZ1j1xlIYqaaaipBoqdqeibkc18PNvA==
|
integrity sha512-aqXhGP7//Gui2+UrEtvxZxSquQVXTpZ7KDxfCcKAF3Vysvw0CViVaW9RZ1j1xlIYqaaaipBoqdqeibkc18PNvA==
|
||||||
|
|
||||||
|
immutable@*:
|
||||||
|
version "3.8.2"
|
||||||
|
resolved "https://registry.yarnpkg.com/immutable/-/immutable-3.8.2.tgz#c2439951455bb39913daf281376f1530e104adf3"
|
||||||
|
integrity sha1-wkOZUUVbs5kT2vKBN28VMOEErfM=
|
||||||
|
|
||||||
|
immutable@^4.0.0-rc.14:
|
||||||
|
version "4.0.0-rc.14"
|
||||||
|
resolved "https://registry.yarnpkg.com/immutable/-/immutable-4.0.0-rc.14.tgz#29ba96631ec10867d1348515ac4e6bdba462f071"
|
||||||
|
integrity sha512-pfkvmRKJSoW7JFx0QeYlAmT+kNYvn5j0u7bnpNq4N2RCvHSTlLT208G8jgaquNe+Q8kCPHKOSpxJkyvLDpYq0w==
|
||||||
|
|
||||||
import-cwd@^2.0.0:
|
import-cwd@^2.0.0:
|
||||||
version "2.1.0"
|
version "2.1.0"
|
||||||
resolved "https://registry.npmjs.org/import-cwd/-/import-cwd-2.1.0.tgz"
|
resolved "https://registry.npmjs.org/import-cwd/-/import-cwd-2.1.0.tgz"
|
||||||
|
|
Loading…
Reference in New Issue
Block a user