1.0.0-beta29.2-middleware-1 • Published 7 years ago

link-redux v1.0.0-beta29.2-middleware-1

Weekly downloads
50
License
LGPL-3.0
Repository
github
Last release
7 years ago

Link Redux

A Link to the Web

Build Status Read the Docs npm version npm type definitions

Use this to enable Link in React. It provides components to build data-independent semantic applications for the human consumption of linked data with React and Redux.

To transform your Rails application into a linked-data serving beast, see our Active Model Serializers plugin.

This was built at Argu, if you like what we do, these technologies or open data, send us a mail.

Installation

npm install link-lib link-redux rdflib V yarn add link-lib link-redux rdflib

Demo

See the TODO app for a live example and the source for the implementation. Mind that it isn't connected to a back-end, so it's only a demo for the view rendering mechanism.

Road to stable

We're busy working towards a stable version of link lib/redux, since there are a lot of changes, the documentation might be outdated, if behaviour doesn't seem consistent take a look at the tests or create an issue.

Basic Usage

1. Set up the store

Create a helper to set up an instance of the LinkedRenderStore to use in your application;

// LRS.js
import LinkedRenderStore, { memoizedNamespace } from 'link-lib';
import { FRONTEND_URL } from './config';

const LRS = new LinkedRenderStore();

/**
 * If the app is programmed for a specific backend, it's useful to add it to the namespaces so that
 * it can be used for (hard-coded) entry points.
 */
LRS.namespaces.api = memoizedNamespace(FRONTEND_URL);

/**
 * Set up your own namespace for (virtual) app-specific properties (this might be the same as the
 * api). These SHOULD NOT be shared and have app-specific semantics.
 * 
 * Virtual properties are useful for creating behaviour separate from data-source, while still using
 * the same interface.
 */
LRS.namespaces.app = memoizedNamespace(FRONTEND_URL);

export const NS = LRS.namespaces;

/**
* It's useful to have a central source of valid application topologies. This also provides a 
* location to document the intended usage.
*
* A common issue when beginning with link is forgetting to set the correct topology(ies), so 
* defaulting to registering views under all topologies can prevent a lot of headaches. Mind that
* over-registering might cause the wrong view to be rendered rather than none at all.
*/
export const allTopologies = [
  // This defaults to the `DEFAULT_TOPOLOGY` from link-lib
  // Generally used to mean that the resource is the main content on the page.
  undefined,
  // The resource/property is rendered within the navigation menu (e.g. as a `li`)
  NS.app('navigation'),
  // The resource/prop is rendered in a table (e.g. as a single row within the table).
  NS.app('table'),
  // The resource/prop is rendered in a row (e.g. as a single cell within the row).
  NS.app('row'),
  // The resource/prop is rendered in a cell (e.g. as a raw value or some small representation).
  NS.app('cell'),
];

export default LRS;

2. Add some views

Write some views to render resources:

import LinkedRenderStore, { RENDER_CLASS_NAME } from 'link-lib';
import { link, Property, LinkedResourceContainer } from 'link-redux';
import React from 'react';
import { Link } from 'react-router-dom';
import FontAwesome from 'react-fontawesome';

import { NS } from './LRS';

const Thing = ({ creator, text }) => (
  <div>
    <Property label={NS.schema('name')}/>
    <p>{text.value}</p>
    <LinkedResourceContainer subject={creator} />
  </div>
);

// The `subject` property is always given to rendered views so it can link to itself.
const ThingNavigation = ({ name, subject }) => (
  <li>
    <Link to={subject.value}>
      {name.value}
    </Link>
  </li>
);

// Properties without being connected via the `link` function recieve their prop's value via `linkedProp`
const ThingName = ({ linkedProp }) => <h1>{linkedProp.value}</h1>;

