Skip to content

Commit 6b4f730

Browse files
committed
refactor(ui): update new Snackbar implementation
This commit updates the new Snackbar implementation, based on a plugin. The new component, the plugin, and its helper were updated. The component was added to `AppLayout.vue`, along with the old Snackbar.
1 parent f8cfd27 commit 6b4f730

File tree

5 files changed

+61
-45
lines changed

5 files changed

+61
-45
lines changed

ui/src/components/Snackbar/SnackbarNew.vue

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,20 @@
33
v-model="show"
44
location="top"
55
:timeout="4000"
6-
:color="type"
7-
ransition="slide-x-transition"
6+
:color="color"
7+
transition="slide-x-transition"
88
>
9-
{{ message }}
9+
<p class="w-100 text-center">{{ message }}</p>
1010
</v-snackbar>
1111
</template>
1212

1313
<script setup lang="ts">
14-
import { ref } from "vue";
15-
import { useStore } from "@/store";
14+
import { computed } from "vue";
15+
import { plugin } from "@/plugins/snackbar";
1616
17-
const store = useStore();
18-
const show = ref(false);
19-
const message = ref("");
20-
const type = ref("");
17+
const show = computed(() => plugin.getShow());
2118
22-
store.subscribe((mutation, state) => {
23-
if (mutation.type === "snackbar/showMessage") {
24-
message.value = state.snackbar.message;
25-
type.value = state.snackbar.type;
26-
show.value = true;
27-
}
28-
});
19+
const message = computed(() => plugin.getMessage());
20+
const type = computed(() => plugin.getType());
21+
const color = computed(() => type.value);
2922
</script>

ui/src/helpers/snackbar.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { inject } from "vue";
2-
import { PluginInterface, InjectionKey } from "@/plugins/snackbar";
2+
import { InjectionKey, type ISnackbarPlugin } from "@/plugins/snackbar";
33

4-
function useSnackbar(): PluginInterface {
5-
return inject(InjectionKey) as PluginInterface;
6-
}
4+
const useSnackbar = (): ISnackbarPlugin => inject(InjectionKey) as ISnackbarPlugin;
75

86
export default useSnackbar;

ui/src/layouts/AppLayout.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
</v-navigation-drawer>
119119

120120
<Snackbar />
121+
<SnackbarNew />
121122

122123
<AppBar v-model="showNavigationDrawer" data-test="app-bar" />
123124

@@ -164,6 +165,7 @@ import Namespace from "@/components/Namespace/Namespace.vue";
164165
import AppBar from "../components/AppBar/AppBar.vue";
165166
import QuickConnection from "../components/QuickConnection/QuickConnection.vue";
166167
import NamespaceAdd from "@/components/Namespace/NamespaceAdd.vue";
168+
import SnackbarNew from "@/components/Snackbar/SnackbarNew.vue";
167169
import Snackbar from "@/components/Snackbar/Snackbar.vue";
168170
169171
const router = useRouter();

ui/src/plugins/snackbar.ts

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,56 @@
1-
import { App, Plugin } from "vue";
1+
import { App, Plugin, reactive } from "vue";
22

3-
const InjectionKey = "snackbar";
3+
const InjectionKey = Symbol("snackbar");
44

5-
interface PluginInterface {
6-
showInfo(message: string);
7-
showSuccess(message: string);
8-
showWarning(message: string);
9-
showError(message: string);
5+
type SnackbarType = "success" | "error" | "info" | "warning";
6+
7+
interface SnackbarState {
8+
message: string;
9+
type: SnackbarType;
10+
show: boolean;
11+
}
12+
13+
interface ISnackbarPlugin {
14+
showSuccess: (msg: string) => void;
15+
showError: (msg: string) => void;
16+
showInfo: (msg: string) => void;
17+
showWarning: (msg: string) => void;
18+
getMessage: () => string;
19+
getType: () => SnackbarType;
20+
getShow: () => boolean;
1021
}
1122

23+
const state = reactive<SnackbarState>({
24+
message: "",
25+
type: "info",
26+
show: false,
27+
});
28+
29+
const showSnackbar = (type: SnackbarType, message: string) => {
30+
state.message = message;
31+
state.type = type;
32+
state.show = true;
33+
34+
setTimeout(() => {
35+
state.show = false;
36+
}, 4000);
37+
};
38+
39+
const plugin: ISnackbarPlugin = {
40+
showSuccess: (msg: string) => showSnackbar("success", msg),
41+
showError: (msg: string) => showSnackbar("error", msg),
42+
showInfo: (msg: string) => showSnackbar("info", msg),
43+
showWarning: (msg: string) => showSnackbar("warning", msg),
44+
getMessage: () => state.message,
45+
getType: () => state.type,
46+
getShow: () => state.show,
47+
};
48+
1249
const SnackbarPlugin: Plugin = {
1350
install(app: App) {
14-
const store = app.config.globalProperties.$store;
15-
16-
const plugin = {
17-
showInfo(message: string) {
18-
store.commit("snackbar/showMessage", { type: "info", message });
19-
},
20-
showSuccess(message: string) {
21-
store.commit("snackbar/showMessage", { type: "success", message });
22-
},
23-
showWarning(message: string) {
24-
store.commit("snackbar/showMessage", { type: "warning", message });
25-
},
26-
showError(message: string) {
27-
store.commit("snackbar/showMessage", { type: "error", message });
28-
},
29-
} as PluginInterface;
30-
3151
app.provide(InjectionKey, plugin);
3252
},
3353
};
3454

35-
export { InjectionKey, type PluginInterface, SnackbarPlugin };
55+
export { SnackbarPlugin, InjectionKey, plugin };
56+
export type { SnackbarState, ISnackbarPlugin };

ui/tests/layouts/__snapshots__/AppLayout.spec.ts.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ exports[`App Layout Component > Renders the component 1`] = `
246246
<!---->
247247
<!---->
248248
<!---->
249+
<!---->
250+
<!---->
249251
<header class="v-toolbar v-toolbar--flat v-toolbar--floating v-toolbar--density-default v-theme--dark v-locale--is-ltr v-app-bar bg-background border-b-thin" style="top: 0px; z-index: 1004; transform: translateY(0px); position: fixed; transition: none; left: 0px; width: calc(100% - 0px - 0px);" data-test="app-bar">
250252
<!---->
251253
<div class="v-toolbar__content" style="height: 64px;">

0 commit comments

Comments
 (0)