forked from kolplattformen/skolplattformen
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basically everything in react-native-embedded-api... but rewritten
- Loading branch information
1 parent
d623705
commit d37f3db
Showing
9 changed files
with
165 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
module.exports = { | ||
extends: ['airbnb-typescript'], | ||
parserOptions: { | ||
project: `./tsconfig.json` | ||
}, | ||
rules: { | ||
'@typescript-eslint/semi': ['error', 'never'] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
name: Release | ||
on: | ||
push: | ||
branches: | ||
- main | ||
jobs: | ||
release: | ||
name: Release | ||
runs-on: ubuntu-18.04 | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v2 | ||
with: | ||
fetch-depth: 0 | ||
- name: Setup Node.js | ||
uses: actions/setup-node@v1 | ||
with: | ||
node-version: 14 | ||
- name: Install dependencies | ||
run: yarn install --immutable --silent --non-interactive 2> >(grep -v warning 1>&2) | ||
- name: Build | ||
run: yarn build | ||
- name: Release | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
NPM_TOKEN: ${{ secrets.NPM_TOKEN }} | ||
run: npx semantic-release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# This workflow will do a clean install of node dependencies and run tests | ||
|
||
name: Test | ||
|
||
on: | ||
pull_request: | ||
branches: [main] | ||
|
||
jobs: | ||
unit: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Setup Node.js and run tests | ||
uses: actions/setup-node@v2.1.2 | ||
with: | ||
node-version: 14.x | ||
- run: yarn install --immutable --silent --non-interactive 2> >(grep -v warning 1>&2) | ||
- run: yarn lint | ||
- run: yarn test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
import { ApiCall, EntityAction, EntityName, ExtraActionProps } from './types' | ||
import { | ||
EntityAction, EntityName, ExtraActionProps, | ||
} from './types' | ||
|
||
export const loadAction = <T>(entity: EntityName, extra: ExtraActionProps<T>): EntityAction<T> => { | ||
return { | ||
entity, | ||
extra, | ||
type: 'GET_FROM_API', | ||
} | ||
} | ||
// eslint-disable-next-line import/prefer-default-export | ||
export const loadAction = <T>(entity: EntityName, extra: ExtraActionProps<T>): EntityAction<T> => ({ | ||
entity, | ||
extra, | ||
type: 'GET_FROM_API', | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,81 @@ | ||
/* eslint-disable default-case */ | ||
import { Middleware } from 'redux' | ||
import { EntityAction, EntityStoreRootState } from './types' | ||
|
||
export const apiMiddleware: Middleware<{}, EntityStoreRootState> = | ||
(storeApi) => | ||
(next) => | ||
(action: EntityAction<any>) => { | ||
try { | ||
switch (action.type) { | ||
case 'GET_FROM_API': { | ||
// Call api | ||
const apiCall = action.extra?.apiCall | ||
if (apiCall) { | ||
apiCall() | ||
.then((res: any) => { | ||
const resultAction: EntityAction<any> = { | ||
...action, | ||
type: 'RESULT_FROM_API', | ||
data: res | ||
} | ||
storeApi.dispatch(resultAction) | ||
|
||
if (action.extra?.saveToCache && res) { | ||
const cacheAction: EntityAction<any> = { | ||
...resultAction, | ||
type: 'STORE_IN_CACHE' | ||
} | ||
storeApi.dispatch(cacheAction) | ||
} | ||
}) | ||
type IMiddleware = Middleware<{}, EntityStoreRootState> | ||
export const apiMiddleware: IMiddleware = (storeApi) => (next) => (action: EntityAction<any>) => { | ||
try { | ||
switch (action.type) { | ||
case 'GET_FROM_API': { | ||
// Call api | ||
const apiCall = action.extra?.apiCall | ||
if (apiCall) { | ||
apiCall() | ||
.then((res: any) => { | ||
const resultAction: EntityAction<any> = { | ||
...action, | ||
type: 'RESULT_FROM_API', | ||
data: res, | ||
} | ||
storeApi.dispatch(resultAction) | ||
|
||
// Retrieve from cache | ||
if (action.extra?.getFromCache) { | ||
if (action.extra?.saveToCache && res) { | ||
const cacheAction: EntityAction<any> = { | ||
...action, | ||
type: 'GET_FROM_CACHE' | ||
...resultAction, | ||
type: 'STORE_IN_CACHE', | ||
} | ||
storeApi.dispatch(cacheAction) | ||
} | ||
} | ||
}) | ||
} | ||
|
||
// Retrieve from cache | ||
if (action.extra?.getFromCache) { | ||
const cacheAction: EntityAction<any> = { | ||
...action, | ||
type: 'GET_FROM_CACHE', | ||
} | ||
} catch (err) { | ||
console.error(err) | ||
storeApi.dispatch(cacheAction) | ||
} | ||
return next(action) | ||
} | ||
} | ||
} catch (err) { | ||
// eslint-disable-next-line no-console | ||
console.error(err) | ||
} | ||
return next(action) | ||
} | ||
|
||
export const cacheMiddleware: Middleware<{}, EntityStoreRootState> = | ||
(storeApi) => | ||
(next) => | ||
(action: EntityAction<any>) => { | ||
try { | ||
switch (action.type) { | ||
case 'GET_FROM_CACHE': { | ||
const getFromCache = action.extra?.getFromCache | ||
if (getFromCache) { | ||
getFromCache().then((res: string | null) => { | ||
if (res) { | ||
const cacheResultAction: EntityAction<any> = { | ||
...action, | ||
type: 'RESULT_FROM_CACHE', | ||
data: JSON.parse(res) | ||
} | ||
storeApi.dispatch(cacheResultAction) | ||
} | ||
}) | ||
export const cacheMiddleware: IMiddleware = (storeApi) => (next) => (action: EntityAction<any>) => { | ||
try { | ||
switch (action.type) { | ||
case 'GET_FROM_CACHE': { | ||
const getFromCache = action.extra?.getFromCache | ||
if (getFromCache) { | ||
getFromCache().then((res: string | null) => { | ||
if (res) { | ||
const cacheResultAction: EntityAction<any> = { | ||
...action, | ||
type: 'RESULT_FROM_CACHE', | ||
data: JSON.parse(res), | ||
} | ||
storeApi.dispatch(cacheResultAction) | ||
} | ||
case 'STORE_IN_CACHE': { | ||
const saveToCache = action.extra?.saveToCache | ||
if (saveToCache && action.data) { | ||
saveToCache(JSON.stringify(action.data)) | ||
} | ||
} | ||
} | ||
} catch (err) { | ||
console.error(err) | ||
}) | ||
} | ||
break | ||
} | ||
case 'STORE_IN_CACHE': { | ||
const saveToCache = action.extra?.saveToCache | ||
if (saveToCache && action.data) { | ||
saveToCache(JSON.stringify(action.data)) | ||
} | ||
return next(action) | ||
break | ||
} | ||
} | ||
} catch (err) { | ||
// eslint-disable-next-line no-console | ||
console.error(err) | ||
} | ||
return next(action) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.