2019-06-01 00:40:35 +09:00
|
|
|
import Vue from 'vue';
|
2020-06-06 18:01:56 +09:00
|
|
|
import TagInput from './components/TagInput.vue';
|
2019-06-11 22:16:40 +09:00
|
|
|
import MetadataPreview from './components/MetadataPreview.vue';
|
2020-08-06 21:31:53 +09:00
|
|
|
import { fetchGet, ResponseError } from './fetch';
|
2020-05-17 17:11:35 +09:00
|
|
|
import * as React from 'react';
|
|
|
|
import * as ReactDOM from 'react-dom';
|
2020-08-13 01:57:09 +09:00
|
|
|
import { TagInput as TagInput2 } from './components/TagInput2';
|
2019-06-11 22:16:40 +09:00
|
|
|
|
2020-06-06 18:01:56 +09:00
|
|
|
export const bus = new Vue({ name: 'EventBus' });
|
2019-03-05 01:26:17 +09:00
|
|
|
|
2019-06-28 00:27:31 +09:00
|
|
|
export enum MetadataLoadState {
|
|
|
|
Inactive,
|
|
|
|
Loading,
|
|
|
|
Success,
|
|
|
|
Failed,
|
|
|
|
}
|
|
|
|
|
2020-08-13 00:38:26 +09:00
|
|
|
export type Metadata = {
|
|
|
|
url: string;
|
|
|
|
title: string;
|
|
|
|
description: string;
|
|
|
|
image: string;
|
|
|
|
expires_at: string | null;
|
|
|
|
tags: {
|
|
|
|
name: string;
|
|
|
|
}[];
|
|
|
|
};
|
|
|
|
|
2019-06-01 00:40:35 +09:00
|
|
|
new Vue({
|
|
|
|
el: '#app',
|
2019-06-11 22:16:40 +09:00
|
|
|
data: {
|
2019-06-28 00:27:31 +09:00
|
|
|
metadata: null,
|
|
|
|
metadataLoadState: MetadataLoadState.Inactive,
|
2019-06-11 22:16:40 +09:00
|
|
|
},
|
2019-06-01 00:40:35 +09:00
|
|
|
components: {
|
2019-06-11 22:16:40 +09:00
|
|
|
TagInput,
|
2020-06-06 18:01:56 +09:00
|
|
|
MetadataPreview,
|
2019-06-11 22:16:40 +09:00
|
|
|
},
|
|
|
|
mounted() {
|
2019-06-26 23:20:53 +09:00
|
|
|
// オカズリンクにURLがセットされている場合は、すぐにメタデータを取得する
|
2020-06-06 18:01:56 +09:00
|
|
|
const linkInput = this.$el.querySelector<HTMLInputElement>('#link');
|
2019-06-26 23:20:53 +09:00
|
|
|
if (linkInput && /^https?:\/\//.test(linkInput.value)) {
|
|
|
|
this.fetchMetadata(linkInput.value);
|
|
|
|
}
|
2019-06-11 22:16:40 +09:00
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
// オカズリンクの変更時
|
|
|
|
onChangeLink(event: Event) {
|
|
|
|
if (event.target instanceof HTMLInputElement) {
|
|
|
|
const url = event.target.value;
|
|
|
|
|
|
|
|
if (url.trim() === '' || !/^https?:\/\//.test(url)) {
|
|
|
|
this.metadata = null;
|
2019-06-28 00:27:31 +09:00
|
|
|
this.metadataLoadState = MetadataLoadState.Inactive;
|
2019-06-11 22:16:40 +09:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-26 23:20:53 +09:00
|
|
|
this.fetchMetadata(url);
|
2019-06-11 22:16:40 +09:00
|
|
|
}
|
2019-06-26 23:20:53 +09:00
|
|
|
},
|
|
|
|
// メタデータの取得
|
|
|
|
fetchMetadata(url: string) {
|
2019-06-28 00:27:31 +09:00
|
|
|
this.metadataLoadState = MetadataLoadState.Loading;
|
|
|
|
|
2020-08-06 21:31:53 +09:00
|
|
|
fetchGet('/api/checkin/card', { url })
|
|
|
|
.then((response) => {
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new ResponseError(response);
|
|
|
|
}
|
|
|
|
return response.json();
|
|
|
|
})
|
2020-06-06 18:01:56 +09:00
|
|
|
.then((data) => {
|
|
|
|
this.metadata = data;
|
|
|
|
this.metadataLoadState = MetadataLoadState.Success;
|
|
|
|
})
|
2020-08-06 21:31:53 +09:00
|
|
|
.catch(() => {
|
2020-06-06 18:01:56 +09:00
|
|
|
this.metadata = null;
|
|
|
|
this.metadataLoadState = MetadataLoadState.Failed;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
},
|
2019-03-05 01:26:17 +09:00
|
|
|
});
|
2020-05-17 17:11:35 +09:00
|
|
|
|
|
|
|
ReactDOM.render(
|
2020-08-13 00:38:26 +09:00
|
|
|
<TagInput2 id={'tagInput2'} name={'tags2'} value={''} isInvalid={false} />,
|
2020-05-17 17:11:35 +09:00
|
|
|
document.querySelector('#tagInput2')
|
|
|
|
);
|