Skip to content

Commit

Permalink
Merge pull request #346 from kure-kosen/fix/send-contact
Browse files Browse the repository at this point in the history
send-contactの修正
  • Loading branch information
kobakazu0429 authored Jun 2, 2019
2 parents 4fb11f9 + 5741f23 commit db1f2fe
Show file tree
Hide file tree
Showing 17 changed files with 817 additions and 410 deletions.
47 changes: 24 additions & 23 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,45 @@
"lint": "npx tslint --fix -c ./tslint.json 'src/**/*{.ts,.tsx}'"
},
"devDependencies": {
"@types/react": "^16.8.17",
"@types/react-dom": "^16.8.4",
"@types/react-router-dom": "^4.3.3",
"@types/react-slick": "^0.23.4",
"@types/styled-components": "^4.1.15",
"awesome-typescript-loader": "^5.0.0",
"babel-loader": "^7.1.4",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"clean-webpack-plugin": "^1.0.0",
"css-loader": "^0.28.11",
"file-loader": "^1.1.11",
"prettier": "^1.15.3",
"source-map-loader": "^0.2.3",
"style-loader": "^0.21.0",
"tslint": "^5.11.0",
"tslint-config-prettier": "^1.17.0",
"tslint-plugin-prettier": "^2.0.1",
"typescript": "^3.4.5",
"url-loader": "^1.0.1",
"webpack": "^4.10.2",
"webpack-cli": "^2.1.4"
"webpack-cli": "^2.1.4",
"webpack-dev-server": "^3.1.10",
"webpack-manifest-plugin": "^2.0.4",
"webpack-merge": "^4.1.4"
},
"dependencies": {
"@types/react": "^16.3.16",
"@types/react-dom": "^16.0.5",
"@types/react-router-dom": "^4.2.7",
"@types/react-slick": "^0.23.4",
"@types/styled-components": "^4.1.6",
"axios": "^0.18.0",
"axios": "0.19.0-beta.1",
"babel-polyfill": "^6.26.0",
"clean-webpack-plugin": "^1.0.0",
"file-loader": "^1.1.11",
"mobx": "^5.0.3",
"mobx-react": "^5.2.3",
"mobx": "^5.9.4",
"mobx-react": "^5.4.4",
"mobx-react-lite": "^1.3.2",
"normalize.css": "^8.0.0",
"prettier": "^1.15.3",
"react": "^16.4.0",
"react-dom": "^16.4.0",
"react-router-dom": "^4.3.1",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-router-dom": "^5.0.0",
"react-slick": "^0.24.0",
"react-twitter-widgets": "^1.7.1",
"slick-carousel": "^1.8.1",
"styled-components": "^4.1.3",
"tslint": "^5.11.0",
"tslint-config-prettier": "^1.17.0",
"tslint-plugin-prettier": "^2.0.1",
"url-loader": "^1.0.1",
"webpack-dev-server": "^3.1.10",
"webpack-manifest-plugin": "^2.0.4",
"webpack-merge": "^4.1.4"
"styled-components": "^4.2.0"
}
}
17 changes: 16 additions & 1 deletion frontend/src/api/ContactApi.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
import RestClient from "./RestClient";

export interface IContactEnum {
corners: { [key: string]: number };
departments: { [key: string]: number };
grades: { [key: string]: number };
}

