0.6.9 • Published 3 years ago

@financial-times/tc-ui v0.6.9

Weekly downloads
83
License
ISC
Repository
github
Last release
3 years ago

tc-ui

User interface for administering Treecreeper data. It provides:

  • CMS style view and edit pages
  • primitive components, that can be combined by users how they wish

Limitations

At present the UI is tightly coupled to the FT's origami build service. The plan is to move towards a self-contained implementation, but for the time being it's not possible

API

getCMS(options)

This returns an object containing 3 handlers: { viewHandler, deleteHandler, editHandler }

Each handler accepts input, and returns output, of similar structure to tc-api-rest-handlers:

input

An object with the following properties

{
  type, // the type of record to view/edit
  code, // the code of the record to view/edit
  metadata: // object similar to the metadata objects used by `tc-api-rest-handlers`
  query: req.query || {}, // query string of the request, parsed into an object
  method: req.method, // http method used for the request
  body: req.body, // body sent with the request
}

Output

A {status, body, headers} object

Options

logger

Choice of logger to use

restApiUrl

Base url for the treecreeper REST api

graphqlApiUrl

Base url for the treecreeper GraphQL api

clientId

String to send to the API as the Client-Id header

apiHeaders

Either:

  • Function that is passed the object received as input, and should return an object containing additional headers to use when calling the treecreeper endpoints e.g. Api-Key, Client-User-Id.
  • An object to be use directly as the headers
Subheader

Optional React component for rendering a subheader when viewing records. It is passed all the props used to render the page, and is useful for e.g. rendering links to related resources

customComponents

Array of primitive components to use for rendering types of data, e.g. you may want to render a StarRating type using a different component to a normal numeric field. (see the section on Primitive Components below to see what structure each one should be in)

customTypeMappings

Object defining which components should be used to render which types e.g.

{
    Paragraph: 'LargeText',
    Temperature: 'Slider'
}
renderPage({ template, data, event = {}, status = 200 })

Function for rendering the CMS as a full page. The tc-ui package only provides the main content of the page, and logic for handling any data submitted, but this needs to be contained within a full page of the developer's own choosing and implementation. The object the function will receive contains the following properties

  • template - the React template for rendering the view/edit screen for the current record
  • data - the complete data needed to populate the screen for the current record.
  • event - the original event received by the handler
  • status - the status to respond with

It should return a {status, body, headers} object

handleError(err)

Function for handling any errors. Should ideally return a {status, body, headers} object in orer to present the user with a friendly error message

Client side code

Styling and client side interactivity are dependent on a few things:

  • If using webpack or a similar build tool that respects the browser field of package.json, import @financial-times/tc-ui in your client side bundle
  • On the server side, origamiModules: { css, js } retrieves lists of components you will need to include. Currently no mechanism is provided to automate including them - sorry

Example

See the demo directory of this repository.

Primitive components

At the heart of the CMS are a set of primitive components. These allow any property of any record to be displayed and edited in a consistent way.

tc-ui provides 6 of these by default, but the user may provide more that implement the same interface in order to provide custom views and edit tools. Those bundled in tc-ui are:

  • Boolean
  • Text
  • LargeText
  • Number
  • Relationship (a relationship which may have annotations on the edge)
  • SimpleRelationship (a relationship which has no annotations on the edge)
  • Temporal
  • Enum
  • MultipleChoice

See tc-schema-sdk/data-accessors/primitive-types.js to see which components are used for rendering which default primitive data types

The 'CMS' pages do not make use of SimpleRelationship at present - all relationships use Relationship. This is in order to simplify the CMS code. The SimpleRelationship primitives are provided with an eye to two things

  • making wider use of them to render views in the wider Biz Ops ecosystem without having to always use _rel properties in GraphQL
  • adding the ability to render @cypher driven properties in the CMS views at some point

