Skip to content

Commit 93c7f54

Browse files
committed
chore: cleanup
1 parent 21141fe commit 93c7f54

File tree

14 files changed

+286
-65
lines changed

14 files changed

+286
-65
lines changed

app/components/ImportForm.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
:name="file.key"
4242
:label="file.label"
4343
:error="errors[file.key]"
44-
description="Plak tekst uit het lokale .json-bestand (Literature, Outlines, Songs, Tips)."
44+
:description="`Plak tekst uit het lokale ${capitalize(file.key)}.json bestand.`"
4545
>
4646
<UTextarea v-model="state[file.key]" class="w-full" />
4747
</UFormField>
@@ -61,7 +61,7 @@ defineProps<{
6161
6262
const model = defineModel<Output>();
6363
64-
const jsonFiles: { key: keyof Input; label: string }[] = [
64+
const jsonFiles: { key: JsonKey; label: string }[] = [
6565
{ key: "literature", label: "Literatuur" },
6666
{ key: "outlines", label: "Lezingen" },
6767
{ key: "songs", label: "Liederen" },
@@ -75,7 +75,7 @@ const schema = z.object({
7575
songs: jsonCodec(songsSchema).optional(),
7676
tips: jsonCodec(tipsSchema).optional(),
7777
ui: translationFileSchema,
78-
});
78+
} satisfies Record<JsonKey | UIKey, unknown>);
7979
8080
export type Input = z.input<typeof schema>;
8181
export type Output = z.output<typeof schema>;

app/components/JsonChanges.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
<script setup lang="ts">
2121
const props = defineProps<{
22-
type: "literature" | "outlines" | "songs" | "tips";
22+
type: JsonKey;
2323
}>();
2424
2525
const jsonStore = useJsonStore();

app/pages/import.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,12 @@ watch(originalsString, (ui) => {
6767
6868
watch(translationsString, (ui) => {
6969
translations.value = { ...translations.value, ui };
70+
uiStore.clearConsistentKeys();
7071
});
7172
7273
watch(nwpString, (nwp) => {
7374
translations.value = { ...translations.value, nwp };
75+
uiStore.clearConsistentKeys();
7476
});
7577
7678
watch(originals, (val) => {

app/pages/update.vue

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
/>
5353
</UPageCard>
5454
<UPageCard
55-
v-if="newTranslationsString && isDifferent"
55+
v-if="newTranslationsString"
5656
title="Update afronden"
5757
description="Sla de nieuwe teksten op."
5858
>
@@ -113,19 +113,20 @@ const finishUpdate = () => {
113113
uiStore.nwpString = newTranslationsString.value;
114114
}
115115
116-
const parsed = JSON.parse(newTranslations.value);
117-
const newKeys = Object.keys(parsed).filter(
118-
(key) => !savedKeys.value.has(key),
116+
const newText = changedText.value.filter(
117+
({ key }) => !savedKeys.value.has(key),
119118
);
120119
120+
uiStore.clearConsistentKeys(changedText.value.map((t) => t.key));
121+
121122
if (type.value === "NWS") {
122-
newKeys.forEach((key) => {
123-
uiStore.translations[key] = parsed[key];
123+
newText.forEach((t) => {
124+
uiStore.translations[t.key] = t.value;
124125
});
125126
} else {
126-
newKeys.forEach((key) => {
127+
newText.forEach((t) => {
127128
if (uiStore.nwpTranslations) {
128-
uiStore.nwpTranslations[key] = parsed[key];
129+
uiStore.nwpTranslations[t.key] = t.value;
129130
}
130131
});
131132
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export type TranslationKey = keyof State;
1+
export type EmailKey = Prettify<keyof State>;
22

33
type State = {
44
literature: {
@@ -11,7 +11,7 @@ type State = {
1111
tips: { input?: Tips; originals?: Tips; translations?: Tips };
1212
};
1313

14-
export const useJsonStore = defineStore("json", {
14+
export const useEmailStore = defineStore("email", {
1515
actions: {
1616
async fixInconsistentTips(heading: string, tips: { index: number }[]) {
1717
tips.forEach((t) => {

app/stores/json.ts

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
export type JsonKey = Prettify<keyof State>;
2+
3+
type State = {
4+
literature: {
5+
input?: Literature;
6+
originals?: Literature;
7+
translations?: Literature;
8+
};
9+
outlines: { input?: Outlines; originals?: Outlines; translations?: Outlines };
10+
songs: { input?: Songs; originals?: Songs; translations?: Songs };
11+
tips: { input?: Tips; originals?: Tips; translations?: Tips };
12+
};
13+
14+
export const useJsonStore = defineStore("json", {
15+
actions: {
16+
async fixInconsistentTips(heading: string, tips: { index: number }[]) {
17+
tips.forEach((t) => {
18+
if (!this.tips.translations?.[t.index]) return;
19+
this.tips.translations![t.index]!.heading = heading;
20+
});
21+
await new Promise((resolve) => setTimeout(resolve, 100));
22+
},
23+
setInput({
24+
literature,
25+
outlines,
26+
songs,
27+
tips,
28+
}: {
29+
literature?: Literature;
30+
outlines?: Outlines;
31+
songs?: Songs;
32+
tips?: Tips;
33+
}) {
34+
this.literature = { ...this.literature, input: literature };
35+
this.outlines = { ...this.outlines, input: outlines };
36+
this.songs = { ...this.songs, input: songs };
37+
this.tips = { ...this.tips, input: tips };
38+
},
39+
setOriginals({
40+
literature,
41+
outlines,
42+
songs,
43+
tips,
44+
}: {
45+
literature?: Literature;
46+
outlines?: Outlines;
47+
songs?: Songs;
48+
tips?: Tips;
49+
}) {
50+
this.literature = { ...this.literature, originals: literature };
51+
this.outlines = { ...this.outlines, originals: outlines };
52+
this.songs = { ...this.songs, originals: songs };
53+
this.tips = { ...this.tips, originals: tips };
54+
},
55+
setTranslations(
56+
{
57+
literature,
58+
outlines,
59+
songs,
60+
tips,
61+
}: {
62+
literature?: Literature;
63+
outlines?: Outlines;
64+
songs?: Songs;
65+
tips?: Tips;
66+
},
67+
group?: JsonKey,
68+
) {
69+
if (!group || group === "literature") {
70+
this.literature = { ...this.literature, translations: literature };
71+
}
72+
if (!group || group === "outlines") {
73+
this.outlines = { ...this.outlines, translations: outlines };
74+
}
75+
if (!group || group === "songs") {
76+
this.songs = { ...this.songs, translations: songs };
77+
}
78+
if (!group || group === "tips") {
79+
this.tips = { ...this.tips, translations: tips };
80+
}
81+
},
82+
},
83+
getters: {
84+
changedGroups(state) {
85+
const groups: JsonKey[] = [];
86+
typedKeys(state).forEach((group) => {
87+
if (
88+
state[group] &&
89+
JSON.stringify(state[group].input) !==
90+
JSON.stringify(state[group].translations)
91+
) {
92+
groups.push(group);
93+
}
94+
});
95+
return groups;
96+
},
97+
inconsistentTips(state) {
98+
if (!state.tips.originals?.length) return [];
99+
const headings: Record<string, { index: number; translation: string }[]> =
100+
{};
101+
102+
state.tips.originals.forEach((tip, index) => {
103+
// If the translation is missing, skip it
104+
if (!state.tips.translations?.[index]?.heading) return;
105+
106+
if (headings[tip.heading]) {
107+
headings[tip.heading]!.push({
108+
index,
109+
translation: state.tips.translations[index].heading,
110+
});
111+
} else {
112+
headings[tip.heading] = [
113+
{
114+
index,
115+
translation: state.tips.translations[index].heading,
116+
},
117+
];
118+
}
119+
});
120+
121+
return Object.entries(headings)
122+
.filter(
123+
([, tips]) =>
124+
tips.length > 1 &&
125+
tips.some((t) => t.translation !== tips[0]?.translation),
126+
)
127+
.map(([heading, tips]) => ({
128+
heading,
129+
tips: tips,
130+
translations: [...new Set(tips.map((t) => t.translation))],
131+
}));
132+
},
133+
input(state) {
134+
return {
135+
literature: state.literature.input,
136+
outlines: state.outlines.input,
137+
songs: state.songs.input,
138+
tips: state.tips.input,
139+
};
140+
},
141+
missingLiterature(state) {
142+
return (
143+
state.literature.originals?.filter(
144+
(o) => !state.literature.translations?.some((t) => t.id === o.id),
145+
) ?? []
146+
);
147+
},
148+
missingOutlines(state) {
149+
return (
150+
state.outlines.originals?.filter(
151+
(o) =>
152+
!!o.title &&
153+
!state.outlines.translations?.some(
154+
(t) => t.number === o.number && !!t.title && !!t.updated,
155+
),
156+
) ?? []
157+
);
158+
},
159+
missingSongs(state) {
160+
return (
161+
state.songs.originals?.filter(
162+
(o) => !state.songs.translations?.some((t) => t.number === o.number),
163+
) ?? []
164+
);
165+
},
166+
missingTips(state) {
167+
return (
168+
state.tips.originals?.filter(
169+
(t, i) => !state.tips.translations?.[i]?.heading,
170+
) ?? []
171+
);
172+
},
173+
originals(state) {
174+
return {
175+
literature: state.literature.originals,
176+
outlines: state.outlines.originals,
177+
songs: state.songs.originals,
178+
tips: state.tips.originals,
179+
};
180+
},
181+
translations(state) {
182+
return {
183+
literature: state.literature.translations,
184+
outlines: state.outlines.translations,
185+
songs: state.songs.translations,
186+
tips: state.tips.translations,
187+
};
188+
},
189+
},
190+
persist: true,
191+
state: (): State => ({
192+
literature: {
193+
input: undefined,
194+
originals: undefined,
195+
translations: undefined,
196+
},
197+
outlines: {
198+
input: undefined,
199+
originals: undefined,
200+
translations: undefined,
201+
},
202+
songs: { input: undefined, originals: undefined, translations: undefined },
203+
tips: { input: undefined, originals: undefined, translations: undefined },
204+
}),
205+
});

app/stores/ui.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
export type UIKey = "nwp" | "ui";
2+
13
type State = {
24
consistentNWS: Record<string, string[]>;
35
consistentUI: string[];
@@ -10,6 +12,27 @@ type State = {
1012

1113
export const useUIStore = defineStore("ui", {
1214
actions: {
15+
clearConsistentKeys(keys?: string[]) {
16+
if (!keys) {
17+
this.consistentNWS = {};
18+
this.consistentUI = [];
19+
return;
20+
}
21+
22+
this.consistentNWS = Object.fromEntries(
23+
Object.entries(this.consistentNWS).map(([key, values]) => {
24+
if (keys.includes(key)) {
25+
return [key, []];
26+
} else {
27+
return [key, values.filter((value) => !keys.includes(value))];
28+
}
29+
}),
30+
);
31+
32+
this.consistentUI = this.consistentUI.filter(
33+
(key) => !keys.includes(key),
34+
);
35+
},
1336
markNWSConsistent(key: string, otherKey: string) {
1437
this.consistentNWS = {
1538
...this.consistentNWS,

app/utils/general.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
export const typedKeys = <T extends object>(obj: T) =>
22
Object.keys(obj) as (keyof T)[];
3+
4+
export const capitalize = (str: string) =>
5+
str.charAt(0).toUpperCase() + str.slice(1);

server/api/export.post.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const bodySchema = z.object({
1010
});
1111

1212
export default defineEventHandler(async (event) => {
13-
console.log("Exporting translations...");
1413
const { files } = await readValidatedBody(event, bodySchema.parse);
1514
const zipBuffer = await createZipFile(files);
1615

server/api/outlines/buffer.post.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
export default defineEventHandler(async (event) => {
2-
console.log("Getting outlines...");
32
const [file] = await receiveFiles(event, {
43
ensure: {
54
maxSize: "128MB",
@@ -16,8 +15,6 @@ export default defineEventHandler(async (event) => {
1615
});
1716
}
1817

19-
console.log("File received");
20-
2118
const database = await getJWPUBDatabaseFromBuffer(await file.arrayBuffer());
2219

2320
return await getOutlinesFromJWPUB(database);

0 commit comments

Comments
 (0)