pages_core 3.15.2 → 3.15.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,10 +6,17 @@ export default function useAttachments(
6
6
  records: Attachments.Record[]
7
7
  ): Attachments.State {
8
8
  const [deleted, setDeleted] = useState<Attachments.Record[]>([]);
9
+ const collection = useDragCollection(records);
10
+
11
+ const update = (records: Attachments.Record[]) => {
12
+ collection.dispatch({ type: "reinitialize", payload: records });
13
+ setDeleted([]);
14
+ };
9
15
 
10
16
  return {
11
- collection: useDragCollection(records),
17
+ collection: collection,
12
18
  deleted: deleted,
13
- setDeleted: setDeleted
19
+ setDeleted: setDeleted,
20
+ update: update
14
21
  };
15
22
  }
@@ -4,23 +4,39 @@ import * as Images from "../../types/Images";
4
4
 
5
5
  import { useDragCollection } from "../drag";
6
6
 
7
- export default function useImageGrid(
8
- records: Images.Record[],
9
- enablePrimary = false
10
- ): Images.GridState {
7
+ function filterRecords(records: Images.Record[], enablePrimary = false) {
11
8
  const primaryRecords = enablePrimary
12
9
  ? records.filter((r) => r.primary).slice(0, 1)
13
10
  : [];
14
11
  const imageRecords = records.filter((r) => primaryRecords.indexOf(r) === -1);
12
+ return [primaryRecords, imageRecords];
13
+ }
14
+
15
+ export default function useImageGrid(
16
+ records: Images.Record[],
17
+ enablePrimary = false
18
+ ): Images.GridState {
19
+ const [primaryRecords, imageRecords] = filterRecords(records, enablePrimary);
15
20
 
16
21
  const primary = useDragCollection(primaryRecords);
17
22
  const images = useDragCollection(imageRecords);
18
23
  const [deleted, setDeleted] = useState<Images.Record[]>([]);
19
24
 
25
+ const update = (records: Images.Record[]) => {
26
+ const [primaryRecords, imageRecords] = filterRecords(
27
+ records,
28
+ enablePrimary
29
+ );
30
+ primary.dispatch({ type: "reinitialize", payload: primaryRecords });
31
+ images.dispatch({ type: "reinitialize", payload: imageRecords });
32
+ setDeleted([]);
33
+ };
34
+
20
35
  return {
21
36
  primary: primary,
22
37
  images: images,
23
38
  deleted: deleted,
24
- setDeleted: setDeleted
39
+ setDeleted: setDeleted,
40
+ update: update
25
41
  };
26
42
  }
@@ -82,11 +82,6 @@ export default function PageForm(props: Props) {
82
82
  });
83
83
  };
84
84
 
85
- const clearDeletedObjects = () => {
86
- filesState.setDeleted([]);
87
- imagesState.setDeleted([]);
88
- };
89
-
90
85
  const handleSubmit = (evt: React.MouseEvent) => {
91
86
  evt.preventDefault();
92
87
  let method = postJson;
@@ -110,7 +105,8 @@ export default function PageForm(props: Props) {
110
105
  if (response.errors && response.errors.length > 0) {
111
106
  errorToast("A validation error prevented the page from being saved.");
112
107
  } else {
113
- clearDeletedObjects();
108
+ filesState.update(response.page_files);
109
+ imagesState.update(response.page_images);
114
110
  noticeToast("Your changes were saved");
115
111
  }
116
112
  })
@@ -66,6 +66,8 @@ function dragCollectionReducer<T = Drag.DraggableRecord>(
66
66
  return { ...d, rect: getPosition(d) };
67
67
  });
68
68
  });
69
+ case "reinitialize":
70
+ return action.payload.map((r) => createDraggable(r));
69
71
  case "remove":
70
72
  return state.filter(
71
73
  (d: Drag.Draggable<T>) => d.handle !== action.payload.handle
@@ -26,4 +26,5 @@ export interface State {
26
26
  collection: Drag.Collection<Record>;
27
27
  deleted: Record[];
28
28
  setDeleted: (records: Record[]) => void;
29
+ update: (records: Record[]) => void;
29
30
  }
@@ -15,6 +15,7 @@ export type CollectionAction<T = DraggableRecord> =
15
15
  payload: Item<T>[];
16
16
  }
17
17
  | { type: "update"; payload: Item<T> }
18
+ | { type: "reinitialize"; payload: Array<T> }
18
19
  | { type: "remove"; payload: Draggable<T> }
19
20
  | { type: "updatePositions"; payload?: Draggable<T> };
20
21
 
@@ -44,4 +44,5 @@ export interface GridState {
44
44
  images: Drag.Collection<Record>;
45
45
  deleted: Record[];
46
46
  setDeleted: (records: Record[]) => void;
47
+ update: (records: Record[]) => void;
47
48
  }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pages_core
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.15.2
4
+ version: 3.15.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Inge Jørgensen