Giter Club home page Giter Club logo

thesis-form-lite's Introduction

Thesis-Form-Lite

A lighter version of Thesis Form built in React.

Installation

Add the following code to the page where you quiz will run.

const quizReducer = (state, action) => {
  switch (action.type) {
    case 'answer':
      const answers = [...state.answers, action.payload];
      const step = state.step + 1;
      return { ...state, answers, step };
    case 'set_state':
      return action.payload;
    default:
      throw new Error('Unexpected error');
  }
};

const ComponentMap = {
  SingleSelect: Question,
  MultiSelect: Question,
  // Custom: CustomQuestion, you can add any key/value pair for custom components
  // Interstitial: Interstitial,
};

const DynamicComponent = props => {
  const { question, state, dispatch } = props;

  return React.createElement(ComponentMap[question.type], {
    question: question,
    state: state,
    dispatch: dispatch,
  });
};

const Quiz = ({ initialQuizState }) => {
  let [state, dispatch] = useReducer(quizReducer, initialQuizState);

  useEffect(() => {
    // console.log('Pull in initial quiz state and set it')
    dispatch({ type: 'set_state', payload: state });
  }, [initialQuizState]);

  return (
    <div>
      {state.answers.length < state.questions.length ? (
        <DynamicComponent
          question={state.questions[state.step]}
          state={state}
          dispatch={dispatch}
        />
      ) : (
        <Results state={state} dispatch={dispatch} />
      )}
    </div>
  );
};

In a separate file create the quiz state. Below is an example of what it should include.

const initialQuizState = {
  step: 0,
  questions: [
    {
      id: 1,
      type: 'MultiSelect',
      title: 'Single Select Question',
      text: 'Here is some text',
      image:
        'https://uploads-ssl.webflow.com/5f8efd7611c46166adb2f1aa/60259f77e5d4f1879bc98285_Combined.svg',
      options: [{ value: 'First choice' }, { value: 'Second choice' }],
    },
    {
      id: 2,
      type: 'SingleSelect',
      title: 'Second question',
      ctaText: 'Next question',
      options: [
        { value: 'First choice second' },
        { value: 'Second choice second' },
      ],
    },
    {
      id: 3,
      type: 'MultiSelect',
      text: 'Third question',
      ctaText: 'Next question',
      options: [
        { value: 'First choice third' },
        { value: 'Second choice third' },
        { value: 'Third choice third' },
      ],
    },
    {
      id: 4,
      type: 'MultiSelect',
      text: 'Third question',
      ctaText: 'Next question',
      options: [
        { value: 'First choice fourth' },
        { value: 'Second choice fourth' },
        { value: 'Third choice fourth' },
      ],
    },
  ],
  answers: [],
};

Now you should be able to import the Question component and start building out a quiz!

thesis-form-lite's People

Contributors

vane-toro avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.