chore: Cleanup update banner with localstorage updates (#5209)

Co-authored-by: Aswin Dev P.S <aswindevps@gmail.com>
This commit is contained in:
Pranav Raj S
2022-08-05 13:46:12 +05:30
committed by GitHub
parent fc9699d993
commit 124390a019
5 changed files with 104 additions and 63 deletions

View File

@@ -12,33 +12,26 @@
</template>
<script>
import Banner from 'dashboard/components/ui/Banner.vue';
import LocalStorage from '../../helper/localStorage';
import { LocalStorage, LOCAL_STORAGE_KEYS } from '../../helper/localStorage';
import { mapGetters } from 'vuex';
import adminMixin from 'dashboard/mixins/isAdmin';
const semver = require('semver');
const dismissedUpdates = new LocalStorage('dismissedUpdates');
import { hasAnUpdateAvailable } from './versionCheckHelper';
export default {
components: {
Banner,
},
components: { Banner },
mixins: [adminMixin],
props: {
latestChatwootVersion: {
type: String,
default: '',
},
latestChatwootVersion: { type: String, default: '' },
},
data() {
return { userDismissedBanner: false };
},
computed: {
...mapGetters({ globalConfig: 'globalConfig/get' }),
hasAnUpdateAvailable() {
if (!semver.valid(this.latestChatwootVersion)) {
return false;
}
return semver.lt(
this.globalConfig.appVersion,
this.latestChatwootVersion
updateAvailable() {
return hasAnUpdateAvailable(
this.latestChatwootVersion,
this.globalConfig.appVersion
);
},
bannerMessage() {
@@ -48,8 +41,9 @@ export default {
},
shouldShowBanner() {
return (
!this.userDismissedBanner &&
this.globalConfig.displayManifest &&
this.hasAnUpdateAvailable &&
this.updateAvailable &&
!this.isVersionNotificationDismissed(this.latestChatwootVersion) &&
this.isAdmin
);
@@ -57,17 +51,23 @@ export default {
},
methods: {
isVersionNotificationDismissed(version) {
return dismissedUpdates.get().includes(version);
const dismissedVersions =
LocalStorage.get(LOCAL_STORAGE_KEYS.DISMISSED_UPDATES) || [];
return dismissedVersions.includes(version);
},
dismissUpdateBanner() {
let updatedDismissedItems = dismissedUpdates.get();
let updatedDismissedItems =
LocalStorage.get(LOCAL_STORAGE_KEYS.DISMISSED_UPDATES) || [];
if (updatedDismissedItems instanceof Array) {
updatedDismissedItems.push(this.latestChatwootVersion);
} else {
updatedDismissedItems = [this.latestChatwootVersion];
}
dismissedUpdates.store(updatedDismissedItems);
this.latestChatwootVersion = this.globalConfig.appVersion;
LocalStorage.set(
LOCAL_STORAGE_KEYS.DISMISSED_UPDATES,
updatedDismissedItems
);
this.userDismissedBanner = true;
},
},
};

View File

@@ -0,0 +1,15 @@
import { hasAnUpdateAvailable } from '../versionCheckHelper';
describe('#hasAnUpdateAvailable', () => {
it('return false if latest version is invalid', () => {
expect(hasAnUpdateAvailable('invalid', '1.0.0')).toBe(false);
expect(hasAnUpdateAvailable(null, '1.0.0')).toBe(false);
expect(hasAnUpdateAvailable(undefined, '1.0.0')).toBe(false);
expect(hasAnUpdateAvailable('', '1.0.0')).toBe(false);
});
it('return correct value if latest version is valid', () => {
expect(hasAnUpdateAvailable('1.1.0', '1.0.0')).toBe(true);
expect(hasAnUpdateAvailable('0.1.0', '1.0.0')).toBe(false);
});
});

View File

@@ -0,0 +1,8 @@
const semver = require('semver');
export const hasAnUpdateAvailable = (latestVersion, currentVersion) => {
if (!semver.valid(latestVersion)) {
return false;
}
return semver.lt(currentVersion, latestVersion);
};