oc-template-react-compiler 6.0.2 → 6.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,4 @@
1
- export default function reactOCProviderTemplate({ viewPath }: {
1
+ export default function reactOCProviderTemplate({ viewPath, providerFunctions }: {
2
2
  viewPath: string;
3
+ providerFunctions: string;
3
4
  }): string;
@@ -1,53 +1,29 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const removeExtension = (path) => path.replace(/\.(t|j)sx?$/, '');
4
- function reactOCProviderTemplate({ viewPath }) {
4
+ function reactOCProviderTemplate({ viewPath, providerFunctions }) {
5
5
  return `
6
6
  import React from 'react';
7
7
  import View from '${removeExtension(viewPath)}';
8
8
  import { DataProvider } from 'oc-template-react-compiler/dist/utils/useData'
9
9
 
10
- class OCProvider extends React.Component {
11
- componentDidMount(){
12
- const { _staticPath, _baseUrl, _componentName, _componentVersion, ...rest } = (this.props as any);
13
- (window as any).oc.events.fire('oc:componentDidMount', rest);
14
- }
10
+ function OCProvider(props) {
11
+ const { _staticPath, _baseUrl, _componentName, _componentVersion, ...rest } = props;
15
12
 
16
- getData(providerProps: any, parameters: any, cb: (error: any, parameters?: any, props?: any) => void) {
17
- return (window as any).oc.getData({
18
- name: providerProps._componentName,
19
- version: providerProps._componentVersion,
20
- baseUrl: providerProps._baseUrl,
21
- parameters
22
- }, (err: any, data: any) => {
23
- if (err) {
24
- return cb(err);
25
- }
26
- const { _staticPath, _baseUrl, _componentName, _componentVersion, ...rest } = (data.component.props as any);
27
- cb(null, rest, data.component.props);
28
- });
29
- }
13
+ React.useEffect(() => {
14
+ window.oc.events.fire('oc:componentDidMount', rest);
15
+ }, []);
30
16
 
31
- getSetting(providerProps: any, setting: string) {
32
- const settingHash = {
33
- name: providerProps._componentName,
34
- version: providerProps._componentVersion,
35
- baseUrl: providerProps._baseUrl,
36
- staticPath: providerProps._staticPath
37
- };
38
- return (settingHash as any)[setting];
39
- }
17
+ ${providerFunctions}
18
+
19
+ rest.getData = getData;
20
+ rest.getSetting = getSetting;
40
21
 
41
- render() {
42
- const { _staticPath, _baseUrl, _componentName, _componentVersion, ...rest } = (this.props as any);
43
- (rest as any).getData = (parameters: any, cb: (error: any, parameters?: any, props?: any) => void) => this.getData(this.props, parameters, cb);
44
- (rest as any).getSetting = (setting: string) => this.getSetting(this.props, setting);
45
- return (
46
- <DataProvider value={{...rest}}>
47
- <View { ...rest } />
48
- </DataProvider>
49
- );
50
- }
22
+ return (
23
+ <DataProvider value={{...rest}}>
24
+ <View { ...rest } />
25
+ </DataProvider>
26
+ );
51
27
  }
52
28
 
53
29
  function renderer(props, element, ssr) {
@@ -66,6 +42,8 @@ function reactOCProviderTemplate({ viewPath }) {
66
42
  }
67
43
  }
68
44
 
45
+ renderer.component = OCProvider;
46
+
69
47
  export default renderer;
70
48
  `;
71
49
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oc-template-react-compiler",
3
- "version": "6.0.2",
3
+ "version": "6.0.4",
4
4
  "description": "Compiler for the React OC template",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -30,8 +30,8 @@
30
30
  "license": "MIT",
31
31
  "dependencies": {
32
32
  "@vitejs/plugin-react": "^4.0.0",
33
- "oc-template-react": "^4.0.0",
34
- "oc-vite-compiler": "^3.2.0",
33
+ "oc-template-react": "^4.0.1",
34
+ "oc-vite-compiler": "^3.4.0",
35
35
  "react": "18.2.0",
36
36
  "react-dom": "18.2.0"
37
37
  },
@@ -35,7 +35,7 @@
35
35
  "@types/react": "^18.0.28",
36
36
  "jsdom": "^21.1.1",
37
37
  "oc-server": "*",
38
- "oc-template-react-compiler": "6.0.0",
38
+ "oc-template-react-compiler": "^6.0.0",
39
39
  "react": "18.2.0",
40
40
  "react-dom": "18.2.0",
41
41
  "typescript": "5.1.3",
@@ -1,23 +1,21 @@
1
1
  import { useState } from 'react';
2
- import { useData } from 'oc-template-react-compiler/dist/utils/useData';
3
- import { serverClient } from 'oc-server';
2
+ import { serverClient, InitialData, ActionOutput } from 'oc-server';
4
3
  import styles from './styles.css';
5
4
  import logo from '../public/logo.png';
6
- import type { AdditionalData, ClientProps } from './types';
7
5
 
8
- interface AppProps extends ClientProps {
9
- getMoreData?: boolean;
10
- }
6
+ type AdditionalData = ActionOutput<'getMoreData'>;
11
7
 
12
- const App: React.FC<ClientProps> = () => {
13
- const { firstName, lastName, userId, getData } = useData<AppProps>();
14
- const [additionalData, setAdditionalData] = useState<AdditionalData | null>(null);
8
+ const App: React.FC<InitialData> = (props: InitialData) => {
9
+ const { firstName, lastName, born, hobbies } = props;
10
+ const [additionalData, setAdditionalData] = useState<AdditionalData | null>(
11
+ null
12
+ );
15
13
  const [error, setError] = useState('');
16
14
 
17
15
  const fetchMoreData = async () => {
18
16
  setError('');
19
17
  try {
20
- const data = await serverClient.getMoreData({ userId });
18
+ const data = await serverClient.getMoreData({ year: born });
21
19
  setAdditionalData(data);
22
20
  } catch (err) {
23
21
  setError(String(err));
@@ -32,18 +30,18 @@ const App: React.FC<ClientProps> = () => {
32
30
  <div className={styles.container}>
33
31
  <img width="50" height="50" src={logo} alt="Logo" />
34
32
  <h1 style={{ margin: '0 0 20px 0' }}>
35
- Hello, <span style={{ textDecoration: 'underline' }}>{firstName}</span> {lastName}
33
+ Hello, <span style={{ textDecoration: 'underline' }}>{firstName}</span>{' '}
34
+ {lastName}
36
35
  </h1>
37
- {additionalData && (
38
- <div className={styles.info}>
39
- <div className={styles.block}>Age: {additionalData.age}</div>
40
- <div className={styles.block}>
41
- Hobbies: {additionalData.hobbies.map((x) => x.toLowerCase()).join(', ')}
42
- </div>
36
+ <div className={styles.info}>
37
+ <div className={styles.block}>Born: {born}</div>
38
+ <div className={styles.block}>
39
+ Hobbies: {hobbies.map((x) => x.toLowerCase()).join(', ')}
43
40
  </div>
44
- )}
41
+ </div>
42
+ {additionalData && <div>{additionalData.funFact}</div>}
45
43
  <button className={styles.button} onClick={fetchMoreData}>
46
- Get extra information
44
+ Fun year fact
47
45
  </button>
48
46
  </div>
49
47
  );
@@ -1,32 +1,45 @@
1
1
  import { Server } from 'oc-server';
2
- import { AdditionalData, ClientProps, OcParameters } from './types';
3
2
 
4
- const database = [
5
- { name: 'John Doe', age: 34, hobbies: ['Swimming', 'Basketball'] },
6
- { name: 'Jane Doe', age: 35, hobbies: ['Running', 'Rugby'] }
3
+ const userDatabase = [
4
+ { name: 'John Doe', born: 1986, hobbies: ['Swimming', 'Basketball'] },
5
+ { name: 'Jane Doe', born: 1991, hobbies: ['Running', 'Rugby'] },
7
6
  ];
8
7
 
8
+ const yearsFunFactDatabase: Record<number, string> = {
9
+ 1986: "Halley's Comet made its closest approach to Earth in 1986, visible to the naked eye.",
10
+ 1987: 'The first Final Fantasy game was released in 1987.',
11
+ 1991: 'The first web page was created in 1991 by Tim Berners-Lee.',
12
+ };
13
+
9
14
  async function getUser(userId: number) {
10
- return database[userId];
15
+ return userDatabase[userId];
16
+ }
17
+
18
+ async function getFunFact(year: number) {
19
+ return yearsFunFactDatabase[year];
11
20
  }
12
21
 
13
- export const server = new Server(async (params: OcParameters): Promise<ClientProps> => {
22
+ export const server = new Server(async (params: { userId: number }) => {
14
23
  const { userId } = params;
15
24
  const user = await getUser(userId);
16
25
  const [firstName, lastName] = user.name.split(/\s+/);
17
26
 
27
+ if (firstName === 'Invalid') {
28
+ return;
29
+ }
30
+
18
31
  return {
19
- userId,
20
32
  firstName,
21
- lastName
33
+ lastName,
34
+ born: user.born,
35
+ hobbies: user.hobbies,
22
36
  };
23
- }).action('getMoreData', async (params: OcParameters): Promise<AdditionalData> => {
24
- const { userId } = params;
25
- const user = await getUser(userId);
37
+ }).action('getMoreData', async (params: { year: number }) => {
38
+ const { year } = params;
39
+ const funFact = await getFunFact(year);
26
40
 
27
41
  return {
28
- age: user.age,
29
- hobbies: user.hobbies
42
+ funFact,
30
43
  };
31
44
  });
32
45