示例#1
0
  doAction: function* fetchModelSaga(
    { schema, props, llClient }
  ) {
    const schemaClass = schemas[schema];
    const safeProps = schemaClass.preSave(fromJS(props));
    const { status, body } = yield call(llClient.postModel, schema, safeProps);

    // check the status and throw errors if not valid
    if (status >= 400) {
      if (status === 401) { throw new Unauthorised('Unauthorised'); }
      if (body.code === 11000 && schema === 'personaIdentifier') {
        throw new Error(
          'Another persona already uses that identifier and it cannot be used twice.'
        );
      }
      const message = body.errmsg || body.message || body;
      throw new HttpError(message, { status });
    }

    const result = normalize({ ...safeProps.toJS(), ...body }, schemaClass);
    const entities = entityReviver(result);
    const model = entities.get(schema).first();
    yield put(mergeEntitiesDuck.actions.mergeEntitiesAction(entities));
    yield put(clearModelsCacheDuck.actions.clearModelsCache({ schema }));
    const id = model.get('_id');
    yield put(setInMetadata({ schema, id, path: ['isExpanded'], value: true }));
    yield put(setInMetadata({ schema, id, path: ['isNew'], value: true }));
    // map the ids against the filter in the pagination store
    return yield { schema, model };
  }
  doAction: function* fetchModelSaga({
    schema,
    filter,
    sort,
    direction,
    cursor,
    llClient,
    first,
    last
  }) {
    const plainFilter = Iterable.isIterable(filter) ? filter.toJS() : filter;
    const plainSort = Iterable.isIterable(sort) ? sort.toJS() : sort;
    const plainCursor = Iterable.isIterable(cursor) ? cursor.toJS() : cursor;

    const schemaClass = schemas[schema];
    const { status, body } = yield call(llClient.getConnection, {
      schema,
      filter: plainFilter,
      sort: plainSort,
      cursor: plainCursor,
      first,
      last
    });

    if (status === 401) {
      throw new Unauthorised('Unauthorised');
    }
    if (status >= 300) {
      throw new HttpError(body.message || body, { status });
    }

    const models = map(body.edges, 'node');
    const ids = map(models, '_id');
    const edges = map(body.edges, item => (new OrderedMap({ id: item.node._id, cursor: item.cursor })));
    const normalizedModels = normalize(models, arrayOf(schemaClass));
    const entities = entityReviver(normalizedModels);
    const pageInfo = fromJS(body.pageInfo);

    // put all of the models into the master record in the model store
    yield put(mergeEntitiesDuck.actions.mergeEntitiesAction(entities));

    // map the ids against the filter in the pagination store
    return yield { schema, filter, sort, cursor, direction, edges, pageInfo, ids };
  }