Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#129 Migrate Stores to TypeScript #130

Merged
merged 1 commit into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions client/components/list/VideoEntry.vue
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
<script>
import 'tippy.js/dist/tippy.css';
import InfoIcon from 'vue-material-design-icons/Information';
import SavedPosition from '@/store/videoProgress';
import Commons from '@/plugins/commons.js';

export default {
Expand All @@ -87,11 +86,15 @@ export default {
}),
computed: {
videoProgressPercentage() {
return (SavedPosition.getSavedPosition(this.video.videoId) / this.video.lengthSeconds) * 100;
return (
(this.$accessor.videoProgress.getSavedPositionForId(this.video.videoId) /
this.video.lengthSeconds) *
100
);
},
videoProgressTooltip() {
const watchTime = this.$formatting.getTimestampFromSeconds(
SavedPosition.getSavedPosition(this.video.videoId)
this.$accessor.videoProgress.getSavedPositionForId(this.video.videoId)
);
const totalTime = this.$formatting.getTimestampFromSeconds(this.video.lengthSeconds);
return `${watchTime} of ${totalTime}`;
Expand Down
11 changes: 8 additions & 3 deletions client/components/videoplayer/VideoPlayer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,6 @@ import FullscreenExitIcon from 'vue-material-design-icons/FullscreenExit';
import OpenInPlayerIcon from 'vue-material-design-icons/OpenInNew';
import CloseIcon from 'vue-material-design-icons/Close';
import Spinner from '@/components/Spinner';
import SavedPosition from '@/store/videoProgress';
// import VideoEndscreen from '@/components/videoplayer/VideoEndscreen'
import VolumeControl from '@/components/videoplayer/VolumeControl';
// import QualitySelection from '@/components/videoplayer/QualitySelection'
Expand Down Expand Up @@ -412,7 +411,9 @@ export default {
},
onVideoCanplay() {
if (this.$refs.video && this.videoElement.firstTimeBuffering) {
this.$refs.video.currentTime = SavedPosition.getSavedPosition(this.video.videoId);
this.$refs.video.currentTime = this.$accessor.videoProgress.getSavedPositionForId(
this.video.videoId
);
this.videoElement.firstTimeBuffering = false;
if (this.autoplay) {
this.$refs.video.play();
Expand Down Expand Up @@ -613,7 +614,11 @@ export default {
saveVideoPosition() {
const video = this.$refs.video;
if (video !== undefined) {
SavedPosition.setSavedPosition(video.currentTime, this.video.videoId);
this.$accessor.videoProgress.addVideoProgress({
videoId: this.video.videoId,
value: video.currentTime
});
// return this.$localforage.setItem(`savedVideoPositionId${videoId}`, value)
}
},
showPlayerOverlay(noTimeout) {
Expand Down
13 changes: 13 additions & 0 deletions client/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { accessorType } from '~/store';

declare module 'vue/types/vue' {
interface Vue {
$accessor: typeof accessorType;
}
}

declare module '@nuxt/types' {
interface NuxtAppOptions {
$accessor: typeof accessorType;
}
}
27 changes: 0 additions & 27 deletions client/plugins/formatting.js

This file was deleted.

31 changes: 31 additions & 0 deletions client/plugins/formatting.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import Vue from 'vue';

declare module 'vue/types/vue' {
interface Vue {
$formatting(message: string): void;
}
}

Vue.prototype.$formatting = {
getTimestampFromSeconds: seconds => {
const hours = Math.floor(seconds / 3600);
seconds -= hours * 3600;
const minutes = Math.floor(seconds / 60);
const timestampMinutes = toDoubleDigit(minutes);
seconds -= minutes * 60;
const timestampSeconds = toDoubleDigit(Math.floor(seconds));
if (hours >= 1) {
const timestampHours = toDoubleDigit(Math.floor(hours));
return `${timestampHours}:${timestampMinutes}:${timestampSeconds}`;
} else {
return `${timestampMinutes}:${timestampSeconds}`;
}

function toDoubleDigit(i) {
if (i >= 0 && i < 10) {
i = '0' + i;
}
return i;
}
}
};
29 changes: 0 additions & 29 deletions client/store/captcha.js

This file was deleted.

35 changes: 35 additions & 0 deletions client/store/captcha.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { getterTree, mutationTree, actionTree } from 'nuxt-typed-vuex';

export const state = () => ({
token: '' as string,
solution: '' as string,
image: '' as string
});

export const getters = getterTree(state, {
image: state => state.image
});

export const mutations = mutationTree(state, {
setToken(state, token) {
state.token = token;
},
setImage(state, image) {
state.image = image;
}
});

export const actions = actionTree(
{ state, getters, mutations },
{
getCaptcha({ commit }) {
this.$axios
.get(this.app.$accessor.environment.env + 'auth/captcha')
.then(response => {
commit('setToken', response.data.token);
commit('setImage', response.data.captchaImage);
})
.catch(err => console.error(err));
}
}
);
8 changes: 0 additions & 8 deletions client/store/environment.js

This file was deleted.

34 changes: 34 additions & 0 deletions client/store/environment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { getterTree, mutationTree } from 'nuxt-typed-vuex';

export const state = () => ({
env: {} as {
apiUrl: string;
vapidKey: string;
nodeEnv: string;
host: string;
port: string | number;
baseUrl: string;
}
});

export const getters = getterTree(state, {
apiUrl(state) {
return state.env.apiUrl;
}
});

export const mutations = mutationTree(state, {
setEnv(
state,
env: {
apiUrl: string;
vapidKey: string;
nodeEnv: string;
host: string;
port: string | number;
baseUrl: string;
}
) {
state.env = env;
}
});
18 changes: 0 additions & 18 deletions client/store/index.js

This file was deleted.

52 changes: 52 additions & 0 deletions client/store/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { getAccessorType, actionTree } from 'nuxt-typed-vuex';
import * as captcha from '~/store/captcha';
import * as environment from '~/store/environment';
import * as instances from '~/store/instances';
import * as messages from '~/store/messages';
import * as miniplayer from '~/store/miniplayer';
import * as settings from '~/store/settings';
import * as user from '~/store/user';
import * as videoPlayer from '~/store/videoPlayer';
import * as videoProgress from '~/store/videoProgress';

export const state = () => ({});

export type RootState = ReturnType<typeof state>;

export const actions = actionTree(
{ state },
{
nuxtServerInit({ dispatch, getters }) {
if (process.server) {
this.app.$accessor.environment.setEnv({
apiUrl: process.env.VIEWTUBE_API_URL,
vapidKey: process.env.VIEWTUBE_PUBLIC_VAPID,
nodeEnv: process.env.NODE_ENV,
host: process.env.HOST || 'localhost',
port: process.env.PORT || 8066,
baseUrl: process.env.BASE_URL || 'http://localhost:8066'
});
dispatch('user/getUser');
if (getters['instances/instances'].length === 0) {
dispatch('instances/fetchInstances');
}
}
}
}
);

export const accessorType = getAccessorType({
actions,
state,
modules: {
captcha,
environment,
instances,
messages,
miniplayer,
settings,
user,
videoPlayer,
videoProgress
}
});
70 changes: 0 additions & 70 deletions client/store/instances.js

This file was deleted.