-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from story2Dev/develop
Develop
- Loading branch information
Showing
17 changed files
with
443 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<template> | ||
<div class="w-full"> | ||
<n-select | ||
v-if="type == 'select'" | ||
:options="terms" | ||
value-field="id" | ||
label-field="name" | ||
v-bind="$attrs" | ||
/> | ||
<article v-else-if="type == 'checkbox'"> | ||
<n-checkbox-group v-bind="$attrs"> | ||
<nav :class="wrapperClass"> | ||
<n-checkbox | ||
v-for="(item, index) in terms" | ||
:key="index" | ||
:value="item.id" | ||
:label="item.name" | ||
/> | ||
</nav> | ||
</n-checkbox-group> | ||
</article> | ||
<article v-else-if="type == 'radio'"> | ||
<n-radio-group v-bind="$attrs" :name="name"> | ||
<nav :class="wrapperClass"> | ||
<n-radio | ||
v-for="(item, index) in terms" | ||
:key="index" | ||
:value="item.id" | ||
:label="item.name" | ||
/> | ||
</nav> | ||
</n-radio-group> | ||
</article> | ||
</div> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
import type { Term } from '~/types'; | ||
interface Props { | ||
groupId: number; | ||
type?: 'select' | 'radio' | 'checkbox'; | ||
wrapperClass?: string; | ||
name?: string; | ||
} | ||
const props = withDefaults(defineProps<Props>(), { | ||
type: 'select', | ||
wrapperClass: 'flex flex-wrap gap-2', | ||
name: 'term', | ||
}); | ||
const { getTerms } = useTerm(); | ||
const terms = ref<Term[]>([]); | ||
const { items } = await getTerms({ | ||
where: { | ||
group_id: { | ||
_eq: props.groupId, | ||
}, | ||
}, | ||
}); | ||
terms.value = items; | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import { DELETE_PRODUCT, INSERT_PRODUCT, UPDATE_PRODUCT } from '~/gql/mutate'; | ||
import { PRODUCTS_QUERY } from '~/gql/query'; | ||
import type { Product, ProductInput, Variable } from '~/types'; | ||
|
||
export const useProduct = () => { | ||
const { client } = useApolloClient(); | ||
|
||
const frm = useState<Partial<Product>>('frm', () => ({ | ||
name: '', | ||
sku: '', | ||
barcode: '', | ||
description: '', | ||
unitPrice: 0, | ||
stockQuantity: 0, | ||
stockTrackable: false, | ||
})); | ||
const count = useState<number>('count', () => 0); | ||
|
||
async function getProducts(variables?: Partial<Variable>) { | ||
try { | ||
const { data, errors } = await client.query({ | ||
query: PRODUCTS_QUERY, | ||
variables, | ||
}); | ||
if (errors) { | ||
throw new Error(`[useProduct]: getProducts errors: ${errors}`); | ||
} | ||
const _count = data?.aggregate?.aggregate?.count || 0; | ||
count.value = _count; | ||
const items: Product[] = data?.products || []; | ||
return { | ||
items, | ||
errors, | ||
}; | ||
} catch (error) { | ||
throw new Error(`[useProduct]: getProducts error: ${error}`); | ||
} | ||
} | ||
|
||
async function insert(object: ProductInput) { | ||
try { | ||
const { data, errors } = await client.mutate({ | ||
mutation: INSERT_PRODUCT, | ||
variables: { | ||
object: toHasuraInput(object), | ||
}, | ||
}); | ||
|
||
if (errors) { | ||
throw new Error(`[insert]: insert errors: ${errors}`); | ||
} | ||
|
||
return { | ||
product: data?.insertProduct as Product, | ||
errors, | ||
}; | ||
} catch (error) { | ||
throw new Error(`[useProduct]: insert error: ${error}`); | ||
} | ||
} | ||
|
||
async function update(id: string, object: Partial<ProductInput>) { | ||
try { | ||
const { data, errors } = await client.mutate({ | ||
mutation: UPDATE_PRODUCT, | ||
variables: { | ||
id, | ||
object: toHasuraInput(object), | ||
}, | ||
}); | ||
|
||
if (errors) { | ||
throw new Error(`[update]: update errors: ${errors}`); | ||
} | ||
|
||
return { | ||
product: data?.updateProduct as Product, | ||
errors, | ||
}; | ||
} catch (error) { | ||
throw new Error(`[useProduct]: update error: ${error}`); | ||
} | ||
} | ||
|
||
// remove | ||
async function remove(id: string) { | ||
try { | ||
const { data, errors } = await client.mutate({ | ||
mutation: DELETE_PRODUCT, | ||
variables: { | ||
id, | ||
}, | ||
}); | ||
|
||
if (errors) { | ||
throw new Error(`[remove]: remove errors: ${errors}`); | ||
} | ||
|
||
return { | ||
product: data?.deleteProduct as Product, | ||
errors, | ||
}; | ||
} catch (error) { | ||
throw new Error(`[useProduct]: remove error: ${error}`); | ||
} | ||
} | ||
|
||
return { | ||
frm, | ||
count, | ||
getProducts, | ||
insert, | ||
update, | ||
remove, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './term-mutate'; | ||
export * from './product-mutate'; |
Oops, something went wrong.