Trash Page List に関するメモ
<PageList />
const updatePageList = useCallback(async() => { const res = await appContainer.apiv3Get('/pages/list', { path: '/trash', limit, offset });
pathに/trash
を入れれば/trashのページを取得できる。
includeTrashed: true
apiv3/pages.jsrouter.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を指定しているところ
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
orfindListFromBuilderAndViewer
models/page.jsasync 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してるっぽい。
models/page.js// exclude trashed pages if (!opt.includeTrashed) { builder.addConditionToExcludeTrashed(); }
- さらに詳しく
addConditionToExcludeTrashed
models/page.jsaddConditionToExcludeTrashed() { this.query = this.query .and({ $or: [ { status: null }, { status: STATUS_PUBLISHED }, ], }); return this; }
↑これはmongoDB独特の記述のようだ
補足
壮大なる勘違い
TrashPageList.jsxconst 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を返してないのに参照していた。。。