@datawheel/bespoke 0.1.20 → 0.1.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/dist/index.js +487 -295
  2. package/dist/server.js +137 -42
  3. package/package.json +11 -11
package/dist/server.js CHANGED
@@ -22,12 +22,11 @@ import fs from 'fs';
22
22
  import auth0 from 'auth0';
23
23
  import NextCors from 'nextjs-cors';
24
24
  import formidable from 'formidable';
25
- import glob from 'fast-glob';
26
25
  import toposort from 'toposort';
27
26
  import { schema, normalize } from 'normalizr';
28
27
  import { createSlice, configureStore } from '@reduxjs/toolkit';
29
28
  import { HYDRATE, createWrapper } from 'next-redux-wrapper';
30
- import { showNotification } from '@mantine/notifications';
29
+ import { notifications } from '@mantine/notifications';
31
30
  import { createContext } from 'react';
32
31
  import 'react-redux';
33
32
  import 'react/jsx-runtime';
@@ -3488,19 +3487,45 @@ function dbUrlProxyFactory() {
3488
3487
  };
3489
3488
  }
3490
3489
 
3491
- // api/db/revalidate.ts
3492
- function dbRevalidateFactory(db) {
3490
+ // api/db/revalidateReport.ts
3491
+ function dbRevalidateReportFactory(db) {
3493
3492
  return async (request) => {
3494
- const { reportId, qty, samples } = request;
3495
3493
  try {
3496
- return {
3497
- ok: true,
3498
- status: 200,
3499
- data: {
3500
- reportId,
3501
- revalidated: qty,
3502
- samples
3494
+ const { variantPaths } = request;
3495
+ const paths = [];
3496
+ if (variantPaths) {
3497
+ for (let index = 0; index < variantPaths.length; index++) {
3498
+ const variant = variantPaths[index];
3499
+ const members = await db.search.findAll({
3500
+ attributes: ["slug"],
3501
+ where: {
3502
+ variant_id: variant.variantId
3503
+ }
3504
+ });
3505
+ members.forEach((m) => {
3506
+ paths.push(`/${variant.prefix}/${variant.variantSlug}/${m.slug}`);
3507
+ });
3503
3508
  }
3509
+ }
3510
+ return {
3511
+ ...request,
3512
+ paths,
3513
+ revalidated: 0,
3514
+ samples: []
3515
+ };
3516
+ } catch (err) {
3517
+ throw new BackendError(err.response?.status || 500, err.message);
3518
+ }
3519
+ };
3520
+ }
3521
+
3522
+ // api/db/revalidateUrl.ts
3523
+ function dbRevalidateUrlFactory() {
3524
+ return async (request) => {
3525
+ try {
3526
+ const { target } = request;
3527
+ return {
3528
+ target
3504
3529
  };
3505
3530
  } catch (err) {
3506
3531
  throw new BackendError(err.response?.status || 500, err.message);
@@ -3737,7 +3762,8 @@ function apiFactory(dbModels) {
3737
3762
  readUser: resultWrapper(dbReadUser()),
3738
3763
  updateUser: resultWrapper(dbUpdateUser()),
3739
3764
  addNewReportToCurrentUser: resultWrapper(dbAddNewReportToCurrentUser()),
3740
- revalidate: resultWrapper(dbRevalidateFactory()),
3765
+ revalidateReport: resultWrapper(dbRevalidateReportFactory(dbModels)),
3766
+ revalidateUrl: resultWrapper(dbRevalidateUrlFactory()),
3741
3767
  readPrivateBlocks: resultWrapper(dbReadPrivateBlocksFactory(dbModels))
3742
3768
  };
3743
3769
  }
@@ -3869,8 +3895,10 @@ function endpointUrlProxyFactory(operations) {
3869
3895
  });
3870
3896
  });
3871
3897
  }
3872
- function endpointRevalidateFactory(operations) {
3873
- const { readMetadata: readMetadata2, revalidate } = operations;
3898
+
3899
+ // api/endpoints/revalidateReport.ts
3900
+ function endpointRevalidateReportFactory(operations) {
3901
+ const { readMetadata: readMetadata2, revalidateReport: revalidateReport2 } = operations;
3874
3902
  return endpoint("GET", "revalidate/report", async (req, res) => {
3875
3903
  const { reportId, profilePrefix } = req.query;
3876
3904
  if (!reportId || !profilePrefix) {
@@ -3879,32 +3907,50 @@ function endpointRevalidateFactory(operations) {
3879
3907
  try {
3880
3908
  const reportIdInt = parseInt(`${reportId}`, 10);
3881
3909
  const profilePrefixClean = `${profilePrefix}`.split("/").filter((p) => p !== "").join("/");
3882
- const initialPathSelector = [
3883
- ".next",
3884
- "server",
3885
- "pages"
3886
- ];
3887
- const pathSelector = [
3888
- ...initialPathSelector,
3889
- profilePrefixClean
3890
- ];
3891
- const paths = [];
3910
+ const variantPaths = [];
3892
3911
  const report = await readMetadata2({}).then((metadata) => metadata.ok ? metadata.data.data.find((r) => r.id === reportIdInt) : []);
3893
3912
  if (report && report.mode === REPORT_MODES.UNILATERAL) {
3894
3913
  report.dimensions.forEach((d) => {
3895
3914
  d.variants.forEach((v) => {
3896
- paths.push([
3897
- ...pathSelector,
3898
- v.slug,
3899
- "*.html"
3900
- ].join("/"));
3915
+ variantPaths.push({
3916
+ prefix: profilePrefixClean,
3917
+ variantId: v.id,
3918
+ variantSlug: v.slug
3919
+ });
3901
3920
  });
3902
3921
  });
3903
3922
  }
3904
- const files = await glob(paths);
3905
- const urls = files.map((file) => `${file.replace(initialPathSelector.join("/"), "").replace(".html", "")}`);
3906
- await Promise.all(urls.map((path) => res.revalidate(path)));
3907
- return revalidate({ reportId: reportIdInt, qty: urls.length, samples: urls.slice(0, 5) });
3923
+ const urlsToRevalidate = await revalidateReport2(
3924
+ {
3925
+ profilePrefix: profilePrefixClean,
3926
+ reportId: reportIdInt,
3927
+ variantPaths
3928
+ }
3929
+ );
3930
+ let samples = [];
3931
+ let qty = 0;
3932
+ if (urlsToRevalidate.ok) {
3933
+ qty = urlsToRevalidate.data.paths.length;
3934
+ samples = urlsToRevalidate.data.paths.slice(0, 5);
3935
+ Promise.all(urlsToRevalidate.data.paths.map((path) => {
3936
+ let rev;
3937
+ try {
3938
+ rev = res.revalidate(path, { unstable_onlyGenerated: true });
3939
+ } catch (error) {
3940
+ console.log("error", path, error);
3941
+ }
3942
+ return rev;
3943
+ }));
3944
+ }
3945
+ return {
3946
+ ok: true,
3947
+ status: 200,
3948
+ data: {
3949
+ reportId,
3950
+ revalidated: qty,
3951
+ samples
3952
+ }
3953
+ };
3908
3954
  } catch (err) {
3909
3955
  console.log(err);
3910
3956
  throw new BackendError(500, "Error revalidating");
@@ -3912,6 +3958,30 @@ function endpointRevalidateFactory(operations) {
3912
3958
  }, [CMS_ROLES.EDITOR]);
3913
3959
  }
3914
3960
 
3961
+ // api/endpoints/revalidateUrl.ts
3962
+ function endpointRevalidateUrlFactory() {
3963
+ return endpoint("GET", "revalidate/url", async (req, res) => {
3964
+ const { target } = req.query;
3965
+ if (!target) {
3966
+ throw new BackendError(400, "Missing target parameter in revalidate factory");
3967
+ } else {
3968
+ try {
3969
+ res.revalidate(target, { unstable_onlyGenerated: true });
3970
+ return {
3971
+ ok: true,
3972
+ status: 200,
3973
+ data: {
3974
+ target
3975
+ }
3976
+ };
3977
+ } catch (err) {
3978
+ console.log(err);
3979
+ throw new BackendError(500, "Error revalidating");
3980
+ }
3981
+ }
3982
+ }, [CMS_ROLES.ADMIN, CMS_ROLES.EDITOR, CMS_ROLES.WRITER]);
3983
+ }
3984
+
3915
3985
  // api/endpoints/readPrivateBlocks.ts
3916
3986
  var { localeDefault: localeDefault5 } = getLocales_default();
3917
3987
  function endpointReadPrivateBlocksFactory(operations) {
@@ -4049,7 +4119,8 @@ function getEndpointMap(db) {
4049
4119
  endpointGetUserDataFactory(api),
4050
4120
  endpointUpdateUserDataFactory(api),
4051
4121
  endpointAddNewReportToCurrentUserFactory(api),
4052
- endpointRevalidateFactory(api),
4122
+ endpointRevalidateReportFactory(api),
4123
+ endpointRevalidateUrlFactory(),
4053
4124
  endpointReadPrivateBlocksFactory(api)
4054
4125
  ].map(({ handler, method, path, roleRequired }) => [endpointKey(method, path), { handler, roleRequired }]));
4055
4126
  }
@@ -4133,6 +4204,8 @@ __export(actions_exports, {
4133
4204
  recalculateVariables: () => recalculateVariables,
4134
4205
  removeBlocksFromState: () => removeBlocksFromState,
4135
4206
  reportSearch: () => reportSearch,
4207
+ revalidateReport: () => revalidateReport,
4208
+ revalidateUrl: () => revalidateUrl,
4136
4209
  searchMember: () => searchMember,
4137
4210
  searchRegenerate: () => searchRegenerate,
4138
4211
  searchRole: () => searchRole,
@@ -4254,7 +4327,8 @@ function apiFactory2(baseURL) {
4254
4327
  readUser: httpGET(axios6, "auth/read/user"),
4255
4328
  updateUser: httpPOST(axios6, "auth/update/user"),
4256
4329
  addNewReportToCurrentUser: httpPOST(axios6, "auth/update/me"),
4257
- revalidate: httpGET(axios6, "revalidate/report"),
4330
+ revalidateReport: httpGET(axios6, "revalidate/report"),
4331
+ revalidateUrl: httpGET(axios6, "revalidate/url"),
4258
4332
  readPrivateBlocks: httpPOST(axios6, "read/blocks/private")
4259
4333
  };
4260
4334
  }
@@ -4615,9 +4689,9 @@ var getDependencies = (bid, blocks, acc = [], crawlUp = true, crawlDown2 = true)
4615
4689
  if (rootBlock.consumers.length && crawlDown2) {
4616
4690
  rootBlock.consumers.forEach((cid) => {
4617
4691
  const rel = `${bid}-${cid}`;
4618
- if (!acc.includes(rel))
4692
+ if (!acc.includes(rel) && blocks[cid].section_id === blocks[bid].section_id)
4619
4693
  acc.push(rel);
4620
- rootBlock.consumers.forEach((cid2) => getDependencies(cid2, blocks, acc, crawlUp, crawlDown2));
4694
+ rootBlock.consumers.filter((cid2) => blocks[cid2].section_id === blocks[bid].section_id).forEach((cid2) => getDependencies(cid2, blocks, acc, crawlUp, crawlDown2));
4621
4695
  });
4622
4696
  }
4623
4697
  return acc;
@@ -4685,13 +4759,14 @@ function comparePositions(firstPos, secondPos) {
4685
4759
  function orderSort(a, b, accessor = "ordering") {
4686
4760
  return comparePositions(b[accessor], a[accessor]);
4687
4761
  }
4688
- var parse = (config, formatters = {}, locale = "en", actions = {}) => {
4762
+ var parse = (config, formatters = {}, locale = "en", actions = {}, extraGlobals = {}) => {
4689
4763
  const globals = {
4690
4764
  setVariables: actions.onSetVariables ? actions.onSetVariables : (d) => d,
4691
4765
  openModal: actions.onOpenModal ? actions.onOpenModal : (d) => d,
4692
4766
  formatters,
4693
4767
  libs: libraries,
4694
- locale
4768
+ locale,
4769
+ ...extraGlobals
4695
4770
  };
4696
4771
  function parseFunction(obj) {
4697
4772
  return Function("globals", ...obj.vars, `with (globals) { ${obj.logic} }`).bind(globals, globals);
@@ -5192,7 +5267,7 @@ function rpcAnnounce(entity, key) {
5192
5267
  function rpcFailure(entity, key, code, message) {
5193
5268
  return (dispatch) => {
5194
5269
  if (typeof window === "object") {
5195
- showNotification({
5270
+ notifications.show({
5196
5271
  autoClose: 3e3,
5197
5272
  color: "red",
5198
5273
  id: key,
@@ -5401,6 +5476,7 @@ function recalculateVariables(resource, params) {
5401
5476
  locale: currentLocale
5402
5477
  };
5403
5478
  const section = state.records.entities.section[sid];
5479
+ console.log(section?.blocks);
5404
5480
  const data = await runConsumersV2(
5405
5481
  blocks,
5406
5482
  section && [section],
@@ -5496,6 +5572,26 @@ function readPrivateBlocks(params) {
5496
5572
  return result.data;
5497
5573
  };
5498
5574
  }
5575
+ function revalidateUrl(params) {
5576
+ return async (_, __, api) => {
5577
+ const result = await api.revalidateUrl({ target: params.target });
5578
+ if ("error" in result) {
5579
+ throw new Error(result.error);
5580
+ }
5581
+ return result.data;
5582
+ };
5583
+ }
5584
+ function revalidateReport(params) {
5585
+ return async (_, __, api) => {
5586
+ const result = await api.revalidateReport(
5587
+ { reportId: params.reportId, profilePrefix: params.profilePrefix }
5588
+ );
5589
+ if ("error" in result) {
5590
+ throw new Error(result.error);
5591
+ }
5592
+ return result.data;
5593
+ };
5594
+ }
5499
5595
  var storeFactory = (_context) => configureStore({
5500
5596
  reducer: {
5501
5597
  [recordsSlice.name]: recordsSlice.reducer,
@@ -5660,7 +5756,6 @@ function BespokeRendererStaticProps(options) {
5660
5756
  const publicBlockRecords = { ...blockRecords };
5661
5757
  let privateBlockIds = Object.values(blockRecords).reduce((acc, block) => {
5662
5758
  if ("access" in block.settings && !["public", "guest"].some((grant) => !block.settings.access || block.settings.access.includes(grant))) {
5663
- console.log("is Private", block.id);
5664
5759
  acc.push(block.id);
5665
5760
  }
5666
5761
  return acc;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@datawheel/bespoke",
3
- "version": "0.1.20",
3
+ "version": "0.1.22",
4
4
  "description": "Content management system for creating automated data reports",
5
5
  "exports": {
6
6
  ".": {
@@ -41,14 +41,15 @@
41
41
  "dependencies": {
42
42
  "@auth0/nextjs-auth0": "^2.0.1",
43
43
  "@emotion/react": "^11.10.4",
44
- "@mantine/core": "^5.6.3",
45
- "@mantine/dates": "^5.6.3",
46
- "@mantine/dropzone": "^5.6.3",
47
- "@mantine/form": "^5.6.3",
48
- "@mantine/hooks": "^5.6.3",
49
- "@mantine/next": "^6.0.1",
50
- "@mantine/notifications": "^5.6.3",
51
- "@mantine/prism": "^5.6.3",
44
+ "@hello-pangea/dnd": "^16.2.0",
45
+ "@mantine/core": "^6.0.6",
46
+ "@mantine/dates": "^6.0.6",
47
+ "@mantine/dropzone": "^6.0.6",
48
+ "@mantine/form": "^6.0.6",
49
+ "@mantine/hooks": "^6.0.6",
50
+ "@mantine/next": "^6.0.6",
51
+ "@mantine/notifications": "^6.0.6",
52
+ "@mantine/prism": "^6.0.6",
52
53
  "@monaco-editor/react": "^4.4.5",
53
54
  "@reduxjs/toolkit": "^1.8.4",
54
55
  "@tabler/icons": "^1.106.0",
@@ -80,7 +81,7 @@
80
81
  "jszip": "^3.10.1",
81
82
  "lunr": "^2.3.9",
82
83
  "lunr-languages": "^1.9.0",
83
- "mantine-datatable": "^1.7.9",
84
+ "mantine-datatable": "^2.3.3",
84
85
  "monaco-themes": "^0.4.2",
85
86
  "next-redux-wrapper": "^7.0.0",
86
87
  "nextjs-cors": "^2.1.1",
@@ -88,7 +89,6 @@
88
89
  "pg": "^8.7.3",
89
90
  "pretty-format": "^28.1.1",
90
91
  "react": "^18.0.2",
91
- "react-beautiful-dnd": "^13.1.0",
92
92
  "react-clipboard.js": "^2.0.16",
93
93
  "react-icons": "^4.3.1",
94
94
  "react-redux": "^7.2.6",