init git
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
export const useCartStore = defineStore('cart', {
|
||||
state: () => {
|
||||
return {
|
||||
items: [],
|
||||
loading: false
|
||||
}
|
||||
},
|
||||
getters: {
|
||||
count: (state) => state.items.reduce((sum, item) => sum + item.quantity, 0) || 0,
|
||||
find: (state) => (id) => state.items.find((item) => item.product?.id == id)
|
||||
},
|
||||
actions: {
|
||||
async get() {
|
||||
const { status, data } = await useFetch('/api/users/current/cart', {
|
||||
headers: { Authorization: useCookie('token') }
|
||||
})
|
||||
|
||||
if (status.value == 'success') {
|
||||
this.items = data.value.cart
|
||||
}
|
||||
},
|
||||
async clear() {
|
||||
const { status } = await useFetch('/api/users/cart', {
|
||||
headers: { Authorization: useCookie('token') }, method: 'delete', body: {}
|
||||
})
|
||||
|
||||
if (status.value == 'success') {
|
||||
this.items = []
|
||||
}
|
||||
},
|
||||
async update(product, quantity = 0) {
|
||||
this.loading = true
|
||||
const cart = { product }
|
||||
if (quantity > 0) {
|
||||
//update
|
||||
let item = this.find(product)
|
||||
if (item) item.quantity = quantity
|
||||
|
||||
cart.quantity = quantity
|
||||
const { status, data } = await useFetch('/api/users/cart', {
|
||||
headers: { Authorization: useCookie('token') }, method: 'put', body: { cart }
|
||||
})
|
||||
if (status.value == 'success') {
|
||||
const temp = { product: data.value, quantity }
|
||||
if (item) item = temp
|
||||
else this.items.push(temp)
|
||||
}
|
||||
} else {
|
||||
//remove
|
||||
const { status } = await useFetch('/api/users/cart', {
|
||||
headers: { Authorization: useCookie('token') }, method: 'patch', body: { cart }
|
||||
})
|
||||
if (status.value == 'success') {
|
||||
this.items = this.items.filter((item) => item.product.id != product)
|
||||
}
|
||||
}
|
||||
this.loading = false
|
||||
}
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,16 @@
|
||||
export const useCategoriesStore = defineStore('categories', {
|
||||
state: () => {
|
||||
return {
|
||||
items: []
|
||||
}
|
||||
},
|
||||
actions: {
|
||||
async get() {
|
||||
const { status, data } = await useFetch('/api/categories');
|
||||
if (status.value == 'success')
|
||||
this.items = data.value
|
||||
},
|
||||
|
||||
}
|
||||
|
||||
})
|
||||
@@ -0,0 +1,24 @@
|
||||
export const useFavoritesStore = defineStore('favorites', {
|
||||
state: () => {
|
||||
return {
|
||||
items: []
|
||||
}
|
||||
},
|
||||
getters: {
|
||||
find: (state) => (id) => state.items.find((item) => item.id == id)
|
||||
},
|
||||
actions: {
|
||||
async update(id) {
|
||||
const item = this.find(id)
|
||||
let stat = item ? 'delete' : 'add'
|
||||
const body = { favoriteProducts: id, stat }
|
||||
const { status } = await useFetch(`/api/users/fave`, {
|
||||
headers: { Authorization: useCookie('token') }, method: 'put', body
|
||||
})
|
||||
if (status.value == 'success') {
|
||||
if (item) this.items = this.items.filter((item) => item.id != id)
|
||||
else this.items.push({ id })
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,46 @@
|
||||
export const useOtpCodeStore = defineStore('otp-code', {
|
||||
state: () => {
|
||||
return {
|
||||
number: null,
|
||||
code: null,
|
||||
type: null,
|
||||
verify: false
|
||||
}
|
||||
},
|
||||
actions: {
|
||||
async withoutToken(body) {
|
||||
const method = 'post'
|
||||
const result = await useFetch(`/api/otp/numberwithouttoken`, { method, body });
|
||||
|
||||
if (result.status.value == 'success')
|
||||
this.number = body.number
|
||||
|
||||
return result;
|
||||
},
|
||||
async forgotPassword(body) {
|
||||
const method = 'post'
|
||||
const result = await useFetch(`/api/otp/forgotpassword`, { method, body });
|
||||
|
||||
if (result.status.value == 'success')
|
||||
this.number = body.number
|
||||
|
||||
return result;
|
||||
},
|
||||
async check(code) {
|
||||
const method = 'post'
|
||||
const body = { phoneNumber: this.number, otp: parseInt(code) }
|
||||
const result = await useFetch(`/api/otp/numberCheck`, { method, body });
|
||||
if (result.status.value == 'success') {
|
||||
this.verify = true
|
||||
this.code = body.otp
|
||||
}
|
||||
return result;
|
||||
},
|
||||
async send(type, form) {
|
||||
this.type = type
|
||||
this[type] = form[type]
|
||||
return await axios.post(`/${type}`, form);
|
||||
},
|
||||
}
|
||||
|
||||
})
|
||||
@@ -0,0 +1,37 @@
|
||||
export const useProductsStore = defineStore('products', {
|
||||
state: () => {
|
||||
return {
|
||||
items: [],
|
||||
item: null,
|
||||
filters: {},
|
||||
pages: 0
|
||||
}
|
||||
},
|
||||
actions: {
|
||||
async addReminder(id, form) {
|
||||
const result = await useFetch(`/api/products/reminder/${id}`, {
|
||||
headers: { Authorization: useCookie('token') }, method: 'put', body: form
|
||||
})
|
||||
|
||||
return result;
|
||||
|
||||
},
|
||||
async addReport(id, form) {
|
||||
const body = {
|
||||
items: Object.keys(form.items),
|
||||
content: form.content
|
||||
}
|
||||
const result = await useFetch(`/api/products/reports/${id}`, {
|
||||
headers: { Authorization: useCookie('token') }, method: 'post', body
|
||||
})
|
||||
|
||||
return result;
|
||||
},
|
||||
async compareSearch(ids, query = "") {
|
||||
const { status, data } = await useFetch(`/api/products/comparison/${ids.join('-')}/search?q=${query}`)
|
||||
if (status.value == 'success') {
|
||||
this.items = data.value
|
||||
}
|
||||
},
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,74 @@
|
||||
|
||||
export const useUserStore = defineStore('user', {
|
||||
state: () => {
|
||||
return {
|
||||
id: null,
|
||||
profile: {}
|
||||
}
|
||||
},
|
||||
actions: {
|
||||
async register(body) {
|
||||
body.password = this.profile.password
|
||||
const method = 'post'
|
||||
const result = await useFetch(`/api/users/register`, { method, body })
|
||||
|
||||
if (result.status.value == 'success') {
|
||||
this.id = result.data.value._id
|
||||
this.setToken(result.data.value)
|
||||
}
|
||||
|
||||
return result;
|
||||
},
|
||||
async login(form) {
|
||||
const isEmail = form.username?.includes('@')
|
||||
const type = isEmail ? 'email' : 'number'
|
||||
const key = isEmail ? 'email' : 'phoneNumber'
|
||||
|
||||
const body = {
|
||||
[key]: form.username,
|
||||
password: form.password,
|
||||
remember: form.remember
|
||||
}
|
||||
|
||||
const method = 'post'
|
||||
const result = await useFetch(`/api/users/login/${type}`, { method, body })
|
||||
|
||||
if (result.status.value == 'success') {
|
||||
this.id = result.data.value._id
|
||||
this.setToken(result.data.value)
|
||||
}
|
||||
|
||||
return result;
|
||||
},
|
||||
async get() {
|
||||
const { status, data, error } = await useFetch('/api/users/current', {
|
||||
headers: {
|
||||
Authorization: useCookie('token')
|
||||
}
|
||||
})
|
||||
|
||||
if (status.value == 'success') {
|
||||
this.id = data.value._id
|
||||
this.profile = data.value
|
||||
const favorites = useFavoritesStore()
|
||||
favorites.items = data.value.favoriteProducts
|
||||
|
||||
const cart = useCartStore()
|
||||
cart.get()
|
||||
}
|
||||
},
|
||||
async forgetPassword(form) {
|
||||
const result = await useFetch('/api/users/forgot', {
|
||||
method: 'post', body: form
|
||||
})
|
||||
|
||||
return result;
|
||||
},
|
||||
setToken(data) {
|
||||
const token = useCookie('token', { expires: new Date(data.expiredDate) })
|
||||
|
||||
token.value = data.accessToken
|
||||
document.cookie
|
||||
}
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user