Hangar/frontend/components/mixins.ts

104 lines
3.1 KiB
TypeScript
Raw Normal View History

2021-03-13 18:10:56 +08:00
import { Component, mixins, Prop, Vue, Watch } from 'nuxt-property-decorator';
2021-02-10 07:56:02 +08:00
import { PropType } from 'vue';
2021-03-18 04:16:54 +08:00
import { HangarProject, HangarUser, HangarVersion, IPlatform, ProjectPage } from 'hangar-internal';
2021-02-10 07:56:02 +08:00
import MarkdownEditor from '~/components/MarkdownEditor.vue';
import { Platform, ReviewState } from '~/types/enums';
2021-03-12 15:59:36 +08:00
import { RootState } from '~/store';
2021-02-10 07:56:02 +08:00
2021-03-18 04:16:54 +08:00
@Component
2021-03-18 04:41:18 +08:00
export class HangarUserMixin extends Vue {
2021-03-18 04:16:54 +08:00
@Prop({ type: Object as PropType<HangarUser>, required: true })
user!: HangarUser;
}
2021-02-10 07:56:02 +08:00
@Component
export class HangarProjectMixin extends Vue {
@Prop({ type: Object as PropType<HangarProject>, required: true })
project!: HangarProject;
}
@Component
export class HangarProjectVersionMixin extends HangarProjectMixin {
@Prop({ type: Map as PropType<Map<Platform, HangarVersion>>, required: true })
versions!: Map<Platform, HangarVersion>;
2021-03-12 15:59:36 +08:00
get projectVersion(): HangarVersion {
return this.versions.get(<Platform>this.$route.params.platform.toUpperCase())!;
}
2021-03-12 15:59:36 +08:00
get platform(): IPlatform {
return (this.$store.state as RootState).platforms.get(this.$route.params.platform.toUpperCase() as Platform)!;
}
get isReviewStateChecked(): boolean {
return this.projectVersion.reviewState === ReviewState.PARTIALLY_REVIEWED || this.projectVersion.reviewState === ReviewState.REVIEWED;
}
get isUnderReview(): boolean {
return this.projectVersion.reviewState === ReviewState.UNDER_REVIEW;
}
}
2021-02-10 07:56:02 +08:00
@Component
export class DocPageMixin extends HangarProjectMixin {
editingPage: boolean = false;
page = {
contents: '',
deletable: false,
} as ProjectPage;
$refs!: {
editor: MarkdownEditor;
};
savePage(content: string) {
this.$api
.requestInternal(`pages/save/${this.project.id}/${this.page.id}`, true, 'post', {
content,
})
.then(() => {
this.page.contents = content;
this.editingPage = false;
})
.catch((err) => {
this.$refs.editor.loading.save = false;
2021-02-13 14:36:53 +08:00
this.$util.handleRequestError(err, 'page.new.error.save');
2021-02-10 07:56:02 +08:00
});
}
deletePage() {
this.$api
.requestInternal(`pages/delete/${this.project.id}/${this.page.id}`, true, 'post')
.then(() => {
this.$refs.editor.loading.delete = false;
this.$router.replace(`/${this.$route.params.author}/${this.$route.params.slug}`);
})
.catch(this.$util.handleRequestError);
}
}
@Component
export class HangarModal extends Vue {
dialog: boolean = false;
@Prop({ type: String, default: '' })
activatorClass!: string;
@Watch('dialog')
onToggleView() {
if (typeof this.$refs.modalForm !== 'undefined') {
// @ts-ignore
this.$refs.modalForm.reset();
}
}
}
@Component
2021-03-13 18:10:56 +08:00
export class HangarForm extends Vue {
2021-02-10 07:56:02 +08:00
loading: boolean = false;
validForm: boolean = false;
}
2021-03-13 18:10:56 +08:00
@Component
export class HangarFormModal extends mixins(HangarModal, HangarForm) {}