Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/composables/useAsync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { useAsyncState } from "@vueuse/core";
import axios from "axios";
import { computed } from "vue";

export function useAsync<T>(promise: Promise<T> | ((...args: any[]) => Promise<T>)) {
const { state, isLoading, execute, error } = useAsyncState(promise, null, {
immediate: false,
});

const errorMsg = computed(() => {
if (!error.value) return null;
else if (axios.isAxiosError(error.value) && error.value.response?.data?.message) {
return error.value.response.data.message;
} else {
return "Unknown error occurred :(";
}
Comment on lines +11 to +16
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to use early return to eliminate some nested code.

Suggested change
if (!error.value) return null;
else if (axios.isAxiosError(error.value) && error.value.response?.data?.message) {
return error.value.response.data.message;
} else {
return "Unknown error occurred :(";
}
if (!error.value) return null;
if (axios.isAxiosError(error.value) && error.value.response?.data?.message) {
return error.value.response.data.message;
}
return "Unknown error occurred :(";

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's a good point, will fix this.

});

return {
state,
isLoading,
execute,
errorMsg,
};
}
38 changes: 16 additions & 22 deletions src/pages/admin.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { useAxios } from "@vueuse/integrations/useAxios";
import axios from "axios";
import { computed, ref, watchEffect } from "vue";
import { useRoute, useRouter } from "vue-router";
import { useAsync } from "@/composables/useAsync";

const route = useRoute();
const router = useRouter();
Expand All @@ -28,7 +29,7 @@ const {
data: users,
error: fetchError,
isLoading: fetchLoading,
execute,
execute: fetchUsers,
} = useAxios<UserInfo[]>("/user", fetcher);
const searchName = ref("");
const searchRole = ref(null);
Expand All @@ -39,8 +40,6 @@ const filteredUsers = computed(() =>
);

const edittingUsername = ref("");
const isLoading = ref(false);
const errorMsg = ref("");
const initialUserForm = {
displayedName: "",
role: 0,
Expand All @@ -63,26 +62,21 @@ function editUser(username: string) {
};
edittingUsername.value = username;
}

const {
isLoading,
errorMsg,
execute: modifyUser,
} = useAsync(async () => {
if (!userForm.value.password) userForm.value.password = null;
await api.User.modify(edittingUsername.value, { ...userForm.value });
fetchUsers();
userForm.value = { ...initialUserForm };
edittingUsername.value = "";
});
async function submit() {
if (!(await v$.value.$validate())) return;

isLoading.value = true;
try {
if (!userForm.value.password) userForm.value.password = null;
await api.User.modify(edittingUsername.value, { ...userForm.value });
execute();
userForm.value = { ...initialUserForm };
edittingUsername.value = "";
} catch (error) {
if (axios.isAxiosError(error) && error.response?.data?.message) {
errorMsg.value = error.response.data.message;
} else {
errorMsg.value = "Unknown error occurred :(";
}
throw error;
} finally {
isLoading.value = false;
}
modifyUser();
}
</script>

Expand Down Expand Up @@ -135,7 +129,7 @@ async function submit() {
<td>{{ displayedName }}</td>
<td>{{ ROLE[role] }}</td>
<td>
<div class="btn btn-ghost btn-circle btn-sm" @click="() => editUser(username)">
<div class="btn btn-circle btn-ghost btn-sm" @click="() => editUser(username)">
<i-uil-pen />
</div>
</td>
Expand Down