Skip to content

Commit

Permalink
DAMO-214|Created root README, fixed editUrl bug, minor fixes on img s…
Browse files Browse the repository at this point in the history
…election
  • Loading branch information
Marco Monti authored and l-besenyei committed Sep 10, 2024
1 parent b05573b commit 018e740
Show file tree
Hide file tree
Showing 16 changed files with 138 additions and 19 deletions.
115 changes: 115 additions & 0 deletions react_widgets/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# React Widgets Monorepo

This is a monorepo containing multiple React widgets and a shared library. The project is managed using [npm workspaces](https://docs.npmjs.com/cli/v7/using-npm/workspaces) for easier dependency management and build processes across multiple packages.

## Workspace Structure

- **apps/damo_upload**: A React widget for uploading images to DAMopen.
- **apps/damo_wfa**: A React widget for managing approval process for images on DAMopen.
- **apps/shared**: A shared library containing helper functions, types, and reusable components.

## Setup

To set up the workspace and install dependencies, run the following command from the root location:

```bash
npm install
```

## Development

Since drupal libraries track built files, to be able to run the application in develoment mode, you should build the application every time
a change is made. For this, a watcher is configured to build files accordingly. Please run the corresponding `watch` command as you need it:

Ex:

```bash
npm run build:watch:damo_upload
```

## Building the projects

To build all the workspaces at once, please run:

```bash
npm run build-all
```

## Linting

To run ESLint on all workspaces, please run:

```bash
npm run lint-all
```

## Formatting

To check if the code is properly formatted according to `prettier` across all workspaces:

```bash
npm run format:check-all
```

To automatically fix formatting issues for all workspaces:

```bash
npm run format:fix-all
```

## Vite configuration

Each react application is built using [Vite](https://vitejs.dev/). Each app has its own configuration file.

## Chakra UI
The project uses Chakra UI as a component library. Chakra UI is installed globally in the workspace and can be imported and used within any widget.


## Adding new widgets

1. Create a new folder under `apps/` with the widget's name.
2. Create the React application using [Vite](https://vitejs.dev/) and modify the `vite.config.ts` as necessary.
Please bear in mind to use Typescript to help maintain the structure of the project.
3. Create the new `build-watch` command in the root `package.json` file:

```json
"scripts": {
"build:watch:{your-new-widget-name}": "npm run build-watch --workspace=apps/{your-new-widget-name}",
},
```

4. Install dependencies for the new widget if necessary. If they are widget specific you can do it within the new react widget itself.
Otherwise you should install it on the root so it's available to share across all widgets.

5. Make sure to modify the `damo_theme.libraries.yml` file to add the paths to the built files for the new widget.

Ex:

```yml
{your-new-widget-name}:
css:
theme:
react_widgets/apps/{your-new-widget-name}/dist/assets/index.css: {}
js:
react_widgets/apps/{your-new-widget-name}/dist/assets/index.js: {}
```
and attach the library on the corresponding `twig` template

```twig
{{ attach_library('damo_theme/{your-new-widget-name}') }}
```

6. Remember to create an entry point for the react application to be embedded within the Drupal site.

Ex:

```tsx
// main.tsx
createRoot(document.getElementById('---ID of the widget entry point --- ')!).render(
<StrictMode>
<App />
</StrictMode>
);
```

2 changes: 1 addition & 1 deletion react_widgets/apps/damo_upload/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default {
project: ['./tsconfig.json', './tsconfig.node.json', './tsconfig.app.json'],
tsconfigRootDir: __dirname,
},
}
};
```

- Replace `plugin:@typescript-eslint/recommended` to `plugin:@typescript-eslint/recommended-type-checked` or `plugin:@typescript-eslint/strict-type-checked`
Expand Down
2 changes: 1 addition & 1 deletion react_widgets/apps/damo_upload/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"scripts": {
"dev": "vite",
"build": "tsc -b && vite build",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"format": "prettier --write .",
"format-check": "prettier --check .",
"preview": "vite preview",
"build-watch": "vite build --watch"
},
Expand Down
2 changes: 1 addition & 1 deletion react_widgets/apps/damo_upload/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default defineConfig({
resolve: {
alias: {
'@shared': path.resolve(__dirname, '../shared/src'),
}
},
},
build: {
rollupOptions: {
Expand Down
6 changes: 3 additions & 3 deletions react_widgets/apps/damo_wfa/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default tseslint.config({
tsconfigRootDir: import.meta.dirname,
},
},
})
});
```

- Replace `tseslint.configs.recommended` to `tseslint.configs.recommendedTypeChecked` or `tseslint.configs.strictTypeChecked`
Expand All @@ -31,7 +31,7 @@ export default tseslint.config({

```js
// eslint.config.js
import react from 'eslint-plugin-react'
import react from 'eslint-plugin-react';

export default tseslint.config({
// Set the react version
Expand All @@ -46,5 +46,5 @@ export default tseslint.config({
...react.configs.recommended.rules,
...react.configs['jsx-runtime'].rules,
},
})
});
```
4 changes: 2 additions & 2 deletions react_widgets/apps/damo_wfa/dist/assets/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion react_widgets/apps/damo_wfa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"scripts": {
"dev": "vite",
"build": "tsc -b && vite build",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"format": "prettier --write .",
"format-check": "prettier --check .",
"preview": "vite preview",
"build-watch": "vite build --watch"
},
Expand Down
4 changes: 2 additions & 2 deletions react_widgets/apps/damo_wfa/src/components/CardPopover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export const CardPopover = ({ open, fileInfo }: CardPopoverProps) => {
<Text as="span" textDecor="underline">
{cat.name}
</Text>
{index < fileInfo.categories?.length! - 1 && ', '}
{index < fileInfo.categories!.length - 1 && ', '}
</Fragment>
))}
</Text>
Expand Down Expand Up @@ -107,7 +107,7 @@ export const CardPopover = ({ open, fileInfo }: CardPopoverProps) => {
fontWeight="normal"
onClick={(e) => {
e.stopPropagation();
window.location.assign(fileInfo.file.editUrl);
window.location.assign(fileInfo.editUrl);
}}
>
Edit asset
Expand Down
3 changes: 2 additions & 1 deletion react_widgets/apps/damo_wfa/src/hooks/useImgSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export const useImgSelection = () => {
})
);
setSelectedImgs([]);
setShowSelectedOnly(false);

