Skip to content

Commit

Permalink
react-hooks as a package (#187)
Browse files Browse the repository at this point in the history
* react-hooks

* add readme

* 2022

* restore readme

* fix types

* fix types
  • Loading branch information
rileylnapier authored May 10, 2022
1 parent c66337d commit 7eae37a
Show file tree
Hide file tree
Showing 32 changed files with 772 additions and 281 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2021 Courier
Copyright (c) 2022 Courier

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
10 changes: 0 additions & 10 deletions packages/client-graphql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,14 @@ or you can create the client separtly and pass the client in:

```
import { createCourierClient } from "@trycourier/client-graphql";
const courierClient = createCourierClient({
clientKey: "abc123",
userId: "@me",
userSignature: "SUPER_SECRET",
});
const messages = Messages({ client: courierClient })
const events = Events({ events: courierClient })
const events = Brands({ events: courierClient })
```

### Messages
Expand All @@ -40,7 +37,6 @@ const messagesApi = Messages({
userId: "@me",
userSignature: "SUPER_SECRET",
});

const getMessageCount = async (params?: {
isRead?: boolean,
from?: number,
Expand All @@ -49,7 +45,6 @@ const getMessageCount = async (params?: {
const messageCount = await messagesApi.getMessageCount(params);
return messageCount;
};

const getMessages = async (
params?: {
isRead?: boolean,
Expand All @@ -59,7 +54,6 @@ const getMessages = async (
after?: string
) => {
const { startCursor, messages } = await messagesApi.getMessages(params);

return {
startCursor,
messages,
Expand All @@ -76,7 +70,6 @@ const eventsApi = Events({
userId: "@me",
userSignature: "SUPER_SECRET",
});

const trackEvent = async (trackingId: string) => {
await eventsApi.trackEvent(trackingId);
};
Expand All @@ -91,7 +84,6 @@ const brandsApi = Brands({
userId: "@me",
userSignature: "SUPER_SECRET",
});

const getBrand = async (brandId?: string) => {
const myBrand = await brandsApi.getBrand(brandId);
return myBrand;
Expand All @@ -109,7 +101,6 @@ const bannerApi = Banner({
userId: "@me",
userSignature: "SUPER_SECRET", //optional
});

const getBanners = async (params?: { tags?: string[], locale?: string }) => {
const myBanners = await bannerApi.getBanners(params);
return myBanners;
Expand All @@ -124,7 +115,6 @@ const bannerApi = Banner({
clientKey: "abc123",
authorization: "MY JWT TOKEN",
});

const getBanners = async (params?: { tags?: string[], locale?: string }) => {
const myBanners = await bannerApi.getBanners(params);
return myBanners;
Expand Down
1 change: 0 additions & 1 deletion packages/client-graphql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"clean": "rimraf dist",
"test": "jest -c jest.config.js --runInBand",
"type-check": "tsc --noEmit",
"readme": "concat-md --toc --decrease-title-levels --dir-name-as-title docs > README.md",
"types": "tsc --declaration --outDir typings/ --emitDeclarationOnly --declarationMap --allowJs false --checkJs false"
},
"license": "ISC",
Expand Down
1 change: 0 additions & 1 deletion packages/client-graphql/src/banner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ export const getBanners = (client?: Client): GetBanners => async (
const startCursor = results?.data?.banners?.pageInfo?.startCursor;

return {
appendMessages: Boolean(after),
banners,
startCursor,
};
Expand Down
21 changes: 21 additions & 0 deletions packages/react-hooks/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Courier

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
74 changes: 74 additions & 0 deletions packages/react-hooks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->

- [Overview](#overview)
- [Types](#types)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

<a name="0overviewmd"></a>

### [Overview](#overview)

`@trycourier/react-hooks` exist as a separate package so that you can build your own interface using our api and state management without having to install all the dependencies that `@trycourier/react-inbox` or other `react-dom` based packages include.

This also enables using this package with `react-native` in a much simpler way.

<a name="1typesmd"></a>

### [Types](#types)

```
const inbox: IInbox & IInboxActions = useInbox();
interface ITab {
filters: {
isRead?: boolean;
};
label: string;
id: string;
}
interface IMessage {
unread?: number;
messageId: string;
created: number;
title: string;
body: string;
blocks?: Array<IActionBlock | ITextBlock>;
icon?: string;
read?: boolean;
data?: {
clickAction: string;
};
trackingIds?: {
clickTrackingId: string;
deliveredTrackingId: string;
readTrackingId: string;
unreadTrackingId: string;
};
}
interface IInboxActions {
init: (inbox: IInbox) => void;
toggleInbox: (isOpen?: boolean) => void;
setView: (view: "messages" | "preferences") => void;
setCurrentTab: (newTab: ITab) => void;
fetchMessages: (params?: IFetchMessagesParams) => void;
getMessageCount: (params?: IGetMessagesParams) => void;
markMessageRead: (messageId: string, trackingId: string) => Promise<void>;
markMessageUnread: (messageId: string, trackingId: string) => Promise<void>;
markAllAsRead: () => void;
}
interface IInbox {
isOpen?: boolean;
tabs?: ITab[];
currentTab?: ITab;
isLoading?: boolean;
messages?: Array<IMessage>;
startCursor?: string;
unreadMessageCount?: number;
view?: "messages" | "preferences";
}
```
31 changes: 31 additions & 0 deletions packages/react-hooks/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module.exports = {
sourceType: "unambiguous",
plugins: [
"inline-react-svg",
"@babel/transform-runtime",
"babel-plugin-styled-components",
"transform-inline-environment-variables",
"transform-class-properties",
[
"babel-plugin-inline-import",
{
extensions: [".css"],
},
],
["babel-plugin-react-remove-properties", { properties: ["data-testid"] }],
[
"babel-plugin-root-import",
{
root: __dirname,
rootPathSuffix: "./src",
rootPathPrefix: "~/",
},
],
].filter(Boolean),
presets: [
"@babel/preset-typescript",
"@babel/preset-env",
"@babel/preset-react",
],
ignore: ["src/__tests__", "src/__mocks__"],
};
5 changes: 5 additions & 0 deletions packages/react-hooks/docs/0.overview.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
### [Overview](#overview)

`@trycourier/react-hooks` exist as a separate package so that you can build your own interface using our api and state management without having to install all the dependencies that `@trycourier/react-inbox` or other `react-dom` based packages include.

This also enables using this package with `react-native` in a much simpler way.
56 changes: 56 additions & 0 deletions packages/react-hooks/docs/1.types.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
### [Types](#types)

```
const inbox: IInbox & IInboxActions = useInbox();
interface ITab {
filters: {
isRead?: boolean;
};
label: string;
id: string;
}
interface IMessage {
unread?: number;
messageId: string;
created: number;
title: string;
body: string;
blocks?: Array<IActionBlock | ITextBlock>;
icon?: string;
read?: boolean;
data?: {
clickAction: string;
};
trackingIds?: {
clickTrackingId: string;
deliveredTrackingId: string;
readTrackingId: string;
unreadTrackingId: string;
};
}
interface IInboxActions {
init: (inbox: IInbox) => void;
toggleInbox: (isOpen?: boolean) => void;
setView: (view: "messages" | "preferences") => void;
setCurrentTab: (newTab: ITab) => void;
fetchMessages: (params?: IFetchMessagesParams) => void;
getMessageCount: (params?: IGetMessagesParams) => void;
markMessageRead: (messageId: string, trackingId: string) => Promise<void>;
markMessageUnread: (messageId: string, trackingId: string) => Promise<void>;
markAllAsRead: () => void;
}
interface IInbox {
isOpen?: boolean;
tabs?: ITab[];
currentTab?: ITab;
isLoading?: boolean;
messages?: Array<IMessage>;
startCursor?: string;
unreadMessageCount?: number;
view?: "messages" | "preferences";
}
```
37 changes: 37 additions & 0 deletions packages/react-hooks/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module.exports = {
// The root of your source code, typically /src
// `<rootDir>` is a token Jest substitutes
roots: ["<rootDir>"],

globals: {
"ts-jest": {
babelConfig: require("./babel.config.js"),
},
},

// Jest transformations -- this adds support for TypeScript
// using ts-jest
transform: {
"\\.(ts|tsx)$": "ts-jest",
},

// Runs special logic, such as cleaning up components
// when using React Testing Library and adds special
// extended assertions to Jest
setupFilesAfterEnv: ["@testing-library/jest-dom/extend-expect"],

// Test spec file resolution pattern
// Matches parent folder `__tests__` and filename
// should contain `test` or `spec`.
testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",

// Module file extensions for importing
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
moduleNameMapper: {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$":
"<rootDir>/__mocks__/fileMock.js",
"\\.(scss|sass|css)$": "identity-obj-proxy",
"~(.*)$": "<rootDir>/src/$1",
},
testPathIgnorePatterns: ["/node_modules/", "helpers"],
};
39 changes: 39 additions & 0 deletions packages/react-hooks/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "@trycourier/react-hooks",
"version": "1.14.2",
"description": "",
"main": "dist/index.js",
"types": "typings/index.d.ts",
"scripts": {
"babel": "babel src -d dist --extensions \".ts,.tsx\" --ignore \"src/**/__tests__/**\"",
"build:watch": "yarn babel --watch",
"build": "rimraf dist && yarn babel",
"clean": "rimraf dist",
"test": "jest -c jest.config.js --runInBand --silent",
"type-check": "tsc --noEmit",
"readme": "concat-md --toc --decrease-title-levels --dir-name-as-title docs > README.md",
"types": "tsc --declaration --outDir typings/ --emitDeclarationOnly --declarationMap --allowJs false --checkJs false"
},
"license": "ISC",
"devDependencies": {
"@types/deep-extend": "^0.4.32",
"concat-md": "^0.3.5"
},
"dependencies": {
"@trycourier/client-graphql": "^1.14.2",
"@trycourier/react-provider": "^1.14.2",
"deep-extend": "^0.6.0",
"rimraf": "^3.0.2"
},
"peerDependencies": {
"react": "^17.0.1"
},
"files": [
"dist/",
"typings/"
],
"exports": {
".": "./dist/index.js",
"./use-inbox": "./dist/inbox/use-inbox.js"
}
}
Loading

0 comments on commit 7eae37a

Please sign in to comment.