## Trash Page List に関するメモ


## `<PageList />`
```
  const updatePageList = useCallback(async() => {
    const res = await appContainer.apiv3Get('/pages/list', { path: '/trash', limit, offset });
```
pathに`/trash`を入れれば/trashのページを取得できる。



```
includeTrashed: true
```

```js:apiv3/pages.js
  router.get('/list', accessTokenParser, loginRequired, async(req, res) => {
    const { path } = req.query;
    const limit = +req.query.limit || 30;
    const offset = +req.query.offset || 0;
    const queryOptions = { offset, limit };

    try {
      const result = await Page.findListWithDescendants(path, req.user, queryOptions);
      return res.apiv3(result);
    }
    catch (err) {
      logger.error('Failed to get Descendants Pages', err);
      return res.apiv3Err(err, 500);
    }
  });
```
QueryOptionsに offsetとlimitしか指定していない!
`const queryOptions = { offset, limit };`

ここに、`includeTrashed`を指定すれば良さそう??


### optionを指定しているところ

```js:models/page.js
  /**
   * find pages that is match with `path` and its descendants
   */
  pageSchema.statics.findListWithDescendants = async function(path, user, option = {}) {
    const builder = new PageQueryBuilder(this.find());
    builder.addConditionToListWithDescendants(path, option);

    return await findListFromBuilderAndViewer(builder, user, false, option);
  };
```
- optionは何もなければ、defaultで空の状態になるらしい。

- optionを渡しているところ
    - `addConditionToListWithDescendants` or `findListFromBuilderAndViewer`




```js:models/page.js
  async function findListFromBuilderAndViewer(builder, user, showAnyoneKnowsLink, option) {
    validateCrowi();

    const User = crowi.model('User');

    const opt = Object.assign({ sort: 'updatedAt', desc: -1 }, option);
    const sortOpt = {};
    sortOpt[opt.sort] = opt.desc;

    // exclude trashed pages
    if (!opt.includeTrashed) {
      builder.addConditionToExcludeTrashed();
    }
    // exclude redirect pages
    if (!opt.includeRedirect) {
      builder.addConditionToExcludeRedirect();
    }

    // add grant conditions
    await addConditionToFilteringByViewerForList(builder, user, showAnyoneKnowsLink);

    // count
    const totalCount = await builder.query.exec('count');

    // find
    builder.addConditionToPagenate(opt.offset, opt.limit, sortOpt);
    builder.populateDataToList(User.USER_PUBLIC_FIELDS);
    const pages = await builder.query.exec('find');

    const result = {
      pages, totalCount, offset: opt.offset, limit: opt.limit,
    };
    return result;
  }
```
- 注目!! (抜粋)
- `includeTrashed`がfalseだったら、trashページをexcludeしてるっぽい。
```js:models/page.js
// exclude trashed pages
if (!opt.includeTrashed) {
  builder.addConditionToExcludeTrashed();
}
```
- さらに詳しく
    - `addConditionToExcludeTrashed`

```js:models/page.js
  addConditionToExcludeTrashed() {
    this.query = this.query
      .and({
        $or: [
          { status: null },
          { status: STATUS_PUBLISHED },
        ],
      });

    return this;
  }
```
↑これはmongoDB独特の記述のようだ




## 補足
壮大なる勘違い

```jsx:TrashPageList.jsx
const TrashPageList = (props) => {

  const [trashPageList, setTrashPageList] = useState([]);

  async function getTrashPageList() {
    const res = await props.appContainer.apiGet('/trash$');
    setTrashPageList(res.data);
    return trashPageList;
  }

  useEffect(() => {
    console.log('111');
    async function fetchData() {
      await getTrashPageList();
    }
    fetchData();
  }, [trashPageList]);


  return (
    <div>
      Trash page list
      {getTrashPageList}
      Trash page list
    </div>
  );
};
```

- `/trash/*/$`これの中身では、特にpagelistを返してないのに参照していた。。。





---

## Page Navigation

- Canonical URL: https://dev.growi.org/user/kaori-t/開発日記/TrashPageList
- Permalink: https://dev.growi.org/5f8548ecf27045004827cab0
- Parent: [開発日記](/62a71cdeb1519362cc2761e8.md)
- Children: 0 total
- Total descendants: 0
- Siblings: 4 total
  - [CustomNavigation作成の流れ](/5f850697ce4a7100495e5384.md)
  - [validator](/5fce07a56c0c650048f51df8.md)
  - [ドロップボタンの共通化](/5f74438ecf5add004959ed7d.md)
  - [ロボ3Tの使い方](/5f8931f12b6301004898068f.md)
- Last updated: 2020-10-13T07:15:21.930Z by kaori-t
- Full page listing (all children regardless of count): https://dev.growi.org/_api/v3/page-listing/children?id=5f8548ecf27045004827cab0
