Skip to content
This repository has been archived by the owner on Sep 24, 2024. It is now read-only.

Setup app to be loaded as MFE #11

Merged
merged 4 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ parameters:
default: "inventory"

orbs:
aws-s3: circleci/aws-s3@3.1.1
aws-cli: circleci/aws-cli@4.1.3
aws-s3: circleci/aws-s3@4.0.0

image: &image
docker:
- image: cimg/node:18.15.0
- image: cimg/node:20.11.0
resource_class: medium+

setup: &setup
Expand All @@ -28,10 +29,10 @@ jobs:
- <<: *setup
- run:
name: Audit
command: pnpm audit --audit-level high && pnpm audit || exit 0
command: pnpm audit || exit 0
- run:
name: Test
command: pnpm lint && pnpm ts:check && pnpm test
name: Test
command: pnpm --if-present lint && pnpm --if-present ts:check && pnpm --if-present test
build:
<<: *image
environment:
Expand All @@ -40,13 +41,15 @@ jobs:
- checkout
- <<: *setup
- run:
name: Build
name: Build
command: pnpm build
- aws-cli/setup:
aws_access_key_id: AWS_ACCESS_KEY
aws_secret_access_key: AWS_SECRET_ACCESS_KEY
- aws-s3/sync:
aws-access-key-id: AWS_ACCESS_KEY
aws-secret-access-key: AWS_SECRET_ACCESS_KEY
from: packages/app/dist
to: "s3://$S3_ASSETS_BUCKET/team/fe-static-apps/<< pipeline.parameters.project-name >>/<< pipeline.git.tag >>"

preview:
<<: *image
parameters:
Expand All @@ -64,23 +67,22 @@ jobs:
- run:
name: Inject configuration
command: "cp packages/app/public/config.preview-<< parameters.preview-env >>.js packages/app/dist/config.local.js"
- aws-cli/setup:
aws_access_key_id: AWS_ACCESS_KEY
aws_secret_access_key: AWS_SECRET_ACCESS_KEY
- aws-s3/sync:
aws-access-key-id: AWS_ACCESS_KEY
aws-secret-access-key: AWS_SECRET_ACCESS_KEY
from: packages/app/dist
to: "s3://$S3_ASSETS_DEV_BUCKET/team/fe-static-apps/<< pipeline.parameters.project-name >>/<< pipeline.git.tag >>-<< parameters.preview-env >>"

workflows:
version: 2

run-tests:
jobs:
- test:
context: commercelayer
filters:
tags:
ignore: /v.*/
ignore: /v.*|pr-.*/

test-build-and-push:
jobs:
- test:
Expand Down
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ typings/
.yarn-integrity

# dotenv environment variables file
.env
.env.test
# .env --> we want to keep it versionated by default. Use `.env.(local|production)` for custom env to be ignored
.env.*

# parcel-bundler cache (https://parceljs.org/)
.cache
Expand Down
2 changes: 1 addition & 1 deletion .npmrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
//registry.npmjs.org/:_authToken=${NPM_READ_TOKEN}
use-node-version=18.15.0
use-node-version=20.11.0
auto-install-peers=true
13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@
"packages/*"
],
"devDependencies": {
"husky": "^8.0.3",
"lerna": "^7.3.1",
"lint-staged": "^15.0.1",
"npm-check-updates": "^16.14.6"
"husky": "^9.0.11",
"lerna": "^8.1.2",
"lint-staged": "^15.2.2",
"npm-check-updates": "^16.14.15"
},
"resolutions": {
"ip": "^2.0.1"
}
}
}
4 changes: 4 additions & 0 deletions packages/app/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Base router path for the project
PUBLIC_PROJECT_PATH=
# When set it defines the slug for the self hosted version of the project
PUBLIC_SELF_HOSTED_SLUG=
2 changes: 0 additions & 2 deletions packages/app/.env.local.sample

This file was deleted.

