How to use the normalizr.schema.Array function in normalizr

To help you get started, we’ve selected a few normalizr examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Azure / pcs-remote-monitoring-webui / src / store / reducers / deploymentsReducer.js View on Github external
.catch(handleError(fromAction))
  },
  /** Delete deployment */
  deleteDeployment: {
    type: 'DEPLOYMENTS_DELETE',
    epic: fromAction =>
      IoTHubManagerService.deleteDeployment(fromAction.payload)
        .map(toActionCreator(redux.actions.deleteDeployment, fromAction))
        .catch(handleError(fromAction))
  }
});
// ========================= Epics - END

// ========================= Schemas - START
const deploymentSchema = new schema.Entity('deployments');
const deploymentListSchema = new schema.Array(deploymentSchema);
const deployedDevicesSchema = new schema.Entity('deployedDevices');
const deployedDevicesListSchema = new schema.Array(deployedDevicesSchema);
// ========================= Schemas - END

// ========================= Reducers - START
const initialState = { ...errorPendingInitialState, entities: {} };

const insertDeploymentReducer = (state, { payload, fromAction }) => {
  const { entities: { deployments }, result } = normalize({ ...payload, isNew: true }, deploymentSchema);
  if (state.entities && state.entities.deployments) {
    return update(state, {
      entities: { deployments: { $merge: deployments } },
      items: { $splice: [[0, 0, result]] },
      ...setPending(fromAction.type, false)
    });
  }
github commercetools / merchant-center-application-kit / playground / src / reducers / cache.js View on Github external
import omit from 'lodash/omit';
import omitBy from 'lodash/omitBy';
// eslint-disable-next-line import/named
import { normalize, schema } from 'normalizr';
import createReducer from '../utils/create-reducer';

const transitionSchema = new schema.Entity(
  'transitions',
  {},
  {
    processStrategy: entity => entity.obj || entity,
  }
);
const transitionListSchema = new schema.Array(transitionSchema);
const stateMachineSchema = new schema.Entity('stateMachine', {
  transitions: transitionListSchema,
});
const stateMachinesSchema = new schema.Array(stateMachineSchema);

export const normalizers = {
  stateMachine: data => normalize(data, stateMachineSchema),
  stateMachines: data => normalize(data, stateMachinesSchema),
};

export const actionTypes = {
  SET_STATE_MACHINE: 'SET_STATE_MACHINE',
  UNSET_STATE_MACHINE: 'UNSET_STATE_MACHINE',
  SET_STATE_MACHINES: 'SET_STATE_MACHINES',
};
github xkawi / react-universal-saga / src / services / api / index.js View on Github external
// Read more about Normalizr: https://github.com/paularmstrong/normalizr

// Schemas for Github API responses.
const userSchema = new schema.Entity('users', {}, {
  idAttribute: 'login'
});

const repoSchema = new schema.Entity('repos', {
  owner: userSchema
}, {
  idAttribute: 'fullName'
});

const userSchemaArray = new schema.Array(userSchema);
const repoSchemaArray = new schema.Array(repoSchema);

// api services
export const fetchUser = login => callApi(`users/${login}`, userSchema);
export const fetchRepo = fullName => callApi(`repos/${fullName}`, repoSchema);
export const fetchStarred = url => callApi(url, repoSchemaArray);
export const fetchStargazers = url => callApi(url, userSchemaArray);
github richardscarrott / snippets / src / api / fetch-source / fetch-source.js View on Github external
const fileSchema = new schema.Entity('files');
const dirSchema = new schema.Entity('dirs');
const contentSchema = new schema.Union(
  {
    files: fileSchema,
    dirs: dirSchema
  },
  input => (Array.isArray(input.content) ? 'dirs' : 'files')
);
dirSchema.define({
  content: [contentSchema]
});
const sourceSchema = new schema.Entity('sources', {
  content: [contentSchema]
});
export const sourceListSchema = new schema.Array(sourceSchema);

const fetchSource = async request => {
  const { api, owner, repo, path, branch } = parseGitHubUrl(request.url);
  const content = await fetchContents(
    // NOTE: We pass in the path as the name however the content is later reparented so is largely irrelevant
    path,
    api,
    request.accessToken,
    owner,
    repo,
    path,
    branch
  );
  const source = {
    ...request,
    // Source extends Dir (if only I had typescript to document this 😞) so
github redux-saga / redux-saga / examples / real-world / services / api.js View on Github external
// Schemas for Github API responses.
const userSchema = new schema.Entity('users', {
  idAttribute: 'login',
})

const repoSchema = new schema.Entity('repos', {
  idAttribute: 'fullName',
})

repoSchema.define({
  owner: userSchema,
})

const userSchemaArray = new schema.Array(userSchema)
const repoSchemaArray = new schema.Array(repoSchema)

// api services
export const fetchUser = login => callApi(`users/${login}`, userSchema)
export const fetchRepo = fullName => callApi(`repos/${fullName}`, repoSchema)
export const fetchStarred = url => callApi(url, repoSchemaArray)
export const fetchStargazers = url => callApi(url, userSchemaArray)
github gpbl / denormalizr / test / index.js View on Github external
it('should return an array of denormalized entities given an array of ids', () => {
      const denormalized = denormalize([1, 2], data.entities, new schema.Array(articleSchema));
      expect(denormalized).to.be.eql(expectedArticles);
    });
  });
github gpbl / denormalizr / test / index.js View on Github external
describe('parsing nested plain objects', () => {
    const articleSchema = new schema.Entity('article');
    const userSchema = new schema.Entity('user');

    articleSchema.define({
      likes: new schema.Array({
        user: userSchema,
      }),
    });

    const response = {
      articles: [{
        id: 1,
        title: 'Article 1',
        likes: [{
          user: {
            id: 1,
            name: 'John',
          },
        }, {
          user: {
            id: 2,
github Superjo149 / auryo / src / common / api / fetchPlaylists.ts View on Github external
.then((json) => {

            const normalized = normalize(json, new schema.Array({
                playlists: playlistSchema
            }, (input) => `${input.kind}s`));

            return {
                normalized,
                json
            };
        });
}
github daostack / alchemy / src / schemas / index.ts View on Github external
import { schema } from "normalizr";

export const accountSchema = new schema.Entity("accounts", {}, { idAttribute: (value) => `${value.address}-${value.daoAvatarAddress}` });
export const daoSchema = new schema.Entity("daos", {}, { idAttribute: "address" });
export const proposalSchema = new schema.Entity("proposals", {}, { idAttribute: "proposalId" });
export const redemptionSchema = new schema.Entity("redemptions", {}, { idAttribute: (value) => `${value.proposalId}-${value.accountAddress}` });
export const stakeSchema = new schema.Entity("stakes", {}, { idAttribute: (value) => `${value.proposalId}-${value.stakerAddress}` });
export const voteSchema = new schema.Entity("votes", {}, { idAttribute: (value) => `${value.proposalId}-${value.voterAddress}` });

export const accountList = new schema.Array(accountSchema);
export const daoList = new schema.Array(daoSchema);
export const proposalList = new schema.Array(proposalSchema);
export const redemptionList = new schema.Array(redemptionSchema);
export const stakeList = new schema.Array(stakeSchema);
export const voteList = new schema.Array(voteSchema);

accountSchema.define({
  redemptions: redemptionList,
  stakes: stakeList,
  votes: voteList
});

daoSchema.define({
  members: accountList,
  proposals: proposalList
});

proposalSchema.define({
  redemptions: redemptionList,
  stakes: stakeList,
  votes: voteList