Can you show me to build a simple app using Vue 3, TypeScript, Pinia for state management, and Vuetify for the UI. I would like it to have a simple input where a user can put the VIN for a vehicle, and it will use the API accessible from https://vpic.nhtsa.dot.gov/api/vehicles/decodevin/ to add the Make, Model, and Year of that vehicle to a list. The list will have a button for deleting vehicles from that list.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Below is a step-by-step guide to building a simple Vue 3 application with TypeScript, Pinia for state management, and Vuetify for the UI. The app will allow users to input a Vehicle Identification Number (VIN), fetch vehicle details (Make, Model, Year) from the NHTSA API, and display/manage them in a list.
Step 1: Setup Your Project
First, ensure you have Node.js (LTS version recommended) and npm installed. Then, create a new Vue project with TypeScript and select Vuetify as the UI framework.
bash
npm install -g @vue/cli @vue/cli-service-global
vue create my-vue-app
cd my-vue-app
# Select:
# - Manual setup
# - TypeScript
# - Vuetify (among other selections if desired, e.g., Router, Vuex/Pinia)
For Pinia (since Vuex/Pinia choice might not directly offer Pinia in the initial CLI setup at the time of writing), you'll manually add it later.
Step 2: Add Pinia to Your Project
bash
npm install pinia
Step 3: Setup Vuetify (Continuation from Vue CLI Setup)
If you selected Vuetify during project creation, this step might be partially done. Ensure your main.ts reflects Vuetify setup:
typescript
import { createApp } from 'vue'
import App from './App.vue'
import { createVuetify } from 'vuetify'
import 'vuetify/styles'
// Initialize Vuetify
const vuetify = createVuetify()
// Mount the app with Vuetify
createApp(App).use(vuetify).mount('#app')
Step 4: Setup Pinia Store
Create a store with Pinia for managing vehicle data:
Create Store File: src/stores/vehicleStore.ts
typescript
import { defineStore } from 'pinia'
// Define the Vehicle interface
interface Vehicle {
vin: string
make: string
model: string
year: number
}
// Pinia store for vehicle data management
export const useVehicleStore = defineStore('vehicles', {
state: () => ({
vehicles: [] as Vehicle[]
}),
actions: {
// Fetch vehicle data from NHTSA API and add it to the store
async addVehicle(vin: string) {
try {
// Validate VIN length (17 characters)
if (vin.length !== 17) {
console.error('Invalid VIN. Please enter a 17-character VIN.')
return
}
// Fetch vehicle data from the NHTSA API
const response = await fetch(`https://vpic.nhtsa.dot.gov/api/vehicles/decodevin/${vin}?format=json`)
// Check if the API response is OK
if (!response.ok) {
throw new Error('Network response was not ok')
}
// Parse the JSON response
const data = await response.json()
// Extract vehicle details
const vehicle: Vehicle = {
vin,
make: data.Results[0].Make,
model: data.Results[0].Model,
year: parseInt(data.Results[0].ModelYear)
}
// Add the vehicle to the store
this.vehicles.push(vehicle)
} catch (error) {
console.error('Failed to fetch vehicle data:', error)
}
},
// Remove a vehicle from the store by VIN
removeVehicle(vin: string) {
this.vehicles = this.vehicles.filter(vehicle => vehicle.vin !== vin)
}
}
})
Initialize Pinia in main.ts:
typescript
import { createPinia } from 'pinia'
// ...
// Initialize Pinia store
createApp(App).use(createPinia()).use(vuetify).mount('#app')
Step 5: Create the UI with Vuetify
Update src/components/HelloWorld.vue to VehicleDecoder.vue:
vue
<template>
<div>
<h1>Vehicle Decoder</h1>
<!-- Input field for VIN -->
<v-text-field v-model="vin" label="Enter VIN" />
<!-- Button to decode the VIN -->
<v-btn @click="decodeVin">Decode VIN</v-btn>
<!-- List of decoded vehicles -->
<v-list lines="one">
<v-list-item v-for="vehicle in vehicleStore.vehicles" :key="vehicle.vin">
<template v-slot:prepend>
<!-- Delete button for each vehicle -->
<v-btn icon="mdi-delete" @click="vehicleStore.removeVehicle(vehicle.vin)" />
</template>
<!-- Display vehicle details -->
<v-list-item-title>{{ vehicle.year }} {{ vehicle.make }} {{ vehicle.model }}</v-list-item-title>
<v-list-item-subtitle>VIN: {{ vehicle.vin }}</v-list-item-subtitle>
</v-list-item>
</v-list>
</div>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import { useVehicleStore } from '../stores/vehicleStore'
export default defineComponent({
data() {
return {
vin: '', // Input field for VIN
}
},
setup() {
const vehicleStore = useVehicleStore()
return { vehicleStore }
},
methods: {
// Call the store action to decode the VIN and add the vehicle
decodeVin() {
this.vehicleStore.addVehicle(this.vin)
this.vin = '' // Clear input after submission
}
}
})
</script>
Step 6: Update App.vue to Use VehicleDecoder.vue
vue
<template>
<v-app>
<!-- Use the VehicleDecoder component -->
<VehicleDecoder />
</v-app>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import VehicleDecoder from './components/VehicleDecoder.vue'
export default defineComponent({
name: 'App',
components: {
VehicleDecoder
}
})
</script>
Step 7: Run Your Application
bash
npm run serve
Navigate to http://localhost:8080/ (or the port indicated by the CLI output) to interact with your application. Enter a valid VIN in the input field, click "Decode VIN", and the vehicle's details should be fetched and displayed in the list below, with an option to delete each entry.
Important Notes:
API Usage: Always respect API usage policies. The NHTSA API might have specific requirements or limitations on usage, especially in production environments. Ensure you comply with these to avoid service disruptions.
Production Deployment: For production deployment, consider implementing additional features such as:
API rate limiting to prevent abuse.
Error handling for network issues or invalid responses.
Input validation to ensure only valid VINs are processed.
Potential caching mechanisms to reduce repeated API calls for the same VIN.