2 changes: 1 addition & 1 deletion packages/app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ You need a local Node.JS (version 18+) environment and some React.JS knowledge t
git clone https://github.com/<your username>/app-inventory.git && cd app-inventory
```

3. Set your environment by creating a new `/src/app/.env.local` file starting from `/src/app/.env.local.sample` (not required for local development).
3. Set your environment by creating a new `/src/app/.env.local` file starting from `/src/app/.env` (not required for local development).

4. Install dependencies and run the development server:

Expand Down
9 changes: 0 additions & 9 deletions packages/app/global.d.ts

This file was deleted.

15 changes: 14 additions & 1 deletion packages/app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,28 @@
href="https://data.commercelayer.app/assets/images/favicons/favicon-32x32.png"
/>
<link rel="preconnect" href="https://fonts.gstatic.com" />
<script src="https://unpkg.com/react@18/umd/react.production.min.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js"></script>
<link
href="https://fonts.googleapis.com/css2?family=Manrope:wght@400;500;600;700;800&display=swap"
rel="stylesheet"
/>
</head>
<body>
<div id="root"></div>
<div id="root-app"></div>

<script src="/config.local.js"></script>
<script src="/config.js"></script>

<script type="module" src="/src/main.tsx"></script>

<script>
window.addEventListener('DOMContentLoaded', () => {
window.clApp_inventory.init(document.getElementById('root-app'), {
organizationSlug: '%PUBLIC_SELF_HOSTED_SLUG%',
routerBase: '%PUBLIC_PROJECT_PATH%'
})
})
</script>
</body>
</html>
26 changes: 14 additions & 12 deletions packages/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,32 @@
"prepare": "touch ./public/config.local.js"
},
"dependencies": {
"@commercelayer/app-elements": "1.14.1",
"@commercelayer/sdk": "5.31.1",
"@commercelayer/app-elements": "1.16.0",
"@commercelayer/sdk": "5.33.1",
"@hookform/resolvers": "^3.3.4",
"lodash": "^4.17.21",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-hook-form": "7.49.3",
"swr": "^2.2.4",
"type-fest": "^4.9.0",
"wouter": "^2.12.1",
"react-hook-form": "7.51.0",
"swr": "^2.2.5",
"type-fest": "^4.11.1",
"wouter": "^3.0.1",
"zod": "^3.22.4"
},
"devDependencies": {
"@commercelayer/eslint-config-ts-react": "^1.3.0",
"@testing-library/react": "^14.1.2",
"@testing-library/react": "^14.2.1",
"@types/lodash": "^4.14.202",
"@types/react": "^18.2.48",
"@types/react-dom": "^18.2.18",
"@types/node": "20.11.24",
"@types/react": "^18.2.63",
"@types/react-dom": "^18.2.20",
"@vitejs/plugin-react": "^4.2.1",
"eslint": "^8.56.0",
"eslint": "^8.57.0",
"jsdom": "^24.0.0",
"rollup-plugin-external-globals": "^0.9.2",
"typescript": "^5.3.3",
"vite": "^5.0.12",
"vite": "^5.1.5",
"vite-tsconfig-paths": "^4.3.1",
"vitest": "^1.2.1"
"vitest": "^1.3.1"
}
}
83 changes: 27 additions & 56 deletions packages/app/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,66 +4,37 @@ import { StockItemDetails } from '#pages/StockItemDetails'
import { StockItemEdit } from '#pages/StockItemEdit'
import { StockItemNew } from '#pages/StockItemNew'
import { StockItemsList } from '#pages/StockItemsList'
import {
CoreSdkProvider,
ErrorBoundary,
MetaTags,
TokenProvider
} from '@commercelayer/app-elements'
import { SWRConfig } from 'swr'
import type { FC } from 'react'
import { Route, Router, Switch } from 'wouter'
import { appRoutes } from './data/routes'

const isDev = Boolean(import.meta.env.DEV)
const basePath =
import.meta.env.PUBLIC_PROJECT_PATH != null
? `/${import.meta.env.PUBLIC_PROJECT_PATH}`
: undefined
interface AppProps {
routerBase?: string
}

export function App(): JSX.Element {
export const App: FC<AppProps> = ({ routerBase }) => {
return (
<ErrorBoundary hasContainer>
<SWRConfig
value={{
revalidateOnFocus: false
}}
>
<TokenProvider
kind='inventory'
appSlug='inventory'
domain={window.clAppConfig.domain}
reauthenticateOnInvalidAuth={!isDev}
devMode={isDev}
loadingElement={<div />}
organizationSlug={import.meta.env.PUBLIC_SELF_HOSTED_SLUG}
>
<MetaTags />
<CoreSdkProvider>
<Router base={basePath}>
<Switch>
<Route path={appRoutes.home.path}>
<Home />
</Route>
<Route path={appRoutes.stockLocation.path}>
<StockItemsList />
</Route>
<Route path={appRoutes.stockItem.path}>
<StockItemDetails />
</Route>
<Route path={appRoutes.newStockItem.path}>
<StockItemNew />
</Route>
<Route path={appRoutes.editStockItem.path}>
<StockItemEdit />
</Route>
<Route>
<ErrorNotFound />
</Route>
</Switch>
</Router>
</CoreSdkProvider>
</TokenProvider>
</SWRConfig>
</ErrorBoundary>
<Router base={routerBase}>
<Switch>
<Route path={appRoutes.home.path}>
<Home />
</Route>
<Route path={appRoutes.stockLocation.path}>
<StockItemsList />
</Route>
<Route path={appRoutes.stockItem.path}>
<StockItemDetails />
</Route>
<Route path={appRoutes.newStockItem.path}>
<StockItemNew />
</Route>
<Route path={appRoutes.editStockItem.path}>
<StockItemEdit />
</Route>
<Route>
<ErrorNotFound />
</Route>
</Switch>
</Router>
)
}
10 changes: 8 additions & 2 deletions packages/app/src/components/ListEmptyState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@ import { Link, useRoute } from 'wouter'
export const ListEmptyState: FC = () => {
const { canUser } = useTokenProvider()

const [, params] = useRoute<{ stockLocationId: string }>(
const [, paramsNewStockItem] = useRoute<{ stockLocationId: string }>(
appRoutes.newStockItem.path
)
const [, paramsNewStockLocation] = useRoute<{ stockLocationId: string }>(
appRoutes.stockLocation.path
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this fix.. The issue here is that the ListEmptyState is shared between two pages:

  • stock items list
  • new stock items
    both sharing the logic to have a stockLocationId param in the URL.
    I should fix this by differentiating the behavior of the component with a brand new type prop like in orders app.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed, thanks for pointing this out

)

const stockLocationId = params?.stockLocationId ?? ''
const stockLocationId =
paramsNewStockItem?.stockLocationId ??
paramsNewStockLocation?.stockLocationId ??
''

if (canUser('create', 'stock_items')) {
return (
Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/hooks/useAddItemOverlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import type { FiltersInstructions } from '@commercelayer/app-elements/dist/ui/resources/useResourceFilters/types'
import type { Sku } from '@commercelayer/sdk'
import { useState } from 'react'
import { navigate, useSearch } from 'wouter/use-location'
import { navigate, useSearch } from 'wouter/use-browser-location'

interface OverlayHook {
show: ({
Expand Down
46 changes: 39 additions & 7 deletions packages/app/src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,43 @@
import {
CoreSdkProvider,
ErrorBoundary,
MetaTags,
PageSkeleton,
TokenProvider,
createApp
} from '@commercelayer/app-elements'
import '@commercelayer/app-elements/style.css'
import React from 'react'
import ReactDOM from 'react-dom/client'
import { StrictMode } from 'react'
import { SWRConfig } from 'swr'
import { App } from './App'

// eslint-disable-next-line @typescript-eslint/non-nullable-type-assertion-style
ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
<React.StrictMode>
<App />
</React.StrictMode>
const isDev = Boolean(import.meta.env.DEV)

createApp(
(props) => (
<StrictMode>
<ErrorBoundary hasContainer>
<SWRConfig
value={{
revalidateOnFocus: false
}}
>
<TokenProvider
kind='inventory'
appSlug='inventory'
devMode={isDev}
reauthenticateOnInvalidAuth={!isDev && props?.onInvalidAuth == null}
loadingElement={<PageSkeleton />}
{...props}
>
<CoreSdkProvider>
<MetaTags />
<App routerBase={props?.routerBase} />
</CoreSdkProvider>
</TokenProvider>
</SWRConfig>
</ErrorBoundary>
</StrictMode>
),
'inventory'
)
Loading
Loading