setIsUpdating(false);
},
Expand All @@ -90,7 +91,7 @@ export const useImgSelection = () => {
};

fetchImages();
}, []);
}, [toast]);

const displayedImages = useMemo(() => {
return showSelectedOnly ? selectedImgs : images;
Expand Down
2 changes: 1 addition & 1 deletion react_widgets/apps/damo_wfa/src/services/deleteImgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const deleteImgs = async (files: MediaImage[]) => {
throw new Error('No CSRF token found');
}

let deleteResults = [];
const deleteResults = [];

for (const file of files) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const getUnpublishedImgs = async () => {
.addFields(resourceType, [
'name',
'status',
'drupal_internal__mid',
'field_category',
'field_keywords',
'field_image',
Expand Down
2 changes: 1 addition & 1 deletion react_widgets/apps/damo_wfa/src/services/updateImgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const updateImgs = async (files: MediaImage[]) => {
throw new Error('No CSRF token found');
}

let updateResults = [];
const updateResults = [];

for (const file of files) {
const updatedMediaEntity = {
Expand Down
2 changes: 1 addition & 1 deletion react_widgets/apps/shared/src/utils/typeMappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export const mapFile = (data: TJsonaModel) => {
fileSize: data.filesize,
url: data.image_style_uri?.medium || null,
alt: data.resourceIdObjMeta?.alt || null,
editUrl: `/media/${data.resourceIdObjMeta?.drupal_internal__target_id}/edit?destination=/admin/assets/all-unpublished`,
};
return file;
};
Expand All @@ -48,6 +47,7 @@ export const mapMediaImage = (data: TJsonaModel) => {
? data.field_keywords.map(mapKeyword)
: null,
file: mapFile(data.field_image),
editUrl: `/media/${data.drupal_internal__mid}/edit`,
};
return mediaImg;
};
3 changes: 1 addition & 2 deletions react_widgets/apps/shared/src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,17 @@ export interface MediaFile {
fileSize: number;
url: string | null;
alt: string | null;
editUrl: string;
}

export interface MediaImage {
id: string;
type: string;
status: boolean;
name: string;
//auhtor: not returne from drupal yet
categories: Category[] | null;
keywords: Keyword[] | null;
file: MediaFile;
editUrl: string;
}

export type ModifyImgsAction = 'approve' | 'decline';
2 changes: 1 addition & 1 deletion react_widgets/apps/shared/tsconfig.tsbuildinfo

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion react_widgets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
"scripts": {
"build-all": "npm run build --workspaces",
"build:watch:damo_upload": "npm run build-watch --workspace=apps/damo_upload",
"build:watch:damo_wfa": "npm run build-watch --workspace=apps/damo_wfa"
"build:watch:damo_wfa": "npm run build-watch --workspace=apps/damo_wfa",
"lint-all": "eslint 'apps/**/*.{ts,tsx}' --report-unused-disable-directives --max-warnings 0",
"format:check-all": "prettier --check 'apps/**/*.{js,ts,tsx,md,json}'",
"format:fix-all": "prettier --write 'apps/**/*.{js,ts,tsx,md,json}'"
},
"workspaces": [
"apps/*"
Expand Down

0 comments on commit 018e740

Please sign in to comment.