Skip to content

Commit

Permalink
refactor: types for mastered elements
Browse files Browse the repository at this point in the history
  • Loading branch information
cuixiaorui committed Jul 31, 2024
1 parent 469fbb5 commit a2811a4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 28 deletions.
25 changes: 18 additions & 7 deletions apps/client/api/masteredElements.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
import type { MasteredElement, MasteredElementContent } from "~/types/models/mastered-elements";
import { http } from "./http";

interface ElementItem {
element: {
interface ElementItemApiResponse {
content: {
english: string;
};
masteredAt: string;
id: string;
}

export async function fetchAddMasteredElement(element: { english: string }) {
return await http.post<ElementItem, ElementItem>(`/mastered-elements`, { element });
export async function fetchAddMasteredElement(content: MasteredElementContent) {
const result: MasteredElement = await http.post<ElementItemApiResponse, ElementItemApiResponse>(
`/mastered-elements`,
{
content,
},
);

return result;
}

export async function fetchGetMasteredElements() {
return await http.get<{ element: { english: string }[] }, { element: { english: string }[] }>(
`/mastered-elements`,
);
const result: MasteredElement[] = await http.get<
ElementItemApiResponse[],
ElementItemApiResponse[]
>(`/mastered-elements`);

return result;
}

export async function fetchRemoveMasteredElements(elementId: string) {
Expand Down
8 changes: 3 additions & 5 deletions apps/client/pages/mastered-elements.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
>
<div>
<div class="text-lg font-bold text-purple-800 dark:text-white">
{{ item.element.english }}
{{ item.content.english }}
</div>
<div class="text-purple-600 dark:text-purple-300">
Added on {{ formatDate(item.masteredAt) }}
Expand Down Expand Up @@ -51,7 +51,7 @@ const searchQuery = ref("");
const fuse = computed(
() =>
new Fuse(masteredElementsStore.masteredElements, {
keys: ["element.english"],
keys: ["content.english"],
threshold: 0.4,
}),
);
Expand All @@ -75,6 +75,4 @@ onMounted(async () => {
});
</script>

<style scoped>
/* 可以在这里添加额外的样式 */
</style>
<style scoped></style>
21 changes: 5 additions & 16 deletions apps/client/store/masteredElements.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,22 @@
import { defineStore } from "pinia";
import { computed, ref } from "vue";

import type { MasteredElement, MasteredElementContent } from "~/types/models/mastered-elements";
import {
fetchAddMasteredElement,
fetchGetMasteredElements,
fetchRemoveMasteredElements,
} from "~/api/masteredElements";

interface Element {
english: string;
}

interface ElementItem {
element: {
english: string;
};
masteredAt: string;
id: string;
}

export const useMasteredElementsStore = defineStore("masteredElements", () => {
const masteredElements = ref<ElementItem[]>([]);
const masteredElements = ref<MasteredElement[]>([]);

const totalMasteredElementsCount = computed(() => {
return masteredElements.value.length;
});

async function addElement(element: Element) {
const result = await fetchAddMasteredElement(element);
async function addElement(content: MasteredElementContent) {
const result = await fetchAddMasteredElement(content);
masteredElements.value.unshift(result);
}

Expand All @@ -41,7 +30,7 @@ export const useMasteredElementsStore = defineStore("masteredElements", () => {

function checkMastered(english: string) {
return !!masteredElements.value.find((element) => {
return element.element.english.toLowerCase() === english.toLowerCase();
return element.content.english.toLowerCase() === english.toLowerCase();
});
}

Expand Down
9 changes: 9 additions & 0 deletions apps/client/types/models/mastered-elements.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface MasteredElement {
content: MasteredElementContent;
masteredAt: string;
id: string;
}

export interface MasteredElementContent {
english: string;
}

0 comments on commit a2811a4

Please sign in to comment.