2019-02-05 19:30:31 -08:00
|
|
|
import { pick } from 'lodash';
|
2018-10-30 09:35:47 -07:00
|
|
|
import { store } from 'react-easy-state';
|
|
|
|
import axios, { AxiosError } from 'axios';
|
2019-02-05 19:30:31 -08:00
|
|
|
import {
|
|
|
|
User,
|
|
|
|
Proposal,
|
|
|
|
RFP,
|
|
|
|
RFPArgs,
|
|
|
|
EmailExample,
|
|
|
|
PageQuery,
|
|
|
|
PROPOSAL_STATUS,
|
|
|
|
} from './types';
|
2018-10-30 09:35:47 -07:00
|
|
|
|
|
|
|
// API
|
|
|
|
const api = axios.create({
|
|
|
|
baseURL: process.env.BACKEND_URL + '/api/v1',
|
|
|
|
withCredentials: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
async function login(username: string, password: string) {
|
|
|
|
const { data } = await api.post('/admin/login', {
|
|
|
|
username,
|
|
|
|
password,
|
|
|
|
});
|
|
|
|
return data.isLoggedIn;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function logout() {
|
|
|
|
const { data } = await api.get('/admin/logout');
|
|
|
|
return data.isLoggedIn;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function checkLogin() {
|
|
|
|
const { data } = await api.get('/admin/checklogin');
|
|
|
|
return data.isLoggedIn;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function fetchStats() {
|
|
|
|
const { data } = await api.get('/admin/stats');
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function fetchUsers() {
|
|
|
|
const { data } = await api.get('/admin/users');
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2019-01-16 21:01:29 -08:00
|
|
|
async function fetchUserDetail(id: number) {
|
|
|
|
const { data } = await api.get(`/admin/users/${id}`);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2019-02-04 13:18:50 -08:00
|
|
|
async function deleteUser(id: number) {
|
2018-10-30 09:35:47 -07:00
|
|
|
const { data } = await api.delete('/admin/users/' + id);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2019-02-06 10:38:07 -08:00
|
|
|
async function fetchArbiters(search: string) {
|
|
|
|
const { data } = await api.get(`/admin/arbiters`, { params: { search } });
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function setArbiter(proposalId: number, userId: number) {
|
|
|
|
const { data } = await api.put(`/admin/arbiters`, { proposalId, userId });
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2019-02-05 12:34:19 -08:00
|
|
|
async function fetchProposals(params: Partial<PageQuery>) {
|
2019-01-09 10:23:08 -08:00
|
|
|
const { data } = await api.get('/admin/proposals', {
|
2019-02-05 12:34:19 -08:00
|
|
|
params,
|
2019-01-09 10:23:08 -08:00
|
|
|
});
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function fetchProposalDetail(id: number) {
|
|
|
|
const { data } = await api.get(`/admin/proposals/${id}`);
|
2018-10-30 09:35:47 -07:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2019-01-29 15:50:27 -08:00
|
|
|
async function updateProposal(p: Partial<Proposal>) {
|
|
|
|
const { data } = await api.put('/admin/proposals/' + p.proposalId, p);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2018-11-09 10:48:55 -08:00
|
|
|
async function deleteProposal(id: number) {
|
2018-10-30 09:35:47 -07:00
|
|
|
const { data } = await api.delete('/admin/proposals/' + id);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2019-01-09 10:23:08 -08:00
|
|
|
async function approveProposal(id: number, isApprove: boolean, rejectReason?: string) {
|
|
|
|
const { data } = await api.put(`/admin/proposals/${id}/approve`, {
|
|
|
|
isApprove,
|
|
|
|
rejectReason,
|
|
|
|
});
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2019-01-09 11:08:25 -08:00
|
|
|
async function getEmailExample(type: string) {
|
|
|
|
const { data } = await api.get(`/admin/email/example/${type}`);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2019-01-30 09:59:15 -08:00
|
|
|
async function getRFPs() {
|
|
|
|
const { data } = await api.get(`/admin/rfps`);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function createRFP(args: RFPArgs) {
|
|
|
|
const { data } = await api.post('/admin/rfps', args);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function editRFP(id: number, args: RFPArgs) {
|
|
|
|
const { data } = await api.put(`/admin/rfps/${id}`, args);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function deleteRFP(id: number) {
|
|
|
|
await api.delete(`/admin/rfps/${id}`);
|
|
|
|
}
|
|
|
|
|
2018-10-30 09:35:47 -07:00
|
|
|
// STORE
|
|
|
|
const app = store({
|
|
|
|
hasCheckedLogin: false,
|
|
|
|
isLoggedIn: false,
|
|
|
|
loginError: '',
|
|
|
|
generalError: [] as string[],
|
2019-01-09 10:23:08 -08:00
|
|
|
statsFetched: false,
|
|
|
|
statsFetching: false,
|
2018-10-30 09:35:47 -07:00
|
|
|
stats: {
|
2019-01-09 10:23:08 -08:00
|
|
|
userCount: 0,
|
|
|
|
proposalCount: 0,
|
|
|
|
proposalPendingCount: 0,
|
2019-02-05 12:45:26 -08:00
|
|
|
proposalNoArbiterCount: 0,
|
2018-10-30 09:35:47 -07:00
|
|
|
},
|
2019-01-16 21:01:29 -08:00
|
|
|
|
|
|
|
usersFetching: false,
|
2018-10-30 09:35:47 -07:00
|
|
|
usersFetched: false,
|
|
|
|
users: [] as User[],
|
2019-01-16 21:01:29 -08:00
|
|
|
userDetailFetching: false,
|
|
|
|
userDetail: null as null | User,
|
2019-02-04 13:18:50 -08:00
|
|
|
userDeleting: false,
|
|
|
|
userDeleted: false,
|
2019-01-16 21:01:29 -08:00
|
|
|
|
2019-02-06 10:38:07 -08:00
|
|
|
arbitersSearch: {
|
|
|
|
search: '',
|
|
|
|
results: [] as User[],
|
|
|
|
fetching: false,
|
|
|
|
error: null as string | null,
|
|
|
|
},
|
|
|
|
|
2019-02-05 12:34:19 -08:00
|
|
|
proposals: {
|
|
|
|
page: {
|
|
|
|
page: 1,
|
|
|
|
search: '',
|
|
|
|
sort: 'CREATED:DESC',
|
2019-02-05 19:30:31 -08:00
|
|
|
filters: {
|
|
|
|
status: [] as PROPOSAL_STATUS[],
|
|
|
|
other: [] as string[],
|
|
|
|
},
|
2019-02-05 12:34:19 -08:00
|
|
|
pageSize: 0,
|
|
|
|
total: 0,
|
|
|
|
items: [] as Proposal[],
|
|
|
|
fetching: false,
|
|
|
|
fetched: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2019-01-09 10:23:08 -08:00
|
|
|
proposalDetailFetching: false,
|
|
|
|
proposalDetail: null as null | Proposal,
|
|
|
|
proposalDetailApproving: false,
|
2019-01-16 21:01:29 -08:00
|
|
|
|
2019-01-30 09:59:15 -08:00
|
|
|
rfps: [] as RFP[],
|
|
|
|
rfpsFetching: false,
|
|
|
|
rfpsFetched: false,
|
|
|
|
rfpSaving: false,
|
|
|
|
rfpSaved: false,
|
|
|
|
rfpDeleting: false,
|
|
|
|
rfpDeleted: false,
|
|
|
|
|
2019-01-09 11:08:25 -08:00
|
|
|
emailExamples: {} as { [type: string]: EmailExample },
|
2018-10-30 09:35:47 -07:00
|
|
|
|
|
|
|
removeGeneralError(i: number) {
|
|
|
|
app.generalError.splice(i, 1);
|
|
|
|
},
|
|
|
|
|
2019-01-09 10:23:08 -08:00
|
|
|
updateProposalInStore(p: Proposal) {
|
2019-02-05 12:34:19 -08:00
|
|
|
const index = app.proposals.page.items.findIndex(x => x.proposalId === p.proposalId);
|
2019-01-09 10:23:08 -08:00
|
|
|
if (index > -1) {
|
2019-02-05 12:34:19 -08:00
|
|
|
app.proposals.page.items[index] = p;
|
2019-01-09 10:23:08 -08:00
|
|
|
}
|
|
|
|
if (app.proposalDetail && app.proposalDetail.proposalId === p.proposalId) {
|
|
|
|
app.proposalDetail = p;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-02-06 10:38:07 -08:00
|
|
|
updateUserInStore(u: User) {
|
|
|
|
const index = app.users.findIndex(x => x.userid === u.userid);
|
|
|
|
if (index > -1) {
|
|
|
|
app.users[index] = u;
|
|
|
|
}
|
|
|
|
if (app.userDetail && app.userDetail.userid === u.userid) {
|
|
|
|
app.userDetail = u;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-10-30 09:35:47 -07:00
|
|
|
async checkLogin() {
|
|
|
|
app.isLoggedIn = await checkLogin();
|
|
|
|
app.hasCheckedLogin = true;
|
|
|
|
},
|
|
|
|
|
|
|
|
async login(username: string, password: string) {
|
|
|
|
try {
|
|
|
|
app.isLoggedIn = await login(username, password);
|
|
|
|
} catch (e) {
|
|
|
|
app.loginError = e.response.data.message;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
async logout() {
|
|
|
|
try {
|
|
|
|
app.isLoggedIn = await logout();
|
|
|
|
} catch (e) {
|
|
|
|
app.generalError.push(e.toString());
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
async fetchStats() {
|
2019-01-09 10:23:08 -08:00
|
|
|
app.statsFetching = true;
|
2018-10-30 09:35:47 -07:00
|
|
|
try {
|
|
|
|
app.stats = await fetchStats();
|
2019-01-09 10:23:08 -08:00
|
|
|
app.statsFetched = true;
|
2018-10-30 09:35:47 -07:00
|
|
|
} catch (e) {
|
|
|
|
handleApiError(e);
|
|
|
|
}
|
2019-01-09 10:23:08 -08:00
|
|
|
app.statsFetching = false;
|
2018-10-30 09:35:47 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
async fetchUsers() {
|
2019-01-16 21:01:29 -08:00
|
|
|
app.usersFetching = true;
|
2018-10-30 09:35:47 -07:00
|
|
|
try {
|
|
|
|
app.users = await fetchUsers();
|
|
|
|
app.usersFetched = true;
|
|
|
|
} catch (e) {
|
|
|
|
handleApiError(e);
|
|
|
|
}
|
2019-01-16 21:01:29 -08:00
|
|
|
app.usersFetching = false;
|
|
|
|
},
|
|
|
|
|
|
|
|
async fetchUserDetail(id: number) {
|
|
|
|
app.userDetailFetching = true;
|
|
|
|
try {
|
|
|
|
app.userDetail = await fetchUserDetail(id);
|
|
|
|
} catch (e) {
|
|
|
|
handleApiError(e);
|
|
|
|
}
|
|
|
|
app.userDetailFetching = false;
|
2018-10-30 09:35:47 -07:00
|
|
|
},
|
|
|
|
|
2019-02-04 13:18:50 -08:00
|
|
|
async deleteUser(id: number) {
|
|
|
|
app.userDeleting = false;
|
|
|
|
app.userDeleted = false;
|
2018-10-30 09:35:47 -07:00
|
|
|
try {
|
|
|
|
await deleteUser(id);
|
2019-02-04 13:18:50 -08:00
|
|
|
app.users = app.users.filter(u => u.userid !== id);
|
|
|
|
app.userDeleted = true;
|
|
|
|
app.userDetail = null;
|
2018-10-30 09:35:47 -07:00
|
|
|
} catch (e) {
|
|
|
|
handleApiError(e);
|
|
|
|
}
|
2019-02-04 13:18:50 -08:00
|
|
|
app.userDeleting = false;
|
2018-10-30 09:35:47 -07:00
|
|
|
},
|
|
|
|
|
2019-02-06 10:38:07 -08:00
|
|
|
async searchArbiters(search: string) {
|
|
|
|
app.arbitersSearch = {
|
|
|
|
...app.arbitersSearch,
|
|
|
|
search,
|
|
|
|
fetching: true,
|
|
|
|
};
|
|
|
|
try {
|
|
|
|
const data = await fetchArbiters(search);
|
|
|
|
app.arbitersSearch = {
|
|
|
|
...app.arbitersSearch,
|
|
|
|
...data,
|
|
|
|
};
|
|
|
|
} catch (e) {
|
|
|
|
handleApiError(e);
|
|
|
|
}
|
|
|
|
app.arbitersSearch.fetching = false;
|
|
|
|
},
|
|
|
|
|
|
|
|
async searchArbitersClear() {
|
|
|
|
app.arbitersSearch = {
|
|
|
|
search: '',
|
|
|
|
results: [] as User[],
|
|
|
|
fetching: false,
|
|
|
|
error: null,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
async setArbiter(proposalId: number, userId: number) {
|
|
|
|
// let component handle errors for this one
|
|
|
|
const { proposal, user } = await setArbiter(proposalId, userId);
|
|
|
|
this.updateProposalInStore(proposal);
|
|
|
|
this.updateUserInStore(user);
|
|
|
|
},
|
|
|
|
|
2019-02-05 12:34:19 -08:00
|
|
|
async fetchProposals() {
|
|
|
|
app.proposals.page.fetching = true;
|
2018-10-30 09:35:47 -07:00
|
|
|
try {
|
2019-02-05 12:34:19 -08:00
|
|
|
const page = await fetchProposals(app.getProposalPageQuery());
|
2019-02-05 19:30:31 -08:00
|
|
|
// filter strings with prefix p, and remove the prefix
|
|
|
|
const swp = (p: string, a: string[]) =>
|
|
|
|
a.filter((s: string) => s.startsWith(p)).map(x => x.replace(p, ''));
|
2019-02-05 12:34:19 -08:00
|
|
|
app.proposals.page = {
|
|
|
|
...app.proposals.page,
|
|
|
|
...page,
|
2019-02-05 19:30:31 -08:00
|
|
|
filters: {
|
|
|
|
status: swp('STATUS_', page.filters),
|
|
|
|
other: swp('OTHER_', page.filters),
|
|
|
|
},
|
2019-02-05 12:34:19 -08:00
|
|
|
fetched: true,
|
|
|
|
};
|
2018-10-30 09:35:47 -07:00
|
|
|
} catch (e) {
|
|
|
|
handleApiError(e);
|
|
|
|
}
|
2019-02-05 12:34:19 -08:00
|
|
|
app.proposals.page.fetching = false;
|
|
|
|
},
|
|
|
|
|
|
|
|
getProposalPageQuery() {
|
2019-02-05 19:30:31 -08:00
|
|
|
const pq = pick(app.proposals.page, ['page', 'search', 'filters', 'sort']) as any;
|
|
|
|
const pfx = (p: string) => (s: string) => p + s;
|
|
|
|
pq.filters = [
|
|
|
|
...pq.filters.status.map(pfx('STATUS_')),
|
|
|
|
...pq.filters.other.map(pfx('OTHER_')),
|
|
|
|
];
|
|
|
|
return pq as PageQuery;
|
2019-02-05 12:34:19 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
resetProposalPageQuery() {
|
2019-02-05 19:30:31 -08:00
|
|
|
app.proposals.page = {
|
|
|
|
...app.proposals.page,
|
|
|
|
page: 1,
|
|
|
|
search: '',
|
|
|
|
sort: 'CREATED:DESC',
|
|
|
|
filters: { status: [], other: [] },
|
|
|
|
};
|
2019-01-09 10:23:08 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
async fetchProposalDetail(id: number) {
|
|
|
|
app.proposalDetailFetching = true;
|
|
|
|
try {
|
|
|
|
app.proposalDetail = await fetchProposalDetail(id);
|
|
|
|
} catch (e) {
|
|
|
|
handleApiError(e);
|
|
|
|
}
|
|
|
|
app.proposalDetailFetching = false;
|
2018-10-30 09:35:47 -07:00
|
|
|
},
|
|
|
|
|
2019-01-29 15:50:27 -08:00
|
|
|
async updateProposalDetail(updates: Partial<Proposal>) {
|
|
|
|
if (!app.proposalDetail) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const res = await updateProposal({
|
|
|
|
...updates,
|
|
|
|
proposalId: app.proposalDetail.proposalId,
|
|
|
|
});
|
|
|
|
app.updateProposalInStore(res);
|
|
|
|
} catch (e) {
|
|
|
|
handleApiError(e);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-11-09 10:48:55 -08:00
|
|
|
async deleteProposal(id: number) {
|
2018-10-30 09:35:47 -07:00
|
|
|
try {
|
|
|
|
await deleteProposal(id);
|
2019-02-05 12:34:19 -08:00
|
|
|
app.proposals.page.items = app.proposals.page.items.filter(
|
|
|
|
p => p.proposalId === id,
|
|
|
|
);
|
2018-10-30 09:35:47 -07:00
|
|
|
} catch (e) {
|
|
|
|
handleApiError(e);
|
|
|
|
}
|
|
|
|
},
|
2019-01-09 10:23:08 -08:00
|
|
|
|
|
|
|
async approveProposal(isApprove: boolean, rejectReason?: string) {
|
|
|
|
if (!app.proposalDetail) {
|
2019-01-29 15:50:27 -08:00
|
|
|
const m = 'store.approveProposal(): Expected proposalDetail to be populated!';
|
|
|
|
app.generalError.push(m);
|
|
|
|
console.error(m);
|
2019-01-09 10:23:08 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
app.proposalDetailApproving = true;
|
|
|
|
try {
|
|
|
|
const { proposalId } = app.proposalDetail;
|
|
|
|
const res = await approveProposal(proposalId, isApprove, rejectReason);
|
|
|
|
app.updateProposalInStore(res);
|
|
|
|
} catch (e) {
|
|
|
|
handleApiError(e);
|
|
|
|
}
|
|
|
|
app.proposalDetailApproving = false;
|
|
|
|
},
|
2019-01-09 13:57:15 -08:00
|
|
|
|
2019-01-09 11:08:25 -08:00
|
|
|
async getEmailExample(type: string) {
|
|
|
|
try {
|
|
|
|
const example = await getEmailExample(type);
|
|
|
|
app.emailExamples = {
|
|
|
|
...app.emailExamples,
|
|
|
|
[type]: example,
|
|
|
|
};
|
|
|
|
} catch (e) {
|
|
|
|
handleApiError(e);
|
|
|
|
}
|
|
|
|
},
|
2019-01-30 09:59:15 -08:00
|
|
|
|
|
|
|
async fetchRFPs() {
|
|
|
|
app.rfpsFetching = true;
|
|
|
|
try {
|
|
|
|
app.rfps = await getRFPs();
|
|
|
|
app.rfpsFetched = true;
|
|
|
|
} catch (e) {
|
|
|
|
handleApiError(e);
|
|
|
|
}
|
|
|
|
app.rfpsFetching = false;
|
|
|
|
},
|
|
|
|
|
|
|
|
async createRFP(args: RFPArgs) {
|
|
|
|
app.rfpSaving = true;
|
|
|
|
try {
|
|
|
|
const data = await createRFP(args);
|
|
|
|
app.rfps = [data, ...app.rfps];
|
|
|
|
app.rfpSaved = true;
|
|
|
|
} catch (e) {
|
|
|
|
handleApiError(e);
|
|
|
|
}
|
|
|
|
app.rfpSaving = false;
|
|
|
|
},
|
|
|
|
|
|
|
|
async editRFP(id: number, args: RFPArgs) {
|
|
|
|
app.rfpSaving = true;
|
|
|
|
app.rfpSaved = false;
|
|
|
|
try {
|
|
|
|
await editRFP(id, args);
|
|
|
|
app.rfpSaved = true;
|
|
|
|
await app.fetchRFPs();
|
|
|
|
} catch (e) {
|
|
|
|
handleApiError(e);
|
|
|
|
}
|
|
|
|
app.rfpSaving = false;
|
|
|
|
},
|
|
|
|
|
|
|
|
async deleteRFP(id: number) {
|
|
|
|
app.rfpDeleting = true;
|
|
|
|
app.rfpDeleted = false;
|
|
|
|
try {
|
|
|
|
await deleteRFP(id);
|
|
|
|
app.rfps = app.rfps.filter(rfp => rfp.id !== id);
|
|
|
|
app.rfpDeleted = true;
|
|
|
|
} catch (e) {
|
|
|
|
handleApiError(e);
|
|
|
|
}
|
|
|
|
app.rfpDeleting = false;
|
|
|
|
},
|
2018-10-30 09:35:47 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
function handleApiError(e: AxiosError) {
|
|
|
|
if (e.response && e.response.data!.message) {
|
|
|
|
app.generalError.push(e.response!.data.message);
|
|
|
|
} else if (e.response && e.response.data!.data!) {
|
|
|
|
app.generalError.push(e.response!.data.data);
|
|
|
|
} else {
|
|
|
|
app.generalError.push(e.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(window as any).appStore = app;
|
|
|
|
|
|
|
|
// check login status periodically
|
|
|
|
app.checkLogin();
|
|
|
|
window.setInterval(app.checkLogin, 10000);
|
|
|
|
|
|
|
|
export type TApp = typeof app;
|
|
|
|
export default app;
|