Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use API v2 in web client #1153

Merged
merged 24 commits into from
Nov 5, 2022
Merged
Show file tree
Hide file tree
Changes from 9 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
10 changes: 10 additions & 0 deletions node_modules/.yarn-integrity

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions web/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
"popper.js": "1.16.1",
"vue": "2.6.12",
"vue-class-component": "7.2.6",
"vue-json-viewer": "2.2.22",
"vue-phone-number-input": "1.12.13",
"vue-property-decorator": "9.1.2",
"vue-router": "3.5.1",
"vuex": "3.6.2"
Expand Down
108 changes: 108 additions & 0 deletions web/client/src/components/Scanner.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<template>
<b-container class="mb-3">
<b-row align-h="between" align-v="center">
<h3>{{ name }}</h3>
<b-button
v-if="!error && !loading && !data"
@click="runScan"
variant="outline-primary"
size="lg"
>Run</b-button
>
<b-spinner v-if="loading && !error" type="grow"></b-spinner>
<b-row v-if="error && !loading">
<b-alert class="m-0" show variant="danger" fade>{{ error }}</b-alert>
<b-button
v-if="!dryrunError"
@click="runScan"
variant="outline-primary"
size="lg"
>Retry</b-button
>
</b-row>
</b-row>
<b-collapse :id="collapseId" class="mt-2">
<JsonViewer :value="data"></JsonViewer>
</b-collapse>
</b-container>
</template>

<script lang="ts">
import { Component, Vue, Prop } from "vue-property-decorator";
import axios from "axios";
import { mapState, mapMutations } from "vuex";
import JsonViewer from "vue-json-viewer";
import config from "@/config";

@Component({
components: {
JsonViewer,
},
})
export default class Scanner extends Vue {
data = null;
loading = false;
dryrunError = false;
error = null;
computed = {
...mapState(["number"]),
...mapMutations(["pushError"]),
};

@Prop() scanId!: string;
@Prop() name!: string;

collapseId = "scanner-collapse" + this.scanId;

mounted(): void {
this.dryRun();
}

private async dryRun(): Promise<void> {
try {
const res = await axios.post(
`${config.apiUrl}/v2/scanners/${this.scanId}/dryrun`,
{
number: this.$store.state.number,
},
{
validateStatus: () => true,
}
);

if (!res.data.success && res.data.error) {
throw res.data.error;
}
} catch (error) {
this.dryrunError = true;
this.error = error;
}
}

private async runScan(): Promise<void> {
this.error = null;
this.loading = true;
try {
const res = await axios.post(
`${config.apiUrl}/v2/scanners/${this.scanId}/run`,
{
number: this.$store.state.number,
},
{
validateStatus: () => true,
}
);

if (!res.data.success && res.data.error) {
throw res.data.error;
}
this.data = res.data.result;
this.$root.$emit("bv::toggle::collapse", this.collapseId);
} catch (error) {
this.error = error;
}

this.loading = false;
}
}
</script>
6 changes: 6 additions & 0 deletions web/client/src/router/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Vue from "vue";
import VueRouter from "vue-router";
import Scan from "../views/Scan.vue";
import Number from "../views/Number.vue";
import NotFound from "../views/NotFound.vue";

Vue.use(VueRouter);
Expand All @@ -11,6 +12,11 @@ const routes = [
name: "Scan",
component: Scan,
},
{
path: "/numbers/:number",
name: "Number",
component: Number,
},
{
path: "*",
name: "NotFound",
Expand Down
2 changes: 2 additions & 0 deletions web/client/src/shims-vue.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ declare module "*.vue" {
import Vue from "vue";
export default Vue;
}
declare module "vue-json-viewer" {}
declare module "vue-phone-number-input" {}
128 changes: 128 additions & 0 deletions web/client/src/views/Number.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<template>
<div>
<b-container v-if="isLookup" class="border p-4 mb-3">
<h3 class="text-center">Information</h3>
<b-container>
<b-row v-for="(value, name) in localData" :key="name" align-v="center">
<h5 class="text-capitalize m-0 mr-4">{{ name }}:</h5>
<p class="m-0">{{ value }}</p>
</b-row>
</b-container>
</b-container>

<b-container v-if="isLookup" class="border p-4">
<h3 class="text-center">Scanners</h3>
<Scanner
v-for="(scanner, index) in scanners"
:key="index"
:name="scanner.name.charAt(0).toUpperCase() + scanner.name.slice(1)"
:scanId="scanner.name"
/>
</b-container>
</div>
</template>

<script lang="ts">
import Vue from "vue";
import { mapMutations, mapState } from "vuex";
import { formatNumber, isValid } from "../utils";
import Scanner from "../components/Scanner.vue";
import axios, { AxiosResponse } from "axios";
import config from "@/config";

interface ScannerObject {
name: string;
description: string;
}

interface Data {
loading: boolean;
isLookup: boolean;
scanners: Array<ScannerObject>;
localData: {
valid: boolean;
raw_local: string;
local: string;
e164: string;
international: string;
countryCode: number;
country: string;
carrier: string;
};
}

export type ScanResponse<T> = AxiosResponse<{
success: boolean;
result: T;
error: string;
}>;

export default Vue.extend({
components: { Scanner },
computed: {
...mapState(["number"]),
...mapMutations(["pushError"]),
},
data(): Data {
return {
loading: false,
isLookup: false,
scanners: [],
localData: {
valid: false,
raw_local: "",
local: "",
e164: "",
international: "",
countryCode: 33,
country: "",
carrier: "",
},
};
},
mounted() {
this.runScans();
},
methods: {
async getScanners() {
try {
const res = await axios.get(`${config.apiUrl}/v2/scanners`);

// TODO: Remove this filter once the scanner local is remove
this.scanners = res.data.scanners.filter(
(scanner: ScannerObject) => scanner.name !== "local"
);
sundowndev marked this conversation as resolved.
Show resolved Hide resolved
} catch (error) {
this.$store.commit("pushError", { message: error });
}
},
async runScans(): Promise<void> {
if (!isValid(this.$route.params.number)) {
this.$store.commit("pushError", { message: "Number is not valid." });
return;
}

this.loading = true;

this.$store.commit("setNumber", formatNumber(this.$route.params.number));

try {
const res = await axios.post(`${config.apiUrl}/v2/numbers`, {
number: this.$store.state.number,
});

this.localData = res.data;

if (this.localData.valid) {
this.getScanners();
this.isLookup = true;
}
} catch (error) {
this.$store.commit("pushError", { message: error });
}

this.loading = false;
},
},
});
</script>
Loading