// We want to show whether it's the users' birthday
const PersonName = ({ birthDay, name }) => {
  const birthdayIcon = isToday(birthDay) ? <FontAwesome name="birthday-cake" /> : null;
  
  return <h1>{name}{birthdayIcon}</h1>;
}
export default [
  LinkedRenderStore.registerRenderer(
    link([NS.schema('name'), NS.schema('text'), NS.schema('creator')])(Thing),
    NS.schema('Thing')
  ),
  LinkedRenderStore.registerRenderer(
    link([NS.schema('name')])(ThingNavigation),
    NS.schema('Thing'),
    RENDER_CLASS_NAME, // We want to register a type renderer (name subject to change)
    NS.app('navigation')
  ),
  LinkedRenderStore.registerRenderer(
    ThingName,
    NS.schema('Thing'),
    NS.schema('name')
  ),
  LinkedRenderStore.registerRenderer(
    link(
      [NS.schema('name'), NS.schema('birthDate')],
      { returnType: 'value' } // We can adjust whether we get passed (string) values, Nodes, Statements.
    )(PersonName),
    NS.schema('Person'),
    NS.schema('name')
  ),
];

3. Wire the views into the store

Register the views so link can resolve them when the data comes in;

import LRS from './LRS';

import Thing from './views';

/**
* Notice the spread (...) operator, this is because the (default) export of `./views` returns an
* array rather than a single ComponentRegistration array (returned by 
* LinkedRenderStore.registerRenderer), but `registerAll` doesn't handle deeply nested arrays, so
* they should be spread either here, or in the views.
* 
* CAUTION: This is the LinkedRenderStore app *instance*, not class from `link-lib`.
*/
LRS.registerAll(
  ...Thing,
);

4. Enable the store

Now that we have views, lets enable link in the React tree with our LinkedRenderStore instance:

import { RenderStoreProvider } from 'link-redux';
import { withRouter } from 'react-router';
import configureReduxStore from './configureStore';
import LinkedRenderStore from './LRS';

// Either with react-router, or without and just take the current location (but listen for pushstate).
const App = ReactRouter
  ? withRouter(() => <LinkedResourceContainer subject={} />)         
  : () => <LinkedResourceContainer subject={namedNodeByIRI(window.location.href)} />;

export default () => (
  <Provider store={configureReduxStore()}>
    <RenderStoreProvider value={LinkedRenderStore}>
      <Router history={example}>
        <App /> 
      </Router>
    </RenderStoreProvider>
  </Provider>
);

5. Start the application

It should render (and fetch) resources passed as subject to a LinkedResourceContainer (The type of subject MUST be a NamedNode instance, e.g. NS.api('todos/5')).

Further usage

String IRI to NamedNode

It sometimes happens that you recieve an IRI in string form (e.g. window.location.href) which needs to be converted to a link-enabled NamedNode, see the namedNodeByIRI function;

Multi-IRI

Most components wouldn't be very useful if they can only render one type of term. Therefore, most of the methods accept an array of terms as well:

LinkedRenderStore.registerRenderer(Thing, [NS.schema('Thing'), NS.owl('Thing')]);
<Property label={[NS.schema('name'), NS.rdfs('label')]} />
3.0.0-116fe7bd

3 years ago

4.0.0-29c4944b

4 years ago

4.0.0-8d08586a

4 years ago

4.0.0-116fe7bd

3 years ago

4.0.0-b47b8e2b

4 years ago

4.0.0-ff427c1c

3 years ago

4.0.0-904aabea

4 years ago

4.0.0-37b23a4e

4 years ago

4.0.0-95f9a178

4 years ago

4.0.0-28c25352

4 years ago

0.0.0-6a1c36e8

4 years ago

0.0.0-79e21553

4 years ago

4.0.0-e79bf920

4 years ago

0.0.0-5493c69d

4 years ago

3.0.0-30

4 years ago

0.0.0-8994e43e

4 years ago

0.0.0-d9f07de1

4 years ago

0.0.0-f4796fd0

4 years ago

0.0.0-53b3c05e

4 years ago

0.0.0-54fdcd16

4 years ago

0.0.0-3442dc4b

4 years ago

3.0.0-28

4 years ago

3.0.0-29

4 years ago

3.0.0-26

4 years ago

3.0.0-27

4 years ago

3.0.0-24

4 years ago

3.0.0-25

4 years ago

3.0.0-22

4 years ago

3.0.0-23

4 years ago

3.0.0-20