export default class ContactApi {
public restClient: RestClient;

constructor(restClient: RestClient) {
this.restClient = restClient;
}

public saveContact(json: object, succussed: (res: object) => void, errored: (err: object) => void, always = () => {}) {
public saveContact(
json: object,
succussed: (res: object) => void,
errored: (err: object) => void,
always = () => {}
) {
return this.restClient.post("/api/v1/contacts", json, succussed, errored, always);
}

public fetchContactEnum() {
return this.restClient.get<IContactEnum>("/api/v1/contacts/enum");
}
}
125 changes: 68 additions & 57 deletions frontend/src/api/RestClient.ts
Original file line number Diff line number Diff line change
@@ -1,83 +1,94 @@
import axios, { AxiosInstance } from "axios";

export default class RestClient {
public axios: any;
public axios: AxiosInstance;

constructor(baseUrl: string) {
const axiosBase = require("axios");
const csrfToken = (document.querySelector("meta[name=csrf-token]") as HTMLMetaElement).content;

this.axios = axiosBase.create({
this.axios = axios.create({
baseURL: baseUrl,
timeout: 1000,
headers: {
"Content-Type": "application/json",
"X-CSRF-TOKEN": csrfToken
}
});

// TODO: Productionではログを流さないようにする
this.axios.interceptors.response.use(
response => {
const { config, data, status } = response;
const { method, params, url } = config;

console.group(`${method ? method.toUpperCase() : "undefined method"}:${status} - ${url}`);
if (params) console.table(params);
console.log(data);
console.groupEnd();

return response;
},
error => {
console.log(error);
return Promise.reject(error);
}
);
}

public get(
public async get<T>(
path: string,
params: object,
successed: (res: object) => void,
errored: (res: object) => void,
always = () => {}
params?: object,
successed?: (res: object) => void,
errored?: (res: object) => void,
always: () => any = () => {}
) {
return this.axios
.get(path, { params })
.then((result: object) => {
console.log(`GET ${this.axios.baseURL}/${path}`);
console.log(`result: ${JSON.stringify(result)}`);
successed(result);
})
.catch((error: object) => {
console.log(`ERROR! GET ${this.axios.baseURL}/${path}`);
console.log(`error: ${JSON.stringify(error)}`);
errored(error);
})
.then(always());
try {
const response = await this.axios.get<T>(path, { params });
if (successed) successed(response);
return response;
} catch (error) {
if (errored) errored(error);
throw error;
} finally {
always();
}
}

public post(
public async post<T>(
path: string,
params: object,
successed: (res: object) => void,
errored: (res: object) => void,
always = () => {}
params?: object,
successed?: (res: object) => void,
errored?: (res: object) => void,
always: () => any = () => {}
) {
return this.axios
.post(path, params)
.then((result: object) => {
console.log(`POST ${this.axios.baseURL}/${path}`);
console.log(`result: ${result}`);
successed(result);
})
.catch((error: object) => {
console.log(`ERROR! POST ${this.axios.baseURL}/${path}`);
console.log(`error: ${error}`);
errored(error);
})
.then(always());
try {
const response = await this.axios.post<T>(path, params);
if (successed) successed(response);
return response;
} catch (error) {
if (errored) errored(error);
throw error;
} finally {
always();
}
}

public delete(
public async delete<T>(
path: string,
params: object,
successed: (res: object) => void,
errored: (res: object) => void,
always = () => {}
params?: object,
successed?: (res: object) => void,
errored?: (res: object) => void,
always: () => any = () => {}
) {
return this.axios
.delete(path, { data: { params } })
.then((result: object) => {
console.log(`DELETE ${this.axios.baseURL}/${path}`);
console.log(`result: ${result}`);
successed(result);
})
.catch((error: object) => {
console.log(`ERROR! DELETE ${this.axios.baseURL}/${path}`);
console.log(`error: ${error}`);
errored(error);
})
.then(always());
try {
const response = await this.axios.delete<T>(path, { data: { params } });
if (successed) successed(response);
return response;
} catch (error) {
if (errored) errored(error);
throw error;
} finally {
always();
}
}
}
10 changes: 5 additions & 5 deletions frontend/src/commons/ChkButtonBase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,29 @@ interface IPropsChkButtonBase extends IPropsCss {
to?: string;
text: string | React.ReactNode;
name?: string;
onClick?: (e: any) => any;
}

const ChkButtonBase = (props: IPropsChkButtonBase) => {
const { to, text, name } = props;
const { color, bgcolor, border } = props;
const { to, text, name, color, bgcolor, border, onClick, ...otherProps } = props;

if (to) {
if (to.startsWith("http")) {
return (
<ATag color={color} bgcolor={bgcolor} border={border} href={to}>
<ATag color={color} bgcolor={bgcolor} border={border} href={to} {...otherProps}>
{text}
</ATag>
);
} else {
return (
<LinkTag color={color} bgcolor={bgcolor} border={border} to={to}>
<LinkTag color={color} bgcolor={bgcolor} border={border} to={to} {...otherProps}>
{text}
</LinkTag>
);
}
} else {
return (
<ButtonTag name={name} color={color} bgcolor={bgcolor} border={border}>
<ButtonTag name={name} color={color} bgcolor={bgcolor} border={border} onClick={onClick} {...otherProps}>
{text}
</ButtonTag>
);
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/commons/color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ export const chkColors: IColor = {
white: "#ffffff",
placeholder: "#b0bec5",
aqua: "#b3dfe2",
darkenAqua: "#50AAB7"
darkenAqua: "#50AAB7",
error: "#ff4b42",
disabled: "#cccccc"
};
Loading

0 comments on commit db1f2fe

Please sign in to comment.