summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_shared/components/local_storage_sync.vue
blob: b5d6b872547f2b5a54e78dd9681a8ffd932e89f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<script>
export default {
  props: {
    storageKey: {
      type: String,
      required: true,
    },
    value: {
      type: String,
      required: false,
      default: '',
    },
  },
  watch: {
    value(newVal) {
      this.saveValue(newVal);
    },
  },
  mounted() {
    // On mount, trigger update if we actually have a localStorageValue
    const value = this.getValue();

    if (value && this.value !== value) {
      this.$emit('input', value);
    }
  },
  methods: {
    getValue() {
      return localStorage.getItem(this.storageKey);
    },
    saveValue(val) {
      localStorage.setItem(this.storageKey, val);
    },
  },
  render() {
    return this.$slots.default;
  },
};
</script>