4 years ago

3.0.0-21

4 years ago

3.0.0-7

4 years ago

3.0.0-6

4 years ago

3.0.0-9

4 years ago

3.0.0-8

4 years ago

3.0.0-3

4 years ago

3.0.0-5

4 years ago

3.0.0-4

4 years ago

3.0.0-19

4 years ago

3.0.0-17

4 years ago

3.0.0-18

4 years ago

3.0.0-15

4 years ago

3.0.0-16

4 years ago

3.0.0-13

4 years ago

3.0.0-14

4 years ago

3.0.0-11

4 years ago

3.0.0-12

4 years ago

3.0.0-10

4 years ago

3.0.0-2

4 years ago

3.0.0-alpha-1

4 years ago

2.1.2

4 years ago

2.1.1

4 years ago

2.1.0

5 years ago

0.0.0-380164d2

5 years ago

0.0.0-c4431892

5 years ago

0.0.0-0452814a

5 years ago

0.0.0-f304f8d8

5 years ago

0.0.0-1db1e3f5

5 years ago

0.0.0-f52a7539

5 years ago

0.0.0-44bc30c1

5 years ago

0.0.0-d2777515

5 years ago

0.0.0-83f678454

5 years ago

0.0.0-2f8a61fd

5 years ago

0.0.0-356e1799

5 years ago

0.0.0-4494c454

5 years ago

2.0.0

6 years ago

2.0.0-6

6 years ago

2.0.0-5

6 years ago

2.0.0-4

6 years ago

2.0.0-3

6 years ago

2.0.0-plain.2

6 years ago

2.0.0-2

6 years ago

2.0.0-1

6 years ago

2.0.0-0

6 years ago

1.0.0-rc4.8

6 years ago

1.0.0-rc4.7

6 years ago

1.0.0-rc4.6

6 years ago

1.0.0-rc4

6 years ago

1.0.0-rc4.5

6 years ago

1.0.0-rc4.4

6 years ago

1.0.0-rc4.3

6 years ago

1.0.0-rc4.2

6 years ago

1.0.0-rc4.1

6 years ago

1.0.0-rc3

7 years ago

1.0.0-rc2

7 years ago

1.0.0-rc1

7 years ago

1.0.0-beta33

7 years ago

1.0.0-beta32

7 years ago

1.0.0-beta31-rdx

7 years ago

1.0.0-beta31.16

7 years ago

1.0.0-beta31.15

7 years ago

1.0.0-beta31.14

7 years ago

1.0.0-beta31.13

7 years ago

1.0.0-beta31.12

7 years ago

1.0.0-beta31.11

7 years ago

1.0.0-beta31.10

7 years ago

1.0.0-beta31.9

7 years ago

1.0.0-beta31.8

7 years ago

1.0.0-beta31.7

7 years ago

1.0.0-beta31.6

7 years ago

1.0.0-beta31.5

7 years ago

1.0.0-beta31.4

7 years ago

1.0.0-beta31.3

7 years ago

1.0.0-beta31.2

7 years ago

1.0.0-beta31.1

7 years ago

1.0.0-beta31

7 years ago

1.0.0-beta30.10

7 years ago

1.0.0-beta30.9

7 years ago

1.0.0-beta30.8

7 years ago

1.0.0-beta30.7

7 years ago

1.0.0-beta30.6

7 years ago

1.0.0-beta30.5

7 years ago

1.0.0-beta30.4

7 years ago

1.0.0-beta30.3

7 years ago

1.0.0-beta30.2

7 years ago

1.0.0-beta30.1

7 years ago

1.0.0-beta30

7 years ago

1.0.0-beta29.2

7 years ago

1.0.0-beta29.1

7 years ago

1.0.0-beta29

7 years ago

1.0.0-beta28.48

7 years ago

1.0.0-beta28.47

8 years ago

1.0.0-beta28.46

8 years ago

1.0.0-beta28.45

8 years ago

1.0.0-beta28.44

8 years ago

1.0.0-beta28.43

8 years ago

1.0.0-beta28.42

8 years ago

1.0.0-beta28.41

8 years ago

1.0.0-beta28.40

