HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/shop.komma.nl/resources/js/services/propertyService.js
import {AxiosResponse} from 'axios'
import {axios} from '../../../vendor/komma/kms/resources/js/global/axiosBootstrapper'
import Key from "../components/vue/properties/resources/key";
import KeyValueTranslation from "../components/vue/properties/resources/translation";
import Property from "../components/vue/properties/resources/property";

export default class PropertyService {
    constructor() {
        this.idCounter = -1;
    }

    /**
     * @param {string} iso_2
     * @return {Promise<AxiosResponse<KeyValueTranslation>>}
     */
    keyTranslations(iso_2) {
        return axios.get('/properties/keys/translations' + (iso_2 ? '/' + iso_2 : '')).then((response) => {
            return response.data.data.map(translationData => new KeyValueTranslation(translationData));
        })
    }

    /**
     * @param {string} iso_2
     * @return {Promise<AxiosResponse<KeyValueTranslation>>}
     */
    valueTranslations(iso_2) {
        return axios.get('/properties/values/translations' + (iso_2 ? '/' + iso_2 : '')).then((response) => {
            return response.data.data.map(translationData => new KeyValueTranslation(translationData));
        })
    }

    /**
     * @param {string} category_ids (csv)
     * @return {Promise<AxiosResponse<Key>>}
     */
    requiredKeysTranslations(category_ids) {
        return axios.get('/properties/required_keys_for_categories/' + category_ids.replace(/,/g, '-')).then((response) => {
            return response.data.data.map(keyData => new Key(keyData))
        })
    }

    /**
     * @return {Promise<AxiosResponse<Property>>}
     */
    properties() {
        return axios.get('/properties/all').then((response) => {
            return response.data.data.map(propertyData => {
                return new Property(propertyData)
            })
        })
    }

    /**
     * @return {Promise<AxiosResponse<Property>>}
     */
    propertyKeys() {
        return axios.get('/properties/keys/all').then((response) => {
            return response.data.data.map(propertyData => {
                return new Key(propertyData)
            })
        })
    }

    /**
     * @param data
     * @return {Promise<Property[]>}
     */
    create(data) {
        return axios.post('/properties/create', data).then((response) => {
            return response.data.data.map(propertyData => new Property(propertyData))
        })
    }

    /**
     * @param data
     * @return {Promise<Property>}
     */
    createBasedOnExisting(data) {
        return axios.post('/properties/create_based_on_existing', data).then((response) => {
            return new Property(response.data)
        })
    }

    /**
     * @param {Property} property
     * @param iso2
     * @return {KeyValueTranslation|undefined}
     */
    propertyKeyTranslationForIso2UsingProperty(property, iso2) {
        return property.key.translations.filter(
            (translation) => {
                 return (translation.language.iso_2 === iso2)
            }
        ).shift();
    }

    /**
     * @param {Key} propertyKey
     * @param iso2
     * @return {KeyValueTranslation|undefined}
     */
    propertyKeyTranslationForIso2UsingPropertyKey(propertyKey, iso2) {
        return propertyKey.translations.find((translation) => {
            return (translation.language.iso_2 === iso2)
        });
    }

    /**
     * Finds the translation that has the same iso_2 as the given iso_2
     * in one of the properties values. It starts with the first property, and
     * then loops till it finds a translation. If it cannot be found, null will be returned.
     *
     * @param {Property} property
     * @param {string} iso2
     * @return {KeyValueTranslation|null}
     */
    propertyValueTranslationForIso2(property, iso2) {
        let valueCount = property.values.length;

        for(let index = 0; index < valueCount; index++) {
            /** @param {Value} value */
            const value = property.values[index];

            const translation = value.translations.find(
                /** @param {KeyValueTranslation} translation */
                (translation) => translation.language.iso_2 === iso2
            )

            if (translation) return translation;
        }
        return null;
    }
}