Current File : /home/n742ef5/royalanteam.com/wp-content/plugins/myhome-updater/vue/src/Updater.vue
<script>
    export default {
        name: "myhome-updater",
        data() {
            return {
                currentPlugins: [],
                currentThemeStatus: ''
            }
        },
        props: {
            queryUrl: String,
            plugins: {
                type: Array,
                default: () => {
                    return []
                }
            },
            themeStatus: String,
        },
        computed: {
            updateInProgress() {
                if (this.currentThemeStatus === 'updating') {
                    return true
                }

                return typeof this.plugins.find((plugin) => {
                    return plugin.status === 'updating'
                }) !== 'undefined'
            }
        },
        methods: {
            onStart() {
                if (this.currentThemeStatus === 'ok') {
                    this.updatePlugins()
                } else {
                    this.updateTheme()
                }
            },
            updatePlugins() {
                let plugin = this.currentPlugins.find((plugin) => {
                    return plugin.status === 'need_update'
                });

                if (typeof plugin === 'undefined') {
                    return
                }

                this.updatePlugin(plugin)
            },
            updatePlugin(plugin) {
                plugin.status = 'updating';

                jQuery.ajax({
                    url: this.queryUrl + 'myhome_updater_plugin',
                    type: 'post',
                    data: {
                        pluginKey: plugin.key
                    }
                }).success(() => {
                    plugin.status = 'ok';
                    this.updatePlugins()
                }).error(() => {
                    plugin.status = 'need_update';
                    this.updatePlugins()
                })
            },
            updateTheme() {
                this.currentThemeStatus = 'updating';

                jQuery.ajax({
                    url: this.queryUrl + 'myhome_updater_theme',
                    type: 'post',
                }).success(() => {
                    this.currentThemeStatus = 'ok';
                    this.updatePlugins()
                }).error(() => {
                    this.updateTheme()
                })
            }
        },
        render() {
            return this.$scopedSlots.default({
                onStart: this.onStart,
                plugins: this.currentPlugins,
                themeStatus: this.currentThemeStatus,
                updateInProgress: this.updateInProgress,
            })
        },
        created() {
            this.currentPlugins = this.plugins;
            this.currentThemeStatus = this.themeStatus;
        }
    }
</script>