8 years ago

1.0.0-beta28.39

8 years ago

1.0.0-beta28.38

8 years ago

1.0.0-beta28.37

8 years ago

1.0.0-beta28.36

8 years ago

1.0.0-beta28.35

8 years ago

1.0.0-beta28.34

8 years ago

1.0.0-beta28.33

8 years ago

1.0.0-beta28.32

8 years ago

1.0.0-beta28.31

8 years ago

1.0.0-beta28.30

8 years ago

1.0.0-beta28.29

8 years ago

1.0.0-beta28.28

8 years ago

1.0.0-beta28.27

8 years ago

1.0.0-beta28.26

8 years ago

1.0.0-beta28.25

8 years ago

1.0.0-beta28.24

8 years ago

1.0.0-beta28.23

8 years ago

1.0.0-beta28.22

8 years ago

1.0.0-beta28.21

8 years ago

1.0.0-beta28.20

8 years ago

1.0.0-beta28.19

8 years ago

1.0.0-beta28.18

8 years ago

1.0.0-beta28.17

8 years ago

1.0.0-beta28.16

8 years ago

1.0.0-beta28.15

8 years ago

1.0.0-beta28.14

8 years ago

1.0.0-beta28.13

8 years ago

1.0.0-beta28.12

8 years ago

1.0.0-beta28.11

8 years ago

1.0.0-beta28.10

8 years ago

1.0.0-beta28.9

8 years ago

1.0.0-beta28.8

8 years ago

1.0.0-beta28.7

8 years ago

1.0.0-beta28.6

8 years ago

1.0.0-beta28.5

8 years ago

1.0.0-beta28.4

8 years ago

1.0.0-beta28.3

8 years ago

1.0.0-beta28.2

8 years ago

1.0.0-beta28.1

8 years ago

1.0.0-beta28

8 years ago

1.0.0-beta27.11

8 years ago

1.0.0-beta27.10

8 years ago

1.0.0-beta27.9

8 years ago

1.0.0-beta27.8

8 years ago

1.0.0-beta27.7

8 years ago

1.0.0-beta27.6

8 years ago

1.0.0-beta27.5

8 years ago

1.0.0-beta27.4

8 years ago

1.0.0-beta27.3

8 years ago

1.0.0-beta27.2

8 years ago

1.0.0-beta27.1

8 years ago

1.0.0-beta27

8 years ago

1.0.0-beta26.11

8 years ago

1.0.0-beta26.10

8 years ago

1.0.0-beta26.9

8 years ago

1.0.0-beta26.8

8 years ago

1.0.0-beta26.7

8 years ago

1.0.0-beta26.6

8 years ago

1.0.0-beta26.5

8 years ago

1.0.0-beta26.4

8 years ago

1.0.0-beta26.3

8 years ago

1.0.0-beta26.2

8 years ago

1.0.0-beta26.1

8 years ago

1.0.0-beta26

8 years ago

1.0.0-beta25

8 years ago

1.0.0-beta24

8 years ago

1.0.0-beta23

8 years ago

1.0.0-beta22

8 years ago

1.0.0-beta21

8 years ago

1.0.0-beta20

8 years ago

1.0.0-beta19

8 years ago

1.0.0-beta18

8 years ago

1.0.0-beta17

8 years ago

1.0.0-beta16

8 years ago

1.0.0-beta15

8 years ago

1.0.0-beta14

8 years ago

1.0.0-beta13

8 years ago

1.0.0-beta12

8 years ago

1.0.0-beta11

8 years ago

1.0.0-beta10

8 years ago

1.0.0-beta9

8 years ago

1.0.0-beta8

8 years ago

1.0.0-beta7

8 years ago

1.0.0-beta6

8 years ago

1.0.0-beta5

8 years ago

1.0.0-beta4

8 years ago

1.0.0-beta3

8 years ago

1.0.0-beta2

8 years ago

1.0.0-beta1

8 years ago

0.2.1

9 years ago

0.2.0

9 years ago

0.1.2

9 years ago

0.1.2-alpha.1

9 years ago

0.1.1

9 years ago

0.1.0

9 years ago