/
contentTypes.ts
147 lines (135 loc) · 3.84 KB
/
contentTypes.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { when } from 'mobx';
import makeInspectable from 'mobx-devtools-mst';
import { flow, Instance, types } from 'mobx-state-tree';
import { client } from '../utils/client';
import { ContentType } from './models/ContentType';
import { CREATE_CONTENT_TYPE } from './mutations';
import { ALL_CONTENT_TYPES, CONTENT_TYPE_BY_ID } from './queries';
export const ContentTypes = types
.model('ContentTypes', {
items: types.map(types.late(() => ContentType)),
loading: false,
loaded: false,
error: false,
})
.views(self => ({
get list() {
const entries = Array.from(self.items.values());
entries.sort((a, b) => a.title.localeCompare(b.title));
return entries;
},
}))
.actions(self => {
const loadByName = flow(function*(
name: string
): Generator<Promise<any>, Instance<typeof ContentType> | void, any> {
let item;
const entries = Array.from(self.items.values());
const entry = entries.find(n => n.name.toLocaleLowerCase() === name.toLocaleLowerCase());
if (entry) {
return entry;
}
const { data } = yield client.query({
query: CONTENT_TYPE_BY_ID,
variables: { name },
fetchPolicy: 'network-only',
});
if (data.Schema) {
item = ContentType.create(data.Schema);
if (self.items.has(item.id)) {
item = self.items.get(item.id);
if (item) {
item.replace(data.Schema);
}
} else {
self.items.put(item);
}
}
return item;
});
const loadById = flow(function*(
id: string
): Generator<Promise<any>, Instance<typeof ContentType> | undefined, any> {
let item;
const { data } = yield client.query({
query: CONTENT_TYPE_BY_ID,
variables: { id },
fetchPolicy: 'network-only',
});
if (data.Schema) {
item = ContentType.create(data.Schema);
if (self.items.has(id)) {
item = self.items.get(id);
if (item) {
item.replace(data.Schema);
}
} else {
self.items.put(item);
}
}
return item;
});
const loadAll = flow(function*(): Generator<Promise<any>, void, any> {
if (self.loading) {
yield new Promise(resolve => {
when(() => self.loading === false, resolve);
});
return;
}
self.loading = true;
try {
const { data } = yield client.query({
query: ALL_CONTENT_TYPES,
});
data.allSchemas.edges.forEach((contentType: any) => {
const prevItem = self.items.get(contentType.id);
if (prevItem) {
prevItem.replace(contentType.node);
} else {
const item = ContentType.create(contentType.node);
self.items.put(item);
}
});
self.loaded = true;
} catch (err) {
self.error = true;
}
self.loading = false;
});
const create = flow(function*(
input: any
): Generator<Promise<any>, Instance<typeof ContentType> | void, any> {
try {
const { data } = yield client.mutate({
mutation: CREATE_CONTENT_TYPE,
variables: { input },
});
if (data.createSchema) {
const item = ContentType.create(data.createSchema);
return self.items.put(item);
}
} catch (err) {
throw new Error(err);
}
});
const removeById = (id: string) => {
self.items.delete(id);
};
return {
loadAll,
loadById,
loadByName,
create,
removeById,
};
})
.create();
makeInspectable(ContentTypes);
export const ContentTypeRef = types.reference(ContentType, {
get(identifier: string) {
return ContentTypes.items.get(identifier);
},
set(value: Instance<typeof ContentType>) {
return value.id;
},
} as any);