updated NavComponent.vue to use new <script setup lang="ts"> syntax

This commit is contained in:
2022-03-22 10:11:27 +01:00
parent d479573f41
commit d0d17ccd3d

View File

@@ -65,120 +65,90 @@
</nav> </nav>
</template> </template>
<script> <script setup lang="ts">
import { reactive } from "vue"; import { reactive } from "vue";
import { useStore } from "vuex"; import { closeNav, closeNavEventListener, GetUser, GoToPlayer } from "@/utils";
import { closeNav, GetUser, GoToPlayer } from "/src/utils"; import {
CUSTOM_URL_PATTERN,
ID64_PATTERN,
PROFILE_URL_PATTERN,
VANITY_PATTERN,
} from "@/constants";
import { StatusCodes as STATUS } from "http-status-codes"; import { StatusCodes as STATUS } from "http-status-codes";
import { useSearchParamsStore } from "@/stores/searchParams";
import type { infoState } from "@/stores/infoState";
import { useInfoStateStore } from "@/stores/infoState";
import { usePlayerDetailsStore } from "@/stores/playerDetails";
const searchParamsStore = useSearchParamsStore();
const infoStateStore = useInfoStateStore();
const playerDetailsStore = usePlayerDetailsStore();
export default {
name: "NavComponent",
setup() {
const store = useStore();
const data = reactive({ const data = reactive({
searchInput: "", searchInput: "",
}); });
const parseSearch = async () => { const parseSearch = async () => {
const input = data.searchInput; const input = data.searchInput;
const customUrlPattern = "https://steamcommunity.com/id/";
const profileUrlPattern = "https://steamcommunity.com/profiles/";
const id64Pattern = /^\d{17}$/;
const vanityPattern = /^[A-Za-z0-9-_]{3,32}$/;
store.commit({ searchParamsStore.vanity_url = "";
type: "changeVanityUrl", searchParamsStore.id64 = "";
id: "",
});
store.commit({
type: "changeId64",
id: "",
});
if (data.searchInput !== "") { if (data.searchInput !== "") {
if (id64Pattern.test(input)) { if (ID64_PATTERN.test(input)) {
store.commit({ searchParamsStore.id64 = input;
type: "changeId64", } else if (input.match(CUSTOM_URL_PATTERN)) {
id: input, searchParamsStore.vanity_url = input.split("/")[4].split("?")[0];
}); } else if (input.match(PROFILE_URL_PATTERN)) {
} else if (input.match(customUrlPattern)) {
store.commit({
type: "changeVanityUrl",
id: input.split("/")[4].split("?")[0],
});
} else if (input.match(profileUrlPattern)) {
const tmp = input.split("/")[4].split("?")[0]; const tmp = input.split("/")[4].split("?")[0];
if (id64Pattern.test(tmp)) { if (ID64_PATTERN.test(tmp)) {
store.commit({ searchParamsStore.id64 = tmp;
type: "changeId64",
id: tmp,
});
} }
} else { } else {
store.commit({ searchParamsStore.vanity_url = input;
type: "changeVanityUrl",
id: input,
});
} }
if ( if (
store.state.vanityUrl && searchParamsStore.vanity_url !== "" &&
!vanityPattern.test(store.state.vanityUrl) !VANITY_PATTERN.test(searchParamsStore.vanity_url)
) { ) {
store.commit({ const info: infoState = {
type: "changeInfoState",
data: {
statuscode: STATUS.NOT_ACCEPTABLE, statuscode: STATUS.NOT_ACCEPTABLE,
message: message:
'Only alphanumeric symbols, "_", and "-", between 3-32 characters', 'Only alphanumeric symbols, "_", and "-", between 3-32 characters',
type: "warning", type: "warning",
}, };
}); infoStateStore.addInfo(info);
store.commit({ searchParamsStore.vanity_url = "";
type: "changeVanityUrl",
id: "",
});
data.searchInput = ""; data.searchInput = "";
} }
if (store.state.id64 !== "" || store.state.vanityUrl !== "") { if (searchParamsStore.id64 !== "" || searchParamsStore.vanity_url !== "") {
const resData = await GetUser( const resData = await GetUser(
store, playerDetailsStore,
store.state.vanityUrl || store.state.id64 searchParamsStore.vanity_url || searchParamsStore.id64
); );
if (resData !== null) { if (resData !== null) {
data.searchInput = ""; data.searchInput = "";
document.activeElement.blur(); const activeElem = document.activeElement as HTMLInputElement;
activeElem.blur();
store.commit({ playerDetailsStore.playerDetails = resData;
type: "changePlayerDetails",
data: resData,
});
if (store.state.vanityUrl) { if (searchParamsStore.vanity_url) {
closeNav("mainNav"); closeNav("mainNav");
GoToPlayer(store.state.vanityUrl); GoToPlayer(searchParamsStore.vanity_url);
} else if (store.state.id64) { } else if (searchParamsStore.id64) {
closeNav("mainNav"); closeNav("mainNav");
GoToPlayer(store.state.id64); GoToPlayer(searchParamsStore.id64);
} }
} }
} }
} }
}; };
document.addEventListener("click", (e) => { closeNavEventListener("mainNav");
if (!e.target.attributes.id) closeNav("mainNav");
});
return {
data,
parseSearch,
closeNav,
};
},
};
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>