Skip to content

Commit

Permalink
Merge branch 'graphql-checkout' of github.com:rapidez/core into graph…
Browse files Browse the repository at this point in the history
…ql-checkout
  • Loading branch information
indykoning committed Jan 30, 2024
2 parents 173049d + efac131 commit 59ab9bb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
28 changes: 13 additions & 15 deletions resources/js/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,38 @@ Vue.prototype.updateCart = async function (data, response) {

getAttributeValues().then((response) => {
if (!response?.data?.customAttributeMetadata?.items) {
return;
return
}

const mapping = Object.fromEntries(
response.data.customAttributeMetadata.items.map(
(attribute) => [
attribute.attribute_code,
Object.fromEntries(attribute.attribute_options.map((value) => [value.value, value.label]))
]
)
);
response.data.customAttributeMetadata.items.map((attribute) => [
attribute.attribute_code,
Object.fromEntries(attribute.attribute_options.map((value) => [value.value, value.label])),
]),
)

cart.value.items = cart.value.items.map((cartItem => {
cartItem.product.attribute_values = {};
cart.value.items = cart.value.items.map((cartItem) => {
cartItem.product.attribute_values = {}

for (const key in mapping) {
cartItem.product.attribute_values[key] = cartItem.product[key];
cartItem.product.attribute_values[key] = cartItem.product[key]
if (cartItem.product.attribute_values[key] === null) {
continue;
continue
}

if (typeof cartItem.product.attribute_values[key] === 'string') {
cartItem.product.attribute_values[key] = cartItem.product.attribute_values[key].split(',');
cartItem.product.attribute_values[key] = cartItem.product.attribute_values[key].split(',')
}

if (typeof cartItem.product.attribute_values[key] !== 'object') {
cartItem.product.attribute_values[key] = [cartItem.product.attribute_values[key]];
cartItem.product.attribute_values[key] = [cartItem.product.attribute_values[key]]
}

cartItem.product.attribute_values[key] = cartItem.product.attribute_values[key].map((value) => mapping[key][value] || value)
}

return cartItem
}))
})
})

return response.data
Expand Down
17 changes: 11 additions & 6 deletions resources/js/stores/useCart.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ export const fetchCustomerCart = async function () {

export const fetchAttributeValues = async function (attributes = []) {
if (!attributes.length) {
return {"data": {"customAttributeMetadata": {"items": null}}};
return { data: { customAttributeMetadata: { items: null } } }
}

return await window
.magentoGraphQL(`
return await window.magentoGraphQL(
`
query attributeValues($attributes: [AttributeInput!]!) {
customAttributeMetadata(attributes: $attributes) {
items {
Expand All @@ -74,16 +74,21 @@ export const fetchAttributeValues = async function (attributes = []) {
}
}
}
`, {attributes: attributes.map(attribute_code => {return {attribute_code: attribute_code, entity_type: 'catalog_product'}})})
`,
{
attributes: attributes.map((attribute_code) => {
return { attribute_code: attribute_code, entity_type: 'catalog_product' }
}),
},
)
}

const fetchAttributeValuesMemo = useMemoize(fetchAttributeValues);
const fetchAttributeValuesMemo = useMemoize(fetchAttributeValues)

export const getAttributeValues = async function () {
return await fetchAttributeValuesMemo(window.config.cart_attributes)
}


export const cart = computed({
get() {
if (!cartStorage.value?.id && mask.value) {
Expand Down

0 comments on commit 59ab9bb

Please sign in to comment.