Each primitive component constitutes of one or more of the following:

  • browser.js - Client side js needed to interact with the component (not required in most cases)
  • main.css - Client side css needed to render the component (not required in most cases)
  • server.js - The server side code to render the component

server.js will export 5 properties:

ViewComponent

The React component for rendering a view of the data. Should expect the following 2 properties:

  • id - the name of the property
  • value - the value for the property retrieved from the treecreeper api

EditComponent

The React component for rendering an editable view of the data. //TODO document the interface

hasValue(value)

Function for determnining if a property contains a real value. Defaults to whether the property's value is 'truthy' but e.g. for Boolean properties, this is not an adequate check

parser({value, hasMany})

Function for converting the value retrieved from the form POST data into a value to be sent to the treecreeper API. Defaults to a noop, but some components require that the data be preprocessed

graphqlFragment(propName, prodDef)

Function for converting the property into a fragment to put in a GraphQL query. Defaults to just outputting the field name

If all the above are defined correctly, then a new component can be used in your CMS by defining a mapping in your schema's primitive types and then passing in the component as an option when creating the CMS

0.6.9

3 years ago

0.6.7

3 years ago

0.6.8

3 years ago

0.6.6

3 years ago

0.6.5

3 years ago

0.6.5-beta.1

3 years ago

0.6.4

3 years ago

0.6.3

3 years ago

0.6.2

3 years ago

0.6.1

3 years ago

0.6.0-beta.0

3 years ago

0.5.35

3 years ago

0.5.34

3 years ago

0.5.34-beta.2

3 years ago

0.5.33

3 years ago

0.5.33-beta.1

3 years ago

0.5.32

3 years ago

0.5.32-beta.1

3 years ago

0.5.30

3 years ago

0.5.31

3 years ago

0.5.29

3 years ago

0.5.28

3 years ago

0.5.27

4 years ago

0.5.26

4 years ago

0.5.26-beta.1

4 years ago

0.5.25

4 years ago

0.5.24

4 years ago

0.5.23

4 years ago

0.5.22

4 years ago

0.5.21

4 years ago

0.5.20

4 years ago

0.5.18

4 years ago

0.5.16

4 years ago

0.5.17

4 years ago

0.5.15

4 years ago

0.5.13

4 years ago

0.5.11

4 years ago

0.5.12

4 years ago

0.5.10

4 years ago

0.5.9

4 years ago

0.5.8

4 years ago

0.5.7

4 years ago

0.5.6

4 years ago

0.5.5

4 years ago

0.5.4

4 years ago

0.5.3

4 years ago

0.5.2

4 years ago

0.5.1

4 years ago

0.5.0

4 years ago

0.4.9

4 years ago

0.4.8

4 years ago

0.4.10-beta.1

4 years ago

0.4.7

4 years ago

0.4.6

4 years ago

0.4.5

4 years ago

0.4.4

4 years ago

0.4.3

4 years ago

0.4.2

4 years ago

0.4.1

4 years ago

0.4.0

4 years ago

0.4.0-beta.3

4 years ago

0.4.0-beta.1

4 years ago

0.4.0-beta.2

4 years ago

0.3.11

4 years ago

0.3.10

4 years ago

0.3.9

4 years ago

0.3.8

4 years ago

0.3.6

4 years ago

0.3.7

4 years ago

0.3.5

4 years ago

0.3.4

4 years ago

0.3.3

4 years ago

0.3.2

4 years ago

0.3.1

4 years ago

0.3.0-beta.14

4 years ago

0.3.0

4 years ago

0.3.0-beta.13

4 years ago

0.3.0-beta.12

4 years ago

0.3.0-beta.11

4 years ago

0.3.0-beta.10

4 years ago

0.3.0-beta.3

4 years ago

0.3.0-beta.4

4 years ago

0.3.0-beta.5

4 years ago

0.3.0-beta.8

4 years ago

0.3.0-beta.9

4 years ago

0.3.0-beta.2

4 years ago