Skip to content
This repository was archived by the owner on Dec 8, 2022. It is now read-only.
Prev Previous commit
Next Next commit
fixes initials, sends register to voting
  • Loading branch information
net8floz committed Mar 6, 2020
commit da7332bc1c6f03b49c24cb36635e7b6d3ba45236
37 changes: 34 additions & 3 deletions src/api/voting.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,41 @@
import routes from "./routes";
import request from "./request";
import routes from './routes';
import request from './request';

function lastInitial(item) {
if (item.lastName) {
return item.lastName[0];
}

const split = item.username.split(' ');
return split.length >= 2 ? split[1] : '';
}

function firstInitial(item) {
if (item.firstName) {
return item.firstName[0];
}
if (item.display) {
return item.display[0];
}

return item.username.split(' ')[0];
}

function initials(item) {
return `${firstInitial(item)}${lastInitial(item)}`;
}

async function getBallot(page, per) {
return request(routes.voting_ballot, {
const result = await request(routes.voting_ballot, {
params: { page, per }
});

if (result.items) {
result.items = result.items.map(item => {
return { ...item, ...{ initials: initials(item) } };
});
}
return result;
}

async function cast(answerId, email) {
Expand Down
177 changes: 77 additions & 100 deletions src/plugins/router.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Vue from "vue";
import VueRouter from "vue-router";
import { auth } from "@/api";
import store from "@/store";
import Vue from 'vue';
import VueRouter from 'vue-router';
import { auth } from '@/api';
import store from '@/store';

Vue.use(VueRouter);

Expand All @@ -18,220 +18,197 @@ function isChallengeClosed() {
}

async function logout() {
await store.dispatch("Quiz/reset");
await store.dispatch('Quiz/reset');
await auth.logout();
}

const routes = [
{
// basically a dynamic home page
path: "/",
name: "redirect",
path: '/',
name: 'redirect',
beforeEnter(to, from, next) {
if (isChallengeOpen() || isChallengePending()) {
next({ name: "quiz" });
next({ name: 'quiz' });
return;
}

if (isChallengeClosed()) {
next({ name: "voting" });
next({ name: 'voting' });
return;
}
}
},
{
// public routes
path: "/",
component: () => import("@/views/Public/App"),
path: '/',
component: () => import('@/views/Public/App'),
children: [
{
path: "frequently-asked-questions",
name: "faq",
component: () => import("@/views/Public/FAQ")
path: 'frequently-asked-questions',
name: 'faq',
component: () => import('@/views/Public/FAQ')
}
]
},
// FOR EXTERNAL IFRAME
{
path: "/iframe/leaderboard",
name: "iframe-leaderboard",
component: () => import("@/views/IFrame/Leaderboard")
path: '/iframe/leaderboard',
name: 'iframe-leaderboard',
component: () => import('@/views/IFrame/Leaderboard')
},
{
// account routes
path: "/",
component: () => import("@/views/Public/App"),
path: '/',
component: () => import('@/views/Public/App'),
children: [
{
path: "login",
name: "login",
component: () => import("@/views/Accounts/Login"),
path: 'login',
name: 'login',
component: () => import('@/views/Accounts/Login'),
meta: { anon: true }
},
{
path: "forgot-password",
name: "forgot-password",
component: () => import("@/views/Accounts/ForgotPassword")
path: 'forgot-password',
name: 'forgot-password',
component: () => import('@/views/Accounts/ForgotPassword')
},
{
path: "reset-password/:token",
name: "reset-password",
component: () => import("@/views/Accounts/ResetPassword")
path: 'reset-password/:token',
name: 'reset-password',
component: () => import('@/views/Accounts/ResetPassword')
},
{
path: "create-account",
name: "register",
component: () => import("@/views/Accounts/Register"),
meta: { anon: true }
path: 'create-account',
name: 'register',
component: () => import('@/views/Accounts/Register'),
meta: { anon: true, challengeOpenOrPending: true }
},
{
path: "logout",
name: "logout",
beforeEnter: (to, from, next) =>
logout().then(() => next({ name: "login" })),
path: 'logout',
name: 'logout',
beforeEnter: (to, from, next) => logout().then(() => next({ name: 'login' })),
meta: { auth: true }
},
{
path: "admin",
name: "admin",
component: () => import("@/views/Accounts/Admin"),
path: 'admin',
name: 'admin',
component: () => import('@/views/Accounts/Admin'),
meta: { auth: true }
}
]
},
{
// voting routes
path: "/",
component: () => import("@/views/Voting/App"),
path: '/',
component: () => import('@/views/Voting/App'),
meta: { challengeOver: true },
children: [
{
path: "voting",
name: "voting",
component: () => import("@/views/Voting/Ballot")
path: 'voting',
name: 'voting',
component: () => import('@/views/Voting/Ballot')
},
{
path: "vote-confirmation",
name: "voting-confirmation",
component: () => import("@/views/Voting/Confirm")
path: 'vote-confirmation',
name: 'voting-confirmation',
component: () => import('@/views/Voting/Confirm')
}
]
},
{
// quiz routes
path: "/",
component: () => import("@/views/Public/App"),
path: '/',
component: () => import('@/views/Public/App'),
meta: { secured: true, challengeOpenOrPending: true },
children: [
{
path: "quiz",
name: "quiz",
path: 'quiz',
name: 'quiz',
component: async () => {
// CHALLENGE HAS NOT STARTED
if (!isChallengeOpen()) {
return import("@/views/Quiz/QuizCountdown");
return import('@/views/Quiz/QuizCountdown');
}

// USER HAS FINISHED QUIZ
if (store.state.Quiz.maxRank === store.state.User.rank - 1) {
return import("@/views/Quiz/QuizFinished");
return import('@/views/Quiz/QuizFinished');
}

// MUST WAIT FOR NEXT QUESTION
if (store.state.Quiz.awaitNextQuestion) {
return import("@/views/Quiz/QuizCountdown");
return import('@/views/Quiz/QuizCountdown');
}

// SHOW THE LAST QUESTION
if (store.state.Quiz.isLastQuestion) {
return import("@/views/Quiz/QuizFinalQuestion");
return import('@/views/Quiz/QuizFinalQuestion');
}

// NORMAL QUIZ MODE
return import("@/views/Quiz/Quiz");
return import('@/views/Quiz/Quiz');
},
beforeEnter(from, to, next) {
// USER MUST SEE INTRO VIDEO
if (
isChallengeOpen() &&
!store.state.Quiz.hasSeenIntro &&
store.state.User.rank == 1
) {
next({ name: "quiz-intro" });
if (isChallengeOpen() && !store.state.Quiz.hasSeenIntro && store.state.User.rank == 1) {
next({ name: 'quiz-intro' });
return;
}
next();
}
},
{
path: "/quiz/intro",
name: "quiz-intro",
component: () => import("@/views/Quiz/QuizIntro")
path: '/quiz/intro',
name: 'quiz-intro',
component: () => import('@/views/Quiz/QuizIntro')
}
]
},
{
path: "*",
name: "wildcard",
redirect: { name: "redirect" }
path: '*',
name: 'wildcard',
redirect: { name: 'redirect' }
}
];

const router = new VueRouter({
mode: "history",
mode: 'history',
routes
});

router.beforeEach(async (to, from, next) => {
const requireAuth = to.matched.some(record => record.meta.secured);
const requireAnon = to.matched.some(record => record.meta.anon);
const requireChallengePending = to.matched.some(
record => record.meta.challengePending
);
const requireChallengeOpen = to.matched.some(
record => record.meta.challengeOpen
);
const requireChallengeClosed = to.matched.some(
record => record.meta.challengeOver
);
const requireChallengeOpenPending = to.matched.some(
record => record.meta.challengeOpenOrPending
);

if (
requireChallengePending ||
requireChallengeOpen ||
requireChallengeClosed ||
requireChallengeOpenPending
) {
const requireChallengePending = to.matched.some(record => record.meta.challengePending);
const requireChallengeOpen = to.matched.some(record => record.meta.challengeOpen);
const requireChallengeClosed = to.matched.some(record => record.meta.challengeOver);
const requireChallengeOpenPending = to.matched.some(record => record.meta.challengeOpenOrPending);
if (requireChallengePending || requireChallengeOpen || requireChallengeClosed || requireChallengeOpenPending) {
try {
await store.dispatch("Quiz/refresh");
await store.dispatch('Quiz/refresh');

const challengeIsClosed = isChallengeClosed();
const challengeIsPending = isChallengePending();
const challengeIsOpen = isChallengeOpen();

if (!challengeIsClosed && requireChallengeClosed) {
next({ name: "redirect" });
next({ name: 'redirect' });
return;
}

if (!challengeIsOpen && requireChallengeOpen) {
next({ name: "redirect" });
next({ name: 'redirect' });
return;
}

if (!challengeIsPending && requireChallengePending) {
next({ name: "redirect" });
next({ name: 'redirect' });
return;
}

if (
!challengeIsOpen &&
!challengeIsPending &&
requireChallengeOpenPending
) {
next({ name: "redirect" });
if (!challengeIsOpen && !challengeIsPending && requireChallengeOpenPending) {
next({ name: 'redirect' });
return;
}
} catch (err) {
Expand All @@ -243,12 +220,12 @@ router.beforeEach(async (to, from, next) => {
const isAuthenticated = !!auth.currentUser().auth;

if (!isAuthenticated && requireAuth) {
next({ name: "register" });
next({ name: 'register' });
return;
}

if (isAuthenticated && requireAnon) {
next({ name: "redirect" });
next({ name: 'redirect' });
return;
}
}
Expand Down
Loading